double real_value(const Chrom & _chrom) { double sum = 0; for (unsigned i = 0; i < _chrom.size(); i++) sum += _chrom[i]; return sum/_chrom.size(); }
// Return true if the given chromosome corresponds to a permutation // There must be an nicer way to do it (set?) ... bool check_permutation(const Chrom & _chrom) { for (unsigned i = 0; i < _chrom.size(); ++i) for (unsigned j = 0; j < _chrom.size(); ++j) if(i!=j) if(_chrom[i]==_chrom[j]){ std::cout << " Error: Wrong permutation !" << std::endl; std::string s; s.append( " Wrong permutation in t-eoInitPermutation"); throw std::runtime_error( s ); } return true; }
main() { const unsigned POP_SIZE = 8, CHROM_SIZE = 4; unsigned i; eoBinRandom<Chrom> random; eoPop<Chrom> pop; // Create the population for (i = 0; i < POP_SIZE; ++i) { Chrom chrom(CHROM_SIZE); random(chrom); BinaryValue()(chrom); pop.push_back(chrom); } // print population std::cout << "population:" << std::endl; for (i = 0; i < pop.size(); ++i) std::cout << pop[i] << " " << pop[i].fitness() << std::endl; // Declare 1-selectors eoUniformSelect<Chrom> uSelect; Chrom aChrom; aChrom = uSelect( pop ); std::cout << "Uniform Select " << aChrom << " " << aChrom.fitness() << std::endl; eoStochTournament<Chrom> sSelect(0.7); aChrom = sSelect( pop ); std::cout << "Stochastic Tournament " << aChrom << " " << aChrom.fitness() << std::endl; eoDetTournament<Chrom> dSelect(3); aChrom = dSelect( pop ); std::cout << "Deterministic Tournament " << aChrom << " " << aChrom.fitness() << std::endl; return 0; }
//----------------------------------------------------------------------------- // Return true if the given chromosome corresponds to a permutation bool check_permutation(const Chrom& _chrom){ unsigned size= _chrom.size(); std::set<unsigned> verif; for(unsigned i=0; i< size; i++){ if(verif.insert(_chrom[i]).second==false){ std::cout << " Error: Wrong permutation !" << std::endl; std::string s; s.append( " Wrong permutation in t-eoShiftMutation"); throw std::runtime_error( s ); return false; } } return true; }
main( int argc, char** argv) { unsigned numGenes = 2, // Number of genes in the chromosome numGenerations, // Number of generations popSize; // Population size float xOverPr, // Crossover rate, from 0 to 1 mutPr; // Mutation rate, from 0 to 1 bool verbose; // true if you want many things printed // Obtain command line parameters or default values getParams( argc, argv, verbose, popSize, numGenerations, xOverPr, mutPr ); // Create an initial population and the population-level operators Pop pop; unsigned i, j; Uniform<float> u( 0, 1); for ( j = 0; j < popSize; j ++ ) { Chrom* aChrom = new Chrom; for ( i = 0; i < numGenes; i ++ ) { aChrom->push_back( u() ); } pop.push_back( *aChrom ); if ( verbose ) { copy( aChrom->begin(), aChrom->end(), ostream_iterator<int>( cout, " ") ); cout << endl; } delete aChrom; } // Create an instance of the evaluation function object deJongF2float thisEvalFunc; // From that, create an object that evaluates all the population EOEvalAll<Chrom> eval( thisEvalFunc) ; // Then, an object that select who's going to reproduce EOLottery<Chrom> select; // Another object that mates them EORandomBreed<Chrom> breed; // And another that weeds out the worst EOElimAll<Chrom> replace; // This one check if the algorithm has finishd or not EOGenTerm<Chrom> term( numGenerations ); // Then the EO-level operators // This is a mutation-like operator EOFloatArrayRndMutate<Chrom> rndMut( 0.1, mutPr ); // And this is the classical 2-point crossover EOXOver2<Chrom> xOver(xOverPr); // Add operators to breeder breed.addOp( &rndMut ); breed.addOp( &xOver ); // Then, the algorithm itself is instantiated EOEasyGA<Chrom> ga( eval, select, breed, term, replace, verbose ); // Now, apply the algorithm to the population eval( pop ); ga( pop ); // Sort Population. First is the best sort( pop.begin(), pop.end(), SortEO<Chrom>() ); // And print results! cout << "Best fitness ....... " << pop[0].fitness() << endl << "Value............... " << pop[0] << endl; return 0; }
float operator()(Chrom& chrom) const { return accumulate(chrom.begin(), chrom.end(), 0); }