00001 #ifndef eoEpsMOEA_h
00002 #define eoEpsMOEA_h
00003
00004 #include <eoAlgo.h>
00005 #include <moo/eoEpsilonArchive.h>
00006 #include <utils/eoStat.h>
00007
00008
00009 template <class EOT>
00010 class eoEpsMOEA : public eoAlgo<EOT> {
00011
00012 public:
00013
00014 eoEpsMOEA(
00015 eoContinue<EOT>& _continuator,
00016 eoEvalFunc<EOT>& _eval,
00017 eoGenOp<EOT>& _op,
00018 const std::vector<double>& eps,
00019 unsigned max_archive_size = std::numeric_limits<unsigned>::max()
00020 ) : continuator(_continuator),
00021 eval (_eval),
00022 loopEval(_eval),
00023 popEval(loopEval),
00024 op(_op),
00025 archive(eps, max_archive_size)
00026 {
00027 }
00028
00029
00030 void operator()(eoPop<EOT>& pop) {
00031
00032 eoPop<EOT> offspring;
00033 popEval(offspring, pop);
00034 for (unsigned i = 0; i < pop.size(); ++i) pop[i].fitnessReference().setWorth(1.0);
00035
00036 do {
00037 unsigned nProcessed = 0;
00038
00039 while (nProcessed < pop.size()) {
00040 offspring.clear();
00041
00042 epsPopulator populator(archive, pop, offspring);
00043 op(populator);
00044
00045 nProcessed += offspring.size();
00046 popEval(pop, offspring);
00047
00048 for (unsigned i = 0; i < offspring.size(); ++i) {
00049 offspring[i].fitnessReference().setWorth(1.0);
00050 archive(offspring[i]);
00051 update_pop(pop, offspring[i]);
00052 }
00053 }
00054
00055
00056 offspring.clear();
00057 archive.appendTo(offspring);
00058
00059 } while (continuator(offspring));
00060
00061
00062 pop.clear();
00063 archive.appendTo(pop);
00064
00065 }
00066
00067 private :
00068 void update_pop(eoPop<EOT>& pop, const EOT& offspring) {
00069 std::vector<unsigned> dominated;
00070
00071 for (unsigned i = 0; i < pop.size(); ++i) {
00072 int dom = offspring.fitness().check_dominance(pop[i].fitness());
00073 switch (dom) {
00074 case 1 :
00075 dominated.push_back(i);
00076 break;
00077 case -1 :
00078 return;
00079 case 0:
00080 break;
00081 }
00082 }
00083
00084 if (dominated.size()) {
00085 pop[ dominated[ rng.random(dominated.size()) ] ] = offspring;
00086 }
00087
00088
00089 pop[ rng.random(pop.size()) ] = offspring;
00090 }
00091
00092 class epsPopulator : public eoPopulator<EOT> {
00093
00094 using eoPopulator< EOT >::src;
00095
00096 eoEpsilonArchive<EOT>& archive;
00097 bool fromArchive;
00098 public:
00099 epsPopulator(eoEpsilonArchive<EOT>& arch, const eoPop<EOT>& pop, eoPop<EOT>& res) : eoPopulator<EOT>(pop, res), archive(arch), fromArchive(true) {}
00100
00101 const EOT& select() {
00102 fromArchive = !fromArchive;
00103
00104 using std::cout;
00105 using std::endl;
00106
00107 if (fromArchive && !archive.empty()) {
00108 return archive.selectRandom();
00109 }
00110
00111
00112 const EOT& eo1 = rng.choice(src);
00113 const EOT& eo2 = rng.choice(src);
00114
00115 if (eo1.fitness().dominates(eo2.fitness())) return eo1;
00116 return eo2;
00117 }
00118
00119 };
00120
00121
00122 eoContinue<EOT>& continuator;
00123
00124 eoEvalFunc <EOT> & eval ;
00125 eoPopLoopEval<EOT> loopEval;
00126
00127 eoPopEvalFunc<EOT>& popEval;
00128
00129 eoGenOp<EOT>& op;
00130
00131 eoEpsilonArchive<EOT> archive;
00132
00133 };
00134
00135 #endif
00136