home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 01 Manslow / GPExample / CGPBinaryNode.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-09  |  3.1 KB  |  111 lines

  1. //GPExample
  2. //Copyright John Manslow
  3. //29/09/2001
  4.  
  5. ////////////////////////////////////////////////////////////
  6. //Only when compiling under Windows
  7. #include "stdafx.h"
  8. #define new DEBUG_NEW
  9. ////////////////////////////////////////////////////////////
  10.  
  11. #include "CGPBinaryNode.h"
  12. #include "CGPNode.h"
  13. #include "CGP.h"
  14. #include "assert.h"
  15.  
  16. //This call is necessary to create the subtrees attached to this node
  17. extern CGPNode *pGPCreateRandomSubtree(void);
  18.  
  19. extern CGPNode **pPrototypeList;
  20. extern unsigned long ulNumberOfPrototypes;
  21.  
  22. CGPBinaryNode::CGPBinaryNode()
  23. {
  24. }
  25.  
  26. CGPBinaryNode::~CGPBinaryNode()
  27. {
  28.     //Destroy the subtrees
  29.     unsigned long i;
  30.     if(pChildren!=NULL)
  31.     {
  32.         for(i=0;i<ulNumberOfChildren;i++)
  33.         {
  34.             if(pChildren[i]!=NULL)
  35.             {
  36.                 delete pChildren[i];
  37.             }
  38.         }
  39.         delete []pChildren;
  40.     }
  41. }
  42.  
  43. CGPNode *CGPBinaryNode::pGetCopy(CGP* pGP)
  44. {
  45.     //Creates a copy of this node. If this node is in the prototype list, it does not have a valid subtree attached to it
  46.     //so a random one is created. If it is not a prototype, the subtree attached to this node is copied by calls to the
  47.     //copy functions of this node's children's children, etc.
  48.     unsigned long i;
  49.  
  50.     //Create a copy
  51.     CGPBinaryNode *pNewNode=new CGPBinaryNode;
  52.     assert(!(pNewNode==NULL));
  53.  
  54.     //For each child,
  55.     for(i=0;i<ulNumberOfChildren;i++)
  56.     {
  57.         //If this node has children, delete them because we're about to create new ones (children are usually
  58.         //created automatically in the constructors even though we don't always want them)
  59.         if(pNewNode->pChildren[i]!=NULL)
  60.         {
  61.             delete pNewNode->pChildren[i];
  62.         }
  63.  
  64.         //If this is not a prototype,
  65.         if(!nIsPrototype)
  66.         {
  67.             //Make the children of the new node copies of this node's children
  68.             pNewNode->pChildren[i]=pChildren[i]->pGetCopy(pGP);
  69.         }
  70.         else
  71.         {
  72.             //Otherwise make the children of the new node random subprograms
  73.             pNewNode->pChildren[i]=pGP->pGetRandomSubtree();
  74.         }
  75.     }
  76.  
  77.     //Return the new node and associated subtree
  78.     return (CGPNode*)pNewNode;
  79. }
  80.  
  81. unsigned long CGPBinaryNode::ulGetNumberOfNodesInSubtree(unsigned long ulNodesFoundSoFar)
  82. {
  83.     //Add the number of nodes in the subtree attached to this to the number of nodes found so far
  84.     //Note that the +2 is necessary because node don't count themselves
  85.     return ulNodesFoundSoFar+2+pChildren[0]->ulGetNumberOfNodesInSubtree(0)+pChildren[1]->ulGetNumberOfNodesInSubtree(0);
  86. }
  87.  
  88. void CGPBinaryNode::GetnthNode(unsigned long& ulNodesSoFar,unsigned long ulTargetNode,CGPNode **&pTheNode)
  89. {
  90.     //If p TheNode is not equal to NULL, the node has already been found
  91.     if(pTheNode==NULL)
  92.     {
  93.         //If the first child is the target node, get its address
  94.         if(ulTargetNode==ulNodesSoFar)
  95.         {
  96.             pTheNode=&pChildren[0];
  97.         }
  98.         //If the second child is the target node, get its address
  99.         if(ulTargetNode==ulNodesSoFar+1)
  100.         {
  101.             pTheNode=&pChildren[1];
  102.         }
  103.     }
  104.     //Increment the nodes counter
  105.     ulNodesSoFar+=2;
  106.  
  107.     //And check the nodes in this node's subtree
  108.     pChildren[0]->GetnthNode(ulNodesSoFar,ulTargetNode,pTheNode);
  109.     pChildren[1]->GetnthNode(ulNodesSoFar,ulTargetNode,pTheNode);
  110. }
  111.