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 eoValueParam<bool>& useTimeParam = _parser.createParam(true, "useTime", "Display time (s) every generation", '\0', "Output");
00055
00056
00057 eoTimeCounter * tCounter = NULL;
00058
00059
00060
00061
00062 eoIncrementorParam<unsigned> *generationCounter = new eoIncrementorParam<unsigned>("Gen.");
00063
00064 _state.storeFunctor(generationCounter);
00065
00066 checkpoint->add(*generationCounter);
00067
00068
00069 eoValueParam<std::string>& dirNameParam = _parser.createParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output - Disk");
00070
00071 eoValueParam<bool>& eraseParam = _parser.createParam(true, "eraseDir", "erase files in dirName if any", '\0', "Output - Disk");
00072 bool dirOK = false;
00073
00075
00077
00087
00088
00089 eoValueParam<bool>& printBestParam = _parser.createParam(true, "printBestStat", "Print Best/avg/stdev every gen.", '\0', "Output");
00090 eoValueParam<bool>& plotBestParam = _parser.createParam(false, "plotBestStat", "Plot Best/avg Stat", '\0', "Output - Graphical");
00091 eoValueParam<bool>& fileBestParam = _parser.createParam(false, "fileBestStat", "Output bes/avg/std to file", '\0', "Output - Disk");
00092
00093 eoBestFitnessStat<EOT> *bestStat = NULL;
00094 if ( printBestParam.value() || plotBestParam.value() || fileBestParam.value() )
00095
00096 {
00097 bestStat = new eoBestFitnessStat<EOT>;
00098
00099 _state.storeFunctor(bestStat);
00100
00101 checkpoint->add(*bestStat);
00102 }
00103
00104
00105
00106 eoAverageStat<EOT> *averageStat = NULL;
00107 if ( plotBestParam.value() )
00108 {
00109 averageStat = new eoAverageStat<EOT>;
00110
00111 _state.storeFunctor(averageStat);
00112
00113 checkpoint->add(*averageStat);
00114 }
00115
00116
00117
00118 eoSecondMomentStats<EOT> *secondStat = NULL;
00119 if ( printBestParam.value() )
00120 {
00121 secondStat = new eoSecondMomentStats<EOT>;
00122
00123 _state.storeFunctor(secondStat);
00124
00125 checkpoint->add(*secondStat);
00126 }
00127
00128
00129
00130
00131 eoSortedPopStat<EOT> *popStat = NULL;
00132 eoValueParam<bool>& printPopParam = _parser.createParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output");
00133 if ( printPopParam.value() )
00134 {
00135 popStat = new eoSortedPopStat<EOT>;
00136
00137 _state.storeFunctor(popStat);
00138
00139 checkpoint->add(*popStat);
00140 }
00141
00142
00143
00144 eoValueParam<bool> plotHistogramParam = _parser.createParam(false, "plotHisto", "Plot histogram of fitnesses", '\0', "Output - Graphical");
00145
00147
00149
00150 bool needStdoutMonitor = printBestParam.value()
00151 || printPopParam.value() ;
00152
00153
00154 if ( needStdoutMonitor )
00155 {
00156 eoStdoutMonitor *monitor = new eoStdoutMonitor(false);
00157 _state.storeFunctor(monitor);
00158
00159
00160 checkpoint->add(*monitor);
00161
00162
00163 monitor->add(*generationCounter);
00164 if (useEvalParam.value())
00165 monitor->add(_eval);
00166 if (useTimeParam.value())
00167 {
00168 tCounter = new eoTimeCounter;
00169 _state.storeFunctor(tCounter);
00170 checkpoint->add(*tCounter);
00171 monitor->add(*tCounter);
00172 }
00173 if (printBestParam.value())
00174 {
00175 monitor->add(*bestStat);
00176 monitor->add(*secondStat);
00177 }
00178 if ( printPopParam.value())
00179 monitor->add(*popStat);
00180 }
00181
00182
00183 if ( ( fileBestParam.value() || plotBestParam.value() ||
00184 plotHistogramParam.value() )
00185 && !dirOK )
00186 dirOK = testDirRes(dirNameParam.value(), eraseParam.value());
00187
00188 if (fileBestParam.value())
00189 {
00190 #ifdef _MSVC
00191 std::string stmp = dirNameParam.value() + "\best.xg";
00192 #else
00193 std::string stmp = dirNameParam.value() + "/best.xg";
00194 #endif
00195 eoFileMonitor *fileMonitor = new eoFileMonitor(stmp);
00196
00197 _state.storeFunctor(fileMonitor);
00198 checkpoint->add(*fileMonitor);
00199
00200 fileMonitor->add(*generationCounter);
00201 fileMonitor->add(_eval);
00202 if (tCounter)
00203 {
00204
00205 fileMonitor->add(*tCounter);
00206 }
00207 fileMonitor->add(*bestStat);
00208 fileMonitor->add(*secondStat);
00209 }
00210
00211 if (plotBestParam.value())
00212 {
00213 std::string stmp = dirNameParam.value() + "/gnu_best.xg";
00214 eoGnuplot1DMonitor *gnuMonitor = new eoGnuplot1DMonitor(stmp,minimizing_fitness<EOT>());
00215
00216 _state.storeFunctor(gnuMonitor);
00217 checkpoint->add(*gnuMonitor);
00218
00219 if (useEvalParam.value())
00220 gnuMonitor->add(_eval);
00221 else if (tCounter)
00222 gnuMonitor->add(*tCounter);
00223 else
00224 gnuMonitor->add(*generationCounter);
00225 gnuMonitor->add(*bestStat);
00226 gnuMonitor->add(*averageStat);
00227 }
00228
00229
00230 if (plotHistogramParam.value())
00231 {
00232 eoScalarFitnessStat<EOT> *fitStat = new eoScalarFitnessStat<EOT>;
00233 _state.storeFunctor(fitStat);
00234 checkpoint->add(*fitStat);
00235
00236 eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirNameParam.value());
00237 _state.storeFunctor(fitSnapshot);
00238
00239 fitSnapshot->add(*fitStat);
00240
00241 checkpoint->add(*fitSnapshot);
00242 }
00243
00245
00247
00248
00249
00250 eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
00251
00252 if (_parser.isItThere(saveFrequencyParam))
00253 {
00254
00255 if (! dirOK )
00256 dirOK = testDirRes(dirNameParam.value(), eraseParam.value());
00257
00258 unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
00259 #ifdef _MSVC
00260 std::string stmp = dirNameParam.value() + "\generations";
00261 #else
00262 std::string stmp = dirNameParam.value() + "/generations";
00263 #endif
00264 eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00265 _state.storeFunctor(stateSaver1);
00266 checkpoint->add(*stateSaver1);
00267 }
00268
00269
00270 eoValueParam<unsigned>& saveTimeIntervalParam = _parser.createParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
00271 if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
00272 {
00273
00274 if (! dirOK )
00275 dirOK = testDirRes(dirNameParam.value(), eraseParam.value());
00276
00277 #ifdef _MSVC
00278 std::string stmp = dirNameParam.value() + "\time";
00279 #else
00280 std::string stmp = dirNameParam.value() + "/time";
00281 #endif
00282 eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
00283 _state.storeFunctor(stateSaver2);
00284 checkpoint->add(*stateSaver2);
00285 }
00286
00287
00288 return *checkpoint;
00289 }
00290
00291 #endif