EvolvingObjects
eoSelectFromWorth.h
00001 
00024 //-----------------------------------------------------------------------------
00025 
00026 #ifndef _eoSelectFromWorth_h
00027 #define _eoSelectFromWorth_h
00028 
00029 #include <iostream>
00030 //-----------------------------------------------------------------------------
00031 #include <eoSelectOne.h>
00032 #include <eoPerf2Worth.h>
00033 #include <utils/selectors.h>
00034 //-----------------------------------------------------------------------------
00035 
00052 template <class EOT, class WorthType = double>
00053 class eoSelectFromWorth : public eoSelectOne<EOT>
00054 {
00055 public:
00056 
00057     /* Default ctor from an eoPerf2Worth object */
00058     eoSelectFromWorth(eoPerf2Worth<EOT, WorthType>& _perf2Worth)
00059         : perf2Worth(_perf2Worth)
00060         {}
00061 
00062     /* setup the worthes */
00063     virtual void setup(const eoPop<EOT>& pop) {
00064         perf2Worth(pop);
00065 #ifndef NDEBUG
00066         fitness.resize(pop.size());
00067         for (unsigned i = 0; i < pop.size(); ++i) {
00068             fitness[i] = pop[i].fitness();
00069         }
00070 #endif
00071     }
00072 
00073 
00074 protected:
00075 
00076     eoPerf2Worth<EOT, WorthType>& perf2Worth;
00077 
00078 #ifndef NDEBUG
00079     std::vector<typename EOT::Fitness> fitness;
00080     void check_sync(unsigned index, const EOT& _eo) {
00081         if (fitness[index] != _eo.fitness()) {
00082             throw std::runtime_error("eoSelectFromWorth: fitnesses are not in sync");
00083         }
00084     }
00085 #endif
00086 };
00087 
00088 
00094 template <class EOT, class WorthT = double>
00095 class eoDetTournamentWorthSelect : public eoSelectFromWorth<EOT, WorthT>
00096 {
00097 public:
00098 
00099     using eoSelectFromWorth<EOT, WorthT>::perf2Worth;
00100 
00101     typedef typename std::vector<WorthT>::iterator worthIterator;
00102 
00103     /* Default ctor from an eoPerf2Worth object +  tournament size */
00104     eoDetTournamentWorthSelect(eoPerf2Worth<EOT, WorthT>& perf2Worth,
00105                                unsigned _tSize)
00106         : eoSelectFromWorth<EOT, WorthT>(perf2Worth), tSize(_tSize) {}
00107 
00108     /* Perform deterministic tournament on worthes by calling the
00109        appropriate fn see selectors.h */
00110     virtual const EOT& operator()(const eoPop<EOT>& pop) {
00111         worthIterator it = deterministic_tournament(perf2Worth.value().begin(),
00112                                                     perf2Worth.value().end(),
00113                                                     tSize);
00114         unsigned index = it - perf2Worth.value().begin();
00115 #ifndef NDEBUG
00116         // check whether the stuff is still in sync
00117         check_sync(index, pop[index]);
00118 #endif
00119         return pop[index];
00120     }
00121 
00122 
00123 private:
00124 
00125     unsigned tSize;
00126 };
00127 
00133 template <class EOT, class WorthT = double>
00134 class eoStochTournamentWorthSelect : public eoSelectFromWorth<EOT, WorthT>
00135 {
00136 public:
00137 
00138     using eoSelectFromWorth<EOT, WorthT>::perf2Worth;
00139 
00140     typedef typename std::vector<WorthT>::iterator worthIterator;
00141 
00142     /* Default ctor from an eoPerf2Worth object +  tournament rate */
00143     eoStochTournamentWorthSelect(eoPerf2Worth<EOT, WorthT> &_perf2Worth, double _tRate)
00144         : eoSelectFromWorth<EOT, WorthT>(_perf2Worth), tRate(_tRate)
00145         {}
00146 
00147   /* Perform stochastic tournament on worthes
00148      by calling the appropriate fn in selectors.h
00149   */
00150   virtual const EOT& operator()(const eoPop<EOT>& _pop) {
00151       worthIterator it = stochastic_tournament(perf2Worth.value().begin(),
00152                                                perf2Worth.value().end(),
00153                                                tRate);
00154 
00155     unsigned index = it - perf2Worth.value().begin();
00156 
00157 #ifndef NDEBUG
00158   // check whether the stuff is still in sync
00159   check_sync(index, _pop[index]);
00160 #endif
00161 
00162     return _pop[index];
00163   }
00164 
00165 private:
00166   double tRate;
00167 };
00168 
00174 template <class EOT, class WorthT = double>
00175 class eoRouletteWorthSelect : public eoSelectFromWorth<EOT, WorthT>
00176 {
00177 public:
00178 
00179     using eoSelectFromWorth<EOT, WorthT>::perf2Worth;
00180 
00181     typedef typename std::vector<WorthT>::iterator worthIterator;
00182 
00183 
00184     /* Default ctor from an eoPerf2Worth object */
00185     eoRouletteWorthSelect(eoPerf2Worth<EOT, WorthT> &_perf2Worth)
00186         : eoSelectFromWorth<EOT, WorthT>(_perf2Worth)
00187         {}
00188 
00189   /* We have to override the default behavior to compute the total
00190    * only once!
00191    */
00192   virtual void setup(const eoPop<EOT>& _pop)
00193   {
00194     eoSelectFromWorth<EOT, WorthT>::setup(_pop);
00195     total = 0.0;
00196     for (worthIterator it = perf2Worth.value().begin();
00197          it<perf2Worth.value().end(); ++it)
00198       total += (*it);
00199   }
00200 
00201   /* Perform roulette wheel on worthes
00202      by calling the appropriate fn
00203      see selectors.h
00204   */
00205   virtual const EOT& operator()(const eoPop<EOT>& _pop) {
00206  //     cout << "On affiche les worths\n";
00207  //     for (unsigned i=0;
00208  //      i<perf2Worth.value().size();
00209  //      i++)
00210  //       cout << perf2Worth.value().operator[](i) << "\n";
00211  //     cout << endl;
00212       worthIterator it = roulette_wheel(perf2Worth.value().begin(),
00213                                         perf2Worth.value().end(),
00214                                         total);
00215 
00216    unsigned index = it - perf2Worth.value().begin();
00217 
00218 #ifndef NDEBUG
00219   // check whether the stuff is still in sync
00220   this->check_sync(index, _pop[index]);
00221 #endif
00222 
00223     return _pop[index];
00224     return _pop[it - perf2Worth.value().begin()];
00225   }
00226 
00227 private:
00228   double total;
00229 };
00230 
00231 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends