From 7ec7a856e5b8ffc9b97189970da27c160dc97aa4 Mon Sep 17 00:00:00 2001 From: evomarc Date: Tue, 3 Apr 2001 17:14:08 +0000 Subject: [PATCH] Modified the tempaltes to take into account - the last modifications of the eopopulator class - the include files (were totally missing in the old templates) - JJ's demand for one class - one file :-) --- eo/tutorial/Templates/binCrossover.tmpl | 45 ++++++++++++ .../lessOffspringExternalSelectorGenOp.tmpl | 69 +++++++++++++++++++ .../lessOffspringSameSelectorGenOp.tmpl | 64 +++++++++++++++++ eo/tutorial/Templates/moreOffspringGenOp.tmpl | 65 +++++++++++++++++ eo/tutorial/Templates/mutation.tmpl | 20 ++++-- eo/tutorial/Templates/quadCrossover.tmpl | 45 ++++++++++++ 6 files changed, 301 insertions(+), 7 deletions(-) create mode 100644 eo/tutorial/Templates/binCrossover.tmpl create mode 100644 eo/tutorial/Templates/lessOffspringExternalSelectorGenOp.tmpl create mode 100644 eo/tutorial/Templates/lessOffspringSameSelectorGenOp.tmpl create mode 100644 eo/tutorial/Templates/moreOffspringGenOp.tmpl create mode 100644 eo/tutorial/Templates/quadCrossover.tmpl diff --git a/eo/tutorial/Templates/binCrossover.tmpl b/eo/tutorial/Templates/binCrossover.tmpl new file mode 100644 index 000000000..4bbd5f277 --- /dev/null +++ b/eo/tutorial/Templates/binCrossover.tmpl @@ -0,0 +1,45 @@ +/* +Template for simple binary crossover operators +============================================== + +Binary crossover operators modify the first parent only, +based on the second +*/ + +#ifndef eoMyDerivedBinOp_H +#define eoMyDerivedBinOp_H + +#include + +template +class eoMyDerivedBinOp: public eoBinOp +{ +public: + /** + * (Default) Constructor. + */ + eoMyDerivedBinOp(paramType _anyParameter) : + anyParameter(_anyParameter) {} + + /// The class name. Used to display statistics + string className() const { return "eoMyDerivedBinOp"; } + + /** + * eoBin crossover - modifies first parent only + * @param Indi1 The first parent + * @param Indi2 The second parent - const + */ + bool operator()(Indi& Indi1, const Indi& Indi2) + { + // do whatever needs to be done + // if Indi1 has been modified + return true; + // otherwise + // return false; + } + +protected: + paramType anyParameter +}; + +#endif diff --git a/eo/tutorial/Templates/lessOffspringExternalSelectorGenOp.tmpl b/eo/tutorial/Templates/lessOffspringExternalSelectorGenOp.tmpl new file mode 100644 index 000000000..0bd9bca2f --- /dev/null +++ b/eo/tutorial/Templates/lessOffspringExternalSelectorGenOp.tmpl @@ -0,0 +1,69 @@ +/* +Template for general operators +=============================== +i.e. that takes any number of parents and generates any number of offspring + +a GenOp that creates less offspring than there are parents + +Second version, get parents using an external eoSelectOne +*/ + +#ifndef eoLessOffspringExternalSelectorGenOp_H +#define eoLessOffspringExternalSelectorGenOp_H + +#include + +template +class eoLessOffspringExternalSelectorGenOp: public eoGenOp +{ +public: + /** + * (Default) Constructor. + */ + eoLessOffspringExternalSelectorGenOp(eoSelectOne & _sel, paramType _anyParameter) : + sel(_sel), anyParameter(_anyParameter) {} + + /// The class name. Used to display statistics + string className() const { return "eoLessOffspringExternalSelectorGenOp"; } + + /// 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) + { + // First, select as many parents as you will have offspring + EOT& parent1 = *_plop; // select the first parent + ++_plop; // advance once for each selected parents + ... + EOT& parentN = *_plop; // say you want N offspring + + // 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& parentN+K = sel(_plop.source()); + + // modify (in place) the "true" parents + // (i.e. parent1, ..., parentsN) + ... + + // invalidate fitnesses of modified parents + parent1.invalidate(); + ... + parentN.invalidate(); + } + + +protected: + eoSelectOne & sel; + paramType anyParameter +}; + +#endif diff --git a/eo/tutorial/Templates/lessOffspringSameSelectorGenOp.tmpl b/eo/tutorial/Templates/lessOffspringSameSelectorGenOp.tmpl new file mode 100644 index 000000000..79ca25743 --- /dev/null +++ b/eo/tutorial/Templates/lessOffspringSameSelectorGenOp.tmpl @@ -0,0 +1,64 @@ +/* +Template for general operators +=============================== +i.e. that takes any number of parents and generates any number of offspring + +a GenOp that creates less offspring than there are parents + +First version, get parents from populator using the ibbedded select() method +*/ + +#ifndef eoLessOffspringSameSelectorGenOp_H +#define eoLessOffspringSameSelectorGenOp_H + +#include + +template +class eoLessOffspringSameSelectorGenOp: public eoGenOp +{ +public: + /** + * (Default) Constructor. + */ + eoLessOffspringSameSelectorGenOp(paramType _anyParameter) : + anyParameter(_anyParameter) {} + + /// The class name. Used to display statistics + string className() const { return "eoLessOffspringSameSelectorGenOp"; } + + /// The TOTAL number of offspring (here = nb of remaining modified parents) + unsigned max_production(void) { return NbLeftParents; } + + /** + * eoLesOffspringSameSelectorGenOp operator - + * gets extra parents from the populator + * + * @param _pop a POPULATOR (not a simple population) + */ + void apply(eoPopulator& _plop) + { + // First, select as many parents as you will have offspring + EOT& parent1 = *_plop; // select the first parent + ++_plop; // advance once for each selected parents + ... + EOT& parentN = *_plop; // say you want N offspring + + // Now select extra parents from the populator + EOT& parentN+1 = _plop.select(); + ... + EOT& parentN+K = _plop.select(); + + // modify the first N parents + ... + + // oh right, and invalidate their fitnesses + parent1.invalidate(); + ... + parentN.invalidate(); + } + +protected: + paramType anyParameter +}; + +#endif \ No newline at end of file diff --git a/eo/tutorial/Templates/moreOffspringGenOp.tmpl b/eo/tutorial/Templates/moreOffspringGenOp.tmpl new file mode 100644 index 000000000..24c19b0be --- /dev/null +++ b/eo/tutorial/Templates/moreOffspringGenOp.tmpl @@ -0,0 +1,65 @@ +/* +Template for general operators +=============================== +i.e. that takes any number of parents and generates any number of offspring + +Here, a GenOp that creates more (or same number of) offspring +than there are parents +*/ + +#ifndef eoMoreOffspringGenOp_H +#define eoMoreOffspringGenOp_H + +#include + +template +class eoMoreOffspringGenOp: public eoGenOp +{ +public: + /** + * (Default) Constructor. + */ + eoMoreOffspringGenOp(paramType _anyParameter) : + anyParameter(_anyParameter) {} + + /// The class name. Used to display statistics + string className() const { return "eoMoreOffspringGenOp"; } + + /// The TOTAL number of offspring (including modified parents) + unsigned max_production(void) { return NbOffspring; } + + /** + * eoMoreOffspringGenOp 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; // advance before each insertion + _plop.insert(ofs1); + ... + ++_plop; // advance before each insertion + _plop.insert(ofsN); + + // oh right, and invalidate fitnesses of modified parents + parent1.invalidate(); + ... + parentN.invalidate(); + } + + +protected: + paramType anyParameter +}; + +#endif \ No newline at end of file diff --git a/eo/tutorial/Templates/mutation.tmpl b/eo/tutorial/Templates/mutation.tmpl index 30b1724a4..824904e5c 100644 --- a/eo/tutorial/Templates/mutation.tmpl +++ b/eo/tutorial/Templates/mutation.tmpl @@ -1,11 +1,16 @@ -Template for simple operators -============================= +/* +Template for simple mutation operators +====================================== +*/ -=========================================================================== -eoMon : mutation operators -====== +#ifndef eoMyDerivedMonOp_H +#define eoMyDerivedMonOp_H -template class eoMyDerivedMonOp: public eoMonOp + +#include + +template +class eoMyDerivedMonOp: public eoMonOp { public: /** @@ -18,7 +23,7 @@ public: string className() const { return "eoMyDerivedMonOp"; } /** - * eoMon crossover - modifies the parent + * modifies the parent * @param Indi The parent */ bool operator()(Indi& Indi) @@ -34,3 +39,4 @@ protected: paramType anyParameter }; +#endif diff --git a/eo/tutorial/Templates/quadCrossover.tmpl b/eo/tutorial/Templates/quadCrossover.tmpl new file mode 100644 index 000000000..a5e598bdd --- /dev/null +++ b/eo/tutorial/Templates/quadCrossover.tmpl @@ -0,0 +1,45 @@ +/* +Template for simple quadratic crossover operators +================================================= + +Quadratic crossover operators modify the both parents +*/ + +#ifndef eoMyDerivedQuadOp_H +#define eoMyDerivedQuadOp_H + +#include + +template +class eoMyDerivedQuadOp: public eoQuadOp +{ +public: + /** + * (Default) Constructor. + */ + eoMyDerivedQuadOp(paramType _anyParameter) : + anyParameter(_anyParameter) {} + + /// The class name. Used to display statistics + string className() const { return "eoMyDerivedQuadOp"; } + + /** + * eoQuad crossover - modifies both parents + * @param Indi1 The first parent + * @param Indi2 The second parent + */ + bool operator()(Indi& Indi1, Indi& Indi2) + { + // do whatever needs to be done + + // if at least one individual has been modified - no way to distinguish + return true; + // otherwise + // return false; + } + +protected: + paramType anyParameter +}; + +#endif