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