All Data Structures Files Functions Variables Enumerations Enumerator Properties Defines
/Projects/Cogito/src/Objects/TreeState.m
Go to the documentation of this file.
00001 //
00002 //  TreeState.m
00003 //  Author: Thomas Taylor
00004 //
00005 //  Basic class to hold info about a state in
00006 //  decision tree learning
00007 //
00008 //  09/02/2012: Created class
00009 //
00010 
00011 #import "TreeState.h"
00012 
00013 @implementation TreeState
00014 
00015 #pragma mark -
00016 #pragma mark Initialisation
00017 
00022 -(id)initStateForObject:(GameObject*)_object
00023 {        
00024     self = [super initStateForObject:_object];
00025     
00026     if (self != nil) 
00027     {
00028         nodeValue = 0;
00029         action = -1;;
00030         
00031         children = [[CCArray alloc] init];
00032     }
00033     return self;
00034 }
00035 
00036 #pragma mark -
00037 
00042 -(void)addChild:(TreeState*)_state
00043 {        
00044     // whether the state is already a child
00045     BOOL new = YES;
00046     
00047     // look for duplicates
00048     for (int i = 0; i < [children count]; i++) 
00049         if([[children objectAtIndex:i] isEqualTo:_state]) new = NO;
00050     
00051     // Don't add the child if it already exists
00052     if(new) 
00053     {
00054         [children addObject:_state];
00055         [_state setParent:self];
00056     }
00057 }
00058 
00063 -(void)setAsLeafNode:(CharacterStates)_state
00064 {        
00065     switch (_state) 
00066     {
00067         case kStateWin:
00068             nodeValue = 1;
00069             break;
00070             
00071         case kStateDead:
00072             nodeValue = -1;
00073             break;
00074             
00075         default:
00076             CCLOG(@"%@.setAsLeafNode: Error, agent not dead/won", NSStringFromClass([self class]));
00077             break;
00078     }
00079 }
00080 
00085 -(void)setParent:(TreeState*)_parent
00086 {    
00087     parent = _parent;
00088 }
00089 
00094 -(BOOL)isLeaf
00095 {
00096     return (nodeValue == 1 || nodeValue == -1);
00097 }
00098 
00099 
00106 -(BOOL)isEqualTo:(TreeState*)_state
00107 {
00108     return (gameObject == [_state getGameObject] && action == [_state getAction]);
00109 }
00110 
00115 -(int)getValue
00116 {
00117     return nodeValue;
00118 }
00119 
00124 -(CCArray*)getChildren
00125 {    
00126     return children;
00127 }
00128 
00133 -(void)setAction:(Action)_action
00134 {
00135     action = _action;
00136 }
00137 
00142 -(Action)getAction
00143 {
00144     return action;
00145 }
00146 
00151 -(CCArray*)getActions
00152 {        
00153     CCArray* actionsArray = [CCArray arrayWithCapacity:0];
00154     
00155     for (int i = 0; i < [actions count]; i++) 
00156         [actionsArray addObject:[[actions objectAtIndex:i] objectAtIndex:0]];
00157     
00158     return actionsArray;
00159 }
00160 
00166 -(void)setActions:(CCArray*)_actions
00167 {        
00168     for (int i = 0; i < [_actions count]; i++) 
00169         [actions addObject:[NSMutableArray arrayWithObjects:[_actions objectAtIndex:i], [NSNull null], nil]];    
00170 }
00171 
00176 -(CCArray*)buildRoute:(CCArray*)_route
00177 {    
00178     // don't add the root
00179     if(parent != nil) 
00180     {
00181         [_route addObject:self];
00182         return [parent buildRoute:_route];
00183     }
00184     else return _route;
00185 }
00186 
00190 -(CCArray*)buildRoutes:(CCArray*)_routes
00191 {    
00192     for (int i = 0; i < [children count]; i++) 
00193     {
00194         TreeState* node = [children objectAtIndex:i];
00195         if([node getValue] == 1) 
00196             [_routes addObject:[[children objectAtIndex:i] buildRoute:[[CCArray alloc] init]]];
00197         else [node buildRoutes:_routes];
00198     }
00199     return _routes;
00200 }
00201 
00205 -(void)printStructure
00206 {
00207     NSString* outputString = [NSString stringWithFormat:@"%i: Node: %@ Action: %i Parent: %@", [children count], [Utils getObjectAsString:gameObject.gameObjectType], action, [Utils getBooleanAsString:(parent != nil)]];
00208     
00209     for (int i = 0; i < [children count]; i++) 
00210         [NSString stringWithFormat:@"%@%@, ", outputString, [Utils getObjectAsString:[[children objectAtIndex:i] getGameObject].gameObjectType]];
00211     
00212     CCLOG(@"%@", outputString);
00213     
00214     for (int i = 0; i < [children count]; i++) [[children objectAtIndex:i] printStructure];
00215 }
00216 
00217 @end