EvolvingObjects
|
00001 00024 //----------------------------------------------------------------------------- 00025 00026 #ifndef _eoMGGReplacement_h 00027 #define _eoMGGReplacement_h 00028 00029 00030 //----------------------------------------------------------------------------- 00031 #include <eoPop.h> 00032 #include <eoFunctor.h> 00033 #include <eoMerge.h> 00034 #include <eoReduce.h> 00035 #include <utils/eoHowMany.h> 00036 #include <eoReduceSplit.h> 00037 //----------------------------------------------------------------------------- 00038 00050 template <class EOT> 00051 class eoMGGReplacement : public eoReplacement<EOT> 00052 { 00053 public: 00054 eoMGGReplacement(eoHowMany _howManyEliminatedParents = eoHowMany(2, false), 00055 unsigned _tSize=2) : 00056 // split truncates the parents and returns eliminated parents 00057 split(_howManyEliminatedParents, true), 00058 tSize(_tSize) 00059 { 00060 if (tSize < 2) 00061 { 00062 eo::log << eo::warnings << "Warning, Size for eoDetTournamentTruncateSplit adjusted to 2" << std::endl; 00063 tSize = 2; 00064 } 00065 } 00066 00067 void operator()(eoPop<EOT> & _parents, eoPop<EOT> & _offspring) 00068 { 00069 eoPop<EOT> temp; 00070 split(_parents, temp); 00071 unsigned toKeep = temp.size(); // how many to keep from merged populations 00072 // minimal check 00073 if (toKeep < 2) 00074 throw std::runtime_error("Not enough parents killed in eoMGGReplacement"); 00075 00076 // select best offspring 00077 typename eoPop<EOT>::iterator it = _offspring.it_best_element(); 00078 // add to parents 00079 _parents.push_back(*it); 00080 // remove from offspring 00081 _offspring.erase(it); 00082 00083 // merge temp into offspring 00084 plus(temp, _offspring); 00085 00086 // repeatedly add selected offspring to parents 00087 for (unsigned i=0; i<toKeep-1; i++) 00088 { 00089 // select 00090 it = deterministic_tournament(_offspring.begin(), _offspring.end(), tSize); 00091 // add to parents 00092 _parents.push_back(*it); 00093 // remove from offspring 00094 _offspring.erase(it); 00095 } 00096 } 00097 00098 private: 00099 eoLinearTruncateSplit<EOT> split; // few parents to truncate -> linear 00100 eoPlus<EOT> plus; 00101 unsigned int tSize; 00102 }; 00103 00104 #endif