EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoSequential.h 00005 // (c) GeNeura Team, 1998 - Maarten Keijzer 2000, Marc Schoenauer, 2002 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: todos@geneura.ugr.es, http://geneura.ugr.es 00022 Marc.Schoenauer@polytechnique.fr 00023 mak@dhi.dk 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 00054 template <class EOT> class eoSequentialSelect: public eoSelectOne<EOT> 00055 { 00056 public: 00060 eoSequentialSelect(bool _ordered = true) 00061 : ordered(_ordered), current(std::numeric_limits<unsigned>::max()) {} 00062 00063 void setup(const eoPop<EOT>& _pop) 00064 { 00065 eoPters.resize(_pop.size()); 00066 if (ordered) // probably we could have a marker to avoid re-sorting 00067 _pop.sort(eoPters); 00068 else 00069 _pop.shuffle(eoPters); 00070 current=0; 00071 } 00072 00073 virtual const EOT& operator()(const eoPop<EOT>& _pop) 00074 { 00075 if (current >= _pop.size()) 00076 setup(_pop); 00077 00078 unsigned eoN = current; 00079 current++; 00080 return *eoPters[eoN] ; 00081 } 00082 private: 00083 bool ordered; 00084 unsigned current; 00085 std::vector<const EOT*> eoPters; 00086 }; 00087 00088 00089 00101 template <class EOT> class eoEliteSequentialSelect: public eoSelectOne<EOT> 00102 { 00103 public: 00104 00108 eoEliteSequentialSelect(): current(std::numeric_limits<unsigned>::max()) {} 00109 00110 void setup(const eoPop<EOT>& _pop) 00111 { 00112 eoPters.resize(_pop.size()); 00113 _pop.shuffle(eoPters); 00114 // now get the best 00115 unsigned int ibest = 0; 00116 const EOT * best = eoPters[0]; 00117 if (_pop.size() == 1) 00118 throw std::runtime_error("Trying eoEliteSequentialSelect with only one individual!"); 00119 for (unsigned i=1; i<_pop.size(); i++) 00120 if (*eoPters[i]>*best) 00121 { 00122 ibest = i; 00123 best = eoPters[ibest]; 00124 } 00125 // and put it upfront 00126 const EOT *ptmp = eoPters[0]; 00127 eoPters[0]=best; 00128 eoPters[ibest] = ptmp; 00129 // exit after setting current 00130 current=0; 00131 } 00132 00133 virtual const EOT& operator()(const eoPop<EOT>& _pop) 00134 { 00135 if (current >= _pop.size()) 00136 setup(_pop); 00137 00138 unsigned eoN = current; 00139 current++; 00140 return *eoPters[eoN] ; 00141 } 00142 private: 00143 unsigned current; 00144 std::vector<const EOT*> eoPters; 00145 }; 00146 00147 00148 #endif