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
00056 eoSelectFromWorth(eoPerf2Worth<EOT, WorthType>& _perf2Worth)
00057 : perf2Worth(_perf2Worth)
00058 {}
00059
00060
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
00100 eoDetTournamentWorthSelect(eoPerf2Worth<EOT, WorthT>& perf2Worth,
00101 unsigned _tSize)
00102 : eoSelectFromWorth<EOT, WorthT>(perf2Worth), tSize(_tSize) {}
00103
00104
00105
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
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
00137 eoStochTournamentWorthSelect(eoPerf2Worth<EOT, WorthT> &_perf2Worth, double _tRate)
00138 : eoSelectFromWorth<EOT, WorthT>(_perf2Worth), tRate(_tRate)
00139 {}
00140
00141
00142
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
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
00177 eoRouletteWorthSelect(eoPerf2Worth<EOT, WorthT> &_perf2Worth)
00178 : eoSelectFromWorth<EOT, WorthT>(_perf2Worth)
00179 {}
00180
00181
00182
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
00194
00195
00196
00197 virtual const EOT& operator()(const eoPop<EOT>& _pop) {
00198
00199
00200
00201
00202
00203
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
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