home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 01 Manslow / GAPBILExample / CGA.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-07  |  2.7 KB  |  90 lines

  1. //GAPBILExample
  2. //Copyright John Manslow
  3. //29/09/2001
  4.  
  5. #ifndef _CGA_
  6. #define _CGA_
  7.  
  8. class CGA
  9. {
  10. public:
  11.  
  12.     CGA(        
  13.                     const unsigned long,            //Population size
  14.                     const unsigned long,            //Chromosome length
  15.                     const int * const                    //Gene type information
  16.                     );
  17.  
  18.     ~CGA();
  19.  
  20.     //Calls the Mate() function (see below) to create a new chromosome and returns a pointer to it so
  21.     //that its fitness can be evaluated
  22.     double *pdGetChromosomeForEvaluation(void);
  23.  
  24.     //Sets the fitness of the working chromosome. All that is necessary to use the GA is to call the above
  25.     //function to create a new chromosome, evaluate its fitness, and pass the fitness back to the GA 
  26.     //using this function
  27.     void SetFitness(const double);
  28.  
  29.     //Returns a pointer to the best chromosome discovered so far
  30.     double *pdGetBestChromosome(void);
  31.  
  32.     //Returns the performance of the best chromosome found so far
  33.     double dGetBestPerformance(void);
  34.  
  35.     //Load and save the status of the GA
  36.     int Save(const char*const);
  37.     int Load(const char*const);
  38.  
  39. private:
  40.     //These functions don't need to be called from outside the class to make the GA work
  41.  
  42.     //Allocate and deallocate memory
  43.     void AllocateMemory(void);
  44.     void DeallocateMemory(void);
  45.  
  46.     //Create a random population and resets fintess statistics
  47.     void InitialisePopulation(void);
  48.  
  49.     //Mutates the chromosome pointed to by the pointer passed in the parameter list
  50.     void Mutate(double * const);
  51.  
  52.     //Creates a child of the paremts pointed to by the pointers in the parameter list and places
  53.     //it in the population at the location indicated by the last parameter
  54.     void Crossover(const double * const, const double * const,const unsigned long);
  55.  
  56.     //Chooses two parents, crosses them, replaces the less fit of the two with the child, and
  57.     //mutates the child
  58.     void Mate(void);
  59.  
  60.     //Per-gene mutation and crossover probabilities
  61.     double dMutationRate;
  62.     double dCrossoverRate;
  63.  
  64.     //Population size and chromosome length
  65.     unsigned long ulPopulationSize;
  66.     unsigned long ulChromosomeLength;
  67.  
  68.     //Pointer to the actual population of genes
  69.     double **ppdGenes;
  70.  
  71.     //Pointer to the list of fitnesses
  72.     double *pdFitnesses;
  73.  
  74.     //Pointer to a list of gene type indicators: 0=gene is binary, 1=gene is real
  75.     int *pnGeneTypes;
  76.  
  77.     //Storage for the fittest chromosome discovered so far and its fitness
  78.     double dBestFitness;
  79.     double *pdBestChromosome;
  80.  
  81.     //The position of the current working chromosome in the population. The working chromosome is the
  82.     //one that is currently having its fitness evaluated
  83.     unsigned long ulWorkingChromosome;
  84.  
  85.     //The number of iterations (i.e. number of fitness evaluations)
  86.     unsigned long ulIteration;
  87.  
  88. };
  89.  
  90. #endif