- Main Page
- Classes
- Files

- Search for |

# make_checkpoint_moeo.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // make_checkpoint_moeo.h
    00005 // (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
    00006 /*
    00007     This library...
    00008
    00009     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
    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 <metric/moeoContributionMetric.h>
    00025 #include <metric/moeoEntropyMetric.h>
    00026 #include <utils/moeoArchiveUpdater.h>
    00027 #include <utils/moeoArchiveObjectiveVectorSavingUpdater.h>
    00028 #include <utils/moeoBinaryMetricSavingUpdater.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     /* the objective vector type */
    00046     typedef typename MOEOT::ObjectiveVector ObjectiveVector;
    00047
    00049     // Counters
    00051     // is nb Eval to be used as counter?
    00052     //bool useEval = _parser.getORcreateParam(true, "useEval", "Use nb of eval. as counter (vs nb of gen.)", '\0', "Output").value();
    00053     // Create anyway a generation-counter parameter
    00054     eoValueParam<unsigned int> *generationCounter = new eoValueParam<unsigned int>(0, "Gen.");
    00055     // Create an incrementor (sub-class of eoUpdater).
    00056     eoIncrementor<unsigned int> & increment = _state.storeFunctor( new eoIncrementor<unsigned int>(generationCounter->value()) );
    00057     // Add it to the checkpoint
    00058     checkpoint.add(increment);
    00059     // dir for DISK output
    00060     std::string & dirName =  _parser.getORcreateParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output").value();
    00061     // shoudl we empty it if exists
    00062     eoValueParam<bool>& eraseParam = _parser.getORcreateParam(true, "eraseDir", "erase files in dirName if any", '\0', "Output");
    00063     bool dirOK = false;            // not tested yet
    00064
    00065     // Dump of the whole population
    00066     //-----------------------------
    00067     bool printPop = _parser.getORcreateParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output").value();
    00068     eoSortedPopStat<MOEOT> * popStat;
    00069     if ( printPop ) // we do want pop dump
    00070     {
    00071         popStat = & _state.storeFunctor(new eoSortedPopStat<MOEOT>);
    00072         checkpoint.add(*popStat);
    00073     }
    00074
    00076     // State savers
    00078     // feed the state to state savers
    00079     // save state every N  generation
    00080     eoValueParam<unsigned int>& saveFrequencyParam = _parser.createParam((unsigned int)(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
    00081     if (_parser.isItThere(saveFrequencyParam))
    00082     {
    00083         // first make sure dirName is OK
    00084         if (! dirOK )
    00085             dirOK = testDirRes(dirName, eraseParam.value()); // TRUE
    00086         unsigned int 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     // save state every T seconds
    00097     eoValueParam<unsigned int>& saveTimeIntervalParam = _parser.getORcreateParam((unsigned int)(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
    00098     if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
    00099     {
    00100         // first make sure dirName is OK
    00101         if (! dirOK )
    00102             dirOK = testDirRes(dirName, eraseParam.value()); // TRUE
    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     // Archive
    00116     // update the archive every generation
    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     // store the objective vectors contained in the archive every generation
    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()); // TRUE
    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     // store the contribution of the non-dominated solutions
    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()); // TRUE
    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     // store the entropy of the non-dominated solutions
    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()); // TRUE
    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     // and that's it for the (control and) output
    00173     return checkpoint;
    00174 }
    00175
    00176 #endif /*MAKE_CHECKPOINT_MOEO_H_*/