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 00044 template <class EOT> 00045 class eoInit : public eoUF<EOT&, void> 00046 { 00047 public: 00048 00052 virtual std::string className(void) const { return "eoInit"; } 00053 }; 00054 00059 template <class EOT> 00060 class eoInitGenerator : public eoF<EOT> 00061 { 00062 public: 00063 00065 eoInitGenerator(eoInit<EOT> & _init):init(_init) {} 00066 00067 virtual EOT operator()() 00068 { 00069 EOT p; 00070 init(p); 00071 return (p); 00072 } 00073 private: 00074 eoInit<EOT> & init; 00075 }; 00076 00080 template <class EOT> 00081 class eoInitFixedLength: public eoInit<EOT> 00082 { 00083 public: 00084 00085 typedef typename EOT::AtomType AtomType; 00086 00087 eoInitFixedLength(unsigned _combien, eoRndGenerator<AtomType>& _generator) 00088 : combien(_combien), generator(_generator) {} 00089 00090 virtual void operator()(EOT& chrom) 00091 { 00092 chrom.resize(combien); 00093 std::generate(chrom.begin(), chrom.end(), generator); 00094 chrom.invalidate(); 00095 } 00096 00097 private : 00098 unsigned combien; 00100 eoSTLF<AtomType> generator; 00101 }; 00102 00106 template <class EOT> 00107 class eoInitVariableLength: public eoInit<EOT> 00108 { 00109 public: 00110 typedef typename EOT::AtomType AtomType; 00111 00112 // /** Ctor from a generator */ 00113 // eoInitVariableLength(unsigned _minSize, unsigned _maxSize, eoF<typename EOT::AtomType> & _generator = Gen()) 00114 // : offset(_minSize), extent(_maxSize - _minSize), 00115 // repGenerator( eoInitGenerator<typename EOT::AtomType>(*(new eoInit<EOT>)) ), 00116 // generator(_generator) 00117 // { 00118 // if (_minSize >= _maxSize) 00119 // throw std::logic_error("eoInitVariableLength: minSize larger or equal to maxSize"); 00120 // } 00121 00123 eoInitVariableLength(unsigned _minSize, unsigned _maxSize, eoInit<AtomType> & _init) 00124 : offset(_minSize), extent(_maxSize - _minSize), init(_init) 00125 { 00126 if (_minSize >= _maxSize) 00127 throw std::logic_error("eoInitVariableLength: minSize larger or equal to maxSize"); 00128 } 00129 00130 00131 virtual void operator()(EOT& _chrom) 00132 { 00133 _chrom.resize(offset + rng.random(extent)); 00134 typename std::vector<AtomType>::iterator it; 00135 for (it=_chrom.begin(); it<_chrom.end(); it++) 00136 init(*it); 00137 _chrom.invalidate(); 00138 } 00139 00140 // accessor to the atom initializer (needed by operator constructs sometimes) 00141 eoInit<AtomType> & atomInit() {return init;} 00142 00143 private : 00144 unsigned offset; 00145 unsigned extent; 00146 eoInit<AtomType> & init; 00147 }; 00148 00156 template <class EOT> 00157 class eoInitAdaptor : public eoMonOp<EOT> 00158 { 00159 public : 00160 eoInitAdaptor(eoInit<EOT>& _init) : init(_init) {} 00161 00162 bool operator()(EOT& _eot) 00163 { 00164 init(_eot); 00165 return true; 00166 } 00167 private : 00168 00169 eoInit<EOT>& init; 00170 }; 00171 00172 #endif
1.4.7