From ab0fc52a5c7573f2392f37f56085209a630b8b40 Mon Sep 17 00:00:00 2001 From: legrand Date: Thu, 21 Dec 2006 13:34:36 +0000 Subject: [PATCH] Sch example with NSGAII added as lesson2 git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@112 331e1502-861f-0410-8da2-ba01fb791d7f --- .../tutorials/lesson2/Makefile.am | 9 ++ .../paradiseo-moeo/tutorials/lesson2/Sch1.cpp | 93 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 trunk/paradiseo-moeo/tutorials/lesson2/Makefile.am create mode 100644 trunk/paradiseo-moeo/tutorials/lesson2/Sch1.cpp diff --git a/trunk/paradiseo-moeo/tutorials/lesson2/Makefile.am b/trunk/paradiseo-moeo/tutorials/lesson2/Makefile.am new file mode 100644 index 000000000..ee9b121d9 --- /dev/null +++ b/trunk/paradiseo-moeo/tutorials/lesson2/Makefile.am @@ -0,0 +1,9 @@ + +noinst_PROGRAMS = Sch1 + +Sch1_SOURCES = Sch1.cpp + +LDADD = -L$(top_builddir)/src ${EO_DIR}/src/libeo.a ${EO_DIR}/src/utils/libeoutils.a + +INCLUDES = -I${EO_DIR}/src/ -I$(top_srcdir)/src + diff --git a/trunk/paradiseo-moeo/tutorials/lesson2/Sch1.cpp b/trunk/paradiseo-moeo/tutorials/lesson2/Sch1.cpp new file mode 100644 index 000000000..54255e60e --- /dev/null +++ b/trunk/paradiseo-moeo/tutorials/lesson2/Sch1.cpp @@ -0,0 +1,93 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +/* (c) OPAC Team, LIFL, October 2006 + + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +using namespace std; + +#include + +/* ParadisEO-EO */ +#include +#include + +/* ParadisEO-MOEO */ +#include +#include + + +// Extend eoParetoFitnessTraits +class SCH1Traits : public eoParetoFitnessTraits +{ +public : +static bool maximizing(int i) { return false; } // is the i-th objective +static unsigned nObjectives() { return 2;} // number of objectives +}; + +// Code decision variables +typedef eoParetoFitness SCH1Fit; + +class SCH1EO : public eoReal +{ +public: + SCH1EO(): eoReal(1) + {} +}; + +// evaluation of the individuals +class SCH1Eval : public eoEvalFunc +{ +public: + SCH1Eval(): eoEvalFunc() + {} + + void operator()(SCH1EO & _eo) { + SCH1Fit fitness; + double x = _eo[0]; + + fitness[0] = x*x; + fitness[1] = (x-2.0)*(x-2.0); + + _eo.fitness(fitness); + } +}; + +int main(int argc, char* argv[]) { + + unsigned POP_SIZE = 20; + unsigned MAX_GEN = 100; + double M_EPSILON = 0.01; + double P_CROSS = 0.25; + double P_MUT = 0.35; + + // The fitness evaluation + SCH1Eval eval; + + // choose crossover and mutation + eoQuadCloneOp xover; + eoUniformMutation mutation(M_EPSILON); + + // generate initial population + eoRealVectorBounds bounds(1, 0.0, 2.0); // [0, 2] + eoRealInitBounded init(bounds); + eoPop pop(POP_SIZE, init); + + // pass parameters to NSGA2 + moeoNSGA_II nsga2(MAX_GEN, eval, xover, P_CROSS, mutation, P_MUT); + + // run the algo + nsga2(pop); + + // extract first front of the final population (this is the solution of nsga2) + moeoArchive arch; + arch.update(pop); + + // printing of the final archive + cout << "Final Archive\n"; + arch.sortedPrintOn(cout); + cout << endl; + + return EXIT_SUCCESS; +}