00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef eoSequential_h
00028 #define eoSequential_h
00029
00030 #include <limits>
00031
00032 #include "utils/eoData.h"
00033 #include "utils/eoRNG.h"
00034 #include "eoSelectOne.h"
00035
00052 template <class EOT> class eoSequentialSelect: public eoSelectOne<EOT>
00053 {
00054 public:
00058 eoSequentialSelect(bool _ordered = true)
00059 : ordered(_ordered), current(std::numeric_limits<unsigned>::max()) {}
00060
00061 void setup(const eoPop<EOT>& _pop)
00062 {
00063 eoPters.resize(_pop.size());
00064 if (ordered)
00065 _pop.sort(eoPters);
00066 else
00067 _pop.shuffle(eoPters);
00068 current=0;
00069 }
00070
00071 virtual const EOT& operator()(const eoPop<EOT>& _pop)
00072 {
00073 if (current >= _pop.size())
00074 setup(_pop);
00075
00076 unsigned eoN = current;
00077 current++;
00078 return *eoPters[eoN] ;
00079 }
00080 private:
00081 bool ordered;
00082 unsigned current;
00083 std::vector<const EOT*> eoPters;
00084 };
00085
00086
00087
00097 template <class EOT> class eoEliteSequentialSelect: public eoSelectOne<EOT>
00098 {
00099 public:
00100
00104 eoEliteSequentialSelect(): current(std::numeric_limits<unsigned>::max()) {}
00105
00106 void setup(const eoPop<EOT>& _pop)
00107 {
00108 eoPters.resize(_pop.size());
00109 _pop.shuffle(eoPters);
00110
00111 unsigned int ibest = 0;
00112 const EOT * best = eoPters[0];
00113 if (_pop.size() == 1)
00114 throw std::runtime_error("Trying eoEliteSequentialSelect with only one individual!");
00115 for (unsigned i=1; i<_pop.size(); i++)
00116 if (*eoPters[i]>*best)
00117 {
00118 ibest = i;
00119 best = eoPters[ibest];
00120 }
00121
00122 const EOT *ptmp = eoPters[0];
00123 eoPters[0]=best;
00124 eoPters[ibest] = ptmp;
00125
00126 current=0;
00127 }
00128
00129 virtual const EOT& operator()(const eoPop<EOT>& _pop)
00130 {
00131 if (current >= _pop.size())
00132 setup(_pop);
00133
00134 unsigned eoN = current;
00135 current++;
00136 return *eoPters[eoN] ;
00137 }
00138 private:
00139 unsigned current;
00140 std::vector<const EOT*> eoPters;
00141 };
00142
00143
00144 #endif