#include //#include #include #include using namespace std; // Look: overloading the maximization without overhead (thing can be inlined) class MinimizingFitnessTraits : public eoParetoFitnessTraits { public : static bool maximizing(int) { return false; } }; typedef eoParetoFitness fitness_type; const unsigned chromsize=3; const double minval = -5; const double maxval = 5; struct eoDouble : public EO { double value[chromsize]; }; class Mutate : public eoMonOp { bool operator()(eoDouble& _eo) { for (unsigned i = 0; i < chromsize; ++i) { if (rng.flip(1./10.)) _eo.value[i] += rng.normal() * 0.05 * _eo.value[i]; if (_eo.value[i] < minval) _eo.value[i] = minval; else if (_eo.value[i] > maxval) _eo.value[i] = maxval; } return true; } }; class Eval : public eoEvalFunc { void operator()(eoDouble& _eo) { vector x(_eo.value, _eo.value + chromsize); fitness_type f; for (unsigned i = 0; i < chromsize; ++i) { if (i < chromsize-1) { f[0] += -10.0 * exp(-0.2 * sqrt(x[i]*x[i] + x[i+1]*x[i+1])); } f[1] += pow(fabs(x[i]), 0.8) + 5 * pow(sin(x[i]),3.); } _eo.fitness(f); } }; class Init : public eoInit { void operator()(eoDouble& _eo) { _eo.value[0] = rng.uniform(); for (unsigned i = 1; i < chromsize; ++i) _eo.value[i] = rng.uniform() * 10. - 5; _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 = 100; eoPop pop(pop_size, init); eoDominanceMap dominance; // 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); // One general operator eoProportionalOp opsel; opsel.add(mutate, 1.0); // Three breeders eoGeneralBreeder breeder1(select1, opsel); eoGeneralBreeder breeder2(select2, opsel); eoGeneralBreeder breeder3(select3, opsel); // 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 } }