EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoPopulator.h 00005 // (c) Maarten Keijzer and Marc Schoenauer, 2001 00006 /* 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 Contact: mkeijzer@dhi.dk 00022 Marc.Schoenauer@polytechnique.fr 00023 */ 00024 //----------------------------------------------------------------------------- 00025 00026 #ifndef _eoPopulator_H 00027 #define _eoPopulator_H 00028 00029 #include <eoPop.h> 00030 #include <eoSelectOne.h> 00031 00042 template <class EOT> 00043 class eoPopulator 00044 { 00045 public : 00046 00047 eoPopulator(const eoPop<EOT>& _src, eoPop<EOT>& _dest) : dest(_dest), current(dest.end()), src(_src) 00048 { 00049 dest.reserve(src.size()); // we don't know this, but wth. 00050 current = dest.end(); 00051 } 00052 00054 virtual ~eoPopulator() {}; 00055 00056 struct OutOfIndividuals {}; 00057 00062 EOT& operator*(void) 00063 { 00064 if (current == dest.end()) 00065 get_next(); // get a new individual 00066 00067 return *current; 00068 } 00069 00074 eoPopulator& operator++() 00075 { 00076 if (current == dest.end()) 00077 { // keep the pointer there 00078 return *this; 00079 } 00080 // else 00081 ++current; 00082 return *this; 00083 } 00084 00088 void insert(const EOT& _eo) 00089 { /* not really efficient, but its nice to have */ 00090 current = dest.insert(current, _eo); 00091 } 00092 00095 void reserve(int how_many) 00096 { 00097 size_t sz = current - dest.begin(); 00098 if (dest.capacity() < dest.size() + how_many) 00099 { 00100 dest.reserve(dest.size() + how_many); 00101 } 00102 00103 current = dest.begin() + sz; 00104 } 00105 00109 const eoPop<EOT>& source(void) { return src; } 00110 00114 eoPop<EOT>& offspring(void) { return dest; } 00115 00116 typedef unsigned position_type; 00117 00119 position_type tellp() { return current - dest.begin(); } 00121 void seekp(position_type pos) { current = dest.begin() + pos; } 00123 bool exhausted(void) { return current == dest.end(); } 00124 00128 virtual const EOT& select() = 0; 00129 00130 protected: 00131 eoPop<EOT>& dest; 00132 typename eoPop<EOT>::iterator current; 00133 const eoPop<EOT>& src; 00134 00135 private: 00136 00137 void get_next() { 00138 if(current == dest.end()) 00139 { // get new individual from derived class select() 00140 dest.push_back(select()); 00141 current = dest.end(); 00142 --current; 00143 return; 00144 } 00145 // else 00146 ++current; 00147 return; 00148 } 00149 00150 }; 00151 00152 00157 template <class EOT> 00158 class eoSeqPopulator : public eoPopulator<EOT> 00159 { 00160 public: 00161 00162 using eoPopulator< EOT >::src; 00163 00164 eoSeqPopulator(const eoPop<EOT>& _pop, eoPop<EOT>& _dest) : 00165 eoPopulator<EOT>(_pop, _dest), current(0) {} 00166 00168 const EOT& select(void) { 00169 if(current >= eoPopulator< EOT >::src.size()) { 00170 throw OutOfIndividuals(); 00171 } 00172 00173 const EOT& res = src[current++]; 00174 return res; 00175 } 00176 00177 00178 private: 00179 00180 struct OutOfIndividuals {}; 00181 00182 unsigned current; 00183 }; 00184 00185 00189 template <class EOT> 00190 class eoSelectivePopulator : public eoPopulator<EOT> 00191 { 00192 public : 00193 00194 using eoPopulator< EOT >::src; 00195 00196 eoSelectivePopulator(const eoPop<EOT>& _pop, eoPop<EOT>& _dest, eoSelectOne<EOT>& _sel) 00197 : eoPopulator<EOT>(_pop, _dest), sel(_sel) 00198 { sel.setup(_pop); }; 00199 00201 const EOT& select() { 00202 return sel(src); 00203 } 00204 00205 00206 private: 00207 00208 eoSelectOne<EOT>& sel; 00209 }; 00210 00211 #endif