/* ---------------------------------------------------------------------------- ex11.C mbwall 13apr95 Copyright (c) 1995-1996 Massachusetts Institute of Technology DESCRIPTION: This example shows how to use an order-based list genome. ---------------------------------------------------------------------------- */ #include #include #include #include #include // The objective function tells how good a genome is. The Initializer defines // how the lists should be initialized. float objective(GAGenome &); void ListInitializer(GAGenome &); int main(int argc, char *argv[]) { cout << "Example 11\n\n"; cout << "This program illustrates the use of order-based lists. The\n"; cout << "list in this problem contains 25 numbers, 0 to 24. It tries\n"; cout << "to put them in descending order from 24 to 0.\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. for(int ii=1; ii genome(objective); genome.initializer(ListInitializer); genome.mutator(GAListGenome::SwapMutator); // Now that we have our genome, we create the GA (it clones the genome to // make all of the individuals for its populations). Set the parameters on // the GA then let it evolve. GASteadyStateGA ga(genome); ga.crossover(GAListGenome::PartialMatchCrossover); ga.parameters(params); ga.evolve(); // Assign the best that the GA found to our genome then print out the results. genome = ga.statistics().bestIndividual(); cout << "the ga generated the following list (objective score is "; cout << genome.score() << "):\n" << genome << "\n"; cout << "best of generation data are in '" << ga.scoreFilename() << "'\n"; cout << ga.parameters() << "\n"; // char *fn; // ga.get(gaNscoreFilename, &fn); // cout << "filename is '" << fn << "'\n"; return 0; } /* ---------------------------------------------------------------------------- Objective function The objective function gives one point for every number in the correct position. We're trying to get a sequence of numbers from n to 0 in descending order. ---------------------------------------------------------------------------- */ float objective(GAGenome & c) { GAListGenome & genome = (GAListGenome &)c; float score = (*genome.head() == genome.size()-1); // move to head of list for(int i=genome.size()-2; i>0; i--) score += (*genome.next() == i); // cycle through list and look at nodes return score; } /* ---------------------------------------------------------------------------- List Genome Operators ------------------------------------------------------------------------------- The initializer creates a list with n elements in it and puts a unique digit in each one. After we make the list, we scramble everything up. ---------------------------------------------------------------------------- */ void ListInitializer(GAGenome & c) { GAListGenome &child=(GAListGenome &)c; while(child.head()) child.destroy(); // destroy any pre-existing list int n=25; child.insert(0,GAListBASE::HEAD); // the head node contains a '0' for(int i=1; i::write(ostream & os) const { int *cur, *head; GAListIter tmpiter(*this); if((head=tmpiter.head()) != 0) os << *head << " "; for(cur=tmpiter.next(); cur && cur != head; cur=tmpiter.next()) os << *cur << " "; return os.fail() ? 1 : 0; } // If your compiler does not do automatic instantiation (e.g. g++ 2.6.8), // then define the NO_AUTO_INST directive. #ifdef NO_AUTO_INST #include #include #if defined(__GNUG__) template class GAList; template class GAListGenome; #else GAList; GAListGenome; #endif #endif