EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoInit.h 00005 // (c) Maarten Keijzer 2000, GeNeura Team, 2000 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 _eoInit_H 00028 #define _eoInit_H 00029 00030 #include <algorithm> 00031 00032 #include <eoOp.h> 00033 #include <eoSTLFunctor.h> 00034 #include <utils/eoRndGenerators.h> 00035 #include <utils/rnd_generators.h> // for shuffle method 00036 00037 00052 template <class EOT> 00053 class eoInit : public eoUF<EOT&, void> 00054 { 00055 public: 00056 00060 virtual std::string className(void) const { return "eoInit"; } 00061 }; 00062 00067 template <class EOT> 00068 class eoInitGenerator : public eoF<EOT> 00069 { 00070 public: 00071 00073 eoInitGenerator(eoInit<EOT> & _init):init(_init) {} 00074 00075 virtual EOT operator()() 00076 { 00077 EOT p; 00078 init(p); 00079 return (p); 00080 } 00081 private: 00082 eoInit<EOT> & init; 00083 }; 00084 00088 template <class EOT> 00089 class eoInitFixedLength: public eoInit<EOT> 00090 { 00091 public: 00092 00093 typedef typename EOT::AtomType AtomType; 00094 00095 eoInitFixedLength(unsigned _combien, eoRndGenerator<AtomType>& _generator) 00096 : combien(_combien), generator(_generator) {} 00097 00098 virtual void operator()(EOT& chrom) 00099 { 00100 chrom.resize(combien); 00101 std::generate(chrom.begin(), chrom.end(), generator); 00102 chrom.invalidate(); 00103 } 00104 00105 private : 00106 unsigned combien; 00108 eoSTLF<AtomType> generator; 00109 }; 00110 00114 template <class EOT> 00115 class eoInitVariableLength: public eoInit<EOT> 00116 { 00117 public: 00118 typedef typename EOT::AtomType AtomType; 00119 00120 // /** Ctor from a generator */ 00121 // eoInitVariableLength(unsigned _minSize, unsigned _maxSize, eoF<typename EOT::AtomType> & _generator = Gen()) 00122 // : offset(_minSize), extent(_maxSize - _minSize), 00123 // repGenerator( eoInitGenerator<typename EOT::AtomType>(*(new eoInit<EOT>)) ), 00124 // generator(_generator) 00125 // { 00126 // if (_minSize >= _maxSize) 00127 // throw std::logic_error("eoInitVariableLength: minSize larger or equal to maxSize"); 00128 // } 00129 00131 eoInitVariableLength(unsigned _minSize, unsigned _maxSize, eoInit<AtomType> & _init) 00132 : offset(_minSize), extent(_maxSize - _minSize), init(_init) 00133 { 00134 if (_minSize >= _maxSize) 00135 throw std::logic_error("eoInitVariableLength: minSize larger or equal to maxSize"); 00136 } 00137 00138 00139 virtual void operator()(EOT& _chrom) 00140 { 00141 _chrom.resize(offset + rng.random(extent)); 00142 typename std::vector<AtomType>::iterator it; 00143 for (it=_chrom.begin(); it<_chrom.end(); it++) 00144 init(*it); 00145 _chrom.invalidate(); 00146 } 00147 00148 // accessor to the atom initializer (needed by operator constructs sometimes) 00149 eoInit<AtomType> & atomInit() {return init;} 00150 00151 private : 00152 unsigned offset; 00153 unsigned extent; 00154 eoInit<AtomType> & init; 00155 }; 00156 00157 00161 template <class EOT> 00162 class eoInitPermutation: public eoInit<EOT> 00163 { 00164 public: 00165 00166 typedef typename EOT::AtomType AtomType; 00167 00168 eoInitPermutation(unsigned _chromSize, unsigned _startFrom=0) 00169 : chromSize(_chromSize), startFrom(_startFrom){} 00170 00171 virtual void operator()(EOT& chrom) 00172 { 00173 chrom.resize(chromSize); 00174 for(unsigned idx=0;idx <chrom.size();idx++) 00175 chrom[idx]=idx+startFrom; 00176 00177 std::random_shuffle(chrom.begin(), chrom.end(),gen); 00178 chrom.invalidate(); 00179 } 00180 00181 private : 00182 unsigned chromSize; 00183 unsigned startFrom; 00184 UF_random_generator<unsigned int> gen; 00185 }; 00197 template <class EOT> 00198 class eoInitAdaptor : public eoMonOp<EOT> 00199 { 00200 public : 00201 eoInitAdaptor(eoInit<EOT>& _init) : init(_init) {} 00202 00203 bool operator()(EOT& _eot) 00204 { 00205 init(_eot); 00206 return true; 00207 } 00208 private : 00209 00210 eoInit<EOT>& init; 00211 }; 00212 00213 #endif 00214