make_checkpoint_pareto.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // make_checkpoint_pareto.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@inria.fr
00023              mkeijzer@dhi.dk
00024  */
00025 //-----------------------------------------------------------------------------
00026 
00027 #ifndef _make_checkpoint_pareto_h
00028 #define _make_checkpoint_pareto_h
00029 
00030 #include <stdlib.h>
00031 #include <sstream>
00032 
00033 #include "EO.h"
00034 #include "eoParetoFitness.h"
00035 #include "eoEvalFuncCounter.h"
00036 #include "utils/checkpointing"
00037 #include "utils/selectors.h"
00038 
00039 // at the moment, in utils/make_help.cpp
00040 // this should become some eoUtils.cpp with corresponding eoUtils.h
00041 bool testDirRes(std::string _dirName, bool _erase);
00043 
00046 template <class EOT>
00047 eoCheckPoint<EOT>& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state,
00048                                              eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue)
00049 {
00050     // first, create a checkpoint from the eoContinue - and store in _state
00051     eoCheckPoint<EOT> & checkpoint = _state.storeFunctor(new eoCheckPoint<EOT>(_continue));
00052 
00054     typedef typename EOT::Fitness Fit;
00055     Fit fit;
00056     unsigned nObj = fit.size();
00057 
00059     // Counters
00061     // is nb Eval to be used as counter?
00062     bool useEval = _parser.getORcreateParam(true, "useEval",
00063                                             "Use nb of eval. as counter (vs nb of gen.)",
00064                                             '\0', "Output").value();
00065 
00066     // Create anyway a generation-counter parameter WARNING: not stored anywhere!!!
00067     eoValueParam<unsigned> *generationCounter = new eoValueParam<unsigned>(0, "Gen.");
00068     // Create an incrementor (sub-class of eoUpdater).
00069     eoIncrementor<unsigned> & increment =
00070       _state.storeFunctor(new eoIncrementor<unsigned>(generationCounter->value()) );
00071     // Add it to the checkpoint,
00072     checkpoint.add(increment);
00073 
00074     // dir for DISK output
00075     std::string & dirName =  _parser.getORcreateParam(std::string("Res"), "resDir",
00076                                                       "Directory to store DISK outputs",
00077                                                       '\0', "Output - Disk").value();
00078     // shoudl we empty it if exists
00079     eoValueParam<bool>& eraseParam = _parser.getORcreateParam(true, "eraseDir",
00080                                                               "erase files in dirName if any",
00081                                                               '\0', "Output - Disk");
00082     bool dirOK = false;            // not tested yet
00083 
00085     // now some statistics on the population:
00087 
00093     eoValueParam<eoParamParamType>& fPlotParam
00094         = _parser.getORcreateParam(eoParamParamType("1(0,1)"), "frontFileFrequency",
00095       "File save frequency in objective spaces (std::pairs of comma-separated objectives " \
00096       "in 1 single parentheses std::pair)",
00097                                    '\0', "Output - Disk");
00098 
00099     bool boolGnuplot = _parser.getORcreateParam(false, "plotFront",
00100                                                 "Objective plots (requires corresponding files " \
00101                                                 "- see frontFileFrequency",
00102                                                 '\0', "Output - Graphical").value();
00103 
00104     eoParamParamType & fPlot = fPlotParam.value(); // std::pair<std::string,std::vector<std::string> >
00105     unsigned frequency = atoi(fPlot.first.c_str());
00106     if (frequency)                 // something to plot
00107     {
00108       unsigned nbPlot = fPlot.second.size();
00109       if ( nbPlot % 2 )            // odd!
00110         throw std::runtime_error("Odd number of front description in make_checkpoint_pareto");
00111 
00112       // only create the necessary stats
00113       std::vector<bool> bStat(nObj, false); // track of who's already there
00114       std::vector<eoMOFitnessStat<EOT>* > theStats(nObj);
00115 
00116       // first create the stats
00117       for (unsigned i=0; i<nbPlot; i+=2)
00118         {
00119           unsigned obj1 = atoi(fPlot.second[i].c_str());
00120           unsigned obj2 = atoi(fPlot.second[i+1].c_str());
00121           eoMOFitnessStat<EOT>* fStat;
00122           if (!bStat[obj1]) {              // not already there: create it
00123               std::ostringstream os;
00124               os << "Obj. " << obj1 << std::ends;
00125               fStat = new eoMOFitnessStat<EOT>(obj1, os.str().c_str());
00126               _state.storeFunctor(fStat);
00127               bStat[obj1]=true;
00128               theStats[obj1]=fStat;
00129               checkpoint.add(*fStat);
00130           }
00131           if (!bStat[obj2]) {              // not already there: create it
00132               std::ostringstream os;
00133               os << "Obj. " << obj2 << std::ends;
00134               fStat = new eoMOFitnessStat<EOT>(obj2, os.str().c_str());
00135               _state.storeFunctor(fStat);
00136               bStat[obj2]=true;
00137               theStats[obj2]=fStat;
00138               checkpoint.add(*fStat);
00139           }
00140 
00141           // then the fileSnapshots
00142           std::ostringstream os;
00143           os << "Front." << obj1 << "." << obj2 << "." << std::ends;
00144           eoFileSnapshot& snapshot = _state.storeFunctor(
00145               new eoFileSnapshot(dirName, frequency, os.str().c_str()));
00146           checkpoint.add(snapshot);
00147 
00148           snapshot.add(*theStats[obj1]);
00149           snapshot.add(*theStats[obj2]);
00150 
00151           // and create the gnuplotter from the fileSnapshot
00152           if(boolGnuplot)
00153             {
00154               eoGnuplot1DSnapshot & plotSnapshot = _state.storeFunctor(new
00155                     eoGnuplot1DSnapshot(snapshot));
00156               plotSnapshot.setPointSize(3);
00157               checkpoint.add(plotSnapshot);
00158             }
00159         }
00160     }
00161     // Dump of the whole population
00162     //-----------------------------
00163     bool printPop = _parser.getORcreateParam(false, "printPop",
00164                                              "Print sorted pop. every gen.",
00165                                              '\0', "Output").value();
00166     eoSortedPopStat<EOT> * popStat;
00167     if ( printPop ) // we do want pop dump
00168       {
00169         std::cout << "On cree printpop\n";
00170         popStat = & _state.storeFunctor(new eoSortedPopStat<EOT>);
00171         // add it to the checkpoint
00172         checkpoint.add(*popStat);
00173       }
00174 
00176     // The monitors
00178     // do we want an eoStdoutMonitor?
00179     bool needStdoutMonitor = printPop ; // only this one at the moment
00180 
00181     // The Stdout monitor will print parameters to the screen ...
00182     if ( needStdoutMonitor )
00183       {
00184         eoStdoutMonitor & monitor = _state.storeFunctor(new eoStdoutMonitor(false));
00185 
00186         // when called by the checkpoint (i.e. at every generation)
00187         checkpoint.add(monitor);
00188 
00189         // the monitor will output a series of parameters: add them
00190         monitor.add(*generationCounter);
00191         if (useEval) // we want nb of evaluations
00192           monitor.add(_eval);
00193         if ( printPop)
00194           monitor.add(*popStat);
00195       }
00196 
00197 
00199     // State savers
00201 
00202     // feed the state to state savers
00203     // save state every N  generation
00204     eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
00205 
00206     if (_parser.isItThere(saveFrequencyParam))
00207     {
00208       // first make sure dirName is OK
00209       if (! dirOK )
00210         dirOK = testDirRes(dirName, eraseParam.value()); // TRUE
00211 
00212       unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
00213 #ifdef _MSVC
00214       std::string stmp = dirName + "\generations";
00215 #else
00216       std::string stmp = dirName + "/generations";
00217 #endif
00218       eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
00219       _state.storeFunctor(stateSaver1);
00220     checkpoint.add(*stateSaver1);
00221     }
00222 
00223     // save state every T seconds
00224     eoValueParam<unsigned>& saveTimeIntervalParam
00225         = _parser.getORcreateParam(unsigned(0), "saveTimeInterval",
00226                                    "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
00227     if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
00228     {
00229       // first make sure dirName is OK
00230       if (! dirOK )
00231         dirOK = testDirRes(dirName, eraseParam.value()); // TRUE
00232 
00233 #ifdef _MSVC
00234       std::string stmp = dirName + "\time";
00235 #else
00236       std::string stmp = dirName + "/time";
00237 #endif
00238       eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
00239       _state.storeFunctor(stateSaver2);
00240       checkpoint.add(*stateSaver2);
00241     }
00242 
00243     // and that's it for the (control and) output
00244     return checkpoint;
00245 }
00246 
00247 #endif

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