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 #ifndef _EOPOP_H
00026 #define _EOPOP_H
00027
00028 #include <algorithm>
00029 #include <iostream>
00030 #include <iterator>
00031 #include <vector>
00032
00033
00034 #include <eoOp.h>
00035 #include <eoPersistent.h>
00036 #include <eoInit.h>
00037 #include <utils/rnd_generators.h>
00038
00053 template<class EOT>
00054 class eoPop: public std::vector<EOT>, public eoObject, public eoPersistent
00055 {
00056
00057 public:
00058
00059 using std::vector<EOT>::size;
00060 using std::vector<EOT>::resize;
00061 using std::vector<EOT>::operator[];
00062 using std::vector<EOT>::begin;
00063 using std::vector<EOT>::end;
00064
00065 typedef typename EOT::Fitness Fitness;
00068 eoPop() : std::vector<EOT>(), eoObject(), eoPersistent() {};
00069
00075 eoPop( unsigned _popSize, eoInit<EOT>& _chromInit )
00076 :std::vector<EOT>()
00077 {
00078 resize(_popSize);
00079 for ( unsigned i = 0; i < _popSize; i++ )
00080 {
00081 _chromInit(operator[](i));
00082 }
00083 };
00084
00091 void append( unsigned _newPopSize, eoInit<EOT>& _chromInit )
00092 {
00093 unsigned oldSize = size();
00094 if (_newPopSize < oldSize)
00095 {
00096 throw std::runtime_error("New size smaller than old size in pop.append");
00097 return;
00098 }
00099 if (_newPopSize == oldSize)
00100 return;
00101 resize(_newPopSize);
00102 for ( unsigned i = oldSize; i < _newPopSize; i++ )
00103 {
00104 _chromInit(operator[](i));
00105 }
00106 };
00107
00108
00113 eoPop( std::istream& _is ) :std::vector<EOT>() {
00114 readFrom( _is );
00115 }
00116
00118 virtual ~eoPop() {};
00119
00120
00122 struct Ref { const EOT* operator()(const EOT& eot) { return &eot;}};
00124 struct Cmp {
00125 bool operator()(const EOT* a, const EOT* b) const
00126 { return b->operator<(*a); }
00127 };
00129 struct Cmp2
00130 {
00131 bool operator()(const EOT & a,const EOT & b) const
00132 {
00133 return b.operator<(a);
00134 }
00135 };
00136
00137
00138
00143 void sort(void)
00144 {
00145 std::sort(begin(), end(), Cmp2());
00146 }
00147
00149 void sort(std::vector<const EOT*>& result) const
00150 {
00151 result.resize(size());
00152
00153 std::transform(begin(), end(), result.begin(), Ref());
00154
00155 std::sort(result.begin(), result.end(), Cmp());
00156 }
00157
00162 void shuffle(void)
00163 {
00164 UF_random_generator<unsigned int> gen;
00165 std::random_shuffle(begin(), end(), gen);
00166 }
00167
00169 void shuffle(std::vector<const EOT*>& result) const
00170 {
00171 result.resize(size());
00172
00173 std::transform(begin(), end(), result.begin(), Ref());
00174
00175 UF_random_generator<unsigned int> gen;
00176 std::random_shuffle(result.begin(), result.end(), gen);
00177 }
00178
00180 typename eoPop<EOT>::iterator it_best_element()
00181 {
00182 typename eoPop<EOT>::iterator it = std::max_element(begin(), end());
00183 return it;
00184 }
00185
00187 const EOT & best_element() const
00188 {
00189 typename eoPop<EOT>::const_iterator it = std::max_element(begin(), end());
00190 return (*it);
00191 }
00192
00194 const EOT & worse_element() const
00195 {
00196 typename eoPop<EOT>::const_iterator it = std::min_element(begin(), end());
00197 return (*it);
00198 }
00199
00201 typename eoPop<EOT>::iterator it_worse_element()
00202 {
00203 typename eoPop<EOT>::iterator it = std::min_element(begin(), end());
00204 return it;
00205 }
00206
00211 typename eoPop<EOT>::iterator nth_element(int nth)
00212 {
00213 typename eoPop<EOT>::iterator it = begin() + nth;
00214 std::nth_element(begin(), it, end(), std::greater<EOT>());
00215 return it;
00216 }
00217
00218 struct GetFitness { Fitness operator()(const EOT& _eo) const { return _eo.fitness(); } };
00219
00221 Fitness nth_element_fitness(int which) const
00222 {
00223
00224 std::vector<Fitness> fitness(size());
00225 std::transform(begin(), end(), fitness.begin(), GetFitness());
00226
00227 typename std::vector<Fitness>::iterator it = fitness.begin() + which;
00228 std::nth_element(fitness.begin(), it, fitness.end(), std::greater<Fitness>());
00229 return *it;
00230 }
00231
00235 void nth_element(int which, std::vector<const EOT*>& result) const
00236 {
00237
00238 result.resize(size());
00239 std::transform(begin(), end(), result.begin(), Ref());
00240
00241 typename std::vector<const EOT*>::iterator it = result.begin() + which;
00242
00243 std::nth_element(result.begin(), it, result.end(), Cmp());
00244 }
00245
00247 void swap(eoPop<EOT>& other)
00248 {
00249 std::swap(static_cast<std::vector<EOT>& >(*this), static_cast<std::vector<EOT>& >(other));
00250 }
00251
00257 virtual void sortedPrintOn(std::ostream& _os) const
00258 {
00259 std::vector<const EOT*> result;
00260 sort(result);
00261 _os << size() << '\n';
00262 for (unsigned i = 0; i < size(); ++i)
00263 {
00264 _os << *result[i] << std::endl;
00265 }
00266 }
00267
00272 virtual void printOn(std::ostream& _os) const
00273 {
00274 _os << size() << '\n';
00275 std::copy( begin(), end(), std::ostream_iterator<EOT>( _os, "\n") );
00276 }
00277
00284 virtual void readFrom(std::istream& _is)
00285 {
00286 size_t sz;
00287 _is >> sz;
00288
00289 resize(sz);
00290
00291 for (size_t i = 0; i < sz; ++i) {
00292 operator[](i).readFrom( _is );
00293 }
00294 }
00295
00299 virtual std::string className() const {return "eoPop";};
00301
00302 virtual void invalidate()
00303 {
00304 for (unsigned i=0; i<size(); i++)
00305 this->operator[](i).invalidate();
00306 }
00307
00308 protected:
00309
00310 };
00311
00312 #endif
00313