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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef MAKE_CHECKPOINT_MOEO_H_
00039 #define MAKE_CHECKPOINT_MOEO_H_
00040
00041 #include <stdlib.h>
00042 #include <sstream>
00043 #include <eoContinue.h>
00044 #include <eoEvalFuncCounter.h>
00045 #include <utils/checkpointing>
00046 #include <utils/selectors.h>
00047 #include <utils/eoParser.h>
00048 #include <utils/eoState.h>
00049 #include <metric/moeoContributionMetric.h>
00050 #include <metric/moeoEntropyMetric.h>
00051 #include <utils/moeoArchiveUpdater.h>
00052 #include <utils/moeoArchiveObjectiveVectorSavingUpdater.h>
00053 #include <utils/moeoBinaryMetricSavingUpdater.h>
00054
00055 bool testDirRes(std::string _dirName, bool _erase);
00056
00066 template < class MOEOT >
00067 eoCheckPoint < MOEOT > & do_make_checkpoint_moeo (eoParser & _parser, eoState & _state, eoEvalFuncCounter < MOEOT > & _eval, eoContinue < MOEOT > & _continue, eoPop < MOEOT > & _pop, moeoArchive < MOEOT > & _archive)
00068 {
00069 eoCheckPoint < MOEOT > & checkpoint = _state.storeFunctor(new eoCheckPoint < MOEOT > (_continue));
00070
00071 typedef typename MOEOT::ObjectiveVector ObjectiveVector;
00072
00074
00076
00077
00078
00079 eoValueParam<unsigned int> *generationCounter = new eoValueParam<unsigned int>(0, "Gen.");
00080
00081 eoIncrementor<unsigned int> & increment = _state.storeFunctor( new eoIncrementor<unsigned int>(generationCounter->value()) );
00082
00083 checkpoint.add(increment);
00084
00085 std::string & dirName = _parser.getORcreateParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output").value();
00086
00087 eoValueParam<bool>& eraseParam = _parser.getORcreateParam(true, "eraseDir", "erase files in dirName if any", '\0', "Output");
00088 bool dirOK = false;
00089
00090
00091
00092 bool printPop = _parser.getORcreateParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output").value();
00093 eoSortedPopStat<MOEOT> * popStat;
00094 if ( printPop )
00095 {
00096 popStat = & _state.storeFunctor(new eoSortedPopStat<MOEOT>);
00097 checkpoint.add(*popStat);
00098 }
00099
00101
00103
00104
00105 eoValueParam<unsigned int>& saveFrequencyParam = _parser.createParam((unsigned int)(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
00106 if (_parser.isItThere(saveFrequencyParam))
00107 {
00108
00109 if (! dirOK )
00110 dirOK = testDirRes(dirName, eraseParam.value());
00111 unsigned int freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
00112 #ifdef _MSVC
00113 std::string stmp = dirName + "\generations";
00114 #else
00115 std::string stmp = dirName + "/generations";
00116 #endif
00117 eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00118 _state.storeFunctor(stateSaver1);
00119 checkpoint.add(*stateSaver1);
00120 }
00121
00122 eoValueParam<unsigned int>& saveTimeIntervalParam = _parser.getORcreateParam((unsigned int)(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
00123 if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
00124 {
00125
00126 if (! dirOK )
00127 dirOK = testDirRes(dirName, eraseParam.value());
00128 #ifdef _MSVC
00129 std::string stmp = dirName + "\time";
00130 #else
00131 std::string stmp = dirName + "/time";
00132 #endif
00133 eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
00134 _state.storeFunctor(stateSaver2);
00135 checkpoint.add(*stateSaver2);
00136 }
00137
00139
00141
00142 bool updateArch = _parser.getORcreateParam(true, "updateArch", "Update the archive at each gen.", '\0', "Evolution Engine").value();
00143 if (updateArch)
00144 {
00145 moeoArchiveUpdater < MOEOT > * updater = new moeoArchiveUpdater < MOEOT > (_archive, _pop);
00146 _state.storeFunctor(updater);
00147 checkpoint.add(*updater);
00148 }
00149
00150 bool storeArch = _parser.getORcreateParam(false, "storeArch", "Store the archive's objective vectors at each gen.", '\0', "Output").value();
00151 if (storeArch)
00152 {
00153 if (! dirOK )
00154 dirOK = testDirRes(dirName, eraseParam.value());
00155 #ifdef _MSVC
00156 std::string stmp = dirName + "\arch";
00157 #else
00158 std::string stmp = dirName + "/arch";
00159 #endif
00160 moeoArchiveObjectiveVectorSavingUpdater < MOEOT > * save_updater = new moeoArchiveObjectiveVectorSavingUpdater < MOEOT > (_archive, stmp);
00161 _state.storeFunctor(save_updater);
00162 checkpoint.add(*save_updater);
00163 }
00164
00165 bool cont = _parser.getORcreateParam(false, "contribution", "Store the contribution of the archive at each gen.", '\0', "Output").value();
00166 if (cont)
00167 {
00168 if (! dirOK )
00169 dirOK = testDirRes(dirName, eraseParam.value());
00170 #ifdef _MSVC
00171 std::string stmp = dirName + "\contribution";
00172 #else
00173 std::string stmp = dirName + "/contribution";
00174 #endif
00175 moeoContributionMetric < ObjectiveVector > * contribution = new moeoContributionMetric < ObjectiveVector >;
00176 moeoBinaryMetricSavingUpdater < MOEOT > * contribution_updater = new moeoBinaryMetricSavingUpdater < MOEOT > (*contribution, _archive, stmp);
00177 _state.storeFunctor(contribution_updater);
00178 checkpoint.add(*contribution_updater);
00179 }
00180
00181 bool ent = _parser.getORcreateParam(false, "entropy", "Store the entropy of the archive at each gen.", '\0', "Output").value();
00182 if (ent)
00183 {
00184 if (! dirOK )
00185 dirOK = testDirRes(dirName, eraseParam.value());
00186 #ifdef _MSVC
00187 std::string stmp = dirName + "\entropy";
00188 #else
00189 std::string stmp = dirName + "/entropy";
00190 #endif
00191 moeoEntropyMetric < ObjectiveVector > * entropy = new moeoEntropyMetric < ObjectiveVector >;
00192 moeoBinaryMetricSavingUpdater < MOEOT > * entropy_updater = new moeoBinaryMetricSavingUpdater < MOEOT > (*entropy, _archive, stmp);
00193 _state.storeFunctor(entropy_updater);
00194 checkpoint.add(*entropy_updater);
00195 }
00196
00197
00198 return checkpoint;
00199 }
00200
00201 #endif