EvolvingObjects
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 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036 
00037 #include <climits>
00038 #include <vector>
00039 #include <string>
00040 
00041 #include <eoScalarFitnessAssembled.h>
00042 #include <utils/selectors.h>
00043 #include <EO.h>
00044 #include <eoEvalFuncCounter.h>
00045 #include <utils/checkpointing>
00046 
00047 // at the moment, in utils/make_help.cpp
00048 // this should become some eoUtils.cpp with corresponding eoUtils.h
00049 bool testDirRes(std::string _dirName, bool _erase);
00051 
00057 template <class EOT>
00058 eoCheckPoint<EOT>& do_make_checkpoint_assembled(eoParser& _parser, eoState& _state, eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue)
00059 {
00060 
00061     // SOME PARSER PARAMETERS
00062     // ----------------------
00063     std::string dirName = _parser.getORcreateParam(std::string("Res"), "resDir",
00064                                                    "Directory to store DISK outputs",
00065                                                    '\0', "Output").value();
00066     bool erase = _parser.getORcreateParam(true, "eraseDir",
00067                                           "Erase files in dirName if any",
00068                                           '\0', "Output").value();
00069     bool gnuplots = _parser.getORcreateParam(true, "plots",
00070                                              "Plot stuff using GnuPlot",
00071                                              '\0', "Output").value();
00072     bool printFile = _parser.getORcreateParam(true, "printFile",
00073                                               "Print statistics file",
00074                                               '\0', "Output").value();
00075 
00076   eoValueParam<unsigned>& saveFrequencyParam
00077       = _parser.getORcreateParam(unsigned(0), "saveFrequency",
00078                                  "Save every F generation (0 = only final state, absent = never)",
00079                                  '\0', "Persistence" );
00080 
00081   testDirRes(dirName, erase); // TRUE
00082 
00083   // CREATE CHECKPOINT FROM eoContinue
00084   // ---------------------------------
00085   eoCheckPoint<EOT> *checkpoint = new eoCheckPoint<EOT>(_continue);
00086   _state.storeFunctor(checkpoint);
00087 
00088   // GENERATIONS
00089   // -----------
00090   eoIncrementorParam<unsigned> *generationCounter = new eoIncrementorParam<unsigned>("Gen.");
00091   _state.storeFunctor(generationCounter);
00092   checkpoint->add(*generationCounter);
00093 
00094   // TIME
00095   // ----
00096   eoTimeCounter * tCounter = NULL;
00097   tCounter = new eoTimeCounter;
00098   _state.storeFunctor(tCounter);
00099   checkpoint->add(*tCounter);
00100 
00101   // ACCESS DESCRIPTIONS OF TERMS OF FITNESS CLASS
00102   // ---------------------------------------------
00103   // define a temporary fitness instance
00104   typedef typename EOT::Fitness Fit;
00105   Fit fit;
00106   std::vector<std::string> fitness_descriptions = fit.getDescriptionVector();
00107   unsigned nTerms = fitness_descriptions.size();
00108 
00109   // STAT VALUES OF A POPULATION
00110   // ---------------------------
00111 
00112   // average vals
00113   std::vector<eoAssembledFitnessAverageStat<EOT>* > avgvals( nTerms );
00114   for (unsigned i=0; i < nTerms; ++i){
00115     std::string descr = "Avg. of " + fitness_descriptions[i];
00116     avgvals[i] = new eoAssembledFitnessAverageStat<EOT>(i, descr);
00117     _state.storeFunctor( avgvals[i] );
00118     checkpoint->add( *avgvals[i] );
00119   }
00120 
00121   // best vals
00122   std::vector<eoAssembledFitnessBestStat<EOT>* > bestvals( nTerms );
00123   for (unsigned j=0; j < nTerms; ++j){
00124     std::string descr = fitness_descriptions[j] + " of best ind.";
00125     bestvals[j] = new eoAssembledFitnessBestStat<EOT>(j, descr);
00126     _state.storeFunctor( bestvals[j] );
00127     checkpoint->add( *bestvals[j] );
00128   }
00129 
00130   // STDOUT
00131   // ------
00132   eoStdoutMonitor *monitor = new eoStdoutMonitor(/*false FIXME remove this deprecated prototype*/);
00133   _state.storeFunctor(monitor);
00134   checkpoint->add(*monitor);
00135   monitor->add(*generationCounter);
00136   monitor->add(_eval);
00137   monitor->add(*tCounter);
00138 
00139   // Add best fitness
00140   monitor->add( *bestvals[0] );
00141 
00142   // Add all average vals
00143   for (unsigned l=0; l < nTerms; ++l)
00144     monitor->add( *avgvals[l] );
00145 
00146   // GNUPLOT
00147   // -------
00148   if (gnuplots ){
00149     std::string stmp;
00150 
00151     // Histogramm of the different fitness vals
00152     eoScalarFitnessStat<EOT> *fitStat = new eoScalarFitnessStat<EOT>;
00153     _state.storeFunctor(fitStat);
00154     checkpoint->add(*fitStat);
00155 #ifdef HAVE_GNUPLOT
00156         // a gnuplot-based monitor for snapshots: needs a dir name
00157      eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirName);
00158      _state.storeFunctor(fitSnapshot);
00159     // add any stat that is a vector<double> to it
00160     fitSnapshot->add(*fitStat);
00161     // and of course add it to the checkpoint
00162     checkpoint->add(*fitSnapshot);
00163 
00164     std::vector<eoGnuplot1DMonitor*> gnumonitors(nTerms, NULL );
00165     for (unsigned k=0; k < nTerms; ++k){
00166       stmp = dirName + "/gnuplot_" + fitness_descriptions[k] + ".xg";
00167       gnumonitors[k] = new eoGnuplot1DMonitor(stmp,true);
00168       _state.storeFunctor(gnumonitors[k]);
00169       checkpoint->add(*gnumonitors[k]);
00170       gnumonitors[k]->add(*generationCounter);
00171       gnumonitors[k]->add(*bestvals[k]);
00172       gnumonitors[k]->add(*avgvals[k]);
00173     }
00174 #endif
00175   }
00176 
00177   // WRITE STUFF TO FILE
00178   // -------------------
00179   if( printFile ){
00180     std::string stmp2 = dirName + "/eoStatistics.sav";
00181     eoFileMonitor *fileMonitor = new eoFileMonitor(stmp2);
00182     _state.storeFunctor(fileMonitor);
00183     checkpoint->add(*fileMonitor);
00184     fileMonitor->add(*generationCounter);
00185     fileMonitor->add(_eval);
00186     fileMonitor->add(*tCounter);
00187 
00188     for (unsigned i=0; i < nTerms; ++i){
00189       fileMonitor->add(*bestvals[i]);
00190       fileMonitor->add(*avgvals[i]);
00191     }
00192 
00193   }
00194 
00195   // STATE SAVER
00196   // -----------
00197   // feed the state to state savers
00198 
00199   if (_parser.isItThere(saveFrequencyParam)) {
00200 
00201     unsigned freq = (saveFrequencyParam.value() > 0 ? saveFrequencyParam.value() : UINT_MAX );
00202     std::string stmp = dirName + "/generations";
00203     eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00204     _state.storeFunctor(stateSaver1);
00205     checkpoint->add(*stateSaver1);
00206   }
00207 
00208   // and that's it for the (control and) output
00209   return *checkpoint;
00210 }
00211 
00212 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends