From da6685830866478789dd1252cb1b4b86fe0058bd Mon Sep 17 00:00:00 2001 From: evomarc Date: Thu, 25 Jan 2001 16:14:25 +0000 Subject: [PATCH] Full t-eoSelect file, that allows to test all selectors feature: generation of files suitable for gnuplot to see how individual spread out --- eo/test/t-eoSelect.cpp | 112 ++++++++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 23 deletions(-) diff --git a/eo/test/t-eoSelect.cpp b/eo/test/t-eoSelect.cpp index b0bdcbbe..e136e6dc 100644 --- a/eo/test/t-eoSelect.cpp +++ b/eo/test/t-eoSelect.cpp @@ -15,7 +15,7 @@ // general #include -#include +#include //----------------------------------------------------------------------------- struct Dummy : public EO @@ -28,6 +28,10 @@ struct Dummy : public EO } }; +bool operator==(const Dummy & _d1, const Dummy & _d2) +{ + return _d1.fitness() == _d2.fitness(); +} struct eoDummyPop : public eoPop { @@ -35,26 +39,84 @@ 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! + +template +void testSelectMany(eoSelect & _select, string _name) +{ + cout << "\n\n" << _name << endl; + cout << "===============\n"; + + eoDummyPop parents(pSize); + eoDummyPop offspring(0); + + // initialize parents + for (unsigned i=0; i nb(parents.size(), 0); + for (unsigned i=0; i(offspring[i], parents); + if (trouve == parents.size()) // pas trouve + throw runtime_error("Pas trouve ds parents"); + nb[trouve]++; + } + // dump to file so you can plot using gnuplot + string fName = _name + ".prop"; + ofstream os(fName.c_str()); + for (unsigned i=0; i " << ( (double)nb[i])/offspring.size() << endl; + os << i << " " << ( (double)nb[i])/offspring.size() << endl; + } + +} + +template +void testSelectOne(eoSelectOne & _select, double _rate, string _name) +{ + eoSelectMany percSelect(_select, _rate); + testSelectMany(percSelect, _name); +} + + //----------------------------------------------------------------------------- int the_main(int argc, char **argv) { eoParser parser(argc, argv); eoValueParam parentSizeParam = parser.createParam(10, "parentSize", "Parent size",'P'); - unsigned int pSize = parentSizeParam.value(); + pSize = parentSizeParam.value(); // global variable eoValueParam offsrpringRateParam = parser.createParam(1.0, "offsrpringRate", "Offsrpring rate",'O'); double oRate = offsrpringRateParam.value(); - eoValueParam interpretAsRateParam = parser.createParam(true, "interpretAsRate", "interpret rate as Rate (False = as Number)",'b'); - bool interpretAsRate = interpretAsRateParam.value(); - eoValueParam tournamentSizeParam = parser.createParam(2, "tournamentSize", "Deterministic tournament size",'T'); unsigned int tSize = tournamentSizeParam.value(); eoValueParam tournamentRateParam = parser.createParam(0.75, "tournamentRate", "Stochastic tournament rate",'R'); double tRate = tournamentRateParam.value(); + eoValueParam rankingPressureParam = parser.createParam(1.75, "rankingPressure", "Selective pressure for the ranking selection",'p'); + double rankingPressure = rankingPressureParam.value(); + if (parser.userNeedsHelp()) { parser.printHelp(cout); @@ -64,30 +126,34 @@ eoValueParam tournamentSizeParam = parser.createParam detSelect(oRate, interpretAsRate); + eoDetSelect detSelect(oRate); + testSelectMany(detSelect, "detSelect"); - // here we go - // Deterministic - cout << "eoDetSelect\n"; - cout << "===========\n"; - detSelect(parents, offspring); -cout << "Selected offsprings (origonally all even\n" << offspring << endl; + // Roulette + eoProportionalSelect propSelect; + testSelectOne(propSelect, oRate, "propSelect"); + + // Ranking + eoRankingSelect rankSelect(rankingPressure); + testSelectOne(rankSelect, oRate, "rankSelect"); + + // Det tournament + eoDetTournamentSelect detTourSelect(tSize); + testSelectOne(detTourSelect, oRate, "detTourSelect"); + + // Stoch tournament + eoStochTournamentSelect stochTourSelect(tRate); + testSelectOne(stochTourSelect, oRate, "stochTourSelect"); + + // Fitness scaling + eoFitnessScalingSelect fitScaleSelect(rankingPressure); + testSelectOne(fitScaleSelect, oRate, "fitScaleSelect"); return 1; }