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 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) // probably we could have a marker to avoid re-sorting 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 // now get the best 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 // and put it upfront 00122 const EOT *ptmp = eoPters[0]; 00123 eoPters[0]=best; 00124 eoPters[ibest] = ptmp; 00125 // exit after setting current 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
1.4.7