00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifdef HAVE_CONFIG_H
00010 #include <config.h>
00011 #endif
00012
00013
00014 #include <fstream>
00015 #include <iostream>
00016 #include <stdexcept>
00017
00018
00019 #include <eo>
00020
00021
00022 #include "binary_value.h"
00023
00024
00025
00026
00027 #include <ga.h>
00028
00029 typedef eoBit<eoMinimizingFitness> Indi;
00030
00031 using namespace std;
00032
00033
00034 void main_function(int argc, char **argv)
00035 {
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 eoParser parser(argc, argv);
00046
00047
00048
00049
00050 eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
00051 parser.processParam( seedParam );
00052 unsigned seed = seedParam.value();
00053
00054
00055 eoValueParam<unsigned int> vecSizeParam(100, "vecSize", "Genotype size",'V');
00056 parser.processParam( vecSizeParam, "Representation" );
00057 unsigned vecSize = vecSizeParam.value();
00058
00059
00060 eoValueParam<unsigned int> popSizeParam(100, "popSize", "Population size",'P');
00061 parser.processParam( popSizeParam, "Evolution engine" );
00062 unsigned popSize = popSizeParam.value();
00063
00064 eoValueParam<unsigned int> tSizeParam(10, "tSize", "Tournament size",'T');
00065 parser.processParam( tSizeParam, "Evolution Engine" );
00066 unsigned tSize = tSizeParam.value();
00067
00068
00069 eoValueParam<string> loadNameParam("", "Load","A save file to restart from",'L');
00070 parser.processParam( loadNameParam, "Persistence" );
00071 string loadName = loadNameParam.value();
00072
00073 eoValueParam<unsigned int> maxGenParam(500, "maxGen", "Maximum number of generations",'G');
00074 parser.processParam( maxGenParam, "Stopping criterion" );
00075 unsigned maxGen = maxGenParam.value();
00076
00077 eoValueParam<unsigned int> minGenParam(500, "minGen", "Minimum number of generations",'g');
00078 parser.processParam( minGenParam, "Stopping criterion" );
00079 unsigned minGen = minGenParam.value();
00080
00081 eoValueParam<unsigned int> steadyGenParam(100, "steadyGen", "Number of generations with no improvement",'s');
00082 parser.processParam( steadyGenParam, "Stopping criterion" );
00083 unsigned steadyGen = steadyGenParam.value();
00084
00085
00086 eoValueParam<double> pCrossParam(0.6, "pCross", "Probability of Crossover", 'C');
00087 parser.processParam( pCrossParam, "Genetic Operators" );
00088 double pCross = pCrossParam.value();
00089
00090 eoValueParam<double> pMutParam(0.1, "pMut", "Probability of Mutation", 'M');
00091 parser.processParam( pMutParam, "Genetic Operators" );
00092 double pMut = pMutParam.value();
00093
00094
00095 eoValueParam<double> onePointRateParam(1, "onePointRate", "Relative rate for one point crossover", '1');
00096 parser.processParam( onePointRateParam, "Genetic Operators" );
00097 double onePointRate = onePointRateParam.value();
00098
00099 eoValueParam<double> twoPointsRateParam(1, "twoPointRate", "Relative rate for two point crossover", '2');
00100 parser.processParam( twoPointsRateParam, "Genetic Operators" );
00101 double twoPointsRate = twoPointsRateParam.value();
00102
00103 eoValueParam<double> uRateParam(2, "uRate", "Relative rate for uniform crossover", 'U');
00104 parser.processParam( uRateParam, "Genetic Operators" );
00105 double URate = uRateParam.value();
00106
00107
00108 eoValueParam<double> pMutPerBitParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b');
00109 parser.processParam( pMutPerBitParam, "Genetic Operators" );
00110 double pMutPerBit = pMutPerBitParam.value();
00111
00112 eoValueParam<double> bitFlipRateParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B');
00113 parser.processParam( bitFlipRateParam, "Genetic Operators" );
00114 double bitFlipRate = bitFlipRateParam.value();
00115
00116 eoValueParam<double> oneBitRateParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D');
00117 parser.processParam( oneBitRateParam, "Genetic Operators" );
00118 double oneBitRate = oneBitRateParam.value();
00119
00120
00121 string str_status = parser.ProgramName() + ".status";
00122 eoValueParam<string> statusParam(str_status.c_str(), "status","Status file",'S');
00123 parser.processParam( statusParam, "Persistence" );
00124
00125
00126
00127 if (parser.userNeedsHelp())
00128 {
00129 parser.printHelp(cout);
00130 exit(1);
00131 }
00132 if (statusParam.value() != "")
00133 {
00134 ofstream os(statusParam.value().c_str());
00135 os << parser;
00136 }
00137
00138
00140
00142
00143 eoEvalFuncPtr<Indi, double, const vector<bool>& > plainEval( binary_value );
00144
00145 eoEvalFuncCounter<Indi> eval(plainEval);
00146
00147
00149
00151
00152
00153 eoPop<Indi> pop;
00154
00155 eoState inState;
00156
00157
00158
00159 inState.registerObject(rng);
00160 inState.registerObject(pop);
00161
00162 if (loadName != "")
00163 {
00164 inState.load(loadName);
00165
00166
00167 }
00168 else
00169 {
00170 rng.reseed(seed);
00171
00172
00173 eoUniformGenerator<bool> uGen;
00174 eoInitFixedLength<Indi> random(vecSize, uGen);
00175
00176
00177 pop.append(popSize, random);
00178
00179 apply<Indi>(eval, pop);
00180 }
00181
00182
00183
00184
00185
00186 cout << "Initial Population" << endl << pop ;
00187 cout << "and best is " << pop.best_element() << "\n\n";
00188 cout << "and worse is " << pop.worse_element() << "\n\n";
00189
00191
00193
00194
00195 eoDetTournamentSelect<Indi> selectOne(tSize);
00196
00197 eoSelectPerc<Indi> select(selectOne);
00198
00199
00200
00201
00202 eoGenerationalReplacement<Indi> replace;
00203
00204
00205
00206
00207
00208
00210
00212
00213
00214 eo1PtBitXover<Indi> xover1;
00215
00216 eoUBitXover<Indi> xoverU;
00217
00218 eoNPtsBitXover<Indi> xover2(2);
00219
00220 eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
00221 xover.add(xoverU, URate);
00222 xover.add(xover2, twoPointsRate, true);
00223
00224
00225
00226 eoBitMutation<Indi> mutationBitFlip(pMutPerBit);
00227
00228 eoDetBitFlip<Indi> mutationOneBit;
00229
00230 eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
00231 mutation.add(mutationOneBit, oneBitRate, true);
00232
00233
00234 eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);
00235
00236
00238
00240 eoGenContinue<Indi> genCont(maxGen);
00241 eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen);
00242
00243 eoCombinedContinue<Indi> continuator(genCont);
00244 continuator.add(steadyCont);
00245
00246
00247 #ifndef _MSC_VER
00248 eoCtrlCContinue<Indi> ctrlC;
00249 continuator.add(ctrlC);
00250 #endif
00251
00252
00253
00254
00255
00256
00257
00258
00259 eoCheckPoint<Indi> checkpoint(continuator);
00260
00261
00262 eoValueParam<unsigned> generationCounter(0, "Gen.");
00263
00264
00265
00266
00267
00268 eoIncrementor<unsigned> increment(generationCounter.value());
00269
00270
00271
00272 checkpoint.add(increment);
00273
00274
00275
00276 eoBestFitnessStat<Indi> bestStat;
00277 eoAverageStat<Indi> averageStat;
00278
00279 eoSecondMomentStats<Indi> SecondStat;
00280
00281
00282 eoQuadDistance<Indi> dist;
00283 eoFDCStat<Indi> fdcStat(dist);
00284
00285
00286 checkpoint.add(bestStat);
00287 checkpoint.add(averageStat);
00288 checkpoint.add(SecondStat);
00289 checkpoint.add(fdcStat);
00290
00291
00292 eoStdoutMonitor monitor(false);
00293
00294
00295 checkpoint.add(monitor);
00296
00297
00298 monitor.add(generationCounter);
00299 monitor.add(eval);
00300 monitor.add(bestStat);
00301 monitor.add(SecondStat);
00302 monitor.add(fdcStat);
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 eoFileMonitor fileMonitor("stats.xg", " ");
00313
00314
00315 checkpoint.add(fileMonitor);
00316
00317
00318 fileMonitor.add(generationCounter);
00319 fileMonitor.add(bestStat);
00320 fileMonitor.add(SecondStat);
00321
00322 #ifndef _MSC_VER
00323
00324 eoGnuplot1DMonitor gnuMonitor("best_average.xg",minimizing_fitness<Indi>());
00325
00326 checkpoint.add(gnuMonitor);
00327
00328 gnuMonitor.add(eval);
00329 gnuMonitor.add(bestStat);
00330 gnuMonitor.add(averageStat);
00331
00332
00333 gnuMonitor.gnuplotCommand("set yrange [0:500]");
00334
00335
00336
00337 eoFDCFileSnapshot<Indi> fdcFileSnapshot(fdcStat);
00338
00339 eoGnuplot1DSnapshot fdcGnuplot(fdcFileSnapshot);
00340
00341 checkpoint.add(fdcFileSnapshot);
00342 checkpoint.add(fdcGnuplot);
00343
00344
00345 eoScalarFitnessStat<Indi> fitStat;
00346 checkpoint.add(fitStat);
00347
00348
00349 eoGnuplot1DSnapshot fitSnapshot("Fitnesses");
00350
00351 fitSnapshot.add(fitStat);
00352
00353 checkpoint.add(fitSnapshot);
00354 #endif
00355
00356 eoState outState;
00357
00358 outState.registerObject(rng);
00359 outState.registerObject(pop);
00360
00361
00362
00363 eoCountedStateSaver stateSaver1(100, outState, "generation");
00364
00365 eoTimedStateSaver stateSaver2(1, outState, "time");
00366
00367
00368 checkpoint.add(stateSaver1);
00369 checkpoint.add(stateSaver2);
00370
00371
00372
00374
00376
00377
00378
00379 eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace);
00380
00381
00382 gga(pop);
00383
00384
00385
00386 pop.sort();
00387 cout << "FINAL Population\n" << pop << endl;
00388
00389 }
00390
00391
00392 int main(int argc, char **argv)
00393 {
00394 try
00395 {
00396 main_function(argc, argv);
00397 }
00398 catch(exception& e)
00399 {
00400 cout << "Exception: " << e.what() << '\n';
00401 }
00402
00403 return 1;
00404 }