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