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_pareto_h
00028 #define _make_checkpoint_pareto_h
00029
00030 #include <stdlib.h>
00031 #include <sstream>
00032
00033 #include "EO.h"
00034 #include "eoParetoFitness.h"
00035 #include "eoEvalFuncCounter.h"
00036 #include "utils/checkpointing"
00037 #include "utils/selectors.h"
00038
00039
00040
00041 bool testDirRes(std::string _dirName, bool _erase);
00043
00046 template <class EOT>
00047 eoCheckPoint<EOT>& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state,
00048 eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue)
00049 {
00050
00051 eoCheckPoint<EOT> & checkpoint = _state.storeFunctor(new eoCheckPoint<EOT>(_continue));
00052
00054 typedef typename EOT::Fitness Fit;
00055 Fit fit;
00056 unsigned nObj = fit.size();
00057
00059
00061
00062 bool useEval = _parser.getORcreateParam(true, "useEval",
00063 "Use nb of eval. as counter (vs nb of gen.)",
00064 '\0', "Output").value();
00065
00066
00067 eoValueParam<unsigned> *generationCounter = new eoValueParam<unsigned>(0, "Gen.");
00068
00069 eoIncrementor<unsigned> & increment =
00070 _state.storeFunctor(new eoIncrementor<unsigned>(generationCounter->value()) );
00071
00072 checkpoint.add(increment);
00073
00074
00075 std::string & dirName = _parser.getORcreateParam(std::string("Res"), "resDir",
00076 "Directory to store DISK outputs",
00077 '\0', "Output - Disk").value();
00078
00079 eoValueParam<bool>& eraseParam = _parser.getORcreateParam(true, "eraseDir",
00080 "erase files in dirName if any",
00081 '\0', "Output - Disk");
00082 bool dirOK = false;
00083
00085
00087
00093 eoValueParam<eoParamParamType>& fPlotParam
00094 = _parser.getORcreateParam(eoParamParamType("1(0,1)"), "frontFileFrequency",
00095 "File save frequency in objective spaces (std::pairs of comma-separated objectives " \
00096 "in 1 single parentheses std::pair)",
00097 '\0', "Output - Disk");
00098
00099 bool boolGnuplot = _parser.getORcreateParam(false, "plotFront",
00100 "Objective plots (requires corresponding files " \
00101 "- see frontFileFrequency",
00102 '\0', "Output - Graphical").value();
00103
00104 eoParamParamType & fPlot = fPlotParam.value();
00105 unsigned frequency = atoi(fPlot.first.c_str());
00106 if (frequency)
00107 {
00108 unsigned nbPlot = fPlot.second.size();
00109 if ( nbPlot % 2 )
00110 throw std::runtime_error("Odd number of front description in make_checkpoint_pareto");
00111
00112
00113 std::vector<bool> bStat(nObj, false);
00114 std::vector<eoMOFitnessStat<EOT>* > theStats(nObj);
00115
00116
00117 for (unsigned i=0; i<nbPlot; i+=2)
00118 {
00119 unsigned obj1 = atoi(fPlot.second[i].c_str());
00120 unsigned obj2 = atoi(fPlot.second[i+1].c_str());
00121 eoMOFitnessStat<EOT>* fStat;
00122 if (!bStat[obj1]) {
00123 std::ostringstream os;
00124 os << "Obj. " << obj1 << std::ends;
00125 fStat = new eoMOFitnessStat<EOT>(obj1, os.str().c_str());
00126 _state.storeFunctor(fStat);
00127 bStat[obj1]=true;
00128 theStats[obj1]=fStat;
00129 checkpoint.add(*fStat);
00130 }
00131 if (!bStat[obj2]) {
00132 std::ostringstream os;
00133 os << "Obj. " << obj2 << std::ends;
00134 fStat = new eoMOFitnessStat<EOT>(obj2, os.str().c_str());
00135 _state.storeFunctor(fStat);
00136 bStat[obj2]=true;
00137 theStats[obj2]=fStat;
00138 checkpoint.add(*fStat);
00139 }
00140
00141
00142 std::ostringstream os;
00143 os << "Front." << obj1 << "." << obj2 << "." << std::ends;
00144 eoFileSnapshot& snapshot = _state.storeFunctor(
00145 new eoFileSnapshot(dirName, frequency, os.str().c_str()));
00146 checkpoint.add(snapshot);
00147
00148 snapshot.add(*theStats[obj1]);
00149 snapshot.add(*theStats[obj2]);
00150
00151
00152 if(boolGnuplot)
00153 {
00154 eoGnuplot1DSnapshot & plotSnapshot = _state.storeFunctor(new
00155 eoGnuplot1DSnapshot(snapshot));
00156 plotSnapshot.setPointSize(3);
00157 checkpoint.add(plotSnapshot);
00158 }
00159 }
00160 }
00161
00162
00163 bool printPop = _parser.getORcreateParam(false, "printPop",
00164 "Print sorted pop. every gen.",
00165 '\0', "Output").value();
00166 eoSortedPopStat<EOT> * popStat;
00167 if ( printPop )
00168 {
00169 std::cout << "On cree printpop\n";
00170 popStat = & _state.storeFunctor(new eoSortedPopStat<EOT>);
00171
00172 checkpoint.add(*popStat);
00173 }
00174
00176
00178
00179 bool needStdoutMonitor = printPop ;
00180
00181
00182 if ( needStdoutMonitor )
00183 {
00184 eoStdoutMonitor & monitor = _state.storeFunctor(new eoStdoutMonitor(false));
00185
00186
00187 checkpoint.add(monitor);
00188
00189
00190 monitor.add(*generationCounter);
00191 if (useEval)
00192 monitor.add(_eval);
00193 if ( printPop)
00194 monitor.add(*popStat);
00195 }
00196
00197
00199
00201
00202
00203
00204 eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
00205
00206 if (_parser.isItThere(saveFrequencyParam))
00207 {
00208
00209 if (! dirOK )
00210 dirOK = testDirRes(dirName, eraseParam.value());
00211
00212 unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
00213 #ifdef _MSVC
00214 std::string stmp = dirName + "\generations";
00215 #else
00216 std::string stmp = dirName + "/generations";
00217 #endif
00218 eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00219 _state.storeFunctor(stateSaver1);
00220 checkpoint.add(*stateSaver1);
00221 }
00222
00223
00224 eoValueParam<unsigned>& saveTimeIntervalParam
00225 = _parser.getORcreateParam(unsigned(0), "saveTimeInterval",
00226 "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
00227 if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
00228 {
00229
00230 if (! dirOK )
00231 dirOK = testDirRes(dirName, eraseParam.value());
00232
00233 #ifdef _MSVC
00234 std::string stmp = dirName + "\time";
00235 #else
00236 std::string stmp = dirName + "/time";
00237 #endif
00238 eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
00239 _state.storeFunctor(stateSaver2);
00240 checkpoint.add(*stateSaver2);
00241 }
00242
00243
00244 return checkpoint;
00245 }
00246
00247 #endif