diff --git a/eo/test/t-eoPareto.cpp b/eo/test/t-eoPareto.cpp index 8aa8c05d..15c5149e 100644 --- a/eo/test/t-eoPareto.cpp +++ b/eo/test/t-eoPareto.cpp @@ -77,38 +77,75 @@ class Init : public eoInit } }; -// Test pareto dominance and perf2worth, and while you're at it, test the eoGnuPlot monitor as well -void the_main() +template +eoPerf2Worth& make_perf2worth(eoParser& parser, eoState& state) +{ + eoDominanceMap& dominance = state.storeFunctor(new eoDominanceMap); + + unsigned what = parser.createParam(unsigned(0), "perf2worth", "worth mapping indicator : \n\t \ + 0: non_dominated sorting \n\t\ + 1: non_dominated sorting 2 \n\t\ + 2: simple ranking \n\t", 'w').value(); + + switch (what) + { + case 1 : return state.storeFunctor(new eoNDSorting_II(dominance)); + case 2 : return state.storeFunctor(new eoParetoRanking(dominance)); + } + //default + + if (what > 2) + { + cout << "Warning, need an integer < 3 for perf2worth" << endl; + // should actually set parser flag, but I don't care + } + + return state.storeFunctor(new eoNDSorting_I(dominance, 0.5)); +} + +template +eoSelectFromWorth& make_selector(eoParser& parser, eoState& state, eoPerf2Worth& perf2worth) +{ + unsigned tournamentsize = 2; + double stochtour = 0.95; + + switch (parser.createParam(unsigned(0), "selector", "Which selector (too lazy to explain: use the source)", 's').value()) + { + case 1 : return state.storeFunctor(new eoStochTournamentWorthSelect(perf2worth, stochtour)); + case 2 : return state.storeFunctor(new eoRouletteWorthSelect(perf2worth)); + } + // default + + return state.storeFunctor(new eoDetTournamentWorthSelect(perf2worth, tournamentsize)); +} + +// Test pareto dominance and perf2worth, and while you're at it, test the eoGnuPlot monitor as well +void the_main(int argc, char* argv[]) { Init init; Eval eval; Mutate mutate; - unsigned num_gen = 10; - unsigned pop_size = 100; + eoParser parser(argc, argv); + eoState state; + + unsigned num_gen = parser.createParam(unsigned(10), "num_gen", "number of generations to run", 'g').value(); + unsigned pop_size = parser.createParam(unsigned(100), "pop_size", "population size", 'p').value(); eoPop pop(pop_size, init); - eoDominanceMap dominance; + // Look, a factory function + eoPerf2Worth& perf2worth = make_perf2worth(parser, state); - // Pareto ranking needs a dominance map - //eoParetoRanking perf2worth(dominance); - //eoNDSorting_I perf2worth(dominance, 0.5); - eoNDSorting_II perf2worth(dominance); - - // Three selectors - eoDetTournamentWorthSelect select1(perf2worth, 3); - eoStochTournamentWorthSelect select2(perf2worth, 0.95); - eoRouletteWorthSelect select3(perf2worth); + // Look: another factory function, now for selection + eoSelectFromWorth& select = make_selector(parser, state, perf2worth); // One general operator eoProportionalOp opsel; opsel.add(mutate, 1.0); - // Three breeders - eoGeneralBreeder breeder1(select1, opsel); - eoGeneralBreeder breeder2(select2, opsel); - eoGeneralBreeder breeder3(select3, opsel); + // the breeder + eoGeneralBreeder breeder(select, opsel); // replacement eoCommaReplacement replace; @@ -131,33 +168,19 @@ void the_main() snapshot.add(fitness0); snapshot.add(fitness1); - // Three algos - eoEasyEA ea1(cp, eval, breeder1, replace); - eoEasyEA ea2(cp, eval, breeder2, replace); - eoEasyEA ea3(cp, eval, breeder3, replace); + // the algo + eoEasyEA ea(cp, eval, breeder, replace); apply(eval, pop); - ea1(pop); - - apply(init, pop); - apply(eval, pop); - generation = 0; - - ea2(pop); - apply(init, pop); - apply(eval, pop); - generation = 0; - - ea3(pop); - + ea(pop); } -int main() +int main(int argc, char* argv[]) { try { - the_main(); + the_main(argc, argv); } catch (exception& e) {