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
00030 #include <sstream>
00031
00032 #include <eo>
00033 #include <eoPopulator.h>
00034 #include <eoOpContainer.h>
00035
00036 struct Dummy : public EO<double>
00037 {
00038 Dummy(std::string _s="") : s(_s) {}
00039
00040 void printOn(std::ostream & _os) const
00041 {
00042 EO<double>::printOn(_os);
00043 _os << " - " << s ;
00044 }
00045
00046 std::string s;
00047 };
00048
00049 typedef Dummy EOT;
00050
00051 unsigned int pSize;
00052
00053
00054 class monop : public eoMonOp<EOT>
00055 {
00056 public :
00057 monop(char * _sig){sig=_sig;}
00058 bool operator()(EOT& _eo)
00059 {
00060 _eo.s = sig + "(" + _eo.s + ")";
00061 _eo.fitness(_eo.fitness()+pSize);
00062 return false;
00063 }
00064 std::string className() const {return sig;}
00065 private:
00066 std::string sig;
00067 };
00068
00069 class binop: public eoBinOp<EOT>
00070 {
00071 public :
00072 bool operator()(EOT& _eo1, const EOT& _eo2)
00073 {
00074 _eo1.s = "bin(" + _eo1.s + "," + _eo2.s + ")";
00075 double f= (_eo1.fitness()+_eo2.fitness()) * pSize;
00076 _eo1.fitness(_eo1.fitness()+f);
00077 return false;
00078 }
00079 std::string className() const {return "binop";}
00080 };
00081
00082 class quadop: public eoQuadOp<EOT>
00083 {
00084 public :
00085 std::string className() const {return "quadop";}
00086 bool operator()(EOT& a, EOT& b)
00087 {
00088 EOT oi = a;
00089 EOT oj = b;
00090
00091 a.s = "quad1(" + oi.s + "," + oj.s + ")";
00092 b.s = "quad2(" + oj.s + "," + oi.s + ")";
00093 double f= (a.fitness()+b.fitness()+2*pSize) * pSize;
00094 a.fitness(a.fitness()+f);
00095 b.fitness(b.fitness()+f);
00096 return false;
00097 }
00098 };
00099
00100 class quadClone: public eoQuadOp<EOT>
00101 {
00102 public :
00103 std::string className() const {return "quadclone";}
00104 bool operator()(EOT& , EOT& ) {return false;}
00105 };
00106
00107
00108
00109 class one2threeOp : public eoGenOp<EOT>
00110 {
00111 public:
00112 unsigned max_production(void) { return 3; }
00113
00114 void apply(eoPopulator<EOT>& _plop)
00115 {
00116 EOT& eo = *_plop;
00117
00118 ++_plop;
00119 _plop.insert("v(" + eo.s + ", 1)");
00120 ++_plop;
00121 _plop.insert("v(" + eo.s + ", 2)");
00122 eo.s = "v(" + eo.s + ", 0)";
00123
00124 }
00125 virtual std::string className() const {return "one2threeOp";}
00126 };
00127
00128
00129 class two2oneOp : public eoGenOp<EOT>
00130 {
00131 public:
00132 unsigned max_production(void) { return 1; }
00133
00134 void apply(eoPopulator<EOT>& _plop)
00135 {
00136 EOT& eo = *_plop;
00137 const EOT& eo2 = _plop.select();
00138 eo.s = "221(" + eo.s + ", " + eo2.s + ")";
00139
00140 }
00141 virtual std::string className() const {return "two2oneOp";}
00142 };
00143
00144 class three2threeOp : public eoGenOp<EOT>
00145 {
00146 public:
00147 unsigned max_production(void) { return 3; }
00148
00149 void apply(eoPopulator<EOT>& _plop)
00150 {
00151 EOT& eo1 = *_plop;
00152 EOT& eo2 = *++_plop;
00153 EOT& eo3 = *++_plop;
00154 EOT a = eo1;
00155 EOT b = eo2;
00156 EOT c = eo3;
00157 std::cout << "les selectionnes: a=" << a << " et b=" << b << " et c=" << c << std::endl;
00158 eo1.s = "323-1(" + a.s + ", " + b.s + ", " + c.s + ")";
00159 eo2.s = "323-2(" + a.s + ", " + b.s + ", " + c.s + ")";
00160 eo3.s = "323-3(" + a.s + ", " + b.s + ", " + c.s + ")";
00161
00162 std::cout << "les enfants: a=" << eo1 << " et b=" << eo2 << " et c=" << eo3 << std::endl;
00163 }
00164 virtual std::string className() const {return "three2threeOp";}
00165 };
00166
00167
00168
00169 void init(eoPop<Dummy> & _pop, unsigned _pSize)
00170 {
00171 if (_pSize)
00172 {
00173 _pop.resize(_pSize);
00174 }
00175 else
00176 {
00177 throw std::runtime_error("init pop with 0 size");
00178 }
00179 for (unsigned i=0; i<_pSize; i++)
00180 {
00181 std::ostringstream os;
00182 os << i;
00183 _pop[i] = Dummy(os.str());
00184 _pop[i].fitness(i);
00185 }
00186 }
00187
00188
00189 int the_main(int argc, char **argv)
00190 {
00191 eoParser parser(argc, argv);
00192 eoValueParam<unsigned int> parentSizeParam(
00193 parser.createParam(unsigned(10), "parentSize", "Parent size",'P'));
00194 pSize = parentSizeParam.value();
00195
00196 eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
00197 parser.processParam( seedParam );
00198 eo::rng.reseed(seedParam.value());
00199
00200
00201
00202 if (parser.userNeedsHelp())
00203 {
00204 parser.printHelp(std::cout);
00205 exit(1);
00206 }
00207
00209 monop mon("mon1");
00210 monop clone("clone");
00211 binop bin;
00212 quadop quad;
00213 quadClone quadclone;
00214
00215
00216 one2threeOp o2t;
00217 two2oneOp t2o;
00218 three2threeOp t2t;
00219
00220
00221
00222 eoDetTournamentSelect<EOT> select;
00223
00224 eoBestSelect<EOT> selectBest;
00225
00226
00227
00228 eoProportionalOp<EOT> pOp;
00229 pOp.add(quad, 0.1);
00230 pOp.add(bin, 0.1);
00231
00232
00233 eoSequentialOp<EOT> sOp;
00234 sOp.add(pOp, 0.9);
00235 sOp.add(mon, 0.1);
00236
00237
00238 eoSequentialOp<EOT> sOp2;
00239 sOp2.add(o2t, 1);
00240
00241
00242
00243 eoSequentialOp<EOT> sOp3;
00244 sOp3.add(t2t, 1);
00245
00246
00247
00248
00249
00250
00251
00252
00253 eoSequentialOp<EOT> sOpQuadPlusMon;
00254 sOpQuadPlusMon.add(quad, 1);
00255 sOpQuadPlusMon.add(mon, 1);
00256
00257
00258 eoProportionalOp<EOT> pOpSAGLike;
00259 pOpSAGLike.add(sOpQuadPlusMon, 0.24);
00260 pOpSAGLike.add(quad, 0.56);
00261 pOpSAGLike.add(mon, 0.06);
00262 pOpSAGLike.add(clone, 0.14);
00263
00264
00265 eoPop<EOT> pop;
00266 eoPop<EOT> offspring;
00267
00268 init(pop, pSize);
00269
00270 pop.sort();
00271 std::cout << "Population initiale" << std::endl << pop << std::endl;
00272
00273
00274 eoProportionalOp<EOT> pSGAOp;
00275 pSGAOp.add(quad, 0.8);
00276 pSGAOp.add(quadclone, 0.2);
00277
00278 eoSequentialOp<EOT> virtualSGA;
00279 virtualSGA.add(pSGAOp, 1.0);
00280 virtualSGA.add(mon, 0.3);
00281
00282 eoSeqPopulator<EOT> popit(pop, offspring);
00283
00284
00285 try
00286 {
00287 while (offspring.size() < pop.size())
00288 {
00289 virtualSGA(popit);
00290 std::cout << "SeqPopulator boucle et incremente\n";
00291 ++popit;
00292 }
00293 }
00294 catch(eoPopulator<EOT>::OutOfIndividuals&)
00295 {
00296 std::cout << "Warning: not enough individuals to handle\n";
00297 }
00298
00299
00300 std::swap(pop, offspring);
00301 offspring.clear();
00302
00303
00304 std::cout << "Apres virtualSGA \n" << pop << std::endl;
00305 init(pop, pSize);
00306
00307 std::cout << "=========================================================\n";
00308 std::cout << "Now the eoSelectPopulator version !" << std::endl;
00309
00310 eoSequentialSelect<EOT> seqSelect;
00311
00312 eoSelectivePopulator<EOT> it_step3(pop, offspring, seqSelect);
00313
00314 while (offspring.size() < 2*pop.size())
00315 {
00316 virtualSGA(it_step3);
00317 std::cout << "SelectPopulator boucle et incremente\n";
00318 ++it_step3;
00319 }
00320
00321 std::swap(pop, offspring);
00322 offspring.clear();
00323
00324
00325 std::cout << "Apres SGA-like eoSelectivePopulator\n" << pop << std::endl;
00326
00327 std::cout << "=========================================================\n";
00328 std::cout << "Now the pure addition !" << std::endl;
00329
00330 init(pop, pSize);
00331 eoSelectivePopulator<EOT> it_step4(pop, offspring, seqSelect);
00332 while (offspring.size() < 2*pop.size())
00333 {
00334 sOpQuadPlusMon(it_step4);
00335 ++it_step4;
00336 }
00337
00338 std::swap(pop, offspring);
00339 offspring.clear();
00340
00341
00342 std::cout << "Apres Quad+Mon ds un eoSelectivePopulator\n" << pop << std::endl;
00343
00344
00345 init(pop, pSize);
00346 eoSelectivePopulator<EOT> it_step5(pop, offspring, seqSelect);
00347 while (offspring.size() < 2*pop.size())
00348 {
00349 sOp2(it_step5);
00350 ++it_step5;
00351 }
00352
00353 std::swap(pop, offspring);
00354 offspring.clear();
00355
00356
00357 std::cout << "Apres 1->3 seul ds un eoSelectivePopulator\n" << pop << std::endl;
00358
00359
00360 init(pop, pSize);
00361 eoSelectivePopulator<EOT> it_step6(pop, offspring, seqSelect);
00362 while (offspring.size() < 2*pop.size())
00363 {
00364 sOp3(it_step6);
00365 ++it_step6;
00366 }
00367
00368 std::swap(pop, offspring);
00369 offspring.clear();
00370
00371
00372 std::cout << "Apres 3->3 seul ds un eoSelectivePopulator\n" << pop << std::endl;
00373
00374
00375 return 1;
00376 }
00377
00378 int main(int argc, char **argv)
00379 {
00380 try
00381 {
00382 the_main(argc, argv);
00383 }
00384 catch(std::exception& e)
00385 {
00386 std::cout << "Exception: " << e.what() << std::endl;
00387 }
00388
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427