# make_checkpoint_assembled.h

    00001  /* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- */
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // make_checkpoint_assembled.h
    00005 // Marc Wintermantel & Oliver Koenig
    00006 // IMES-ST@ETHZ.CH
    00007 // March 2003
    00008
    00009 /*
    00010     This library is free software; you can redistribute it and/or
    00011     modify it under the terms of the GNU Lesser General Public
    00012     License as published by the Free Software Foundation; either
    00013     version 2 of the License, or (at your option) any later version.
    00014
    00015     This library is distributed in the hope that it will be useful,
    00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00018     Lesser General Public License for more details.
    00019
    00020     You should have received a copy of the GNU Lesser General Public
    00021     License along with this library; if not, write to the Free Software
    00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00023
    00024     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
    00025              Marc.Schoenauer@inria.fr
    00026              mak@dhi.dk
    00027 */
    00028 //-----------------------------------------------------------------------------
    00029
    00030 #ifndef _make_checkpoint_assembled_h
    00031 #define _make_checkpoint_assembled_h
    00032
    00033 #include <vector>
    00034 #include <string>
    00035
    00036 #include <eoScalarFitnessAssembled.h>
    00037 #include <utils/selectors.h>
    00038 #include <EO.h>
    00039 #include <eoEvalFuncCounter.h>
    00040 #include <utils/checkpointing>
    00041
    00042 // at the moment, in utils/make_help.cpp
    00043 // this should become some eoUtils.cpp with corresponding eoUtils.h
    00044 bool testDirRes(std::string _dirName, bool _erase);
    00046
    00048 template <class EOT>
    00049 eoCheckPoint<EOT>& do_make_checkpoint_assembled(eoParser& _parser, eoState& _state, eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue)
    00050 {
    00051
    00052     // SOME PARSER PARAMETERS
    00053     // ----------------------
    00054     std::string dirName = _parser.getORcreateParam(std::string("Res"), "resDir",
    00055                                                    "Directory to store DISK outputs",
    00056                                                    '\0', "Output").value();
    00057     bool erase = _parser.getORcreateParam(true, "eraseDir",
    00058                                           "Erase files in dirName if any",
    00059                                           '\0', "Output").value();
    00060     bool gnuplots = _parser.getORcreateParam(true, "plots",
    00061                                              "Plot stuff using GnuPlot",
    00062                                              '\0', "Output").value();
    00063     bool printFile = _parser.getORcreateParam(true, "printFile",
    00064                                               "Print statistics file",
    00065                                               '\0', "Output").value();
    00066
    00067   eoValueParam<unsigned>& saveFrequencyParam
    00068       = _parser.getORcreateParam(unsigned(0), "saveFrequency",
    00069                                  "Save every F generation (0 = only final state, absent = never)",
    00070                                  '\0', "Persistence" );
    00071
    00072   testDirRes(dirName, erase); // TRUE
    00073
    00074   // CREATE CHECKPOINT FROM eoContinue
    00075   // ---------------------------------
    00076   eoCheckPoint<EOT> *checkpoint = new eoCheckPoint<EOT>(_continue);
    00077   _state.storeFunctor(checkpoint);
    00078
    00079   // GENERATIONS
    00080   // -----------
    00081   eoIncrementorParam<unsigned> *generationCounter = new eoIncrementorParam<unsigned>("Gen.");
    00082   _state.storeFunctor(generationCounter);
    00083   checkpoint->add(*generationCounter);
    00084
    00085   // TIME
    00086   // ----
    00087   eoTimeCounter * tCounter = NULL;
    00088   tCounter = new eoTimeCounter;
    00089   _state.storeFunctor(tCounter);
    00090   checkpoint->add(*tCounter);
    00091
    00092   // ACCESS DESCRIPTIONS OF TERMS OF FITNESS CLASS
    00093   // ---------------------------------------------
    00094   // define a temporary fitness instance
    00095   typedef typename EOT::Fitness Fit;
    00096   Fit fit;
    00097   std::vector<std::string> fitness_descriptions = fit.getDescriptionVector();
    00098   unsigned nTerms = fitness_descriptions.size();
    00099
    00100   // STAT VALUES OF A POPULATION
    00101   // ---------------------------
    00102
    00103   // average vals
    00104   std::vector<eoAssembledFitnessAverageStat<EOT>* > avgvals( nTerms );
    00105   for (unsigned i=0; i < nTerms; ++i){
    00106     std::string descr = "Avg. of " + fitness_descriptions[i];
    00107     avgvals[i] = new eoAssembledFitnessAverageStat<EOT>(i, descr);
    00108     _state.storeFunctor( avgvals[i] );
    00109     checkpoint->add( *avgvals[i] );
    00110   }
    00111
    00112   // best vals
    00113   std::vector<eoAssembledFitnessBestStat<EOT>* > bestvals( nTerms );
    00114   for (unsigned j=0; j < nTerms; ++j){
    00115     std::string descr = fitness_descriptions[j] + " of best ind.";
    00116     bestvals[j] = new eoAssembledFitnessBestStat<EOT>(j, descr);
    00117     _state.storeFunctor( bestvals[j] );
    00118     checkpoint->add( *bestvals[j] );
    00119   }
    00120
    00121   // STDOUT
    00122   // ------
    00123   eoStdoutMonitor *monitor = new eoStdoutMonitor(false);
    00124   _state.storeFunctor(monitor);
    00125   checkpoint->add(*monitor);
    00126   monitor->add(*generationCounter);
    00127   monitor->add(_eval);
    00128   monitor->add(*tCounter);
    00129
    00130   // Add best fitness
    00131   monitor->add( *bestvals[0] );
    00132
    00133   // Add all average vals
    00134   for (unsigned l=0; l < nTerms; ++l)
    00135     monitor->add( *avgvals[l] );
    00136
    00137   // GNUPLOT
    00138   // -------
    00139   if (gnuplots ){
    00140     std::string stmp;
    00141
    00142     // Histogramm of the different fitness vals
    00143     eoScalarFitnessStat<EOT> *fitStat = new eoScalarFitnessStat<EOT>;
    00144     _state.storeFunctor(fitStat);
    00145     checkpoint->add(*fitStat);
    00146     // a gnuplot-based monitor for snapshots: needs a dir name
    00147     eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirName);
    00148     _state.storeFunctor(fitSnapshot);
    00149     // add any stat that is a vector<double> to it
    00150     fitSnapshot->add(*fitStat);
    00151     // and of course add it to the checkpoint
    00152     checkpoint->add(*fitSnapshot);
    00153
    00154     std::vector<eoGnuplot1DMonitor*> gnumonitors(nTerms, NULL );
    00155     for (unsigned k=0; k < nTerms; ++k){
    00156       stmp = dirName + "/gnuplot_" + fitness_descriptions[k] + ".xg";
    00157       gnumonitors[k] = new eoGnuplot1DMonitor(stmp,true);
    00158       _state.storeFunctor(gnumonitors[k]);
    00159       checkpoint->add(*gnumonitors[k]);
    00160       gnumonitors[k]->add(*generationCounter);
    00161       gnumonitors[k]->add(*bestvals[k]);
    00162       gnumonitors[k]->add(*avgvals[k]);
    00163     }
    00164
    00165   }
    00166
    00167   // WRITE STUFF TO FILE
    00168   // -------------------
    00169   if( printFile ){
    00170     std::string stmp2 = dirName + "/eoStatistics.sav";
    00171     eoFileMonitor *fileMonitor = new eoFileMonitor(stmp2);
    00172     _state.storeFunctor(fileMonitor);
    00173     checkpoint->add(*fileMonitor);
    00174     fileMonitor->add(*generationCounter);
    00175     fileMonitor->add(_eval);
    00176     fileMonitor->add(*tCounter);
    00177
    00178     for (unsigned i=0; i < nTerms; ++i){
    00179       fileMonitor->add(*bestvals[i]);
    00180       fileMonitor->add(*avgvals[i]);
    00181     }
    00182
    00183   }
    00184
    00185   // STATE SAVER
    00186   // -----------
    00187   // feed the state to state savers
    00188
    00189   if (_parser.isItThere(saveFrequencyParam)) {
    00190
    00191     unsigned freq = (saveFrequencyParam.value() > 0 ? saveFrequencyParam.value() : UINT_MAX );
    00192     std::string stmp = dirName + "/generations";
    00193     eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
    00194     _state.storeFunctor(stateSaver1);
    00195     checkpoint->add(*stateSaver1);
    00196   }
    00197
    00198   // and that's it for the (control and) output
    00199   return *checkpoint;
    00200 }
    00201
    00202 #endif