00001
00002 #include <eo>
00003
00004 #include <moo/eoEpsMOEA.h>
00005 #include <utils/eoFuncPtrStat.h>
00006
00007 using namespace std;
00008
00009 class MinimizingFitnessTraits
00010 {
00011 public :
00012 static unsigned nObjectives() { return 2; }
00013 static double direction(unsigned) { return -1; }
00014 static double tol() { return 1e-9; }
00015
00016 static vector<double> eps;
00017 };
00018
00019 vector<double> MinimizingFitnessTraits::eps(2, 0.1);
00020
00021 typedef eoMOFitness<MinimizingFitnessTraits> fitness_type;
00022
00023 const unsigned chromsize=5;
00024 const double minval = -15;
00025 const double maxval = 15;
00026
00027 struct eoDouble : public EO<fitness_type>
00028 {
00029 double value[chromsize];
00030 };
00031
00032 class Mutate : public eoMonOp<eoDouble>
00033 {
00034 bool operator()(eoDouble& _eo)
00035 {
00036 for (unsigned i = 0; i < chromsize; ++i)
00037 {
00038 if (rng.flip(1./chromsize)) {
00039
00040 _eo.value[i] += rng.normal() * 0.1 * _eo.value[i];
00041
00042 if (_eo.value[i] < minval)
00043 _eo.value[i] = minval;
00044 else if (_eo.value[i] > maxval)
00045 _eo.value[i] = maxval;
00046 }
00047 }
00048 return true;
00049 }
00050 };
00051
00052 class Cross : public eoBinOp<eoDouble> {
00053
00054 bool operator()(eoDouble& _eo, const eoDouble& eo2)
00055 {
00056 for (unsigned i = 0; i < chromsize; ++i)
00057 {
00058 if (rng.flip(0.5)) {
00059 _eo.value[i] = eo2.value[i];
00060 }
00061 }
00062 return true;
00063 }
00064
00065 };
00066
00067 class Eval : public eoEvalFunc<eoDouble>
00068 {
00069 void operator()(eoDouble& _eo)
00070 {
00071 vector<double> x(_eo.value, _eo.value + chromsize);
00072 fitness_type f(0.0);
00073
00074 for (unsigned i = 0; i < chromsize; ++i)
00075 {
00076 if (i < chromsize-1)
00077 {
00078 f[0] += -10.0 * exp(-0.2 * sqrt(x[i]*x[i] + x[i+1]*x[i+1]));
00079 }
00080
00081 f[1] += pow(fabs(x[i]), 0.8) + 5 * pow(sin(x[i]),3.);
00082 }
00083
00084 _eo.fitness(f);
00085 }
00086 };
00087
00088 class Eval2 : public eoEvalFunc<eoDouble>
00089 {
00090 void operator()(eoDouble& _eo)
00091 {
00092 vector<double> x(_eo.value, _eo.value + chromsize);
00093 fitness_type f;
00094
00095 for (unsigned i = 0; i < chromsize; ++i)
00096 {
00097 f[0] += x[i] * x[i];
00098 }
00099
00100 f[1] =
00101 3 * x[0] + 2 * x[1]
00102 - x[2]/3 + 0.01*pow(x[3] - x[4], 3);
00103
00104 _eo.fitness(f);
00105 }
00106 };
00107
00108 class Eval3 : public eoEvalFunc<eoDouble>
00109 {
00110 void operator()(eoDouble& _eo)
00111 {
00112 double x = _eo.value[0];
00113 fitness_type f;
00114
00115 f[0] = x * x;
00116 double y = x-10;
00117 f[1] = y * y;
00118
00119 _eo.fitness(f);
00120 }
00121 };
00122
00123 class Init : public eoInit<eoDouble>
00124 {
00125 void operator()(eoDouble& _eo)
00126 {
00127 _eo.value[0] = rng.uniform();
00128
00129 double range = maxval - minval;
00130
00131 for (unsigned i = 1; i < chromsize; ++i)
00132 _eo.value[i] = rng.uniform() * range + minval;
00133 _eo.invalidate();
00134 }
00135 };
00136
00137 template <class EOT>
00138 unsigned get_size(const eoPop<EOT>& pop) {
00139 return pop.size();
00140 }
00141
00142 template <class EOT>
00143 unsigned counter(const eoPop<EOT>& pop) {
00144 static unsigned c = 0;
00145 return c++;
00146 }
00147
00148
00149 void the_main(int argc, char* argv[])
00150 {
00151 Init init;
00152 Eval eval;
00153 Mutate mutate;
00154 Cross cross;
00155
00156 eoParser parser(argc, argv);
00157 eoState state;
00158
00159 unsigned num_gen = parser.createParam(unsigned(50), "num_gen", "number of generations to run", 'g').value();
00160 unsigned pop_size = parser.createParam(unsigned(100), "pop_size", "population size", 'p').value();
00161
00162 eoPop<eoDouble> pop(pop_size, init);
00163
00164
00165 eoDetTournamentSelect<eoDouble> select;
00166
00167
00168 eoProportionalOp<eoDouble> opsel;
00169 opsel.add(mutate, 1.0);
00170 opsel.add(cross, 1.0);
00171
00172 unsigned long generation = 0;
00173 eoGenContinue<eoDouble> gen(num_gen, generation);
00174 eoCheckPoint<eoDouble> cp(gen);
00175
00176 eoMOFitnessStat<eoDouble> fitness0(0, "FirstObjective");
00177 eoMOFitnessStat<eoDouble> fitness1(1, "SecondObjective");
00178
00179 cp.add(fitness0);
00180 cp.add(fitness1);
00181
00182 eoGnuplot1DSnapshot snapshot("pareto");
00183
00184
00185 cp.add(snapshot);
00186
00187 snapshot.add(fitness0);
00188 snapshot.add(fitness1);
00189
00190 eoGnuplot1DMonitor monitor("sizemon");
00191
00192 cp.add(monitor);
00193
00194 eoFuncPtrStat<eoDouble, unsigned> size(get_size<eoDouble>);
00195 eoFuncPtrStat<eoDouble, unsigned> counterStat(counter<eoDouble>);
00196
00197 monitor.add(counterStat);
00198 monitor.add(size);
00199
00200 cp.add(size);
00201 cp.add(counterStat);
00202
00203
00204 eoEpsMOEA<eoDouble> ea(cp, eval, opsel, MinimizingFitnessTraits::eps );
00205
00206 if (parser.userNeedsHelp())
00207 {
00208 parser.printHelp(std::cout);
00209 return;
00210 }
00211
00212 ea(pop);
00213 }
00214
00215
00216 int main(int argc, char* argv[])
00217 {
00218 the_main(argc, argv);
00219 return 0;
00220 try
00221 {
00222 the_main(argc, argv);
00223 }
00224 catch (std::exception& e)
00225 {
00226 std::cout << "Exception thrown: " << e.what() << std::endl;
00227 throw e;
00228 }
00229 }
00230
00231
00232
00233
00234
00235
00236