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

# eoPopulator.h

    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
    00038 template <class EOT>
    00039 class eoPopulator
    00040 {
    00041 public :
    00042
    00043   eoPopulator(const eoPop<EOT>& _src, eoPop<EOT>& _dest) : dest(_dest), current(dest.end()), src(_src)
    00044   {
    00045     dest.reserve(src.size()); // we don't know this, but wth.
    00046     current = dest.end();
    00047   }
    00048
    00050     virtual ~eoPopulator() {};
    00051
    00052   struct OutOfIndividuals {};
    00053
    00058   EOT& operator*(void)
    00059   {
    00060     if (current == dest.end())
    00061       get_next(); // get a new individual
    00062
    00063     return *current;
    00064   }
    00065
    00070   eoPopulator& operator++()
    00071   {
    00072     if (current == dest.end())
    00073       { // keep the pointer there
    00074         return *this;
    00075       }
    00076     // else
    00077     ++current;
    00078     return *this;
    00079   }
    00080
    00084   void insert(const EOT& _eo)
    00085   { /* not really efficient, but its nice to have */
    00086     current = dest.insert(current, _eo);
    00087   }
    00088
    00091   void reserve(int how_many)
    00092   {
    00093     size_t sz = current - dest.begin();
    00094     if (dest.capacity() < dest.size() + how_many)
    00095     {
    00096       dest.reserve(dest.size() + how_many);
    00097     }
    00098
    00099     current = dest.begin() + sz;
    00100   }
    00101
    00105   const eoPop<EOT>& source(void) { return src; }
    00106
    00110   eoPop<EOT>& offspring(void)    { return dest; }
    00111
    00112   typedef unsigned position_type;
    00113
    00115   position_type tellp()         { return current - dest.begin(); }
    00117   void seekp(position_type pos) { current = dest.begin() + pos; }
    00119   bool exhausted(void)          { return current == dest.end(); }
    00120
    00124   virtual const EOT& select() = 0;
    00125
    00126 protected:
    00127     eoPop<EOT>& dest;
    00128     typename eoPop<EOT>::iterator current;
    00129     const eoPop<EOT>& src;
    00130
    00131 private:
    00132
    00133   void get_next() {
    00134     if(current == dest.end())
    00135       { // get new individual from derived class select()
    00136         dest.push_back(select());
    00137         current = dest.end();
    00138         --current;
    00139         return;
    00140       }
    00141     // else
    00142     ++current;
    00143     return;
    00144   }
    00145
    00146 };
    00147
    00148
    00153 template <class EOT>
    00154 class eoSeqPopulator : public eoPopulator<EOT>
    00155 {
    00156 public:
    00157
    00158     using eoPopulator< EOT >::src;
    00159
    00160     eoSeqPopulator(const eoPop<EOT>& _pop, eoPop<EOT>& _dest) :
    00161         eoPopulator<EOT>(_pop, _dest), current(0) {}
    00162
    00164     const EOT& select(void) {
    00165         if(current >= eoPopulator< EOT >::src.size()) {
    00166             throw OutOfIndividuals();
    00167         }
    00168
    00169         const EOT& res = src[current++];
    00170         return res;
    00171     }
    00172
    00173
    00174 private:
    00175
    00176     struct OutOfIndividuals {};
    00177
    00178     unsigned current;
    00179 };
    00180
    00181
    00185 template <class EOT>
    00186 class eoSelectivePopulator : public eoPopulator<EOT>
    00187 {
    00188 public :
    00189
    00190     using eoPopulator< EOT >::src;
    00191
    00192     eoSelectivePopulator(const eoPop<EOT>& _pop, eoPop<EOT>& _dest, eoSelectOne<EOT>& _sel)
    00193         : eoPopulator<EOT>(_pop, _dest), sel(_sel)
    00194         { sel.setup(_pop); };
    00195
    00197     const EOT& select() {
    00198         return sel(src);
    00199     }
    00200
    00201
    00202 private:
    00203
    00204     eoSelectOne<EOT>& sel;
    00205 };
    00206
    00207 #endif
    00208