Added HAVE_GNUPLOT checking

This commit is contained in:
tlegrand 2007-09-11 13:59:49 +00:00
commit e5cd9cb379

View file

@ -1,292 +1,587 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// make_checkpoint.h
// (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2000 //-----------------------------------------------------------------------------
/*
This library is free software; you can redistribute it and/or // make_checkpoint.h
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either // (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2000
version 2 of the License, or (at your option) any later version.
/*
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of This library is free software; you can redistribute it and/or
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. modify it under the terms of the GNU Lesser General Public
You should have received a copy of the GNU Lesser General Public License as published by the Free Software Foundation; either
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA version 2 of the License, or (at your option) any later version.
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr
mkeijzer@dhi.dk This library is distributed in the hope that it will be useful,
*/
//----------------------------------------------------------------------------- but WITHOUT ANY WARRANTY; without even the implied warranty of
#ifndef _make_checkpoint_h MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#define _make_checkpoint_h
Lesser General Public License for more details.
#include <eoScalarFitness.h>
#include <utils/selectors.h> // for minimizing_fitness()
#include <EO.h>
#include <eoEvalFuncCounter.h> You should have received a copy of the GNU Lesser General Public
#include <utils/checkpointing>
License along with this library; if not, write to the Free Software
// at the moment, in utils/make_help.cpp
// this should become some eoUtils.cpp with corresponding eoUtils.h Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
bool testDirRes(std::string _dirName, bool _erase);
/////////////////// The checkpoint and other I/O //////////////
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
template <class EOT>
eoCheckPoint<EOT>& do_make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue) Marc.Schoenauer@polytechnique.fr
{
// first, create a checkpoint from the eoContinue mkeijzer@dhi.dk
eoCheckPoint<EOT> *checkpoint = new eoCheckPoint<EOT>(_continue);
_state.storeFunctor(checkpoint); */
/////////////////// //-----------------------------------------------------------------------------
// Counters
//////////////////
// is nb Eval to be used as counter?
eoValueParam<bool>& useEvalParam = _parser.createParam(true, "useEval", "Use nb of eval. as counter (vs nb of gen.)", '\0', "Output"); #ifndef _make_checkpoint_h
eoValueParam<bool>& useTimeParam = _parser.createParam(true, "useTime", "Display time (s) every generation", '\0', "Output");
#define _make_checkpoint_h
// if we want the time, we need an eoTimeCounter
eoTimeCounter * tCounter = NULL;
#ifdef HAVE_CONFIG_H
// Create anyway a generation-counter #include <config.h>
// Recent change (03/2002): it is now an eoIncrementorParam, both #endif
// a parameter AND updater so you can store it into the eoState
eoIncrementorParam<unsigned> *generationCounter = new eoIncrementorParam<unsigned>("Gen."); #include <eoScalarFitness.h>
// store it in the state
_state.storeFunctor(generationCounter); #include <utils/selectors.h> // for minimizing_fitness()
// And add it to the checkpoint,
checkpoint->add(*generationCounter); #include <EO.h>
// dir for DISK output #include <eoEvalFuncCounter.h>
eoValueParam<std::string>& dirNameParam = _parser.createParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output - Disk");
// shoudl we empty it if exists #include <utils/checkpointing>
eoValueParam<bool>& eraseParam = _parser.createParam(true, "eraseDir", "erase files in dirName if any", '\0', "Output - Disk");
bool dirOK = false; // not tested yet
///////////////////////////////////////// // at the moment, in utils/make_help.cpp
// now some statistics on the population:
///////////////////////////////////////// // this should become some eoUtils.cpp with corresponding eoUtils.h
/**
* existing stats as of today, April 10. 2001 bool testDirRes(std::string _dirName, bool _erase);
*
* eoBestFitnessStat : best value in pop - type EOT::Fitness /////////////////// The checkpoint and other I/O //////////////
* eoAverageStat : average value in pop - type EOT::Fitness
* eoSecondMomentStat: average + stdev - type std::pair<double, double>
* eoSortedPopStat : whole population - type std::string (!!)
* eoScalarFitnessStat: the fitnesses - type std::vector<double>
*/
template <class EOT>
// Best fitness in population
//--------------------------- eoCheckPoint<EOT>& do_make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue)
eoValueParam<bool>& printBestParam = _parser.createParam(true, "printBestStat", "Print Best/avg/stdev every gen.", '\0', "Output");
eoValueParam<bool>& plotBestParam = _parser.createParam(false, "plotBestStat", "Plot Best/avg Stat", '\0', "Output - Graphical"); {
eoValueParam<bool>& fileBestParam = _parser.createParam(false, "fileBestStat", "Output bes/avg/std to file", '\0', "Output - Disk");
// first, create a checkpoint from the eoContinue
eoBestFitnessStat<EOT> *bestStat = NULL;
if ( printBestParam.value() || plotBestParam.value() || fileBestParam.value() ) eoCheckPoint<EOT> *checkpoint = new eoCheckPoint<EOT>(_continue);
// we need the bestStat for at least one of the 3 above
{ _state.storeFunctor(checkpoint);
bestStat = new eoBestFitnessStat<EOT>;
// store it
_state.storeFunctor(bestStat);
// add it to the checkpoint ///////////////////
checkpoint->add(*bestStat);
} // Counters
// Average fitness alone //////////////////
//----------------------
eoAverageStat<EOT> *averageStat = NULL; // do we need averageStat? // is nb Eval to be used as counter?
if ( plotBestParam.value() ) // we need it for gnuplot output
{ eoValueParam<bool>& useEvalParam = _parser.createParam(true, "useEval", "Use nb of eval. as counter (vs nb of gen.)", '\0', "Output");
averageStat = new eoAverageStat<EOT>;
// store it eoValueParam<bool>& useTimeParam = _parser.createParam(true, "useTime", "Display time (s) every generation", '\0', "Output");
_state.storeFunctor(averageStat);
// add it to the checkpoint
checkpoint->add(*averageStat);
} // if we want the time, we need an eoTimeCounter
// Second moment stats: average and stdev eoTimeCounter * tCounter = NULL;
//---------------------------------------
eoSecondMomentStats<EOT> *secondStat = NULL;
if ( printBestParam.value() ) // we need it for sreen output
{ // Create anyway a generation-counter
secondStat = new eoSecondMomentStats<EOT>;
// store it // Recent change (03/2002): it is now an eoIncrementorParam, both
_state.storeFunctor(secondStat);
// add it to the checkpoint // a parameter AND updater so you can store it into the eoState
checkpoint->add(*secondStat);
} eoIncrementorParam<unsigned> *generationCounter = new eoIncrementorParam<unsigned>("Gen.");
// store it in the state
// Dump of the whole population
//----------------------------- _state.storeFunctor(generationCounter);
eoSortedPopStat<EOT> *popStat = NULL;
eoValueParam<bool>& printPopParam = _parser.createParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output"); // And add it to the checkpoint,
if ( printPopParam.value() ) // we do want pop dump
{ checkpoint->add(*generationCounter);
popStat = new eoSortedPopStat<EOT>;
// store it
_state.storeFunctor(popStat);
// add it to the checkpoint // dir for DISK output
checkpoint->add(*popStat);
} eoValueParam<std::string>& dirNameParam = _parser.createParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output - Disk");
// shoudl we empty it if exists
// do we wnat some histogram of fitnesses snpashots?
eoValueParam<bool> plotHistogramParam = _parser.createParam(false, "plotHisto", "Plot histogram of fitnesses", '\0', "Output - Graphical"); eoValueParam<bool>& eraseParam = _parser.createParam(true, "eraseDir", "erase files in dirName if any", '\0', "Output - Disk");
/////////////// bool dirOK = false; // not tested yet
// The monitors
///////////////
// do we want an eoStdoutMonitor?
bool needStdoutMonitor = printBestParam.value() /////////////////////////////////////////
|| printPopParam.value() ;
// now some statistics on the population:
// The Stdout monitor will print parameters to the screen ...
if ( needStdoutMonitor ) /////////////////////////////////////////
{
eoStdoutMonitor *monitor = new eoStdoutMonitor(false); /**
_state.storeFunctor(monitor);
* existing stats as of today, April 10. 2001
// when called by the checkpoint (i.e. at every generation)
checkpoint->add(*monitor); *
// the monitor will output a series of parameters: add them * eoBestFitnessStat : best value in pop - type EOT::Fitness
monitor->add(*generationCounter);
if (useEvalParam.value()) // we want nb of evaluations * eoAverageStat : average value in pop - type EOT::Fitness
monitor->add(_eval);
if (useTimeParam.value()) // we want time * eoSecondMomentStat: average + stdev - type std::pair<double, double>
{
tCounter = new eoTimeCounter; * eoSortedPopStat : whole population - type std::string (!!)
_state.storeFunctor(tCounter);
checkpoint->add(*tCounter); * eoScalarFitnessStat: the fitnesses - type std::vector<double>
monitor->add(*tCounter);
} */
if (printBestParam.value())
{
monitor->add(*bestStat);
monitor->add(*secondStat); // Best fitness in population
}
if ( printPopParam.value()) //---------------------------
monitor->add(*popStat);
} eoValueParam<bool>& printBestParam = _parser.createParam(true, "printBestStat", "Print Best/avg/stdev every gen.", '\0', "Output");
// first handle the dir test - if we need at least one file eoValueParam<bool>& plotBestParam = _parser.createParam(false, "plotBestStat", "Plot Best/avg Stat", '\0', "Output - Graphical");
if ( ( fileBestParam.value() || plotBestParam.value() ||
plotHistogramParam.value() ) eoValueParam<bool>& fileBestParam = _parser.createParam(false, "fileBestStat", "Output bes/avg/std to file", '\0', "Output - Disk");
&& !dirOK ) // just in case we add something before
dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
if (fileBestParam.value()) // A file monitor for best & secondMoment eoBestFitnessStat<EOT> *bestStat = NULL;
{
#ifdef _MSVC if ( printBestParam.value() || plotBestParam.value() || fileBestParam.value() )
std::string stmp = dirNameParam.value() + "\best.xg";
#else // we need the bestStat for at least one of the 3 above
std::string stmp = dirNameParam.value() + "/best.xg";
#endif {
eoFileMonitor *fileMonitor = new eoFileMonitor(stmp);
// save and give to checkpoint bestStat = new eoBestFitnessStat<EOT>;
_state.storeFunctor(fileMonitor);
checkpoint->add(*fileMonitor); // store it
// and feed with some statistics
fileMonitor->add(*generationCounter); _state.storeFunctor(bestStat);
fileMonitor->add(_eval);
if (tCounter) // we want the time as well // add it to the checkpoint
{
// std::cout << "On met timecounter\n"; checkpoint->add(*bestStat);
fileMonitor->add(*tCounter);
} }
fileMonitor->add(*bestStat);
fileMonitor->add(*secondStat);
}
// Average fitness alone
#if !defined(NO_GNUPLOT)
if (plotBestParam.value()) // an eoGnuplot1DMonitor for best & average //----------------------
{
std::string stmp = dirNameParam.value() + "/gnu_best.xg"; eoAverageStat<EOT> *averageStat = NULL; // do we need averageStat?
eoGnuplot1DMonitor *gnuMonitor = new eoGnuplot1DMonitor(stmp,minimizing_fitness<EOT>());
// save and give to checkpoint if ( plotBestParam.value() ) // we need it for gnuplot output
_state.storeFunctor(gnuMonitor);
checkpoint->add(*gnuMonitor); {
// and feed with some statistics
if (useEvalParam.value()) // do we want eval as X coordinate averageStat = new eoAverageStat<EOT>;
gnuMonitor->add(_eval);
else if (tCounter) // or time? // store it
gnuMonitor->add(*tCounter);
else // default: generation _state.storeFunctor(averageStat);
gnuMonitor->add(*generationCounter);
gnuMonitor->add(*bestStat); // add it to the checkpoint
gnuMonitor->add(*averageStat);
} checkpoint->add(*averageStat);
// historgram? }
if (plotHistogramParam.value()) // want to see how the fitness is spread?
{
eoScalarFitnessStat<EOT> *fitStat = new eoScalarFitnessStat<EOT>;
_state.storeFunctor(fitStat); // Second moment stats: average and stdev
checkpoint->add(*fitStat);
// a gnuplot-based monitor for snapshots: needs a dir name //---------------------------------------
eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirNameParam.value());
_state.storeFunctor(fitSnapshot); eoSecondMomentStats<EOT> *secondStat = NULL;
// add any stat that is a std::vector<double> to it
fitSnapshot->add(*fitStat); if ( printBestParam.value() ) // we need it for sreen output
// and of course add it to the checkpoint
checkpoint->add(*fitSnapshot); {
}
#endif secondStat = new eoSecondMomentStats<EOT>;
//////////////////////////////////
// State savers // store it
//////////////////////////////
_state.storeFunctor(secondStat);
// feed the state to state savers
// save state every N generation // add it to the checkpoint
eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
checkpoint->add(*secondStat);
if (_parser.isItThere(saveFrequencyParam))
{ }
// first make sure dirName is OK
if (! dirOK )
dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
#ifdef _MSVC // Dump of the whole population
std::string stmp = dirNameParam.value() + "\generations";
#else //-----------------------------
std::string stmp = dirNameParam.value() + "/generations";
#endif eoSortedPopStat<EOT> *popStat = NULL;
eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
_state.storeFunctor(stateSaver1); eoValueParam<bool>& printPopParam = _parser.createParam(false, "printPop", "Print sorted pop. every gen.", '\0', "Output");
checkpoint->add(*stateSaver1);
} if ( printPopParam.value() ) // we do want pop dump
// save state every T seconds {
eoValueParam<unsigned>& saveTimeIntervalParam = _parser.createParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0) popStat = new eoSortedPopStat<EOT>;
{
// first make sure dirName is OK // store it
if (! dirOK )
dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE _state.storeFunctor(popStat);
#ifdef _MSVC // add it to the checkpoint
std::string stmp = dirNameParam.value() + "\time";
#else checkpoint->add(*popStat);
std::string stmp = dirNameParam.value() + "/time";
#endif }
eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
_state.storeFunctor(stateSaver2);
checkpoint->add(*stateSaver2);
}
// and that's it for the (control and) output // do we wnat some histogram of fitnesses snpashots?
return *checkpoint;
} eoValueParam<bool> plotHistogramParam = _parser.createParam(false, "plotHisto", "Plot histogram of fitnesses", '\0', "Output - Graphical");
#endif
///////////////
// The monitors
///////////////
// do we want an eoStdoutMonitor?
bool needStdoutMonitor = printBestParam.value()
|| printPopParam.value() ;
// The Stdout monitor will print parameters to the screen ...
if ( needStdoutMonitor )
{
eoStdoutMonitor *monitor = new eoStdoutMonitor(false);
_state.storeFunctor(monitor);
// when called by the checkpoint (i.e. at every generation)
checkpoint->add(*monitor);
// the monitor will output a series of parameters: add them
monitor->add(*generationCounter);
if (useEvalParam.value()) // we want nb of evaluations
monitor->add(_eval);
if (useTimeParam.value()) // we want time
{
tCounter = new eoTimeCounter;
_state.storeFunctor(tCounter);
checkpoint->add(*tCounter);
monitor->add(*tCounter);
}
if (printBestParam.value())
{
monitor->add(*bestStat);
monitor->add(*secondStat);
}
if ( printPopParam.value())
monitor->add(*popStat);
}
// first handle the dir test - if we need at least one file
if ( ( fileBestParam.value() || plotBestParam.value() ||
plotHistogramParam.value() )
&& !dirOK ) // just in case we add something before
dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
if (fileBestParam.value()) // A file monitor for best & secondMoment
{
#ifdef _MSVC
std::string stmp = dirNameParam.value() + "\best.xg";
#else
std::string stmp = dirNameParam.value() + "/best.xg";
#endif
eoFileMonitor *fileMonitor = new eoFileMonitor(stmp);
// save and give to checkpoint
_state.storeFunctor(fileMonitor);
checkpoint->add(*fileMonitor);
// and feed with some statistics
fileMonitor->add(*generationCounter);
fileMonitor->add(_eval);
if (tCounter) // we want the time as well
{
// std::cout << "On met timecounter\n";
fileMonitor->add(*tCounter);
}
fileMonitor->add(*bestStat);
fileMonitor->add(*secondStat);
}
#if defined(HAVE_GNUPLOT)
if (plotBestParam.value()) // an eoGnuplot1DMonitor for best & average
{
std::string stmp = dirNameParam.value() + "/gnu_best.xg";
eoGnuplot1DMonitor *gnuMonitor = new eoGnuplot1DMonitor(stmp,minimizing_fitness<EOT>());
// save and give to checkpoint
_state.storeFunctor(gnuMonitor);
checkpoint->add(*gnuMonitor);
// and feed with some statistics
if (useEvalParam.value()) // do we want eval as X coordinate
gnuMonitor->add(_eval);
else if (tCounter) // or time?
gnuMonitor->add(*tCounter);
else // default: generation
gnuMonitor->add(*generationCounter);
gnuMonitor->add(*bestStat);
gnuMonitor->add(*averageStat);
}
// historgram?
if (plotHistogramParam.value()) // want to see how the fitness is spread?
{
eoScalarFitnessStat<EOT> *fitStat = new eoScalarFitnessStat<EOT>;
_state.storeFunctor(fitStat);
checkpoint->add(*fitStat);
// a gnuplot-based monitor for snapshots: needs a dir name
eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirNameParam.value());
_state.storeFunctor(fitSnapshot);
// add any stat that is a std::vector<double> to it
fitSnapshot->add(*fitStat);
// and of course add it to the checkpoint
checkpoint->add(*fitSnapshot);
}
#endif
//////////////////////////////////
// State savers
//////////////////////////////
// feed the state to state savers
// save state every N generation
eoValueParam<unsigned>& saveFrequencyParam = _parser.createParam(unsigned(0), "saveFrequency", "Save every F generation (0 = only final state, absent = never)", '\0', "Persistence" );
if (_parser.isItThere(saveFrequencyParam))
{
// first make sure dirName is OK
if (! dirOK )
dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX );
#ifdef _MSVC
std::string stmp = dirNameParam.value() + "\generations";
#else
std::string stmp = dirNameParam.value() + "/generations";
#endif
eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
_state.storeFunctor(stateSaver1);
checkpoint->add(*stateSaver1);
}
// save state every T seconds
eoValueParam<unsigned>& saveTimeIntervalParam = _parser.createParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" );
if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0)
{
// first make sure dirName is OK
if (! dirOK )
dirOK = testDirRes(dirNameParam.value(), eraseParam.value()); // TRUE
#ifdef _MSVC
std::string stmp = dirNameParam.value() + "\time";
#else
std::string stmp = dirNameParam.value() + "/time";
#endif
eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp);
_state.storeFunctor(stateSaver2);
checkpoint->add(*stateSaver2);
}
// and that's it for the (control and) output
return *checkpoint;
}
#endif