diff --git a/eo/src/eoInclusion.h b/eo/src/eoInclusion.h index deb82b601..709cb0c01 100644 --- a/eo/src/eoInclusion.h +++ b/eo/src/eoInclusion.h @@ -27,21 +27,16 @@ template class eoInclusion: public eoMerge * @param breeders The population of breeders. * @param pop The original population. */ - void operator()(const eoPop& breeders, eoPop& pop) + void operator()(eoPop& breeders, eoPop& pop) { - eoPop all, tmp = breeders; + unsigned target = min(static_cast(rint(pop.size() * rate())), + pop.size() + breeders.size()); - sort(tmp.begin(), tmp.end()); - sort(pop.begin(), pop.end()); - - merge(tmp.begin(), tmp.end(), - pop.begin(), pop.end(), - back_insert_iterator >(all)); - - all.erase(all.begin(), - all.begin() + (unsigned)(all.size() - pop.size() * rate())); - - pop.swap(all); + copy(breeders.begin(), breeders.end(), + back_insert_iterator >(pop)); + partial_sort(pop.begin(), pop.begin() + target, pop.end(), + greater()); + pop.erase(pop.begin() + target, pop.end()); } }; diff --git a/eo/src/eoInsertion.h b/eo/src/eoInsertion.h index 994645e13..be0f43739 100644 --- a/eo/src/eoInsertion.h +++ b/eo/src/eoInsertion.h @@ -7,7 +7,7 @@ //----------------------------------------------------------------------------- -#include +#include /****************************************************************************** * eoInsertion: A replacement algorithm. @@ -26,7 +26,7 @@ template class eoInsertion: public eoMerge * @param breeders The population of breeders. * @param pop The original population. */ - void operator()(const eoPop& breeders, eoPop& pop) + /*void operator()(eoPop& breeders, eoPop& pop) { int new_size = static_cast(pop.size() * rate()); @@ -48,6 +48,28 @@ template class eoInsertion: public eoMerge copy(breeders.begin(), breeders.end(), back_insert_iterator >(pop)); } + }*/ + + void operator()(eoPop& breeders, eoPop& pop) + { + unsigned target = static_cast(rint(pop.size() * rate())); + + pop.swap(breeders); + + if (target < pop.size()) + { + partial_sort(pop.begin(), pop.begin() + target, pop.end(), + greater()); + pop.erase(pop.begin() + target, pop.end()); + } + else + { + target = min(target - pop.size(), breeders.size()); + partial_sort(breeders.begin(), breeders.begin() + target, + breeders.end(), greater()); + copy(breeders.begin(), breeders.begin() + target, + back_insert_iterator >(pop)); + } } }; diff --git a/eo/src/eoMerge.h b/eo/src/eoMerge.h index c8e765cd8..62656af81 100644 --- a/eo/src/eoMerge.h +++ b/eo/src/eoMerge.h @@ -7,55 +7,49 @@ //----------------------------------------------------------------------------- -#include // eoMerge - -//----------------------------------------------------------------------------- -// eoInsertion -//----------------------------------------------------------------------------- - -template class Insertion: public eoMerge -{ - public: - eoInsertion(const float& _rate = 1): eoMerge(rate) {} - - bool compare(const Chrom& chrom1, const Chrom& chrom2) - { - return chrom1.fitness() < chrom2.fitness(); - } - - void operator()(const Pop& breeders, Pop& pop) - { - sort(pop.begin(), pop.end() compare); - - pop.erase(pop.end() + (int)(pop.size() * (rate - 1) - breeders.size()), - pop.end()); - - copy(breeders.begin(), breeders.end(), - back_insert_iterator(pop)); - } -}; +#include // eoPop //----------------------------------------------------------------------------- -template class Inclusion: public Replace -{ - public: - Inclusion(const float& rate = 1): Replace(rate) {} +/** eoMerge involves three populations, that can be merged and transformed to +give a third +*/ +template +class eoMerge: public eoObject{ + + public: + /// (Default) Constructor. + eoMerge(const float& _rate = 1.0): rep_rate(_rate) {} - void operator()(Pop& breeders, Pop& pop) - { - Pop temp; - - sort(breeders.begin(), breeders.end(), compare); - sort(pop.begin(), pop.end(), compare); - - merge(breeders.begin(), breeders.end(), - pop.begin(), pop.end(), - back_insert_iterator(temp), compare); - - temp.erase(temp.begin() + (unsigned)(rate * pop.size()), temp.end()); - pop.swap(temp); - } + /// Dtor + virtual ~eoMerge() {} + + /** Pure virtual transformation function. Extracts something from breeders + * and transfers it to the pop + * @param breeders Tranformed individuals. + * @param pop The original population at the begining, the result at the end + */ + virtual void operator () ( eoPop& breeders, eoPop& pop ) = 0; + + /** @name Methods from eoObject */ + //@{ + /** readFrom and printOn are not overriden + */ + /** Inherited from eoObject. Returns the class name. + @see eoObject + */ + string className() const {return "eoMerge";}; + //@} + + /// Return the rate to be selected from the original population + float rate() const { return rep_rate; } + + /// Set the rate to be obtained after replacement. + /// @param _rate The rate. + void rate(const float& _rate) { rep_rate = _rate; } + +private: + float rep_rate; }; //----------------------------------------------------------------------------- diff --git a/eo/src/eoPopOps.h b/eo/src/eoPopOps.h index 7ece0de6a..9c0dfc66f 100644 --- a/eo/src/eoPopOps.h +++ b/eo/src/eoPopOps.h @@ -97,44 +97,5 @@ class eoSelect: public eoObject{ }; -/** eoMerge involves three populations, that can be merged and transformed to -give a third -*/ -template -class eoMerge: public eoObject{ - - public: - /// (Default) Constructor. - eoMerge(const float& _rate = 1.0): rep_rate(_rate) {} - - /// Dtor - virtual ~eoMerge() {} - - /** Pure virtual transformation function. Extracts something from breeders - * and transfers it to the pop - * @param breeders Tranformed individuals. - * @param pop The original population at the begining, the result at the end - */ - virtual void operator () ( const eoPop& breeders, eoPop& pop ) = 0; - - /** @name Methods from eoObject */ - //@{ - /** readFrom and printOn are not overriden - */ - /** Inherited from eoObject. Returns the class name. - @see eoObject - */ - string className() const {return "eoMerge";}; - //@} - - /// Return the rate to be selected from the original population - float rate() const { return rep_rate; } - /// Set the rate to be obtained after replacement. - /// @param _rate The rate. - void rate(const float& _rate) { rep_rate = _rate; } - -private: - float rep_rate; -}; #endif diff --git a/eo/test/t-eobreeder.cpp b/eo/test/t-eobreeder.cpp index daaf62dea..0e632bc8b 100644 --- a/eo/test/t-eobreeder.cpp +++ b/eo/test/t-eobreeder.cpp @@ -58,6 +58,7 @@ main() breeder(pop); + // reevaluation of fitness for_each(pop.begin(), pop.end(), binary_value); cout << "new population:" << endl; diff --git a/eo/test/t-eofitness.cpp b/eo/test/t-eofitness.cpp index 9efe6dcc8..a8604d245 100644 --- a/eo/test/t-eofitness.cpp +++ b/eo/test/t-eofitness.cpp @@ -3,6 +3,8 @@ // (c) GeNeura Team 1998 //----------------------------------------------------------------------------- +#include // time +#include // srand, rand #include // cout #include // eoFitness @@ -11,6 +13,9 @@ class eoFloat: public eoFitness { public: + eoFloat(const float x) { fitness = x; } + eoFloat(const int x) { fitness = static_cast(x); } + bool operator<(const eoFitness& other) const { const eoFloat& x = (const eoFloat&) other; @@ -40,8 +45,13 @@ private: main() { - eoFloat a, b; + srand(time(0)); + eoFloat a = static_cast(rand()) / RAND_MAX, + b = static_cast(rand()) / RAND_MAX; + + cout.precision(2); + unsigned repeat = 2; while (repeat--) { @@ -62,7 +72,7 @@ main() if (a == b) cout << a << " == " << b << " is true" << endl; else - cout << a << " == " << b << " is false" <