/* ---------------------------------------------------------------------------- ex17.C mbwall 5may95 Copyright (c) 1995-1996 Massachusetts Institute of Technology DESCRIPTION: Example program illustrating the use of a 2DArrayGenome with trinary alleles (-1, 0, 1). The objective function for this program tries to alternate 0 and 1 then put -1 in the corners. ---------------------------------------------------------------------------- */ #include #include #include #include #include float objective(GAGenome &); int main(int argc, char *argv[]) { cout << "Example 17\n\n"; cout << "This program illustrates the use of a 2DArrayGenome with\n"; cout << "three alleles. It tries to fill a 2D array with alternating\n"; cout << "0s and 1s, and -1s at the corners. You will have to run it\n"; cout << "for something like 10000 generations to get the perfect score.\n\n"; cout.flush(); // See if we've been given a seed to use (for testing purposes). When you // specify a random seed, the evolution will be exactly the same each time // you use that seed number. unsigned int seed = 0; for(int ii=1; ii alleles(3, aset); GA2DArrayAlleleGenome genome(20, 20, alleles, objective); genome.initializer(GA2DArrayAlleleGenome::UniformInitializer); genome.mutator(GA2DArrayAlleleGenome::FlipMutator); genome.crossover(GA2DArrayGenome::OnePointCrossover); GASteadyStateGA ga(genome); GASigmaTruncationScaling trunc; ga.scaling(trunc); ga.set(gaNpopulationSize, 40); ga.set(gaNpCrossover, 0.6); ga.set(gaNpMutation, 0.001); ga.set(gaNnGenerations, 10000); ga.set(gaNpReplacement, 0.25); ga.parameters(argc, argv); ga.initialize(seed); cout << "evolving..."; while(!ga.done()){ ga.step(); if(ga.generation() % 50 == 0){ cout << "."; cout.flush(); } } cout << "\n\n"; cout << "the ga generated:\n" << ga.statistics().bestIndividual() << endl; return 0; } float objective(GAGenome & c) { GA2DArrayAlleleGenome & genome = (GA2DArrayAlleleGenome &)c; float value=0.0; int count=0; for(int i=0; i::write(ostream & os) const { for(unsigned int j=0; j #include #if defined(__GNUG__) template class GAAlleleSet; template class GAAlleleSetCore; template class GAArray; template class GA2DArrayGenome; template class GA2DArrayAlleleGenome; #else GAAlleleSet; GAAlleleSetCore; GAArray; GA2DArrayGenome; GA2DArrayAlleleGenome; #endif #endif