00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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>
00036
00045 template <class EOT>
00046 class eoInit : public eoUF<EOT&, void>
00047 {
00048 public:
00049
00053 virtual std::string className(void) const { return "eoInit"; }
00054 };
00055
00060 template <class EOT>
00061 class eoInitGenerator : public eoF<EOT>
00062 {
00063 public:
00064
00066 eoInitGenerator(eoInit<EOT> & _init):init(_init) {}
00067
00068 virtual EOT operator()()
00069 {
00070 EOT p;
00071 init(p);
00072 return (p);
00073 }
00074 private:
00075 eoInit<EOT> & init;
00076 };
00077
00081 template <class EOT>
00082 class eoInitFixedLength: public eoInit<EOT>
00083 {
00084 public:
00085
00086 typedef typename EOT::AtomType AtomType;
00087
00088 eoInitFixedLength(unsigned _combien, eoRndGenerator<AtomType>& _generator)
00089 : combien(_combien), generator(_generator) {}
00090
00091 virtual void operator()(EOT& chrom)
00092 {
00093 chrom.resize(combien);
00094 std::generate(chrom.begin(), chrom.end(), generator);
00095 chrom.invalidate();
00096 }
00097
00098 private :
00099 unsigned combien;
00101 eoSTLF<AtomType> generator;
00102 };
00103
00107 template <class EOT>
00108 class eoInitVariableLength: public eoInit<EOT>
00109 {
00110 public:
00111 typedef typename EOT::AtomType AtomType;
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00124 eoInitVariableLength(unsigned _minSize, unsigned _maxSize, eoInit<AtomType> & _init)
00125 : offset(_minSize), extent(_maxSize - _minSize), init(_init)
00126 {
00127 if (_minSize >= _maxSize)
00128 throw std::logic_error("eoInitVariableLength: minSize larger or equal to maxSize");
00129 }
00130
00131
00132 virtual void operator()(EOT& _chrom)
00133 {
00134 _chrom.resize(offset + rng.random(extent));
00135 typename std::vector<AtomType>::iterator it;
00136 for (it=_chrom.begin(); it<_chrom.end(); it++)
00137 init(*it);
00138 _chrom.invalidate();
00139 }
00140
00141
00142 eoInit<AtomType> & atomInit() {return init;}
00143
00144 private :
00145 unsigned offset;
00146 unsigned extent;
00147 eoInit<AtomType> & init;
00148 };
00149
00150
00154 template <class EOT>
00155 class eoInitPermutation: public eoInit<EOT>
00156 {
00157 public:
00158
00159 typedef typename EOT::AtomType AtomType;
00160
00161 eoInitPermutation(unsigned _chromSize, unsigned _startFrom=0)
00162 : chromSize(_chromSize), startFrom(_startFrom){}
00163
00164 virtual void operator()(EOT& chrom)
00165 {
00166 chrom.resize(chromSize);
00167 for(unsigned idx=0;idx <chrom.size();idx++)
00168 chrom[idx]=idx+startFrom;
00169
00170 std::random_shuffle(chrom.begin(), chrom.end(),gen);
00171 chrom.invalidate();
00172 }
00173
00174 private :
00175 unsigned chromSize;
00176 unsigned startFrom;
00177 UF_random_generator<unsigned int> gen;
00178 };
00179
00180
00188 template <class EOT>
00189 class eoInitAdaptor : public eoMonOp<EOT>
00190 {
00191 public :
00192 eoInitAdaptor(eoInit<EOT>& _init) : init(_init) {}
00193
00194 bool operator()(EOT& _eot)
00195 {
00196 init(_eot);
00197 return true;
00198 }
00199 private :
00200
00201 eoInit<EOT>& init;
00202 };
00203
00204 #endif