#include //#include #include using namespace std; typedef vector fitness_type; struct eoDouble : public EO { double value; }; class Mutate : public eoMonOp { bool operator()(eoDouble& _eo) { _eo.value += rng.normal() * 0.1 * _eo.value; return true; } }; class Eval : public eoEvalFunc { void operator()(eoDouble& _eo) { double v = _eo.value; fitness_type f(2); f[1] = v * v; f[0] = (v - 1.) * (v - 1.); _eo.fitness(f); } }; class Init : public eoInit { void operator()(eoDouble& _eo) { _eo.value = rng.normal() * 10.; _eo.invalidate(); } }; // Test pareto dominance and perf2worth, and while you're at it, test the eoGnuPlot monitor as well void the_main() { Init init; Eval eval; Mutate mutate; unsigned num_gen = 10; unsigned pop_size = 50; eoPop pop(pop_size, init); vector maximizes(2, false); // minimize both objectives // The dominance map needs to know how to compare eoDominanceMap dominance(maximizes); // Pareto ranking needs a dominance map //eoParetoRanking perf2worth(dominance); eoNDSorting perf2worth(dominance, 0.0); // Three selectors eoDetTournamentWorthSelect select1(perf2worth, 3); eoStochTournamentWorthSelect select2(perf2worth, 0.95); eoRouletteWorthSelect select3(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); // Comma replacement eoCommaReplacement replace; unsigned long generation = 0; eoGenContinue gen(num_gen, generation); eoCheckPoint cp(gen); eoMOFitnessStat fitness0(0, "FirstObjective"); eoMOFitnessStat fitness1(1, "SecondObjective"); cp.add(fitness0); cp.add(fitness1); eoGnuplot1DSnapshot snapshot("pareto"); snapshot.pointSize =3; cp.add(snapshot); 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); 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); } int main() { try { the_main(); } catch (exception& e) { cout << "Exception thrown: " << e.what() << endl; throw e; // make sure it does not pass the test } }