EvolvingObjects
make_checkpoint_FDC.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 <climits>
00031 
00032 #include <eoScalarFitness.h>
00033 #include <utils/selectors.h> // for minimizing_fitness()
00034 #include <EO.h>
00035 #include <eoEvalFuncCounter.h>
00036 #include <utils/checkpointing>
00037 
00038 // at the moment, in utils/make_help.cpp
00039 // this should become some eoUtils.cpp with corresponding eoUtils.h
00040 bool testDirRes(std::string _dirName, bool _erase);
00042 
00043 
00047 template <class EOT>
00048 eoCheckPoint<EOT>& do_make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue)
00049 {
00050   // first, create a checkpoint from the eoContinue
00051   eoCheckPoint<EOT> *checkpoint = new eoCheckPoint<EOT>(_continue);
00052   _state.storeFunctor(checkpoint);
00053 
00055   // Counters
00057   // is nb Eval to be used as counter?
00058   eoValueParam<bool>& useEvalParam = _parser.createParam(true, "useEval", "Use nb of eval. as counter (vs nb of gen.)", '\0', "Output");
00059 
00060     // Create anyway a generation-counter parameter
00061     eoValueParam<unsigned> *generationCounter = new eoValueParam<unsigned>(0, "Gen.");
00062     // Create an incrementor (sub-class of eoUpdater).
00063     eoIncrementor<unsigned>* increment = new eoIncrementor<unsigned>(generationCounter->value());
00064     // Add it to the checkpoint,
00065     checkpoint->add(*increment);
00066     // and store it in the state
00067     _state.storeFunctor(increment);
00068 
00069     // dir for DISK output
00070     eoValueParam<std::string>& dirNameParam =  _parser.createParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output - Disk");
00071     // shoudl we empty it if exists
00072     eoValueParam<bool>& eraseParam = _parser.createParam(false, "eraseDir", "erase files in dirName if any", '\0', "Output - Disk");
00073     bool dirOK = false;            // not tested yet
00074 
00076     // now some statistics on the population:
00078 
00091     // Best fitness in population
00092     //---------------------------
00093     eoValueParam<bool>& printBestParam = _parser.createParam(true, "printBestStat", "Print Best/avg/stdev every gen.", '\0', "Output");
00094     eoValueParam<bool>& plotBestParam = _parser.createParam(false, "plotBestStat", "Plot Best/avg Stat", '\0', "Output - Graphical");
00095     eoValueParam<bool>& fileBestParam = _parser.createParam(false, "fileBestStat", "Output bes/avg/std to file", '\0', "Output - Disk");
00096 
00097     eoBestFitnessStat<EOT> *bestStat = NULL;
00098     if ( printBestParam.value() || plotBestParam.value() || fileBestParam.value() )
00099       // we need the bestStat for at least one of the 3 above
00100       {
00101         bestStat = new eoBestFitnessStat<EOT>;
00102         // store it
00103         _state.storeFunctor(bestStat);
00104         // add it to the checkpoint
00105         checkpoint->add(*bestStat);
00106       }
00107 
00108     // Average fitness alone
00109     //----------------------
00110     eoAverageStat<EOT> *averageStat = NULL; // do we need averageStat?
00111     if ( plotBestParam.value() ) // we need it for gnuplot output
00112       {
00113         averageStat = new eoAverageStat<EOT>;
00114         // store it
00115         _state.storeFunctor(averageStat);
00116         // add it to the checkpoint
00117         checkpoint->add(*averageStat);
00118       }
00119 
00120     // Second moment stats: average and stdev
00121     //---------------------------------------
00122     eoSecondMomentStats<EOT> *secondStat = NULL;
00123     if ( printBestParam.value() ) // we need it for sreen output
00124       {
00125         secondStat = new eoSecondMomentStats<EOT>;
00126         // store it
00127         _state.storeFunctor(secondStat);
00128         // add it to the checkpoint
00129         checkpoint->add(*secondStat);
00130       }
00131 
00132 
00133     // Dump of the whole population
00134     //-----------------------------
00135     eoSortedPopStat<EOT> *popStat = NULL;
00136     eoValueParam<bool>& printPopParam = _parser.createParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output");
00137     if ( printPopParam.value() ) // we do want pop dump
00138       {
00139           popStat = new eoSortedPopStat<EOT>("Dump of whole population");
00140         // store it
00141         _state.storeFunctor(popStat);
00142         // add it to the checkpoint
00143         checkpoint->add(*popStat);
00144       }
00145 
00146 
00147     // the Fitness Distance Correlation
00148     //---------------------------------
00149     eoValueParam<bool>& printFDCParam = _parser.createParam(true, "printFDC", "Print FDC coeff. every gen.", '\0', "Output");
00150     eoValueParam<bool>& plotFDCParam = _parser.createParam(false, "plotFDCStat", "Plot FDC scatter plot", '\0', "Output - Graphical");
00151 
00152     eoFDCStat<EOT> *fdcStat = NULL;
00153     if ( printFDCParam.value() || plotFDCParam.value() ) // we need FDCStat
00154       {
00155         // need first an object to compute the distances - here Hamming dist.
00156         eoQuadDistance<EOT> *dist = new eoQuadDistance<EOT>;
00157         _state.storeFunctor(dist);
00158         fdcStat = new eoFDCStat<EOT>(*dist);
00159         // storeFunctor it
00160         _state.storeFunctor(fdcStat);
00161         // add it to the checkpoint
00162         checkpoint->add(*fdcStat);
00163       }
00164 
00165     // do we wnat some histogram of fitnesses snpashots?
00166     eoValueParam<bool> plotHistogramParam = _parser.createParam(false, "plotHisto", "Plot histogram of fitnesses", '\0', "Output - Graphical");
00167 
00169     // The monitors
00171     // do we want an eoStdoutMonitor?
00172     bool needStdoutMonitor = printBestParam.value() || printFDCParam.value()
00173         || printPopParam.value() ;
00174 
00175     // The Stdout monitor will print parameters to the screen ...
00176     if ( needStdoutMonitor )
00177       {
00178         eoStdoutMonitor *monitor = new eoStdoutMonitor(/*false FIXME remove this deprecated prototype*/);
00179         _state.storeFunctor(monitor);
00180 
00181         // when called by the checkpoint (i.e. at every generation)
00182         checkpoint->add(*monitor);
00183 
00184         // the monitor will output a series of parameters: add them
00185         monitor->add(*generationCounter);
00186         if (useEvalParam.value()) // we want nb of evaluations
00187               monitor->add(_eval);
00188         if (printBestParam.value())
00189             {
00190                 monitor->add(*bestStat);
00191                 monitor->add(*secondStat);
00192             }
00193         if (printFDCParam.value())
00194           monitor->add(*fdcStat);
00195         if ( printPopParam.value())
00196           monitor->add(*popStat);
00197       }
00198 
00199     // first handle the dir test - if we need at least one file
00200     if ( ( fileBestParam.value() || plotBestParam.value() ||
00201            plotFDCParam.value() || plotHistogramParam.value() )
00202          && !dirOK )               // just in case we add something before
00203       dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
00204 
00205     if (fileBestParam.value())    // A file monitor for best & secondMoment
00206       {
00207         std::string stmp = dirNameParam.value() + "/best.xg";
00208         eoFileMonitor *fileMonitor = new eoFileMonitor(stmp);
00209         // save and give to checkpoint
00210         _state.storeFunctor(fileMonitor);
00211         checkpoint->add(*fileMonitor);
00212         // and feed with some statistics
00213         fileMonitor->add(*generationCounter);
00214         fileMonitor->add(_eval);
00215         fileMonitor->add(*bestStat);
00216         fileMonitor->add(*secondStat);
00217       }
00218 
00219     if (plotBestParam.value())    // an eoGnuplot1DMonitor for best & average
00220       {
00221         std::string stmp = dirNameParam.value() + "_gnu_best.xg";
00222         eoGnuplot1DMonitor *gnuMonitor = new eoGnuplot1DMonitor(stmp,minimizing_fitness<EOT>());
00223         // save and give to checkpoint
00224         _state.storeFunctor(gnuMonitor);
00225         checkpoint->add(*gnuMonitor);
00226         // and feed with some statistics
00227         if (useEvalParam.value())
00228           gnuMonitor->add(_eval);
00229         else
00230           gnuMonitor->add(*generationCounter);
00231         gnuMonitor->add(*bestStat);
00232         gnuMonitor->add(*averageStat);
00233       }
00234 
00235     if (plotFDCParam.value())    // a specific plot monitor for FDC
00236       {
00237         // first into a file (it adds everything ti itself
00238         eoFDCFileSnapshot<EOT> *fdcFileSnapshot = new eoFDCFileSnapshot<EOT>(*fdcStat, dirNameParam.value());
00239         _state.storeFunctor(fdcFileSnapshot);
00240         // then to a Gnuplot monitor
00241         eoGnuplot1DSnapshot *fdcGnuplot = new eoGnuplot1DSnapshot(*fdcFileSnapshot);
00242         _state.storeFunctor(fdcGnuplot);
00243 
00244         // and of course add them to the checkPoint
00245         checkpoint->add(*fdcFileSnapshot);
00246         checkpoint->add(*fdcGnuplot);
00247       }
00248 
00249     // historgram?
00250     if (plotHistogramParam.value()) // want to see how the fitness is spread?
00251       {
00252         eoScalarFitnessStat<EOT> *fitStat = new eoScalarFitnessStat<EOT>;
00253         _state.storeFunctor(fitStat);
00254         checkpoint->add(*fitStat);
00255         // a gnuplot-based monitor for snapshots: needs a dir name
00256         eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirNameParam.value());
00257         _state.storeFunctor(fitSnapshot);
00258         // add any stat that is a std::vector<double> to it
00259         fitSnapshot->add(*fitStat);
00260         // and of course add it to the checkpoint
00261         checkpoint->add(*fitSnapshot);
00262       }
00263 
00265     // State savers
00267 
00268     // feed the state to state savers
00269     // save state every N  generation
00270     eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
00271 
00272     if (_parser.isItThere(saveFrequencyParam))
00273     {
00274       // first make sure dirName is OK
00275       if (! dirOK )
00276         dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
00277 
00278       unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
00279       std::string stmp = dirNameParam.value() + "/generations";
00280       eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00281       _state.storeFunctor(stateSaver1);
00282     checkpoint->add(*stateSaver1);
00283     }
00284 
00285     // save state every T seconds
00286     eoValueParam<unsigned>& saveTimeIntervalParam = _parser.createParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
00287     if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
00288     {
00289       // first make sure dirName is OK
00290       if (! dirOK )
00291         dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
00292 
00293       std::string stmp = dirNameParam.value() + "/time";
00294       eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
00295       _state.storeFunctor(stateSaver2);
00296       checkpoint->add(*stateSaver2);
00297     }
00298 
00299     // and that's it for the (control and) output
00300     return *checkpoint;
00301 }
00302 
00303 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends