//Population.h // //this file declares a class template for a population of genes #ifndef POPULATION_H #define POPULATION_H template class Population { public: Population (); Population (int populationSize, int chromosomeSize); ~Population (); //assignment operator Population& operator= (const Population &pop); //readers //return the overall fitness of the population double readFitness (); //return the number of chromosomes in the population int readSize (); //display entire population //displays each chromosome as a string of genes void displayPopulation(); //display part of the population //"first" is the first chromosome to display, "last" is the last, //where the chromosomes are numbered from 0 to size-1 void displayPopulation(int first, int last); //calculate fitness of the population from individual chromosome fitnesses; //the population fitness is the sum of the individual fitnesses double determineFitness (); //set up fitness-proportionate selection //this function creates a roulette wheel with slots proportional in size to //the fitnesses of the chromosomes void setSelectionCriteria (); //return the index of the next fitness-selected chromosome //spins the roulette wheel int selectNextChromosome (); //the next few functions allow access/modification //of chromosomes through the population class; functions acting on the //population as a whole should be used in preference to these whenever possible //write a fitness into a single chromosome //"chromosomeNumber" is the index of the chromosome, "newFitness" is the fitness value void writeChromosomeFitness (int chromosomeNumber, double newFitness); //return a chromosome with index "chromosomeNumber" Chromosome returnChromosome (int chromosomeNumber); //replace chromosome "chromosomeNumber" with chromosome "chrom" void replaceChromosome (int chromosomeNumber, const Chromosome &chrom); //reproduce two chromosomes void reproduce (int chromosome1, int chromosome2, int crossoverType); Chromosome reproduceOneOffspring (int chromosome1, int chromosome2, int crossoverType); //functions to reproduce the population: //use fitness-proportionate selection of parents with replacement // (i.e., the same parent may reproduce more than once) //reproduce the population into a second population object ("targetPopulation"), // and use singlePoint, doublePoint, or muliPoint crossover void reproduce (Population &targetPopulation, int crossoverType); //fitness-proportionate selection without replacement //(i.e., the same parent can reproduce only once, and each parent is guaranteed //to reproduce once) void reproduceWithoutReplacement (Population &targetPopulation, int crossoverType); //mutate the entire population //function returns the number of mutations int mutate(double mutationProbability = 0.01); //use quicksort to sort chromosomes in descending order of fitness //"left" and "right" define the subpopulation to be sorted; for the entire //population, left = 0, right = size-1 void quicksort (int left, int right); protected: int size; double fitness, *selectionWheel; Chromosome *chromosome; Chromosome v; //workspace void swap (int a, int b); //used by quicksort }; #endif