make_checkpoint.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // make_checkpoint.h
00005 // (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2000
00006 /*
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00022              Marc.Schoenauer@polytechnique.fr
00023              mkeijzer@dhi.dk
00024  */
00025 //-----------------------------------------------------------------------------
00026 
00027 #ifndef _make_checkpoint_h
00028 #define _make_checkpoint_h
00029 
00030 #include <eoScalarFitness.h>
00031 #include <utils/selectors.h> // for minimizing_fitness()
00032 #include <EO.h>
00033 #include <eoEvalFuncCounter.h>
00034 #include <utils/checkpointing>
00035 
00036 // at the moment, in utils/make_help.cpp
00037 // this should become some eoUtils.cpp with corresponding eoUtils.h
00038 bool testDirRes(std::string _dirName, bool _erase);
00040 
00041 
00042 template <class EOT>
00043 eoCheckPoint<EOT>& do_make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue)
00044 {
00045   // first, create a checkpoint from the eoContinue
00046   eoCheckPoint<EOT> *checkpoint = new eoCheckPoint<EOT>(_continue);
00047   _state.storeFunctor(checkpoint);
00048 
00050   // Counters
00052   // is nb Eval to be used as counter?
00053   eoValueParam<bool>& useEvalParam = _parser.createParam(true, "useEval", "Use nb of eval. as counter (vs nb of gen.)", '\0', "Output");
00054   eoValueParam<bool>& useTimeParam = _parser.createParam(true, "useTime", "Display time (s) every generation", '\0', "Output");
00055 
00056   // if we want the time, we need an eoTimeCounter
00057   eoTimeCounter * tCounter = NULL;
00058 
00059   // Create anyway a generation-counter
00060   // Recent change (03/2002): it is now an eoIncrementorParam, both
00061   // a parameter AND updater so you can store it into the eoState
00062   eoIncrementorParam<unsigned> *generationCounter = new eoIncrementorParam<unsigned>("Gen.");
00063   // store it in the state
00064   _state.storeFunctor(generationCounter);
00065   // And add it to the checkpoint,
00066   checkpoint->add(*generationCounter);
00067 
00068     // dir for DISK output
00069     eoValueParam<std::string>& dirNameParam =  _parser.createParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output - Disk");
00070     // shoudl we empty it if exists
00071     eoValueParam<bool>& eraseParam = _parser.createParam(true, "eraseDir", "erase files in dirName if any", '\0', "Output - Disk");
00072     bool dirOK = false;            // not tested yet
00073 
00075     // now some statistics on the population:
00077 
00087     // Best fitness in population
00088     //---------------------------
00089     eoValueParam<bool>& printBestParam = _parser.createParam(true, "printBestStat", "Print Best/avg/stdev every gen.", '\0', "Output");
00090     eoValueParam<bool>& plotBestParam = _parser.createParam(false, "plotBestStat", "Plot Best/avg Stat", '\0', "Output - Graphical");
00091     eoValueParam<bool>& fileBestParam = _parser.createParam(false, "fileBestStat", "Output bes/avg/std to file", '\0', "Output - Disk");
00092 
00093     eoBestFitnessStat<EOT> *bestStat = NULL;
00094     if ( printBestParam.value() || plotBestParam.value() || fileBestParam.value() )
00095       // we need the bestStat for at least one of the 3 above
00096       {
00097         bestStat = new eoBestFitnessStat<EOT>;
00098         // store it
00099         _state.storeFunctor(bestStat);
00100         // add it to the checkpoint
00101         checkpoint->add(*bestStat);
00102       }
00103 
00104     // Average fitness alone
00105     //----------------------
00106     eoAverageStat<EOT> *averageStat = NULL; // do we need averageStat?
00107     if ( plotBestParam.value() ) // we need it for gnuplot output
00108       {
00109         averageStat = new eoAverageStat<EOT>;
00110         // store it
00111         _state.storeFunctor(averageStat);
00112         // add it to the checkpoint
00113         checkpoint->add(*averageStat);
00114       }
00115 
00116     // Second moment stats: average and stdev
00117     //---------------------------------------
00118     eoSecondMomentStats<EOT> *secondStat = NULL;
00119     if ( printBestParam.value() ) // we need it for sreen output
00120       {
00121         secondStat = new eoSecondMomentStats<EOT>;
00122         // store it
00123         _state.storeFunctor(secondStat);
00124         // add it to the checkpoint
00125         checkpoint->add(*secondStat);
00126       }
00127 
00128 
00129     // Dump of the whole population
00130     //-----------------------------
00131     eoSortedPopStat<EOT> *popStat = NULL;
00132     eoValueParam<bool>& printPopParam = _parser.createParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output");
00133     if ( printPopParam.value() ) // we do want pop dump
00134       {
00135           popStat = new eoSortedPopStat<EOT>;
00136         // store it
00137         _state.storeFunctor(popStat);
00138         // add it to the checkpoint
00139         checkpoint->add(*popStat);
00140       }
00141 
00142 
00143     // do we wnat some histogram of fitnesses snpashots?
00144     eoValueParam<bool> plotHistogramParam = _parser.createParam(false, "plotHisto", "Plot histogram of fitnesses", '\0', "Output - Graphical");
00145 
00147     // The monitors
00149     // do we want an eoStdoutMonitor?
00150     bool needStdoutMonitor = printBestParam.value()
00151         || printPopParam.value() ;
00152 
00153     // The Stdout monitor will print parameters to the screen ...
00154     if ( needStdoutMonitor )
00155       {
00156         eoStdoutMonitor *monitor = new eoStdoutMonitor(false);
00157         _state.storeFunctor(monitor);
00158 
00159         // when called by the checkpoint (i.e. at every generation)
00160         checkpoint->add(*monitor);
00161 
00162         // the monitor will output a series of parameters: add them
00163         monitor->add(*generationCounter);
00164         if (useEvalParam.value()) // we want nb of evaluations
00165               monitor->add(_eval);
00166         if (useTimeParam.value()) // we want time
00167           {
00168             tCounter = new eoTimeCounter;
00169             _state.storeFunctor(tCounter);
00170             checkpoint->add(*tCounter);
00171             monitor->add(*tCounter);
00172           }
00173         if (printBestParam.value())
00174             {
00175                 monitor->add(*bestStat);
00176                 monitor->add(*secondStat);
00177             }
00178         if ( printPopParam.value())
00179           monitor->add(*popStat);
00180       }
00181 
00182     // first handle the dir test - if we need at least one file
00183     if ( ( fileBestParam.value() || plotBestParam.value() ||
00184            plotHistogramParam.value() )
00185          && !dirOK )               // just in case we add something before
00186       dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
00187 
00188     if (fileBestParam.value())    // A file monitor for best & secondMoment
00189       {
00190 #ifdef _MSVC
00191         std::string stmp = dirNameParam.value() + "\best.xg";
00192 #else
00193         std::string stmp = dirNameParam.value() + "/best.xg";
00194 #endif
00195         eoFileMonitor *fileMonitor = new eoFileMonitor(stmp);
00196         // save and give to checkpoint
00197         _state.storeFunctor(fileMonitor);
00198         checkpoint->add(*fileMonitor);
00199         // and feed with some statistics
00200         fileMonitor->add(*generationCounter);
00201         fileMonitor->add(_eval);
00202         if (tCounter)              // we want the time as well
00203           {
00204             //      std::cout << "On met timecounter\n";
00205             fileMonitor->add(*tCounter);
00206           }
00207         fileMonitor->add(*bestStat);
00208         fileMonitor->add(*secondStat);
00209       }
00210 
00211     if (plotBestParam.value())    // an eoGnuplot1DMonitor for best & average
00212       {
00213         std::string stmp = dirNameParam.value() + "/gnu_best.xg";
00214         eoGnuplot1DMonitor *gnuMonitor = new eoGnuplot1DMonitor(stmp,minimizing_fitness<EOT>());
00215         // save and give to checkpoint
00216         _state.storeFunctor(gnuMonitor);
00217         checkpoint->add(*gnuMonitor);
00218         // and feed with some statistics
00219         if (useEvalParam.value())  // do we want eval as X coordinate
00220           gnuMonitor->add(_eval);
00221         else if (tCounter)         // or time?
00222           gnuMonitor->add(*tCounter);
00223         else                       // default: generation
00224           gnuMonitor->add(*generationCounter);
00225         gnuMonitor->add(*bestStat);
00226         gnuMonitor->add(*averageStat);
00227       }
00228 
00229     // historgram?
00230     if (plotHistogramParam.value()) // want to see how the fitness is spread?
00231       {
00232         eoScalarFitnessStat<EOT> *fitStat = new eoScalarFitnessStat<EOT>;
00233         _state.storeFunctor(fitStat);
00234         checkpoint->add(*fitStat);
00235         // a gnuplot-based monitor for snapshots: needs a dir name
00236         eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirNameParam.value());
00237         _state.storeFunctor(fitSnapshot);
00238         // add any stat that is a std::vector<double> to it
00239         fitSnapshot->add(*fitStat);
00240         // and of course add it to the checkpoint
00241         checkpoint->add(*fitSnapshot);
00242       }
00243 
00245     // State savers
00247 
00248     // feed the state to state savers
00249     // save state every N  generation
00250     eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
00251 
00252     if (_parser.isItThere(saveFrequencyParam))
00253     {
00254       // first make sure dirName is OK
00255       if (! dirOK )
00256         dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
00257 
00258       unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
00259 #ifdef _MSVC
00260       std::string stmp = dirNameParam.value() + "\generations";
00261 #else
00262       std::string stmp = dirNameParam.value() + "/generations";
00263 #endif
00264       eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00265       _state.storeFunctor(stateSaver1);
00266     checkpoint->add(*stateSaver1);
00267     }
00268 
00269     // save state every T seconds
00270     eoValueParam<unsigned>& saveTimeIntervalParam = _parser.createParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
00271     if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
00272     {
00273       // first make sure dirName is OK
00274       if (! dirOK )
00275         dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
00276 
00277 #ifdef _MSVC
00278       std::string stmp = dirNameParam.value() + "\time";
00279 #else
00280       std::string stmp = dirNameParam.value() + "/time";
00281 #endif
00282       eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
00283       _state.storeFunctor(stateSaver2);
00284       checkpoint->add(*stateSaver2);
00285     }
00286 
00287     // and that's it for the (control and) output
00288     return *checkpoint;
00289 }
00290 
00291 #endif

Generated on Thu Oct 19 05:06:40 2006 for EO by  doxygen 1.3.9.1