// to avoid long name warnings #ifdef _MSC_VER #pragma warning(disable:4786) #endif #include #include #include //----------------------------------------------------------------------------- struct Dummy : public EO { typedef double Type; void printOn(std::ostream & _os) const { _os << " - "; EO::printOn(_os); } }; bool operator==(const Dummy & _d1, const Dummy & _d2) { return _d1.fitness() == _d2.fitness(); } struct eoDummyPop : public eoPop { public : eoDummyPop(int s=0) { resize(s); } }; // helper - DOES NOT WORK if different individuals have same fitness!!! template unsigned isInPop(EOT & _indi, eoPop & _pop) { for (unsigned i=0; i<_pop.size(); i++) if (_pop[i] == _indi) return i; return _pop.size(); } unsigned int pSize; // global variable, bouh! std::string fitnessType; // yes, a global variable :-) eoDummyPop parentsOrg; template void testSelectMany(eoSelect & _select, std::string _name) { unsigned i; std::cout << "\n\n" << fitnessType + _name << std::endl; std::cout << "===============\n"; eoDummyPop parents(parentsOrg); eoDummyPop offspring(0); // do the selection _select(parents, offspring); // compute stats std::vector nb(parents.size(), 0); for (i=0; i(offspring[i], parents); if (trouve == parents.size()) // pas trouve throw std::runtime_error("Pas trouve ds parents"); nb[trouve]++; } // dump to file so you can plot using gnuplot - dir name is hardcoded! std::string fName = "ResSelect/" + fitnessType + _name + ".select"; std::ofstream os(fName.c_str()); for (i=0; i " << ( (double)nb[i])/offspring.size() << std::endl; os << i << " " << ( (double)nb[i])/offspring.size() << std::endl; } } template void testSelectOne(eoSelectOne & _select, eoHowMany & _offspringRate, eoHowMany & _fertileRate, std::string _name) { eoTruncatedSelectOne truncSelect(_select, _fertileRate); eoSelectMany percSelect(truncSelect, _offspringRate); testSelectMany(percSelect, _name); } //----------------------------------------------------------------------------- int the_main(int argc, char **argv) { eoParser parser(argc, argv); eoValueParam parentSizeParam = parser.createParam(unsigned(10), "parentSize", "Parent size",'P'); pSize = parentSizeParam.value(); // global variable // eoValueParam offsrpringRateParam = parser.createParam(1.0, "offsrpringRate", "Offsrpring rate",'O'); // double oRate = offsrpringRateParam.value(); eoValueParam offsrpringRateParam = parser.createParam(eoHowMany(1.0), "offsrpringRate", "Offsrpring rate (% or absolute)",'O'); eoHowMany oRate = offsrpringRateParam.value(); eoValueParam fertileRateParam = parser.createParam(eoHowMany(1.0), "fertileRate", "Fertility rate (% or absolute)",'F'); eoHowMany fRate = fertileRateParam.value(); eoValueParam tournamentSizeParam = parser.createParam(unsigned(2), "tournamentSize", "Deterministic tournament size",'T'); unsigned int tSize = tournamentSizeParam.value(); eoValueParam tournamentRateParam = parser.createParam(1.0, "tournamentRate", "Stochastic tournament rate",'t'); double tRate = tournamentRateParam.value(); eoValueParam rankingPressureParam = parser.createParam(2.0, "rankingPressure", "Selective pressure for the ranking selection",'p'); double rankingPressure = rankingPressureParam.value(); eoValueParam rankingExponentParam = parser.createParam(1.0, "rankingExponent", "Exponent for the ranking selection",'e'); double rankingExponent = rankingExponentParam.value(); eoValueParam fitTypeParam = parser.createParam(std::string("linear"), "fitType", "Type of fitness (linear, exp, log, super",'f'); fitnessType = fitTypeParam.value(); if (parser.userNeedsHelp()) { parser.printHelp(std::cout); exit(0); } // hard-coded directory name ... system("mkdir ResSelect"); std::cout << "Testing the Selections\nParents size = " << pSize << ", offspring rate = " << oRate; std::cout << " and putting rsulting files in dir ResSelect" << std::endl; // initialize parent population parentsOrg.resize(pSize); if (fitnessType == std::string("linear")) for (unsigned i=0; i& seedParam = parser.createParam(uint32_t(0), "seed", "Random number seed", 'S'); if (seedParam.value() == 0) seedParam.value() = time(0); rng.reseed(seedParam.value()); char fileName[1024]; // the selection procedures under test // eoDetSelect detSelect(oRate); // testSelectMany(detSelect, "detSelect"); // Roulette eoProportionalSelect propSelect; testSelectOne(propSelect, oRate, fRate, "PropSelect"); // Linear ranking using the perf2Worth construct eoRankingSelect newRankingSelect(rankingPressure); sprintf(fileName,"LinRank_%g",rankingPressure); testSelectOne(newRankingSelect, oRate, fRate, fileName); // Exponential ranking using the perf2Worth construct std::cout << "rankingExponent " << rankingExponent << std::endl; eoRankingSelect expRankingSelect(rankingPressure,rankingExponent); sprintf(fileName,"ExpRank_%g_%g",rankingPressure, rankingExponent); testSelectOne(expRankingSelect, oRate, fRate, fileName); // Det tournament eoDetTournamentSelect detTourSelect(tSize); sprintf(fileName,"DetTour_%d",tSize); testSelectOne(detTourSelect, oRate, fRate, fileName); // Stoch tournament eoStochTournamentSelect stochTourSelect(tRate); sprintf(fileName,"StochTour_%g",tRate); testSelectOne(stochTourSelect, oRate, fRate, fileName); // Fitness scaling eoFitnessScalingSelect newFitScaleSelect(rankingPressure); sprintf(fileName,"LinFitScale_%g",rankingPressure); testSelectOne(newFitScaleSelect, oRate, fRate, fileName); // Sequential selections eoSequentialSelect seqSel(false); strcpy(fileName,"Sequential"); testSelectOne(seqSel, oRate, fRate, fileName); eoEliteSequentialSelect eliteSeqSel; strcpy(fileName,"EliteSequential"); testSelectOne(eliteSeqSel, oRate, fRate, fileName); return 1; } int main(int argc, char **argv) { try { the_main(argc, argv); } catch(std::exception& e) { std::cout << "Exception: " << e.what() << std::endl; return 1; } }