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_pareto_h
00028 #define _make_algo_pareto_h
00029
00030 #include "utils/eoData.h"
00031
00032
00033
00034
00035 #include "eoSelectFromWorth.h"
00036 #include "eoNDSorting.h"
00037
00038
00039 #include "eoGeneralBreeder.h"
00040
00041
00042 #include "eoReplacement.h"
00043
00044 template <class EOT, class WorthT = double>
00045 class eoNDPlusReplacement : public eoReplacement<EOT>
00046 {
00047 public:
00048 eoNDPlusReplacement(eoPerf2Worth<EOT, WorthT>& _perf2worth) : perf2worth(_perf2worth) {}
00049
00050 struct WorthPair : public std::pair<WorthT, const EOT*>
00051 {
00052 bool operator<(const WorthPair& other) const { return other.first < this->first; }
00053 };
00054
00055 void operator()(eoPop<EOT>& _parents, eoPop<EOT>& _offspring)
00056 {
00057 unsigned sz = _parents.size();
00058 _parents.reserve(_parents.size() + _offspring.size());
00059 copy(_offspring.begin(), _offspring.end(), back_inserter(_parents));
00060
00061
00062 perf2worth(_parents);
00063 perf2worth.sort_pop(_parents);
00064 perf2worth.resize(_parents, sz);
00065
00066 _offspring.clear();
00067 }
00068
00069 private :
00070 eoPerf2Worth<EOT, WorthT>& perf2worth;
00071 };
00072
00073
00074
00075 #include "eoEasyEA.h"
00076
00077
00078 #include "utils/eoParser.h"
00079 #include "utils/eoState.h"
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 template <class EOT>
00096 eoAlgo<EOT> & do_make_algo_pareto(eoParser& _parser, eoState& _state, eoEvalFunc<EOT>& _eval, eoContinue<EOT>& _continue, eoGenOp<EOT>& _op)
00097 {
00098
00099 std::string & selStr = _parser.createParam(std::string("NSGA-II"), "selCrit", "Pareto Selection Criterion: NSGA, NSGA-II, ParetoRanking", 'S', "Evolution Engine").value();
00100 double nicheSize = _parser.createParam(1.0, "nicheSize", "Size of niche for NSGA-I", '\0', "Evolution Engine").value();
00101 eoPerf2Worth<EOT, double> *p2w;
00102 if ( (selStr == std::string("NSGA")) || (selStr == std::string("NSGA-I") ) )
00103 p2w = new eoNDSorting_I<EOT>(nicheSize);
00104 else if (selStr == std::string("NSGA-II"))
00105 p2w = new eoNDSorting_II<EOT>();
00106 else if (selStr == std::string("ParetoRanking"))
00107 {
00108 eoDominanceMap<EOT>& dominance = _state.storeFunctor(new eoDominanceMap<EOT>);
00109 p2w = new eoParetoRanking<EOT>(dominance);
00110 }
00111
00112 _state.storeFunctor(p2w);
00113
00114
00115
00116
00117 eoValueParam<eoParamParamType>& selectionParam = _parser.createParam(eoParamParamType("DetTour(2)"), "selection", "Selection: Roulette, DetTour(T), StochTour(t) or Random", 'S', "Evolution Engine");
00118
00119 eoParamParamType & ppSelect = selectionParam.value();
00120
00121 eoSelectOne<EOT>* select ;
00122 if (ppSelect.first == std::string("DetTour"))
00123 {
00124 unsigned detSize;
00125
00126 if (!ppSelect.second.size())
00127 {
00128 std::cerr << "WARNING, no parameter passed to DetTour, using 2" << std::endl;
00129 detSize = 2;
00130
00131 ppSelect.second.push_back(std::string("2"));
00132 }
00133 else
00134 detSize = atoi(ppSelect.second[0].c_str());
00135 select = new eoDetTournamentWorthSelect<EOT>(*p2w, detSize);
00136 }
00137 else if (ppSelect.first == std::string("StochTour"))
00138 {
00139 double p;
00140 if (!ppSelect.second.size())
00141 {
00142 std::cerr << "WARNING, no parameter passed to StochTour, using 1" << std::endl;
00143 p = 1;
00144
00145 ppSelect.second.push_back(std::string("1"));
00146 }
00147 else
00148 p = atof(ppSelect.second[0].c_str());
00149
00150 select = new eoStochTournamentWorthSelect<EOT>(*p2w, p);
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 else if (ppSelect.first == std::string("Roulette"))
00166 {
00167 select = new eoRouletteWorthSelect<EOT>(*p2w);
00168 }
00169 else if (ppSelect.first == std::string("Random"))
00170 {
00171 select = new eoRandomSelect<EOT>;
00172 }
00173 else
00174 {
00175 std::string stmp = std::string("Invalid selection: ") + ppSelect.first;
00176 throw std::runtime_error(stmp.c_str());
00177 }
00178
00179 _state.storeFunctor(select);
00180
00181
00182
00183 eoValueParam<eoHowMany>& offspringRateParam = _parser.createParam(eoHowMany(1.0), "nbOffspring", "Nb of offspring (percentage or absolute)", 'O', "Evolution Engine");
00184
00185
00186
00187 eoReplacement<EOT> & replace = _state.storeFunctor(
00188 new eoNDPlusReplacement<EOT, double>(*p2w)
00189 );
00190
00191
00192 eoGeneralBreeder<EOT> *breed =
00193 new eoGeneralBreeder<EOT>(*select, _op, offspringRateParam.value());
00194 _state.storeFunctor(breed);
00195
00196
00197 eoAlgo<EOT> *algo = new eoEasyEA<EOT>(_continue, _eval, *breed, replace);
00198 _state.storeFunctor(algo);
00199
00200 return *algo;
00201 }
00202
00203 #endif