00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifdef HAVE_CONFIG_H
00012 #include <config.h>
00013 #endif
00014
00015
00016 #include <fstream>
00017 #include <iostream>
00018 #include <stdexcept>
00019
00020
00021 #include <eo>
00022 #include <es.h>
00023
00024
00025
00026
00027 typedef eoReal<eoMinimizingFitness> Indi;
00028
00029
00030 using namespace std;
00031
00032
00033
00034
00035
00036
00037 #include "real_value.h"
00038
00039
00040
00041
00042 void main_function(int argc, char **argv)
00043 {
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 eoParser parser(argc, argv);
00054
00055
00056
00057
00058 unsigned seed = parser.createParam(unsigned(time(0)), "seed", "Random number seed", 'S').value();
00059
00060
00061 unsigned vecSize = parser.createParam(unsigned(8), "vecSize", "Genotype size",'V', "Representation" ).value();
00062
00063
00064 unsigned popSize = parser.createParam(unsigned(10), "popSize", "Population size",'P', "Evolution engine" ).value();
00065
00066 unsigned tSize = parser.createParam(unsigned(2), "tSize", "Tournament size",'T', "Evolution Engine" ).value();
00067
00068
00069 string loadName = parser.createParam(string(""), "Load","A save file to restart from",'L', "Persistence" ).value();
00070
00071 unsigned maxGen = parser.createParam(unsigned(100), "maxGen", "Maximum number of generations",'G', "Stopping criterion" ).value();
00072
00073 unsigned minGen = parser.createParam(unsigned(100), "minGen", "Minimum number of generations",'g', "Stopping criterion" ).value();
00074
00075 unsigned steadyGen = parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion" ).value();
00076
00077
00078 double pCross = parser.createParam(double(0.6), "pCross", "Probability of Crossover", 'C', "Genetic Operators" ).value();
00079
00080 double pMut = parser.createParam(double(0.1), "pMut", "Probability of Mutation", 'M', "Genetic Operators" ).value();
00081
00082
00083 double hypercubeRate = parser.createParam(double(1), "hypercubeRate", "Relative rate for hypercube crossover", '\0', "Genetic Operators" ).value();
00084
00085 double segmentRate = parser.createParam(double(1), "segmentRate", "Relative rate for segment crossover", '\0', "Genetic Operators" ).value();
00086
00087
00088 double EPSILON = parser.createParam(double(0.01), "EPSILON", "Width for uniform mutation", '\0', "Genetic Operators" ).value();
00089
00090 double SIGMA = parser.createParam(double(0.3), "SIGMA", "Sigma for normal mutation", '\0', "Genetic Operators" ).value();
00091
00092
00093 double uniformMutRate = parser.createParam(double(1), "uniformMutRate", "Relative rate for uniform mutation", '\0', "Genetic Operators" ).value();
00094
00095 double detMutRate = parser.createParam(double(1), "detMutRate", "Relative rate for det-uniform mutation", '\0', "Genetic Operators" ).value();
00096
00097 double normalMutRate = parser.createParam(double(1), "normalMutRate", "Relative rate for normal mutation", '\0', "Genetic Operators" ).value();
00098
00099
00100 string str_status = parser.ProgramName() + ".status";
00101 string statusName = parser.createParam(str_status, "status","Status file",'S', "Persistence" ).value();
00102
00103
00104
00105 if (parser.userNeedsHelp())
00106 {
00107 parser.printHelp(cout);
00108 exit(1);
00109 }
00110 if (statusName != "")
00111 {
00112 ofstream os(statusName.c_str());
00113 os << parser;
00114 }
00115
00116
00118
00120
00121
00122 eoEvalFuncPtr<Indi, double, const vector<double>& > plainEval( real_value );
00123
00124 eoEvalFuncCounter<Indi> eval(plainEval);
00125
00126
00128
00130
00131
00132 eoPop<Indi> pop;
00133
00134 eoState inState;
00135
00136
00137
00138 inState.registerObject(rng);
00139 inState.registerObject(pop);
00140
00141 if (loadName != "")
00142 {
00143 inState.load(loadName);
00144
00145
00146 }
00147 else
00148 {
00149 rng.reseed(seed);
00150
00151
00152 eoUniformGenerator<double> uGen(-1.0, 1.0);
00153 eoInitFixedLength<Indi> random(vecSize, uGen);
00154
00155
00156 pop.append(popSize, random);
00157
00158 apply<Indi>(eval, pop);
00159 }
00160
00161
00162
00163 pop.sort();
00164
00165 cout << "Initial Population" << endl;
00166 cout << pop;
00167
00168
00170
00172
00173
00174 eoDetTournamentSelect<Indi> selectOne(tSize);
00175
00176 eoSelectPerc<Indi> select(selectOne);
00177
00178
00179
00180
00181 eoGenerationalReplacement<Indi> replace;
00182
00183
00185
00187
00188
00189 eoSegmentCrossover<Indi> xoverS;
00190
00191 eoHypercubeCrossover<Indi> xoverA;
00192
00193 eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);
00194 xover.add(xoverA, hypercubeRate, true);
00195
00196
00197
00198 eoUniformMutation<Indi> mutationU(EPSILON);
00199
00200 eoDetUniformMutation<Indi> mutationD(EPSILON);
00201
00202 eoNormalMutation<Indi> mutationN(SIGMA);
00203
00204 eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);
00205 mutation.add(mutationD, detMutRate);
00206 mutation.add(mutationN, normalMutRate, true);
00207
00208
00209 eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);
00210
00211
00213
00215 eoGenContinue<Indi> genCont(maxGen);
00216 eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen);
00217 eoFitContinue<Indi> fitCont(0);
00218 eoCombinedContinue<Indi> continuator(genCont);
00219 continuator.add(steadyCont);
00220 continuator.add(fitCont);
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 eoCheckPoint<Indi> checkpoint(continuator);
00231
00232
00233 eoValueParam<unsigned> generationCounter(0, "Gen.");
00234
00235
00236
00237
00238
00239 eoIncrementor<unsigned> increment(generationCounter.value());
00240
00241
00242
00243 checkpoint.add(increment);
00244
00245
00246
00247 eoBestFitnessStat<Indi> bestStat;
00248
00249 eoSecondMomentStats<Indi> SecondStat;
00250
00251
00252 checkpoint.add(bestStat);
00253 checkpoint.add(SecondStat);
00254
00255
00256 eoStdoutMonitor monitor(false);
00257
00258
00259 checkpoint.add(monitor);
00260
00261
00262 monitor.add(generationCounter);
00263 monitor.add(eval);
00264 monitor.add(bestStat);
00265 monitor.add(SecondStat);
00266
00267
00268 eoFileMonitor fileMonitor("stats.xg", " ");
00269
00270
00271 checkpoint.add(fileMonitor);
00272
00273
00274 fileMonitor.add(generationCounter);
00275 fileMonitor.add(bestStat);
00276 fileMonitor.add(SecondStat);
00277
00278
00279 eoState outState;
00280
00281 outState.registerObject(parser);
00282 outState.registerObject(pop);
00283 outState.registerObject(rng);
00284
00285
00286
00287 eoCountedStateSaver stateSaver1(20, outState, "generation");
00288
00289 eoTimedStateSaver stateSaver2(1, outState, "time");
00290
00291
00292 checkpoint.add(stateSaver1);
00293 checkpoint.add(stateSaver2);
00294
00295
00296
00298
00300
00301
00302
00303 eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace);
00304
00305
00306 gga(pop);
00307
00308
00309
00310 pop.sort();
00311 cout << "FINAL Population\n" << pop << endl;
00312
00313 }
00314
00315
00316
00317 int main(int argc, char **argv)
00318 {
00319 try
00320 {
00321 main_function(argc, argv);
00322 }
00323 catch(exception& e)
00324 {
00325 cout << "Exception: " << e.what() << '\n';
00326 }
00327
00328 return 1;
00329 }