From 3c19641c70772e1e1ddd07b228eae8c0297cacf2 Mon Sep 17 00:00:00 2001 From: maartenkeijzer Date: Mon, 12 Mar 2001 16:03:08 +0000 Subject: [PATCH] Added pareto based stuff --- eo/test/Makefile.am | 11 +++- eo/test/t-eoGenOp.cpp | 41 ++++++++------ eo/test/t-eoPareto.cpp | 120 +++++++++++++++++++++++++++++++++++++++++ eo/test/t-eoSSGA.cpp | 25 +++++---- 4 files changed, 169 insertions(+), 28 deletions(-) create mode 100644 eo/test/t-eoPareto.cpp diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index 62c5fe62..9563bd84 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -10,10 +10,10 @@ DEPS = $(top_builddir)/src/utils/libeoutils.a $(top_builddir)/src/libeo.a INCLUDES = -I$(top_builddir)/src LDADDS = $(top_builddir)/src/utils/libeoutils.a $(top_builddir)/src/libeo.a -CXXFLAGS = -g -Wall -pg +CXXFLAGS = -g -Wall ############################################################################### -check_PROGRAMS = t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing t-eoSSGA \ +check_PROGRAMS = t-eoPareto t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing t-eoSSGA \ t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoVector TESTS=run_tests t-eoVector t-eoRandom t-eoSSGA # removing temporarily t-eoESFull @@ -121,3 +121,10 @@ t_eoSSGA_LDFLAGS = -lm t_eoSSGA_LDADD = $(LDADDS) ############################################################################### + +t_eoPareto_SOURCES = t-eoPareto.cpp +t_eoPareto_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a +t_eoPareto_LDFLAGS = -lm +t_eoPareto_LDADD = $(LDADDS) +############################################################################### + diff --git a/eo/test/t-eoGenOp.cpp b/eo/test/t-eoGenOp.cpp index 6fe36c11..b91ab576 100644 --- a/eo/test/t-eoGenOp.cpp +++ b/eo/test/t-eoGenOp.cpp @@ -225,7 +225,7 @@ int the_main(int argc, char **argv) sOpQuadPlusMon.add(quad, 1); sOpQuadPlusMon.add(mon, 1); - // this corresponds + // this corresponds eoProportionalOp pOpSAGLike; pOpSAGLike.add(sOpQuadPlusMon, 0.24); pOpSAGLike.add(quad, 0.56); @@ -234,10 +234,11 @@ int the_main(int argc, char **argv) // init eoPop pop; + eoPop offspring; init(pop, pSize); // sort pop so seqPopulator is identical to SelectPopulator(SequentialSelect) - pop.sort(); + pop.sort(); cout << "Population initiale\n" << pop << endl; // To simulate SGA: first a prop between quadOp and quadClone @@ -249,23 +250,25 @@ int the_main(int argc, char **argv) virtualSGA.add(pSGAOp, 1.0); virtualSGA.add(mon, 0.3); - eoSeqPopulator popit(pop); // no selection, a copy of pop + eoSeqPopulator popit(pop, offspring); // no selection, a copy of pop // until we filled a new population try { - while (popit.size() < pop.size()) - { - virtualSGA(popit); - } + while (offspring.size() < pop.size()) + { + virtualSGA(popit); + ++popit; + } } catch(eoPopulator::OutOfIndividuals&) { cout << "Warning: not enough individuals to handle\n"; } - - - swap(pop, popit); + + + swap(pop, offspring); + offspring.clear(); // ok, now print cout << "Apres virtualSGA \n" << pop << endl; @@ -276,14 +279,16 @@ int the_main(int argc, char **argv) eoSequentialSelect seqSelect; // select.init(); should be sorted out: is it the setup method??? - eoSelectivePopulator it_step3(pop, seqSelect); + eoSelectivePopulator it_step3(pop, offspring, seqSelect); - while (it_step3.size() < 2*pop.size()) + while (offspring.size() < 2*pop.size()) { virtualSGA(it_step3); + ++it_step3; } - swap(pop, it_step3); + swap(pop, offspring); + offspring.clear(); // ok, now print cout << "Apres SGA-like eoSelectivePopulator\n" << pop << endl; @@ -292,14 +297,16 @@ int the_main(int argc, char **argv) cout << "Now the pure addition !" << endl; init(pop, pSize); - eoSelectivePopulator it_step4(pop, seqSelect); - while (it_step4.size() < 2*pop.size()) + eoSelectivePopulator it_step4(pop, offspring, seqSelect); + while (offspring.size() < 2*pop.size()) { sOpQuadPlusMon(it_step4); + ++it_step4; } - swap(pop, it_step4); - + swap(pop, offspring); + offspring.clear(); + // ok, now print cout << "Apres Quad+Mon ds un eoSelectivePopulator\n" << pop << endl; diff --git a/eo/test/t-eoPareto.cpp b/eo/test/t-eoPareto.cpp new file mode 100644 index 00000000..6ed5c293 --- /dev/null +++ b/eo/test/t-eoPareto.cpp @@ -0,0 +1,120 @@ + +#include + +using namespace std; +typedef vector fitness_type; + +struct eoDouble : public EO +{ + double value; + + void printOn(ostream& os) const { os << fitness()[0] << ' ' << fitness()[1] << ' ' << value; } + void readFrom(istream& is) { is >> 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[0] = v * v; + f[1] = (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 + +void the_main() +{ + Init init; + Eval eval; + Mutate mutate; + + 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); + + // 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(10, generation); + eoCheckPoint cp(gen); + + // 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 + } +} + diff --git a/eo/test/t-eoSSGA.cpp b/eo/test/t-eoSSGA.cpp index b42db6ef..0dbe5cf4 100644 --- a/eo/test/t-eoSSGA.cpp +++ b/eo/test/t-eoSSGA.cpp @@ -1,6 +1,8 @@ #include +// tests a Steady State GA +// Needed to define this breeder, maybe make it a breeder template class eoBreedOne : public eoBreed { @@ -9,9 +11,9 @@ public : void operator()(const eoPop& _src, eoPop& _dest) { - eoSelectivePopulator pop(_src, select); + _dest.clear(); + eoSelectivePopulator pop(_src, _dest, select); op(pop); - _dest = pop; } private : @@ -72,15 +74,17 @@ int main() opsel.add(xover, 0.8); opsel.add(mutate, 0.2); -/* + eoDetTournamentSelect selector(3); eoBreedOne breed(selector, opsel); - eoSSGAWorseReplacement replace; -*/ - eoRandomSelect selector; - eoGeneralBreeder breed(selector, opsel); - eoPlusReplacement replace; + // Replace a single one + eoSSGAWorseReplacement replace; + + +// eoRandomSelect selector; +// eoGeneralBreeder breed(selector, opsel); +// eoPlusReplacement replace; eoMyEval eval; @@ -92,6 +96,7 @@ int main() eoPop pop(pop_size, init); + // evaluate apply(eval, pop); eoBestFitnessStat best("Best_Fitness"); @@ -100,11 +105,13 @@ int main() cp.add(best); cp.add(avg); - cp.add(mon); + +// cp.add(mon); mon.add(best); mon.add(avg); + // and run algo(pop); } \ No newline at end of file