diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index f2a7885b7..3efec4a15 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -13,7 +13,7 @@ LDADDS = $(top_builddir)/src/libeo.a $(top_builddir)/src/utils/libeoutils.a ############################################################################### -noinst_PROGRAMS = t-eofitness t-eobin t-eoStateAndParser t-eoCheckpointing t-eoExternalEO t-eoESFull t-eoSymreg t-eo +noinst_PROGRAMS = t-eofitness t-eobin t-eoStateAndParser t-eoCheckpointing t-eoExternalEO t-eoESFull t-eoSymreg t-eo t-eoReplacement ############################################################################### @@ -50,6 +50,13 @@ t_eoCheckpointing_LDADD = $(LDADDS) ############################################################################### +t_eoReplacement_SOURCES = t-eoReplacement.cpp +t_eoReplacement_DEPENDENCIES = $(DEPS) +t_eoReplacement_LDFLAGS = -lm +t_eoReplacement_LDADD = $(LDADDS) + +############################################################################### + t_eoExternalEO_SOURCES = t-eoExternalEO.cpp t_eoExternalEO_DEPENDENCIES = $(DEPS) t_eoExternalEO_LDFLAGS = -lm diff --git a/eo/test/t-eoReplacement.cpp b/eo/test/t-eoReplacement.cpp new file mode 100644 index 000000000..2949bdae8 --- /dev/null +++ b/eo/test/t-eoReplacement.cpp @@ -0,0 +1,206 @@ +//----------------------------------------------------------------------------- + +// to avoid long name warnings +#ifdef _MSC_VER +#pragma warning(disable:4786) +#endif + +#include // runtime_error + +//----------------------------------------------------------------------------- +// tt.cpp: +// +//----------------------------------------------------------------------------- + + +// general +#include +//----------------------------------------------------------------------------- + +struct Dummy : public EO +{ + typedef double Type; + void printOn(ostream & _os) const + { + _os << " - "; + EO::printOn(_os); + } +}; + + +struct eoDummyPop : public eoPop +{ +public : + eoDummyPop(int s=0) { resize(s); } +}; + +//----------------------------------------------------------------------------- + +int the_main(int argc, char **argv) +{ + eoParser parser(argc, argv); + eoValueParam& parentSizeParam = parser.createParam(10, "parentSize", "Parnet size",'P'); + unsigned int pSize = parentSizeParam.value(); + + eoValueParam& offsrpringSizeParam = parser.createParam(10, "offsrpringSize", "Offsrpring size",'O'); + unsigned int oSize = offsrpringSizeParam.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& sParentsElitismRateParam = parser.createParam(0.1, "sParentsElitismRateParam", "Strong elitism rate for parents",'E'); + double sParentsElitismRate = sParentsElitismRateParam.value(); + + eoValueParam& sParentsEugenismRateParam = parser.createParam(0, "sParentsEugenismRateParam", "Strong Eugenism rate",'e'); + double sParentsEugenismRate = sParentsEugenismRateParam.value(); + + eoValueParam& sOffspringElitismRateParam = parser.createParam(0, "sOffspringElitismRateParam", "Strong elitism rate for parents",'E'); + double sOffspringElitismRate = sOffspringElitismRateParam.value(); + + eoValueParam& sOffspringEugenismRateParam = parser.createParam(0, "sOffspringEugenismRateParam", "Strong Eugenism rate",'e'); + double sOffspringEugenismRate = sOffspringEugenismRateParam.value(); + + if (parser.userNeedsHelp()) + { + parser.printHelp(cout); + exit(1); + } + + unsigned i; + + cout << "Testing the replacements\nParents SIze = " << pSize + << " and offspring size = " << oSize << endl; + + rng.reseed(42); + + + eoDummyPop orgParents(pSize); + eoDummyPop orgOffspring(oSize); + + // initialize so we can recognize them later! + for (i=0; i genReplace; + eoPlusReplacement plusReplace; + eoCommaReplacement commaReplace; + eoWeakElitistReplacement weakElitistReplace(commaReplace); + // the SSGA replacements + eoSSGAWorseReplacement ssgaWorseReplace; + eoSSGADetTournamentReplacement ssgaDTReplace(tSize); + eoSSGAStochTournamentReplacement ssgaDSReplace(tRate); + + // here we go + // Generational + parents = orgParents; + offspring = orgOffspring; + + cout << "eoGenerationalReplacement\n"; + cout << "=========================\n"; + genReplace(parents, offspring); +cout << "Parents (originally odd)\n" << parents << "\n And offsprings (orogonally even\n" << offspring << endl; + + // Plus + parents = orgParents; + offspring = orgOffspring; + + cout << "eoPlusReplacement\n"; + cout << "=================\n"; + plusReplace(parents, offspring); +cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << endl; + + // Comma + parents = orgParents; + offspring = orgOffspring; + + if (parents.size() > offspring.size() ) + cout << "Skipping Comma Replacement, more parents than offspring\n"; + else + { + cout << "eoCommaReplacement\n"; + cout << "==================\n"; + commaReplace(parents, offspring); + cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << endl; + + // Comma with weak elitism + parents = orgParents; + offspring = orgOffspring; + + cout << "The same, with WEAK elitism\n"; + cout << "===========================\n"; + weakElitistReplace(parents, offspring); + cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << endl; + } + + // preparing SSGA replace worse + parents = orgParents; + offspring = orgOffspring; + + if (parents.size() < offspring.size() ) + cout << "Skipping all SSGA Replacements, more offspring than parents\n"; + else + { + cout << "SSGA replace worse\n"; + cout << "==================\n"; + ssgaWorseReplace(parents, offspring); + cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << endl; + + // SSGA deterministic tournament + parents = orgParents; + offspring = orgOffspring; + + cout << "SSGA deterministic tournament\n"; + cout << "=============================\n"; + ssgaDTReplace(parents, offspring); + cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << endl; + + // SSGA stochastic tournament + parents = orgParents; + offspring = orgOffspring; + + cout << "SSGA stochastic tournament\n"; + cout << "==========================\n"; + ssgaDTReplace(parents, offspring); + cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << endl; + } + + // the general replacement + eoDeterministicSaDReplacement sAdReplace(sParentsElitismRate, sParentsEugenismRate, sOffspringElitismRate, sOffspringEugenismRate);// 10% parents survive + + parents = orgParents; + offspring = orgOffspring; + + cout << "General - strong elitism\n"; + cout << "========================\n"; + sAdReplace(parents, offspring); + cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << endl; + + + return 1; +} + +int main(int argc, char **argv) +{ + try + { + the_main(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << endl; + } + + return 1; +}