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 #include <ga.h>
00021
00022
00023 using namespace std;
00024
00025
00026 #include "binary_value.h"
00027
00028
00029
00030
00031 typedef eoBit<double> Indi;
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(8, "vecSize", "Genotype size",'V');
00056 parser.processParam( vecSizeParam, "Representation" );
00057 unsigned vecSize = vecSizeParam.value();
00058
00059
00060 eoValueParam<unsigned int> popSizeParam(10, "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(100, "maxGen", "Maximum number of generations",'G');
00074 parser.processParam( maxGenParam, "Stopping criterion" );
00075 unsigned maxGen = maxGenParam.value();
00076
00077 eoValueParam<unsigned int> minGenParam(100, "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 pop.sort();
00185
00186 cout << "Initial Population" << endl << pop << endl;
00187
00188
00190
00192
00193
00194 eoDetTournamentSelect<Indi> selectOne(tSize);
00195
00196 eoSelectPerc<Indi> select(selectOne);
00197
00198
00199
00200
00201 eoGenerationalReplacement<Indi> replace;
00202
00203
00205
00207
00208
00209 eo1PtBitXover<Indi> xover1;
00210
00211 eoUBitXover<Indi> xoverU;
00212
00213 eoNPtsBitXover<Indi> xover2(2);
00214
00215 eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
00216 xover.add(xoverU, URate);
00217 xover.add(xover2, twoPointsRate, true);
00218
00219
00220
00221 eoBitMutation<Indi> mutationBitFlip(pMutPerBit);
00222
00223 eoDetBitFlip<Indi> mutationOneBit;
00224
00225 eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
00226 mutation.add(mutationOneBit, oneBitRate, true);
00227
00228
00229 eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);
00230
00231
00233
00235 eoGenContinue<Indi> genCont(maxGen);
00236 eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen);
00237 eoFitContinue<Indi> fitCont(vecSize);
00238 eoCombinedContinue<Indi> continuator(genCont);
00239 continuator.add(steadyCont);
00240 continuator.add(fitCont);
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 eoCheckPoint<Indi> checkpoint(continuator);
00251
00252
00253 eoValueParam<unsigned> generationCounter(0, "Gen.");
00254
00255
00256
00257
00258
00259 eoIncrementor<unsigned> increment(generationCounter.value());
00260
00261
00262
00263 checkpoint.add(increment);
00264
00265
00266
00267 eoBestFitnessStat<Indi> bestStat;
00268
00269 eoSecondMomentStats<Indi> SecondStat;
00270
00271
00272 checkpoint.add(bestStat);
00273 checkpoint.add(SecondStat);
00274
00275
00276 eoStdoutMonitor monitor(false);
00277
00278
00279 checkpoint.add(monitor);
00280
00281
00282 monitor.add(generationCounter);
00283 monitor.add(eval);
00284 monitor.add(bestStat);
00285 monitor.add(SecondStat);
00286
00287
00288 eoFileMonitor fileMonitor("stats.xg", " ");
00289
00290
00291 checkpoint.add(fileMonitor);
00292
00293
00294 fileMonitor.add(generationCounter);
00295 fileMonitor.add(bestStat);
00296 fileMonitor.add(SecondStat);
00297
00298
00299 eoState outState;
00300
00301 outState.registerObject(parser);
00302 outState.registerObject(pop);
00303 outState.registerObject(rng);
00304
00305
00306
00307 eoCountedStateSaver stateSaver1(20, outState, "generation");
00308
00309 eoTimedStateSaver stateSaver2(1, outState, "time");
00310
00311
00312 checkpoint.add(stateSaver1);
00313 checkpoint.add(stateSaver2);
00314
00315
00316
00318
00320
00321
00322
00323 eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace);
00324
00325
00326 gga(pop);
00327
00328
00329
00330 pop.sort();
00331 cout << "FINAL Population\n" << pop << endl;
00332
00333 }
00334
00335
00336 int main(int argc, char **argv)
00337 {
00338 try
00339 {
00340 main_function(argc, argv);
00341 }
00342 catch(exception& e)
00343 {
00344 cout << "Exception: " << e.what() << '\n';
00345 }
00346
00347 return 1;
00348 }