From a86873dcbf8424286e3672820d31358383a3f938 Mon Sep 17 00:00:00 2001 From: gustavo Date: Tue, 9 Feb 1999 17:27:22 +0000 Subject: [PATCH] new replacer --- eo/src/eo | 2 +- eo/src/eoInclusion.h | 49 ++++++++++++++++++++++++++++++++++++++++++++ eo/src/eoInsertion.h | 35 +++++++++++++++++-------------- 3 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 eo/src/eoInclusion.h diff --git a/eo/src/eo b/eo/src/eo index 73fa6753..2a869c1b 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -43,7 +43,7 @@ #include #include - +#include //----------------------------------------------------------------------------- diff --git a/eo/src/eoInclusion.h b/eo/src/eoInclusion.h new file mode 100644 index 00000000..a74986c2 --- /dev/null +++ b/eo/src/eoInclusion.h @@ -0,0 +1,49 @@ +//----------------------------------------------------------------------------- +// eoInclusion.h +//----------------------------------------------------------------------------- + +#ifndef eoInclusion_h +#define eoInclusion_h + +//----------------------------------------------------------------------------- + +#include + +/****************************************************************************** + * eoInclusion: A replacement algorithm. + * Creates a new population by selecting the best individuals from the + * breeders and original populations + *****************************************************************************/ + +template class eoInclusion: public eoMerge +{ + public: + /// (Default) Constructor. + eoInclusion(const float& _rate = 1.0): eoMerge(_rate) {} + + /** + * Creates a new population based on breeders and original populations. + * @param breeders The population of breeders. + * @param pop The original population. + */ + void operator()(const eoPop& breeders, eoPop& pop) + { + eoPop all, tmp = breeders; + + 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); + } +}; + +//----------------------------------------------------------------------------- + +#endif eoInclusion_h diff --git a/eo/src/eoInsertion.h b/eo/src/eoInsertion.h index 16183421..3c6d90a9 100644 --- a/eo/src/eoInsertion.h +++ b/eo/src/eoInsertion.h @@ -11,10 +11,8 @@ /****************************************************************************** * eoInsertion: A replacement algorithm. - * Takes two populations: breeders and original populations. At the en of the - * process, the original population has chenge in the followin way: - * (1) the worst individuals haa been erased - * (2) the best individuals from the breeders has been added + * Creates a new population with all the breeders and the best individuals + * from the original population. *****************************************************************************/ template class eoInsertion: public eoMerge @@ -24,26 +22,33 @@ template class eoInsertion: public eoMerge eoInsertion(const float& _rate = 1.0): eoMerge(_rate) {} /** - * Creates a new population based on breeders and original population + * Creates a new population based on breeders and original populations. * @param breeders The population of breeders. * @param pop The original population. */ void operator()(const eoPop& breeders, eoPop& pop) { - sort(pop.begin(), pop.end()); + int new_size = static_cast(pop.size() * rate()); + cout << "new_size = " << new_size << endl; - if (rated() > 1) - pop.erase(pop.end() + - (int)(pop.size() * (rate() - 1) - breeders.size()), - pop.end()); + if (new_size == breeders.size()) + { + pop = breeders; + } + else if (new_size < breeders.size()) + { + pop = breeders; + sort(pop.begin(), pop.end()); + pop.erase(pop.begin(), pop.begin() - new_size + pop.size()); + } else { - cout << "eoInsertion no funciona con rate < 1" - exit(1); + sort(pop.begin(), pop.end()); + pop.erase(pop.begin(), + pop.begin() + breeders.size() + pop.size() - new_size); + copy(breeders.begin(), breeders.end(), + back_insert_iterator >(pop)); } - - copy(breeders.begin(), breeders.end(), - back_insert_iterator >(pop)); } };