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
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
00113
00114
00115
00116
00117
00118
00119
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
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