diff --git a/eo/src/eo b/eo/src/eo index 98bc61ab..54c4caf1 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -66,6 +66,8 @@ #include #include +#include +#include #include @@ -91,11 +93,13 @@ #include #include #include -// other batch selections -// DetSelect probably shoudl be turned into an eoSelectOne +// other batch selections +// DetSelect probably shoudl be turned into an eoSelectOne // (using setup and an index) #include +// Breeders +#include // Replacement // #include @@ -118,6 +122,7 @@ #include // aliens +#include #include //----------------------------------------------------------------------------- diff --git a/eo/src/eoBreed.h b/eo/src/eoBreed.h index 53498344..8eca4b11 100644 --- a/eo/src/eoBreed.h +++ b/eo/src/eoBreed.h @@ -1,9 +1,9 @@ /** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- ----------------------------------------------------------------------------- - eoBreed.h + eoBreed.h (c) Maarten Keijzer, GeNeura Team, 2000 - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -29,9 +29,11 @@ //----------------------------------------------------------------------------- #include #include +#include +#include //----------------------------------------------------------------------------- -/** +/** eoBreed: breeding is thought of a combination of selecting and transforming a population. For efficiency reasons you might want to build your own eoBreed derived class rather than relying on a seperate select and transform @@ -44,7 +46,7 @@ class eoBreed : public eoBF&, eoPop&, void> {}; /** -eoSelectTransform: special breeder that is just an application of an embedded select, +eoSelectTransform: special breeder that is just an application of an embedded select, followed by an embedded transform */ template diff --git a/eo/src/eoGenOp.h b/eo/src/eoGenOp.h index b24dad7d..3f46137c 100644 --- a/eo/src/eoGenOp.h +++ b/eo/src/eoGenOp.h @@ -58,7 +58,11 @@ class eoGenOp : public eoOp, public eoUF &, void> /// Ctor that honors its superclass eoGenOp(): eoOp( eoOp::general ) {} + /** Max production is used to reserve space for all elements that are used by the operator, + not setting it properly can result in a crash + */ virtual unsigned max_production(void) = 0; + virtual string className() = 0; void operator()(eoPopulator& _pop) { @@ -103,18 +107,18 @@ class eoBinGenOp : public eoGenOp public: eoBinGenOp(eoBinOp& _op) : op(_op) {} - unsigned max_production(void) { return 1; } + unsigned max_production(void) { return 2; } //2 as it will request two individuals /** do the work: get 2 individuals from the population, modifies - only one (it's a eoBinOp) and erases the non-midified one + only one (it's a eoBinOp) */ void apply(eoPopulator& _pop) { EOT& a = *_pop; - EOT& b = *++_pop; + const EOT& b = _pop.select(); + if (op(a, b)) a.invalidate(); - _pop.erase(); // erase the b from the next population } string className() {return op.className();} @@ -206,6 +210,9 @@ class eoQuadGenOp : public eoGenOp case eoOp::quadratic : return _store.storeFunctor(new eoQuadGenOp(static_cast&>(_op))); case eoOp::general : return static_cast&>(_op); } + + assert(false); + return static_cast&>(_op); } #endif diff --git a/eo/src/eoGeneralBreeder.h b/eo/src/eoGeneralBreeder.h index 0c0e4c8f..4c1eff80 100644 --- a/eo/src/eoGeneralBreeder.h +++ b/eo/src/eoGeneralBreeder.h @@ -33,12 +33,16 @@ *****************************************************************************/ #include +#include #include +#include +#include +#include /** Base class for breeders using generalized operators. */ -template +template class eoGeneralBreeder: public eoBreed { public: @@ -49,10 +53,13 @@ class eoGeneralBreeder: public eoBreed * @param _rate pour howMany, le nbre d'enfants a generer * @param _interpret_as_rate explanation */ - eoGeneralBreeder( eoSelectOne& _select, eoGenOp& _op, - double _rate=1.0, bool _interpret_as_rate = true) : - select( _select ), op(_op), howMany(_rate, _interpret_as_rate) {} - + eoGeneralBreeder( + eoSelectOne& _select, + eoGenOp& _op, + double _rate=1.0, + bool _interpret_as_rate = true) : + select( _select ), op(_op), howMany(_rate, _interpret_as_rate) {} + /** The breeder: simply calls the genOp on a selective populator! * * @param _parents the initial population @@ -69,15 +76,16 @@ class eoGeneralBreeder: public eoBreed while (it.size() < target) { op(it); + ++it; } - + swap(_offspring, it); _offspring.resize(target); // you might have generated a few more } - + /// The class name. string className() const { return "eoGeneralBreeder"; } - + private: eoSelectOne& select; eoGenOp& op; diff --git a/eo/src/eoPopulator.h b/eo/src/eoPopulator.h index a16941f8..00ee03f5 100644 --- a/eo/src/eoPopulator.h +++ b/eo/src/eoPopulator.h @@ -45,7 +45,7 @@ public : struct OutOfIndividuals {}; /** a populator behaves like an iterator. Hence the operator* - * it returns the current individual -- eventually getting + * it returns the current individual -- eventually getting * a new one through the operator++ if at the end */ EOT& operator*(void) @@ -83,15 +83,6 @@ public : current = eoPop::insert(current, _eo); } - /** useful for operators that generate less offspring than parents - * though there is another way - using a selector *within* - * the operator to get the extra parents from outside - */ - void erase() - { - current = eoPop::erase(current); - } - /** just to make memory mangement more efficient */ void reserve(int how_many) @@ -115,11 +106,12 @@ public : /** no more individuals */ bool exhausted(void) { return current == end(); } + virtual const EOT& select() = 0; + protected : - /** the pure virtual selection method - will be instanciated in + /** the pure virtual selection method - will be instanciated in * eoSeqPopulator and eoPropPopulator */ - virtual const EOT& select() = 0; eoPop::iterator current; const eoPop& src; }; @@ -133,7 +125,7 @@ class eoSeqPopulator : public eoPopulator { public : - eoSeqPopulator(const eoPop& _pop) : + eoSeqPopulator(const eoPop& _pop) : eoPopulator(_pop), src_it(_pop.begin()) {} const EOT& select(void) @@ -149,7 +141,7 @@ public : private : vector::const_iterator src_it; -}; +}; /** SelectivePopulator an eoPoplator that uses an eoSelectOne to select guys. @@ -169,7 +161,7 @@ public : private : eoSelectOne& sel; -}; +}; #endif diff --git a/eo/src/eoRandomSelect.h b/eo/src/eoRandomSelect.h index fd609f3e..417c0e29 100644 --- a/eo/src/eoRandomSelect.h +++ b/eo/src/eoRandomSelect.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // eoRandomSelect.h // (c) GeNeura Team, 1998 - EEAAX 1999, Maarten Keijzer 2000 -/* +/* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -41,23 +41,23 @@ /** eoRandomSelect: a selection method that selects ONE individual randomly */ //----------------------------------------------------------------------------- -template class eoRandomSelect: public eoSelectOne +template class eoRandomSelect: public eoSelectOne { public: - + /// not a big deal!!! - virtual const EOT& operator()(const eoPop& _pop) + virtual const EOT& operator()(const eoPop& _pop) { - return _pop[eo::rng.random(pop.size())] ; + return _pop[eo::rng.random(_pop.size())] ; } }; //----------------------------------------------------------------------------- -/** eoBestSelect: a selection method that always return the best +/** eoBestSelect: a selection method that always return the best * (mainly for testing purposes) */ //----------------------------------------------------------------------------- -template class eoBestSelect: public eoSelectOne +template class eoBestSelect: public eoSelectOne { public: diff --git a/eo/src/other/eoExternalOpFunctions.h b/eo/src/other/eoExternalOpFunctions.h index 62fd757c..a96991e7 100644 --- a/eo/src/other/eoExternalOpFunctions.h +++ b/eo/src/other/eoExternalOpFunctions.h @@ -7,7 +7,7 @@ and 'genetic' operators (c) Maarten Keijzer (mkeijzer@mad.scientist.com) and GeNeura Team, 1999, 2000 - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -52,7 +52,7 @@ public : void operator()(ExternalEO& _eo) { - _eo = (*init)(); + _eo.External::operator=( (*init)() ); _eo.invalidate(); }