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 #ifndef _make_checkpoint_h
00028 #define _make_checkpoint_h
00029
00030 #include <climits>
00031
00032 #include <eoScalarFitness.h>
00033 #include <utils/selectors.h>
00034 #include <EO.h>
00035 #include <eoEvalFuncCounter.h>
00036 #include <utils/checkpointing>
00037
00038
00039
00040 bool testDirRes(std::string _dirName, bool _erase);
00042
00043
00044 template <class EOT>
00045 eoCheckPoint<EOT>& do_make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue)
00046 {
00047
00048 eoCheckPoint<EOT> *checkpoint = new eoCheckPoint<EOT>(_continue);
00049 _state.storeFunctor(checkpoint);
00050
00052
00054
00055 eoValueParam<bool>& useEvalParam = _parser.createParam(true, "useEval", "Use nb of eval. as counter (vs nb of gen.)", '\0', "Output");
00056
00057
00058 eoValueParam<unsigned> *generationCounter = new eoValueParam<unsigned>(0, "Gen.");
00059
00060 eoIncrementor<unsigned>* increment = new eoIncrementor<unsigned>(generationCounter->value());
00061
00062 checkpoint->add(*increment);
00063
00064 _state.storeFunctor(increment);
00065
00066
00067 eoValueParam<std::string>& dirNameParam = _parser.createParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output - Disk");
00068
00069 eoValueParam<bool>& eraseParam = _parser.createParam(false, "eraseDir", "erase files in dirName if any", '\0', "Output - Disk");
00070 bool dirOK = false;
00071
00073
00075
00088
00089
00090 eoValueParam<bool>& printBestParam = _parser.createParam(true, "printBestStat", "Print Best/avg/stdev every gen.", '\0', "Output");
00091 eoValueParam<bool>& plotBestParam = _parser.createParam(false, "plotBestStat", "Plot Best/avg Stat", '\0', "Output - Graphical");
00092 eoValueParam<bool>& fileBestParam = _parser.createParam(false, "fileBestStat", "Output bes/avg/std to file", '\0', "Output - Disk");
00093
00094 eoBestFitnessStat<EOT> *bestStat = NULL;
00095 if ( printBestParam.value() || plotBestParam.value() || fileBestParam.value() )
00096
00097 {
00098 bestStat = new eoBestFitnessStat<EOT>;
00099
00100 _state.storeFunctor(bestStat);
00101
00102 checkpoint->add(*bestStat);
00103 }
00104
00105
00106
00107 eoAverageStat<EOT> *averageStat = NULL;
00108 if ( plotBestParam.value() )
00109 {
00110 averageStat = new eoAverageStat<EOT>;
00111
00112 _state.storeFunctor(averageStat);
00113
00114 checkpoint->add(*averageStat);
00115 }
00116
00117
00118
00119 eoSecondMomentStats<EOT> *secondStat = NULL;
00120 if ( printBestParam.value() )
00121 {
00122 secondStat = new eoSecondMomentStats<EOT>;
00123
00124 _state.storeFunctor(secondStat);
00125
00126 checkpoint->add(*secondStat);
00127 }
00128
00129
00130
00131
00132 eoSortedPopStat<EOT> *popStat = NULL;
00133 eoValueParam<bool>& printPopParam = _parser.createParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output");
00134 if ( printPopParam.value() )
00135 {
00136 popStat = new eoSortedPopStat<EOT>("Dump of whole population");
00137
00138 _state.storeFunctor(popStat);
00139
00140 checkpoint->add(*popStat);
00141 }
00142
00143
00144
00145
00146 eoValueParam<bool>& printFDCParam = _parser.createParam(true, "printFDC", "Print FDC coeff. every gen.", '\0', "Output");
00147 eoValueParam<bool>& plotFDCParam = _parser.createParam(false, "plotFDCStat", "Plot FDC scatter plot", '\0', "Output - Graphical");
00148
00149 eoFDCStat<EOT> *fdcStat = NULL;
00150 if ( printFDCParam.value() || plotFDCParam.value() )
00151 {
00152
00153 eoQuadDistance<EOT> *dist = new eoQuadDistance<EOT>;
00154 _state.storeFunctor(dist);
00155 fdcStat = new eoFDCStat<EOT>(*dist);
00156
00157 _state.storeFunctor(fdcStat);
00158
00159 checkpoint->add(*fdcStat);
00160 }
00161
00162
00163 eoValueParam<bool> plotHistogramParam = _parser.createParam(false, "plotHisto", "Plot histogram of fitnesses", '\0', "Output - Graphical");
00164
00166
00168
00169 bool needStdoutMonitor = printBestParam.value() || printFDCParam.value()
00170 || printPopParam.value() ;
00171
00172
00173 if ( needStdoutMonitor )
00174 {
00175 eoStdoutMonitor *monitor = new eoStdoutMonitor(false);
00176 _state.storeFunctor(monitor);
00177
00178
00179 checkpoint->add(*monitor);
00180
00181
00182 monitor->add(*generationCounter);
00183 if (useEvalParam.value())
00184 monitor->add(_eval);
00185 if (printBestParam.value())
00186 {
00187 monitor->add(*bestStat);
00188 monitor->add(*secondStat);
00189 }
00190 if (printFDCParam.value())
00191 monitor->add(*fdcStat);
00192 if ( printPopParam.value())
00193 monitor->add(*popStat);
00194 }
00195
00196
00197 if ( ( fileBestParam.value() || plotBestParam.value() ||
00198 plotFDCParam.value() || plotHistogramParam.value() )
00199 && !dirOK )
00200 dirOK = testDirRes(dirNameParam.value(), eraseParam.value());
00201
00202 if (fileBestParam.value())
00203 {
00204 std::string stmp = dirNameParam.value() + "/best.xg";
00205 eoFileMonitor *fileMonitor = new eoFileMonitor(stmp);
00206
00207 _state.storeFunctor(fileMonitor);
00208 checkpoint->add(*fileMonitor);
00209
00210 fileMonitor->add(*generationCounter);
00211 fileMonitor->add(_eval);
00212 fileMonitor->add(*bestStat);
00213 fileMonitor->add(*secondStat);
00214 }
00215
00216 if (plotBestParam.value())
00217 {
00218 std::string stmp = dirNameParam.value() + "_gnu_best.xg";
00219 eoGnuplot1DMonitor *gnuMonitor = new eoGnuplot1DMonitor(stmp,minimizing_fitness<EOT>());
00220
00221 _state.storeFunctor(gnuMonitor);
00222 checkpoint->add(*gnuMonitor);
00223
00224 if (useEvalParam.value())
00225 gnuMonitor->add(_eval);
00226 else
00227 gnuMonitor->add(*generationCounter);
00228 gnuMonitor->add(*bestStat);
00229 gnuMonitor->add(*averageStat);
00230 }
00231
00232 if (plotFDCParam.value())
00233 {
00234
00235 eoFDCFileSnapshot<EOT> *fdcFileSnapshot = new eoFDCFileSnapshot<EOT>(*fdcStat, dirNameParam.value());
00236 _state.storeFunctor(fdcFileSnapshot);
00237
00238 eoGnuplot1DSnapshot *fdcGnuplot = new eoGnuplot1DSnapshot(*fdcFileSnapshot);
00239 _state.storeFunctor(fdcGnuplot);
00240
00241
00242 checkpoint->add(*fdcFileSnapshot);
00243 checkpoint->add(*fdcGnuplot);
00244 }
00245
00246
00247 if (plotHistogramParam.value())
00248 {
00249 eoScalarFitnessStat<EOT> *fitStat = new eoScalarFitnessStat<EOT>;
00250 _state.storeFunctor(fitStat);
00251 checkpoint->add(*fitStat);
00252
00253 eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirNameParam.value());
00254 _state.storeFunctor(fitSnapshot);
00255
00256 fitSnapshot->add(*fitStat);
00257
00258 checkpoint->add(*fitSnapshot);
00259 }
00260
00262
00264
00265
00266
00267 eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
00268
00269 if (_parser.isItThere(saveFrequencyParam))
00270 {
00271
00272 if (! dirOK )
00273 dirOK = testDirRes(dirNameParam.value(), eraseParam.value());
00274
00275 unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
00276 std::string stmp = dirNameParam.value() + "/generations";
00277 eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00278 _state.storeFunctor(stateSaver1);
00279 checkpoint->add(*stateSaver1);
00280 }
00281
00282
00283 eoValueParam<unsigned>& saveTimeIntervalParam = _parser.createParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
00284 if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
00285 {
00286
00287 if (! dirOK )
00288 dirOK = testDirRes(dirNameParam.value(), eraseParam.value());
00289
00290 std::string stmp = dirNameParam.value() + "/time";
00291 eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
00292 _state.storeFunctor(stateSaver2);
00293 checkpoint->add(*stateSaver2);
00294 }
00295
00296
00297 return *checkpoint;
00298 }
00299
00300 #endif