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