//Chromosome.h // //This file declares a Chromosome class template for use in genetic algorithms // #ifndef CHROMOSOME_H #define CHROMOSOME_H //crossover types //crossover may be single point, double point, or multiple point enum {singlePoint, doublePoint, multiPoint}; //forward declaration template class Population; template class Chromosome { friend class Population; public: Chromosome (); ~Chromosome (); //copy constructor Chromosome (const Chromosome &chrom); //initialization function creates a set of genes, and initializes them //with random values (the default is 0 or 1; instantiation of the class //for double values results in setting the genes to random values in the //-1, +1 range //numberGenes is the number of genes on each chromosome void initialize (int numberGenes); //equality operator bool operator== (const Chromosome &chrom); //assignment operator Chromosome& operator= (const Chromosome &chrom); //readers and writers //readGene returns the value of the gene with index geneNumber geneType readGene (int geneNumber); //writeGene writes the value "value" into gene "geneNumber" void writeGene (int geneNumber, geneType value); //readFitness returns the fitness value of the chromosome double readFitness (); //writeFitness sets the fitness of the chromosome to "value" void writeFitness (double value); //mutation function //default (integer values): sets 0 to 1, 1 to 0 //double genes: adds a value in the (-1,1) range to to genes //"mutationProbability" is the probability of mutation of each gene; // the default value gives significant mutation //mutate returns the number of mutations int mutate (double mutationProbability = 0.01); //reproduction/crossover functions //the first function modifies both the calling and parameter chromosomes, // producing two offspring from two parents, replacing the parents //crossoverType is singlePoint, doublePoint, or multiPoint void reproduce (Chromosome &chrom, int crossoverType); //the next function produces one offspring from two parents, without replacement Chromosome reproduceOneOffspring (const Chromosome &chrom, int crossoverType); protected: int size; //number of genes double fitness; geneType *gene; }; #endif