00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef _make_checkpoint_h
00039
00040 #define _make_checkpoint_h
00041
00042
00043 #ifdef HAVE_CONFIG_H
00044 #include <config.h>
00045 #endif
00046
00047 #include <climits>
00048
00049 #include <eoScalarFitness.h>
00050 #include <utils/selectors.h>
00051 #include <EO.h>
00052 #include <eoEvalFuncCounter.h>
00053 #include <utils/checkpointing>
00054
00055
00056
00057
00058
00059
00060
00061 bool testDirRes(std::string _dirName, bool _erase);
00062
00064
00065
00066
00067
00073 template <class EOT>
00074 eoCheckPoint<EOT>& do_make_checkpoint(eoParser& _parser, eoState& _state, eoValueParam<unsigned long>& _eval, eoContinue<EOT>& _continue)
00075 {
00076
00077
00078 eoCheckPoint<EOT> *checkpoint = new eoCheckPoint<EOT>(_continue);
00079
00080 _state.storeFunctor(checkpoint);
00081
00082
00083
00085
00086
00087
00089
00090
00091
00092 eoValueParam<bool>& useEvalParam = _parser.createParam(true, "useEval", "Use nb of eval. as counter (vs nb of gen.)", '\0', "Output");
00093
00094 eoValueParam<bool>& useTimeParam = _parser.createParam(true, "useTime", "Display time (s) every generation", '\0', "Output");
00095
00096
00097
00098
00099
00100 eoTimeCounter * tCounter = NULL;
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 eoIncrementorParam<unsigned> *generationCounter = new eoIncrementorParam<unsigned>("Gen.");
00111
00112
00113
00114 _state.storeFunctor(generationCounter);
00115
00116
00117
00118 checkpoint->add(*generationCounter);
00119
00120
00121
00122
00123
00124 eoValueParam<std::string>& dirNameParam = _parser.createParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output - Disk");
00125
00126
00127
00128 eoValueParam<bool>& eraseParam = _parser.createParam(true, "eraseDir", "erase files in dirName if any", '\0', "Output - Disk");
00129
00130 bool dirOK = false;
00131
00132
00133
00135
00136
00137
00139
00160
00161
00162
00163
00164 eoValueParam<bool>& printBestParam = _parser.createParam(true, "printBestStat", "Print Best/avg/stdev every gen.", '\0', "Output");
00165
00166 eoValueParam<bool>& plotBestParam = _parser.createParam(false, "plotBestStat", "Plot Best/avg Stat", '\0', "Output - Graphical");
00167
00168 eoValueParam<bool>& fileBestParam = _parser.createParam(false, "fileBestStat", "Output bes/avg/std to file", '\0', "Output - Disk");
00169
00170
00171
00172 eoBestFitnessStat<EOT> *bestStat = NULL;
00173
00174 if ( printBestParam.value() || plotBestParam.value() || fileBestParam.value() )
00175
00176
00177
00178 {
00179
00180 bestStat = new eoBestFitnessStat<EOT>;
00181
00182
00183
00184 _state.storeFunctor(bestStat);
00185
00186
00187
00188 checkpoint->add(*bestStat);
00189
00190 }
00191
00192
00193
00194
00195
00196
00197
00198 eoAverageStat<EOT> *averageStat = NULL;
00199
00200 if ( plotBestParam.value() )
00201
00202 {
00203
00204 averageStat = new eoAverageStat<EOT>;
00205
00206
00207
00208 _state.storeFunctor(averageStat);
00209
00210
00211
00212 checkpoint->add(*averageStat);
00213
00214 }
00215
00216
00217
00218
00219
00220
00221
00222 eoSecondMomentStats<EOT> *secondStat = NULL;
00223
00224 if ( printBestParam.value() || fileBestParam.value() )
00225
00226 {
00227
00228 secondStat = new eoSecondMomentStats<EOT>;
00229
00230
00231
00232 _state.storeFunctor(secondStat);
00233
00234
00235
00236 checkpoint->add(*secondStat);
00237
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 eoSortedPopStat<EOT> *popStat = NULL;
00249
00250 eoValueParam<bool>& printPopParam = _parser.createParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output");
00251
00252 if ( printPopParam.value() )
00253
00254 {
00255
00256 popStat = new eoSortedPopStat<EOT>;
00257
00258
00259
00260 _state.storeFunctor(popStat);
00261
00262
00263
00264 checkpoint->add(*popStat);
00265
00266 }
00267
00268
00269
00270
00271
00272
00273
00274 eoValueParam<bool> plotHistogramParam = _parser.createParam(false, "plotHisto", "Plot histogram of fitnesses", '\0', "Output - Graphical");
00275
00276
00277
00279
00280
00281
00283
00284
00285
00286 bool needStdoutMonitor = printBestParam.value()
00287
00288 || printPopParam.value() ;
00289
00290
00291
00292
00293
00294 if ( needStdoutMonitor )
00295
00296 {
00297
00298 eoStdoutMonitor *monitor = new eoStdoutMonitor(false);
00299
00300 _state.storeFunctor(monitor);
00301
00302
00303
00304
00305
00306 checkpoint->add(*monitor);
00307
00308
00309
00310
00311
00312 monitor->add(*generationCounter);
00313
00314 if (useEvalParam.value())
00315
00316 monitor->add(_eval);
00317
00318 if (useTimeParam.value())
00319
00320 {
00321
00322 tCounter = new eoTimeCounter;
00323
00324 _state.storeFunctor(tCounter);
00325
00326 checkpoint->add(*tCounter);
00327
00328 monitor->add(*tCounter);
00329
00330 }
00331
00332 if (printBestParam.value())
00333
00334 {
00335
00336 monitor->add(*bestStat);
00337
00338 monitor->add(*secondStat);
00339
00340 }
00341
00342 if ( printPopParam.value())
00343
00344 monitor->add(*popStat);
00345
00346 }
00347
00348
00349
00350
00351
00352 if ( ( fileBestParam.value() || plotBestParam.value() ||
00353
00354 plotHistogramParam.value() )
00355
00356 && !dirOK )
00357
00358 dirOK = testDirRes(dirNameParam.value(), eraseParam.value());
00359
00360
00361
00362 if (fileBestParam.value())
00363
00364 {
00365
00366 #ifdef _MSVC
00367
00368 std::string stmp = dirNameParam.value() + "\best.xg";
00369
00370 #else
00371
00372 std::string stmp = dirNameParam.value() + "/best.xg";
00373
00374 #endif
00375
00376 eoFileMonitor *fileMonitor = new eoFileMonitor(stmp);
00377
00378
00379
00380 _state.storeFunctor(fileMonitor);
00381
00382 checkpoint->add(*fileMonitor);
00383
00384
00385
00386 fileMonitor->add(*generationCounter);
00387
00388 fileMonitor->add(_eval);
00389
00390 if (tCounter)
00391
00392 {
00393
00394
00395
00396 fileMonitor->add(*tCounter);
00397
00398 }
00399
00400 fileMonitor->add(*bestStat);
00401
00402 fileMonitor->add(*secondStat);
00403
00404 }
00405
00406
00407
00408 #if defined(HAVE_GNUPLOT)
00409
00410 if (plotBestParam.value())
00411
00412 {
00413
00414 std::string stmp = dirNameParam.value() + "/gnu_best.xg";
00415
00416 eoGnuplot1DMonitor *gnuMonitor = new eoGnuplot1DMonitor(stmp,minimizing_fitness<EOT>());
00417
00418
00419
00420 _state.storeFunctor(gnuMonitor);
00421
00422 checkpoint->add(*gnuMonitor);
00423
00424
00425
00426 if (useEvalParam.value())
00427
00428 gnuMonitor->add(_eval);
00429
00430 else if (tCounter)
00431
00432 gnuMonitor->add(*tCounter);
00433
00434 else
00435
00436 gnuMonitor->add(*generationCounter);
00437
00438 gnuMonitor->add(*bestStat);
00439
00440 gnuMonitor->add(*averageStat);
00441
00442 }
00443
00444
00445
00446
00447
00448 if (plotHistogramParam.value())
00449
00450 {
00451
00452 eoScalarFitnessStat<EOT> *fitStat = new eoScalarFitnessStat<EOT>;
00453
00454 _state.storeFunctor(fitStat);
00455
00456 checkpoint->add(*fitStat);
00457
00458
00459
00460 eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirNameParam.value());
00461
00462 _state.storeFunctor(fitSnapshot);
00463
00464
00465
00466 fitSnapshot->add(*fitStat);
00467
00468
00469
00470 checkpoint->add(*fitSnapshot);
00471
00472 }
00473
00474 #endif
00475
00477
00478
00479
00481
00482
00483
00484
00485
00486
00487
00488 eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
00489
00490
00491
00492 if (_parser.isItThere(saveFrequencyParam))
00493
00494 {
00495
00496
00497
00498 if (! dirOK )
00499
00500 dirOK = testDirRes(dirNameParam.value(), eraseParam.value());
00501
00502
00503
00504 unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
00505
00506 #ifdef _MSVC
00507
00508 std::string stmp = dirNameParam.value() + "\generations";
00509
00510 #else
00511
00512 std::string stmp = dirNameParam.value() + "/generations";
00513
00514 #endif
00515
00516 eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00517
00518 _state.storeFunctor(stateSaver1);
00519
00520 checkpoint->add(*stateSaver1);
00521
00522 }
00523
00524
00525
00526
00527
00528 eoValueParam<unsigned>& saveTimeIntervalParam = _parser.createParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
00529
00530 if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
00531
00532 {
00533
00534
00535
00536 if (! dirOK )
00537
00538 dirOK = testDirRes(dirNameParam.value(), eraseParam.value());
00539
00540
00541
00542 #ifdef _MSVC
00543
00544 std::string stmp = dirNameParam.value() + "\time";
00545
00546 #else
00547
00548 std::string stmp = dirNameParam.value() + "/time";
00549
00550 #endif
00551
00552 eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
00553
00554 _state.storeFunctor(stateSaver2);
00555
00556 checkpoint->add(*stateSaver2);
00557
00558 }
00559
00560
00561
00562
00563
00564 return *checkpoint;
00565
00566 }
00567
00568
00569
00570 #endif
00571