Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

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