00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef MAKE_CHECKPOINT_MOEO_H_
00014 #define MAKE_CHECKPOINT_MOEO_H_
00015
00016 #include <stdlib.h>
00017 #include <sstream>
00018 #include <eoContinue.h>
00019 #include <eoEvalFuncCounter.h>
00020 #include <utils/checkpointing>
00021 #include <utils/selectors.h>
00022 #include <utils/eoParser.h>
00023 #include <utils/eoState.h>
00024 #include <moeoArchiveUpdater.h>
00025 #include <moeoArchiveObjectiveVectorSavingUpdater.h>
00026 #include <metric/moeoBinaryMetricSavingUpdater.h>
00027 #include <metric/moeoContributionMetric.h>
00028 #include <metric/moeoEntropyMetric.h>
00029
00030 bool testDirRes(std::string _dirName, bool _erase);
00031
00041 template < class MOEOT >
00042 eoCheckPoint < MOEOT > & do_make_checkpoint_moeo (eoParser & _parser, eoState & _state, eoEvalFuncCounter < MOEOT > & _eval, eoContinue < MOEOT > & _continue, eoPop < MOEOT > & _pop, moeoArchive < MOEOT > & _archive)
00043 {
00044 eoCheckPoint < MOEOT > & checkpoint = _state.storeFunctor(new eoCheckPoint < MOEOT > (_continue));
00045
00046 typedef typename MOEOT::ObjectiveVector ObjectiveVector;
00047
00049
00051
00052
00053
00054 eoValueParam<unsigned> *generationCounter = new eoValueParam<unsigned>(0, "Gen.");
00055
00056 eoIncrementor<unsigned> & increment = _state.storeFunctor( new eoIncrementor<unsigned>(generationCounter->value()) );
00057
00058 checkpoint.add(increment);
00059
00060 std::string & dirName = _parser.getORcreateParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output").value();
00061
00062 eoValueParam<bool>& eraseParam = _parser.getORcreateParam(true, "eraseDir", "erase files in dirName if any", '\0', "Output");
00063 bool dirOK = false;
00064
00065
00066
00067 bool printPop = _parser.getORcreateParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output").value();
00068 eoSortedPopStat<MOEOT> * popStat;
00069 if ( printPop )
00070 {
00071 popStat = & _state.storeFunctor(new eoSortedPopStat<MOEOT>);
00072 checkpoint.add(*popStat);
00073 }
00074
00076
00078
00079
00080 eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
00081 if (_parser.isItThere(saveFrequencyParam))
00082 {
00083
00084 if (! dirOK )
00085 dirOK = testDirRes(dirName, eraseParam.value());
00086 unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
00087 #ifdef _MSVC
00088 std::string stmp = dirName + "\generations";
00089 #else
00090 std::string stmp = dirName + "/generations";
00091 #endif
00092 eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00093 _state.storeFunctor(stateSaver1);
00094 checkpoint.add(*stateSaver1);
00095 }
00096
00097 eoValueParam<unsigned>& saveTimeIntervalParam = _parser.getORcreateParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
00098 if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
00099 {
00100
00101 if (! dirOK )
00102 dirOK = testDirRes(dirName, eraseParam.value());
00103 #ifdef _MSVC
00104 std::string stmp = dirName + "\time";
00105 #else
00106 std::string stmp = dirName + "/time";
00107 #endif
00108 eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
00109 _state.storeFunctor(stateSaver2);
00110 checkpoint.add(*stateSaver2);
00111 }
00112
00114
00116
00117 bool updateArch = _parser.getORcreateParam(true, "updateArch", "Update the archive at each gen.", '\0', "Evolution Engine").value();
00118 if (updateArch)
00119 {
00120 moeoArchiveUpdater < MOEOT > * updater = new moeoArchiveUpdater < MOEOT > (_archive, _pop);
00121 _state.storeFunctor(updater);
00122 checkpoint.add(*updater);
00123 }
00124
00125 bool storeArch = _parser.getORcreateParam(false, "storeArch", "Store the archive's objective vectors at each gen.", '\0', "Output").value();
00126 if (storeArch)
00127 {
00128 if (! dirOK )
00129 dirOK = testDirRes(dirName, eraseParam.value());
00130 #ifdef _MSVC
00131 std::string stmp = dirName + "\arch";
00132 #else
00133 std::string stmp = dirName + "/arch";
00134 #endif
00135 moeoArchiveObjectiveVectorSavingUpdater < MOEOT > * save_updater = new moeoArchiveObjectiveVectorSavingUpdater < MOEOT > (_archive, stmp);
00136 _state.storeFunctor(save_updater);
00137 checkpoint.add(*save_updater);
00138 }
00139
00140 bool cont = _parser.getORcreateParam(false, "contribution", "Store the contribution of the archive at each gen.", '\0', "Output").value();
00141 if (cont)
00142 {
00143 if (! dirOK )
00144 dirOK = testDirRes(dirName, eraseParam.value());
00145 #ifdef _MSVC
00146 std::string stmp = dirName + "\contribution";
00147 #else
00148 std::string stmp = dirName + "/contribution";
00149 #endif
00150 moeoContributionMetric < ObjectiveVector > * contribution = new moeoContributionMetric < ObjectiveVector >;
00151 moeoBinaryMetricSavingUpdater < MOEOT > * contribution_updater = new moeoBinaryMetricSavingUpdater < MOEOT > (*contribution, _archive, stmp);
00152 _state.storeFunctor(contribution_updater);
00153 checkpoint.add(*contribution_updater);
00154 }
00155
00156 bool ent = _parser.getORcreateParam(false, "entropy", "Store the entropy of the archive at each gen.", '\0', "Output").value();
00157 if (ent)
00158 {
00159 if (! dirOK )
00160 dirOK = testDirRes(dirName, eraseParam.value());
00161 #ifdef _MSVC
00162 std::string stmp = dirName + "\entropy";
00163 #else
00164 std::string stmp = dirName + "/entropy";
00165 #endif
00166 moeoEntropyMetric < ObjectiveVector > * entropy = new moeoEntropyMetric < ObjectiveVector >;
00167 moeoBinaryMetricSavingUpdater < MOEOT > * entropy_updater = new moeoBinaryMetricSavingUpdater < MOEOT > (*entropy, _archive, stmp);
00168 _state.storeFunctor(entropy_updater);
00169 checkpoint.add(*entropy_updater);
00170 }
00171
00172
00173 return checkpoint;
00174 }
00175
00176 #endif