All Data Structures Files Functions Variables Enumerations Enumerator Properties Defines
/Projects/Cogito/src/Singletons/AgentStats.m
Go to the documentation of this file.
00001 //
00002 //  AgentStats.m
00003 //  Author: Thomas Taylor
00004 //
00005 //  Used to calculate various stats
00006 //
00007 //  20/02/2011: Created class
00008 //
00009 
00010 #import "AgentStats.h"
00011 
00012 @implementation AgentStats
00013 
00014 static AgentStats* _instance = nil;
00015 
00016 #pragma mark -
00017 #pragma mark Memory Allocation
00018 
00019 -(void)dealloc
00020 {
00021     [learningLengths release];
00022     [nonLearningLengths release];
00023     [learningActions release];
00024     [nonLearningActions release];
00025     
00026     [super dealloc];
00027 }
00028 
00033 +(id)alloc
00034 {    
00035     @synchronized([AgentStats class])
00036     {
00037         // if the _instance already exists, stop
00038         NSAssert(_instance == nil, @"There should only ever be one instance of AgentStats");
00039         _instance = [super alloc];
00040         return _instance;
00041     }
00042     
00043     return nil;
00044 }
00045 
00046 #pragma mark -
00047 #pragma mark Initialisation
00048 
00053 -(id)init 
00054 {        
00055     self = [super init];
00056     
00057     if (self != nil) 
00058     {
00059         learningLengths = [[CCArray alloc] init];
00060         nonLearningLengths = [[CCArray alloc] init];
00061         learningActions = [[CCArray alloc] init];
00062         nonLearningActions = [[CCArray alloc] init];
00063     }
00064     
00065     return self;
00066 }
00067 
00068 #pragma mark -
00069 
00074 +(AgentStats*)sharedAgentStats
00075 {
00076     @synchronized([AgentStats class])
00077     {
00078         if(!_instance) [[self alloc] init];
00079         return _instance;
00080     }
00081     
00082     return nil;
00083 }
00084 
00088 -(void)addEpisode
00089 {
00090     episodesCompleted++;
00091 }
00092 
00099 -(void)addEpisodeWithLength:(int)_time andActions:(int)_actions learningMode:(BOOL)_learning
00100 {
00101     //CCLOG(@"%@.addEpisode: L: %i A: %i [%@]", NSStringFromClass([self class]), _time, _actions, [Utils getBooleanAsString:_learning]);
00102     
00103     if(_learning) 
00104     {
00105         [learningLengths addObject:[NSNumber numberWithInt:_time]];
00106         [learningActions addObject:[NSNumber numberWithInt:_actions]];
00107     }
00108     else 
00109     {
00110         [nonLearningLengths addObject:[NSNumber numberWithInt:_time]];
00111         [nonLearningActions addObject:[NSNumber numberWithInt:_actions]];
00112     }
00113     
00114     [self addEpisode];
00115 }
00116 
00121 -(int)averageTimeLearning
00122 {
00123     int average = 0;
00124     
00125     for (int i = 0; i < [learningLengths count]; i++) average += [[learningLengths objectAtIndex:i] intValue];
00126     if([learningLengths count] > 0) average = average/[learningLengths count];
00127     
00128     return average;
00129 }
00130 
00135 -(int)averageTimeNonLearning
00136 {
00137     int average = 0;
00138     
00139     for (int i = 0; i < [nonLearningLengths count]; i++) average += [[nonLearningLengths objectAtIndex:i] intValue];
00140     if([nonLearningLengths count] > 0) average = average/[nonLearningLengths count];
00141     
00142     return average;
00143 }
00144 
00149 -(int)averageActionsLearning
00150 {    
00151     float average = 0;
00152     
00153     for (int i = 0; i < [learningActions count]; i++) average += [[learningActions objectAtIndex:i] intValue];
00154     if([learningActions count] > 0) average = average/[learningActions count];
00155     
00156     return average;
00157 }
00158 
00163 -(int)averageActionsNonLearning
00164 {
00165     float average = 0;
00166     
00167     for (int i = 0; i < [nonLearningActions count]; i++) average += [[nonLearningActions objectAtIndex:i] intValue];
00168     if([nonLearningActions count] > 0) average = average/[nonLearningActions count];
00169     
00170     return average;
00171 }
00172 
00176 -(int)episodesCompleted
00177 {
00178     return episodesCompleted;
00179 }
00180 
00184 -(void)clearTempData
00185 {
00186     episodesCompleted = 0;
00187     learningLengths = [[CCArray alloc] init];
00188     nonLearningLengths = [[CCArray alloc] init];
00189     learningActions = [[CCArray alloc] init];
00190     nonLearningActions = [[CCArray alloc] init];
00191 }
00192 
00193 @end