From 1662fd2a8baac63d8e5b0bcb90f8a77f35cfffdd Mon Sep 17 00:00:00 2001 From: evomarc Date: Sat, 17 Feb 2001 06:42:20 +0000 Subject: [PATCH] Modified crossover and mutation to account for the new interface (bool ...) Added the genop.tmpl, template for general op, 3 cases, growing pop, shrinking pop through erase, thrinking pop through extra selector --- eo/tutorial/Templates/crossover.tmpl | 17 +-- eo/tutorial/Templates/genop.tmpl | 171 +++++++++++++++++++++++++++ eo/tutorial/Templates/mutation.tmpl | 9 +- 3 files changed, 186 insertions(+), 11 deletions(-) create mode 100644 eo/tutorial/Templates/genop.tmpl diff --git a/eo/tutorial/Templates/crossover.tmpl b/eo/tutorial/Templates/crossover.tmpl index ed8344c9..db1d054f 100644 --- a/eo/tutorial/Templates/crossover.tmpl +++ b/eo/tutorial/Templates/crossover.tmpl @@ -22,13 +22,14 @@ public: * @param Indi1 The first parent * @param Indi2 The first parent */ - void operator()(Indi& Indi1, Indi& Indi2) + bool operator()(Indi& Indi1, Indi& Indi2) { // do whatever needs to be done -// DON'T FORGET if an individual is modified, to invalidate its fitness!!! - Indi1.invalidate(); - Indi2.invalidate(); + // if at least one individual has been modified + return true; + // otherwise + // return false; } protected: @@ -56,11 +57,13 @@ public: * @param Indi1 The first parent * @param Indi2 The first parent - const */ - void operator()(Indi& Indi1, const Indi& Indi2) + bool operator()(Indi& Indi1, const Indi& Indi2) { // do whatever needs to be done - // DON'T FORGET if an individual is modified, to invalidate its fitness!!! - Indi1.invalidate(); + // if Indi1 has been modified + return true; + // otherwise + // return false; } protected: diff --git a/eo/tutorial/Templates/genop.tmpl b/eo/tutorial/Templates/genop.tmpl new file mode 100644 index 00000000..3f0bebc3 --- /dev/null +++ b/eo/tutorial/Templates/genop.tmpl @@ -0,0 +1,171 @@ +Template for simple operators +============================= + +=========================================================================== +eoGen : general operator that takes any number of parents and generates +====== any number of offspring + +First, a GenOp that creates more offspring than there are parents +----------------------------------------------------------------- + +template class eoMyDerivedGrowGenOp: public eoGenOp +{ +public: + /** + * (Default) Constructor. + */ + eoMyDerivedGrowGenOp(paramType _anyParameter) : + anyParameter(_anyParameter) {} + + /// The class name. Used to display statistics + string className() const { return "eoMyDerivedGrowGenOp"; } + + /// The TOTAL number of offspring (including modified parents) + unsigned max_production(void) { return NbOffspring; } + + /** + * eoGrowGen operator - eventually modifies the parents + * BUT does generate more offspring + * + * @param _pop a POPULATOR (not a simple population) + */ + void apply(eoPopulator& _plop) + { + EOT& parent1 = *_plop; // select the first parent + ++_plop; // advance once for each selected parents + ... + EOT& parentN = *_plop; // select the last parent + // don't advance after the last one: _plop always + // points to the last that has already been treated + + // apply operator to the parents (modifying them AND generating + // new individuals ofs1, ofs2, ..., ofsN + _plop.insert(ofs1); + ++_plop; // advance after each insertion + ... + _plop.insert(ofsN); + // don't advance after the last one: _plop always + // points to the last that has already been treated + + // oh right, and invalidate fitnesses of modified parents + parent1.invalidate(); + ... + parentN.invalidate(); + } + + +protected: + paramType anyParameter +}; + +=========================================================================== +Now, a GenOp that creates less offspring than there are parents +--------------------------------------------------------------- +First version, get parents from populator, and erase some of them +----------------------------------------------------------------- + +template class eoMyDerivedShrink1GenOp: public eoGenOp +{ +public: + /** + * (Default) Constructor. + */ + eoMyDerivedShrink1GenOp(paramType _anyParameter) : + anyParameter(_anyParameter) {} + + /// The class name. Used to display statistics + string className() const { return "eoMyDerivedShrink1GenOp"; } + + /// The TOTAL number of offspring (here = nb of remaining modified parents) + unsigned max_production(void) { return NbLeftParents; } + + /** + * eoShrinkGen operator - modifies some of the parents + * and kills the rest + * + * @param _pop a POPULATOR (not a simple population) + */ + void apply(eoPopulator& _plop) + { + EOT& parent1 = *_plop; // select the first parent + ++_plop; // advance once for each selected parents + ... + EOT& parentN = *_plop; // select the last parent + // don't advance after the last one: _plop always + // points to the last that has already been treated + + // modify some of the parents (the first ones + // as erase removes backwards) + ... + + // then kill the ones that are now useless + _plop.erase(); // as many times as necessary + ... + // oh right, and invalidate fitnesses of remaining modified parents + parent1.invalidate(); + ... + parentK.invalidate(); + } + + +protected: + paramType anyParameter +}; + +=========================================================================== +Now, a GenOp that creates less offspring than there are parents +--------------------------------------------------------------- +Second version, get parents from an external selector (no erasing) +------------------------------------------------------------------ + +template class eoMyDerivedShrink2GenOp: public eoGenOp +{ +public: + /** + * (Default) Constructor. + */ + eoMyDerivedShrink2GenOp(paramType _anyParameter) : + anyParameter(_anyParameter) {} + + /// The class name. Used to display statistics + string className() const { return "eoMyDerivedShrink2GenOp"; } + + /// The TOTAL number of offspring (here = nb of parents modified in place) + unsigned max_production(void) { return NbLeftParents; } + + /** + * eoShrinkGen operator - modifies some parents in the populator + * using extra "parents" selected from an external selector + * + * @param _pop a POPULATOR (not a simple population) + */ + void apply(eoPopulator& _plop) + { + EOT& parent1 = *_plop; // select the first parent + ++_plop; // advance once for each selected parents + ... + EOT& parentN = *_plop; // select the last parent you need + // don't advance after the last one: _plop always + // points to the last that has already been treated + + // get extra parents - use private selector + // _plop.source() is the eoPop used by _plop to get parents + // WARNING: you are not allowed to modify them (mandatory "const") + const EOT& parentN+1 = sel(_plop.source()); + ... + const EOT& parentM = sel(_plop.source()); + + // modify (in place) the "true" parents + // (i.e. parent1, ..., parentsN) + ... + + // invalidate fitnesses of modified parents + parent1.invalidate(); + ... + parentN.invalidate(); + } + + +protected: + paramType anyParameter +}; diff --git a/eo/tutorial/Templates/mutation.tmpl b/eo/tutorial/Templates/mutation.tmpl index 3622e214..30b1724a 100644 --- a/eo/tutorial/Templates/mutation.tmpl +++ b/eo/tutorial/Templates/mutation.tmpl @@ -21,12 +21,13 @@ public: * eoMon crossover - modifies the parent * @param Indi The parent */ - void operator()(Indi& Indi) + bool operator()(Indi& Indi) { // do whatever needs to be done - -// DON'T FORGET if an individual is modified, to invalidate its fitness!!! - Indi.invalidate(); + // if Indi has been modified + return true; + // otherwise + // return false; } protected: