home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 01 Manslow / GPExample / CGPORNode.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-10  |  2.0 KB  |  96 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 "CGPORNode.h"
  12. #include "CGPNode.h"
  13. #include "CGP.h"
  14. #include "assert.h"
  15.  
  16. extern CGPNode *pGPCreateRandomSubtree(void);
  17.  
  18. extern CGPNode **pPrototypeList;
  19. extern unsigned long ulNumberOfPrototypes;
  20.  
  21. CGPORNode ORPrototype;
  22.  
  23. CGPORNode::CGPORNode()
  24. {
  25.     unsigned long i;
  26.     nType=105;
  27.  
  28.     pChildren=NULL;
  29.     ulNumberOfChildren=2;
  30.  
  31.     sprintf(psName,"ORNode");
  32.  
  33.     nIsPrototype=1;
  34.     if(AddToPrototypeList())
  35.     {
  36.         nIsPrototype=0;
  37.         pChildren=new CGPNode*[ulNumberOfChildren];
  38.         for(i=0;i<ulNumberOfChildren;i++)
  39.         {
  40.             pChildren[i]=NULL;
  41.         }
  42.     }
  43.     dPrior=0.1;
  44. }
  45.  
  46. CGPORNode::~CGPORNode()
  47. {
  48. }
  49.  
  50. CGPNode *CGPORNode::pGetCopy(CGP* pGP)
  51. {
  52.     unsigned long i;
  53.     CGPORNode *pNewNode=new CGPORNode;
  54.     assert(!(pNewNode==NULL));
  55.     for(i=0;i<ulNumberOfChildren;i++)
  56.     {
  57.         if(pNewNode->pChildren[i]!=NULL)
  58.         {
  59.             delete pNewNode->pChildren[i];
  60.         }
  61.         if(!nIsPrototype)
  62.         {
  63.             pNewNode->pChildren[i]=pChildren[i]->pGetCopy(pGP);
  64.         }
  65.         else
  66.         {
  67.             pNewNode->pChildren[i]=pGP->pGetRandomSubtree();
  68.         }
  69.     }
  70.     return (CGPNode*)pNewNode;
  71. }
  72.  
  73. double CGPORNode::dEvaluate(void)
  74. {
  75.     //This function returns the arithmetical-logical OR of the return values of the node's children
  76.     double dReturn1=pChildren[0]->dEvaluate();
  77.     double dReturn2=pChildren[1]->dEvaluate();
  78.     return dReturn1+dReturn2-dReturn1*dReturn2;
  79. }
  80.  
  81. char* CGPORNode::psGetString(char*pString)
  82. {
  83.     char *pSubString=new char[50000];
  84.     if(pString!=NULL && strlen(pString)<10000)
  85.     {
  86.         sprintf(pSubString,"");
  87.         pSubString=pChildren[0]->psGetString(pSubString);
  88.         sprintf(pString,"%s OR((%s),",pString,pSubString);
  89.         sprintf(pSubString,"");
  90.         pSubString=pChildren[1]->psGetString(pSubString);
  91.         sprintf(pString,"%s(%s))",pString,pSubString);
  92.     }
  93.     delete []pSubString;
  94.     return pString;
  95. }
  96.