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 _make_algo_easea_h
00028 #define _make_algo_easea_h
00029
00030 #include <utils/eoData.h>
00031
00032
00033
00034
00035 #include <eoRandomSelect.h>
00036 #include <eoSequentialSelect.h>
00037 #include <eoDetTournamentSelect.h>
00038 #include <eoProportionalSelect.h>
00039 #include <eoFitnessScalingSelect.h>
00040 #include <eoRankingSelect.h>
00041 #include <eoStochTournamentSelect.h>
00042
00043
00044
00045 #include <eoGeneralBreeder.h>
00046
00047
00048 #include "make_general_replacement.h"
00049 #include "eoMGGReplacement.h"
00050 #include "eoG3Replacement.h"
00051
00052
00053
00054 #include <eoEasyEA.h>
00055
00056
00057 #include <utils/eoParser.h>
00058 #include <utils/eoState.h>
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 template <class EOT>
00074 eoAlgo<EOT> & do_make_algo_scalar(eoParser& _parser, eoState& _state, eoPopEvalFunc<EOT>& _popeval, eoContinue<EOT>& _continue, eoGenOp<EOT>& _op)
00075 {
00076
00077 eoValueParam<eoParamParamType>& selectionParam = _parser.createParam(eoParamParamType("DetTour(2)"), "selection", "Selection: Roulette, Ranking(p,e), DetTour(T), StochTour(t), Sequential(ordered/unordered) or EliteSequentialSelect", 'S', "Evolution Engine");
00078
00079 eoParamParamType & ppSelect = selectionParam.value();
00080
00081 eoSelectOne<EOT>* select ;
00082 if (ppSelect.first == std::string("DetTour"))
00083 {
00084 unsigned detSize;
00085
00086 if (!ppSelect.second.size())
00087 {
00088 std::cerr << "WARNING, no parameter passed to DetTour, using 2" << std::endl;
00089 detSize = 2;
00090
00091 ppSelect.second.push_back(std::string("2"));
00092 }
00093 else
00094 detSize = atoi(ppSelect.second[0].c_str());
00095 select = new eoDetTournamentSelect<EOT>(detSize);
00096 }
00097 else if (ppSelect.first == std::string("StochTour"))
00098 {
00099 double p;
00100 if (!ppSelect.second.size())
00101 {
00102 std::cerr << "WARNING, no parameter passed to StochTour, using 1" << std::endl;
00103 p = 1;
00104
00105 ppSelect.second.push_back(std::string("1"));
00106 }
00107 else
00108 p = atof(ppSelect.second[0].c_str());
00109
00110 select = new eoStochTournamentSelect<EOT>(p);
00111 }
00112 else if (ppSelect.first == std::string("Ranking"))
00113 {
00114 double p,e;
00115 if (ppSelect.second.size()==2)
00116 {
00117 p = atof(ppSelect.second[0].c_str());
00118 e = atof(ppSelect.second[1].c_str());
00119 }
00120 else if (ppSelect.second.size()==1)
00121 {
00122 std::cerr << "WARNING, no exponent to Ranking, using 1" << std::endl;
00123 e = 1;
00124 ppSelect.second.push_back(std::string("1"));
00125 p = atof(ppSelect.second[0].c_str());
00126 }
00127 else
00128 {
00129 std::cerr << "WARNING, no parameter to Ranking, using (2,1)" << std::endl;
00130 p=2;
00131 e=1;
00132
00133 ppSelect.second.resize(2);
00134 ppSelect.second[0] = (std::string("2"));
00135 ppSelect.second[1] = (std::string("1"));
00136 }
00137
00138
00139 if ( (p<=1) || (p>2) )
00140 {
00141 std::cerr << "WARNING, selective pressure must be in (1,2] in Ranking, using 2\n";
00142 p=2;
00143 ppSelect.second[0] = (std::string("2"));
00144 }
00145
00146 if (e<=0)
00147 {
00148 std::cerr << "WARNING, exponent must be positive in Ranking, using 1\n";
00149 e=1;
00150 ppSelect.second[1] = (std::string("1"));
00151 }
00152
00153 eoPerf2Worth<EOT> & p2w = _state.storeFunctor( new eoRanking<EOT>(p,e) );
00154 select = new eoRouletteWorthSelect<EOT>(p2w);
00155 }
00156 else if (ppSelect.first == std::string("Sequential"))
00157 {
00158 bool b;
00159 if (ppSelect.second.size() == 0)
00160 {
00161 b=true;
00162
00163 ppSelect.second.push_back(std::string("ordered"));
00164 }
00165 else
00166 b = !(ppSelect.second[0] == std::string("unordered"));
00167 select = new eoSequentialSelect<EOT>(b);
00168 }
00169 else if (ppSelect.first == std::string("EliteSequential"))
00170 {
00171 select = new eoEliteSequentialSelect<EOT>;
00172 }
00173 else if (ppSelect.first == std::string("Roulette"))
00174 {
00175 select = new eoProportionalSelect<EOT>;
00176 }
00177 else if (ppSelect.first == std::string("Random"))
00178 {
00179 select = new eoRandomSelect<EOT>;
00180 }
00181 else
00182 {
00183 std::string stmp = std::string("Invalid selection: ") + ppSelect.first;
00184 throw std::runtime_error(stmp.c_str());
00185 }
00186
00187 _state.storeFunctor(select);
00188
00189
00190 eoValueParam<eoHowMany>& offspringRateParam = _parser.createParam(eoHowMany(1.0), "nbOffspring", "Nb of offspring (percentage or absolute)", 'O', "Evolution Engine");
00191
00193
00195
00224 eoParamParamType & replacementParam = _parser.createParam(eoParamParamType("General"), "replacement", "Type of replacement: General, or Generational, ESComma, ESPlus, SSGA(T), EP(T), G3, MGG(T)", '\0', "Evolution Engine").value();
00225
00226 eoReplacement<EOT> * ptReplace;
00227
00228
00229
00230 if (replacementParam.first == std::string("G3"))
00231 {
00232
00233 eoHowMany surviveParents = _parser.createParam(eoHowMany(-2,false), "surviveParents", "Nb of surviving parents (percentage or absolute)", '\0', "Evolution Engine / Replacement").value();
00234
00235 ptReplace = new eoG3Replacement<EOT>(-surviveParents);
00236 _state.storeFunctor(ptReplace);
00237 }
00238 else if (replacementParam.first == std::string("MGG"))
00239 {
00240 float t;
00241 unsigned tSize;
00242
00243 eoHowMany surviveParents = _parser.createParam(eoHowMany(-2,false), "surviveParents", "Nb of surviving parents (percentage or absolute)", '\0', "Evolution Engine / Replacement").value();
00244
00245 if (!replacementParam.second.size())
00246 {
00247 std::cerr << "WARNING, no parameter passed to MGG replacement, using 2" << std::endl;
00248 tSize = 2;
00249
00250 replacementParam.second.push_back(std::string("2"));
00251 }
00252 else
00253 {
00254 t = atof(replacementParam.second[0].c_str());
00255 if (t>=2)
00256 {
00257 tSize = unsigned(t);
00258 }
00259 else
00260 {
00261 throw std::runtime_error("Sorry, only deterministic tournament available at the moment");
00262 }
00263 }
00264 ptReplace = new eoMGGReplacement<EOT>(-surviveParents, tSize);
00265 _state.storeFunctor(ptReplace);
00266 }
00267 else {
00268
00269
00270 eoHowMany elite (0.0);
00271 bool strongElitism (false);
00272 eoHowMany surviveParents (0.0);
00273 eoParamParamType reduceParentType ("Deterministic");
00274 eoHowMany surviveOffspring (1.0);
00275 eoParamParamType reduceOffspringType ("Deterministic");
00276 eoParamParamType reduceFinalType ("Deterministic");
00277
00278
00279 double t;
00280
00281
00282 if (replacementParam.first == std::string("General"))
00283 {
00284 ;
00285 }
00286
00287 else if (replacementParam.first == std::string("ESComma"))
00288 {
00289 ;
00290 }
00291
00292 else if (replacementParam.first == std::string("ESPlus"))
00293 {
00294 surviveParents = eoHowMany(1.0);
00295 }
00296
00297 else if (replacementParam.first == std::string("Generational"))
00298 {
00299 ;
00300 }
00301
00302 else if (replacementParam.first == std::string("EP"))
00303 {
00304 if (!replacementParam.second.size())
00305 {
00306 std::cerr << "WARNING, no parameter passed to EP replacement, using 6" << std::endl;
00307
00308 replacementParam.second.push_back(std::string("6"));
00309 }
00310
00311 reduceFinalType = replacementParam;
00312 surviveParents = eoHowMany(1.0);
00313 }
00314
00315 else if (replacementParam.first == std::string("SSGA"))
00316 {
00317 if (!replacementParam.second.size())
00318 {
00319 std::cerr << "WARNING, no parameter passed to SSGA replacement, using 2" << std::endl;
00320
00321 replacementParam.second.push_back(std::string("2"));
00322 reduceParentType = eoParamParamType(std::string("DetTour(2)"));
00323 }
00324 else
00325 {
00326 t = atof(replacementParam.second[0].c_str());
00327 if (t>=2)
00328 {
00329 reduceParentType = eoParamParamType(std::string("DetTour(") + replacementParam.second[0].c_str() + ")");
00330 }
00331 else
00332 {
00333 reduceParentType = eoParamParamType(std::string("StochTour(") + replacementParam.second[0].c_str() + ")");
00334 }
00335 }
00336
00337 surviveParents = eoHowMany(-1);
00338 surviveOffspring = eoHowMany(1);
00339 }
00340 else
00341 {
00342 throw std::runtime_error("Invalid replacement type " + replacementParam.first);
00343 }
00344
00345 ptReplace = & make_general_replacement<EOT>(
00346 _parser, _state, elite, strongElitism, surviveParents, reduceParentType, surviveOffspring, reduceOffspringType, reduceFinalType);
00347
00348 }
00349
00350
00352
00354 eoGeneralBreeder<EOT> *breed =
00355 new eoGeneralBreeder<EOT>(*select, _op, offspringRateParam.value());
00356 _state.storeFunctor(breed);
00357
00359
00361 eoAlgo<EOT> *algo = new eoEasyEA<EOT>(_continue, _popeval, *breed, *ptReplace);
00362 _state.storeFunctor(algo);
00363
00364 return *algo;
00365 }
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 template <class EOT>
00380 eoAlgo<EOT> & do_make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<EOT>& _eval, eoContinue<EOT>& _continue, eoGenOp<EOT>& _op)
00381 {
00382 do_make_algo_scalar( _parser, _state, *(new eoPopLoopEval<EOT>(_eval)), _continue, _op);
00383 }
00384
00385
00386
00387 #endif