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