included new fitness class eoScalarFitnessAssembled, that stores different fitness terms in a std::vector, but still acts as a scalar fitness. A new checkpoint uses these values for statistics.
This commit is contained in:
parent
eb279c8f98
commit
b3e57bedad
8 changed files with 837 additions and 23 deletions
196
eo/src/do/make_checkpoint_assembled.h
Normal file
196
eo/src/do/make_checkpoint_assembled.h
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- */
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// make_checkpoint_assembled.h
|
||||
// Marc Wintermantel & Oliver Koenig
|
||||
// IMES-ST@ETHZ.CH
|
||||
// March 2003
|
||||
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
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
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
Marc.Schoenauer@inria.fr
|
||||
mak@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _make_checkpoint_assembled_h
|
||||
#define _make_checkpoint_assembled_h
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <eoScalarFitnessAssembled.h>
|
||||
#include <utils/selectors.h>
|
||||
#include <EO.h>
|
||||
#include <eoEvalFuncCounter.h>
|
||||
#include <utils/checkpointing>
|
||||
|
||||
// at the moment, in utils/make_help.cpp
|
||||
// this should become some eoUtils.cpp with corresponding eoUtils.h
|
||||
bool testDirRes(std::string _dirName, bool _erase);
|
||||
/////////////////// The checkpoint and other I/O //////////////
|
||||
|
||||
/** Of course, Fitness needs to be an eoScalarFitnessAssembled!!! */
|
||||
template <class EOT>
|
||||
eoCheckPoint<EOT>& do_make_checkpoint_assembled(eoParser& _parser, eoState& _state, eoEvalFuncCounter<EOT>& _eval, eoContinue<EOT>& _continue)
|
||||
{
|
||||
|
||||
// SOME PARSER PARAMETERS
|
||||
// ----------------------
|
||||
std::string dirName = _parser.createParam(std::string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output").value();
|
||||
bool erase = _parser.createParam(true, "eraseDir", "Erase files in dirName if any", '\0', "Output").value();
|
||||
#if !defined(NO_GNUPLOT)
|
||||
bool gnuplots = _parser.createParam(true,"plots","Plot stuff using GnuPlot",'\0',"Output").value();
|
||||
#endif
|
||||
bool printFile = _parser.createParam(true,"printFile","Print statistics file",'\0',"Output").value();
|
||||
|
||||
eoValueParam<unsigned>& saveFrequencyParam =
|
||||
_parser.createParam(unsigned(0),"saveFrequency","Save every F generation (0 = only final state, absent = never)",'\0',"Persistence" );
|
||||
|
||||
testDirRes(dirName, erase); // TRUE
|
||||
|
||||
// CREATE CHECKPOINT FROM eoContinue
|
||||
// ---------------------------------
|
||||
eoCheckPoint<EOT> *checkpoint = new eoCheckPoint<EOT>(_continue);
|
||||
_state.storeFunctor(checkpoint);
|
||||
|
||||
// GENERATIONS
|
||||
// -----------
|
||||
eoIncrementorParam<unsigned> *generationCounter = new eoIncrementorParam<unsigned>("Gen.");
|
||||
_state.storeFunctor(generationCounter);
|
||||
checkpoint->add(*generationCounter);
|
||||
|
||||
// TIME
|
||||
// ----
|
||||
eoTimeCounter * tCounter = NULL;
|
||||
tCounter = new eoTimeCounter;
|
||||
_state.storeFunctor(tCounter);
|
||||
checkpoint->add(*tCounter);
|
||||
|
||||
// ACCESS DESCRIPTIONS OF TERMS OF FITNESS CLASS
|
||||
// ---------------------------------------------
|
||||
// define a temporary fitness instance
|
||||
typedef typename EOT::Fitness Fit;
|
||||
Fit fit;
|
||||
std::vector<std::string> fitness_descriptions = fit.getDescriptionVector();
|
||||
unsigned nTerms = fitness_descriptions.size();
|
||||
|
||||
// STAT VALUES OF A POPULATION
|
||||
// ---------------------------
|
||||
|
||||
// average vals
|
||||
std::vector<eoAverageStat<EOT>* > avgvals( nTerms );
|
||||
for (unsigned i=0; i < nTerms; ++i){
|
||||
std::string descr = "Avg. of " + fitness_descriptions[i];
|
||||
avgvals[i] = new eoAverageStat<EOT>(i, descr);
|
||||
_state.storeFunctor( avgvals[i] );
|
||||
checkpoint->add( *avgvals[i] );
|
||||
}
|
||||
|
||||
// best vals
|
||||
std::vector<eoBestFitnessStat<EOT>* > bestvals( nTerms );
|
||||
for (unsigned i=0; i < nTerms; ++i){
|
||||
std::string descr = fitness_descriptions[i] + " of best ind.";
|
||||
bestvals[i] = new eoBestFitnessStat<EOT>(i, descr);
|
||||
_state.storeFunctor( bestvals[i] );
|
||||
checkpoint->add( *bestvals[i] );
|
||||
}
|
||||
|
||||
// STDOUT
|
||||
// ------
|
||||
eoStdoutMonitor *monitor = new eoStdoutMonitor(false);
|
||||
_state.storeFunctor(monitor);
|
||||
checkpoint->add(*monitor);
|
||||
monitor->add(*generationCounter);
|
||||
monitor->add(_eval);
|
||||
monitor->add(*tCounter);
|
||||
|
||||
// Add best fitness
|
||||
monitor->add( *bestvals[0] );
|
||||
|
||||
// Add all average vals
|
||||
for (unsigned i=0; i < nTerms; ++i)
|
||||
monitor->add( *avgvals[i] );
|
||||
|
||||
// GNUPLOT
|
||||
// -------
|
||||
#if !defined(NO_GNUPLOT)
|
||||
if (gnuplots ){
|
||||
std::string stmp;
|
||||
|
||||
// Histogramm of the different fitness vals
|
||||
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(dirName);
|
||||
_state.storeFunctor(fitSnapshot);
|
||||
// add any stat that is a vector<double> to it
|
||||
fitSnapshot->add(*fitStat);
|
||||
// and of course add it to the checkpoint
|
||||
checkpoint->add(*fitSnapshot);
|
||||
|
||||
std::vector<eoGnuplot1DMonitor*> gnumonitors(nTerms, NULL );
|
||||
for (unsigned i=0; i < nTerms; ++i){
|
||||
stmp = dirName + "/gnuplot_" + fitness_descriptions[i] + ".xg";
|
||||
gnumonitors[i] = new eoGnuplot1DMonitor(stmp,true);
|
||||
_state.storeFunctor(gnumonitors[i]);
|
||||
checkpoint->add(*gnumonitors[i]);
|
||||
gnumonitors[i]->add(*generationCounter);
|
||||
gnumonitors[i]->add(*bestvals[i]);
|
||||
gnumonitors[i]->add(*avgvals[i]);
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
// WRITE STUFF TO FILE
|
||||
// -------------------
|
||||
if( printFile ){
|
||||
std::string stmp2 = dirName + "/eoStatistics.sav";
|
||||
eoFileMonitor *fileMonitor = new eoFileMonitor(stmp2);
|
||||
_state.storeFunctor(fileMonitor);
|
||||
checkpoint->add(*fileMonitor);
|
||||
fileMonitor->add(*generationCounter);
|
||||
fileMonitor->add(_eval);
|
||||
fileMonitor->add(*tCounter);
|
||||
|
||||
for (unsigned i=0; i < nTerms; ++i){
|
||||
fileMonitor->add(*bestvals[i]);
|
||||
fileMonitor->add(*avgvals[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// STATE SAVER
|
||||
// -----------
|
||||
// feed the state to state savers
|
||||
|
||||
if (_parser.isItThere(saveFrequencyParam)) {
|
||||
|
||||
unsigned freq = (saveFrequencyParam.value() > 0 ? saveFrequencyParam.value() : UINT_MAX );
|
||||
std::string stmp = dirName + "/generations";
|
||||
eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp);
|
||||
_state.storeFunctor(stateSaver1);
|
||||
checkpoint->add(*stateSaver1);
|
||||
}
|
||||
|
||||
// and that's it for the (control and) output
|
||||
return *checkpoint;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in a new issue