eoParam, added specialization for eoValueParam<string>::getValue

FDCStat and FileSnapshot: better error messageing
Scalar fitness: is now a vector of doubles
exercise3.1 added gnuplot again

Don't know about eoCombinedContinue
This commit is contained in:
maartenkeijzer 2001-03-28 09:00:54 +00:00
commit 195ad72838
6 changed files with 56 additions and 46 deletions

View file

@ -31,8 +31,8 @@
Combined continuators - logical AND: Combined continuators - logical AND:
Continues until one of the embedded continuators says halt! Continues until one of the embedded continuators says halt!
20/11/00 MS: Changed the 2-continuator construct to a vector<eoContinue<EOT> > 20/11/00 MS: Changed the 2-continuator construct to a vector<eoContinue<EOT> >
to be consistent with other Combined constructs to be consistent with other Combined constructs
and allow to easily handle more than 2 continuators and allow to easily handle more than 2 continuators
*/ */
@ -43,29 +43,29 @@ public:
/// Define Fitness /// Define Fitness
typedef typename EOT::Fitness FitnessType; typedef typename EOT::Fitness FitnessType;
/// Ctor /// Ctor, make sure that at least on continuator is present
eoCombinedContinue( eoContinue<EOT>& _cont) eoCombinedContinue( eoContinue<EOT>& _cont)
: eoContinue<EOT> () : eoContinue<EOT> ()
{ {
continuators.push_back(&_cont); continuators.push_back(&_cont);
} }
/// Ctor - for historical reasons ... should disspear some day /// Ctor - for historical reasons ... should disspear some day
eoCombinedContinue( eoContinue<EOT>& _cont1, eoContinue<EOT>& _cont2) eoCombinedContinue( eoContinue<EOT>& _cont1, eoContinue<EOT>& _cont2)
: eoContinue<EOT> () : eoContinue<EOT> ()
{ {
continuators.push_back(&_cont1); continuators.push_back(&_cont1);
continuators.push_back(&_cont2); continuators.push_back(&_cont2);
} }
void add(eoContinue<EOT> & _cont) void add(eoContinue<EOT> & _cont)
{ {
continuators.push_back(&_cont); continuators.push_back(&_cont);
} }
/** Returns false when one of the embedded continuators say so (logical and) /** Returns false when one of the embedded continuators say so (logical and)
*/ */
virtual bool operator() ( const eoPop<EOT>& _pop ) virtual bool operator() ( const eoPop<EOT>& _pop )
{ {
for (unsigned i = 0; i < continuators.size(); ++i) for (unsigned i = 0; i < continuators.size(); ++i)
if ( !(*continuators[i])(_pop) ) return false; if ( !(*continuators[i])(_pop) ) return false;

View file

@ -32,7 +32,7 @@
#include <utils/eoFileSnapshot.h> #include <utils/eoFileSnapshot.h>
/** /**
The FDC computation - stores the values into eoValueParam<EOT,double> The FDC computation - stores the values into eoValueParam<EOT,double>
so they can be snapshot by some eoGnuplotSnapshot ... so they can be snapshot by some eoGnuplotSnapshot ...
*/ */
template <class EOT> template <class EOT>
@ -46,9 +46,9 @@ public :
/** Ctor with the optimum /** Ctor with the optimum
*/ */
eoFDCStat(eoDistance<EOT> & _dist, EOT & _theBest, eoFDCStat(eoDistance<EOT> & _dist, EOT & _theBest,
std::string _description = "FDC") : std::string _description = "FDC") :
eoStat<EOT,double>(0, _description), dist(_dist), eoStat<EOT,double>(0, _description), dist(_dist),
theBest(_theBest), boolOpt(true) {} theBest(_theBest), boolOpt(true) {}
/** Compute the FDC - either from best in pop, or from absolute best /** Compute the FDC - either from best in pop, or from absolute best
@ -56,7 +56,7 @@ public :
*/ */
virtual void operator()(const eoPop<EOT>& _pop) virtual void operator()(const eoPop<EOT>& _pop)
{ {
if (!boolOpt) // take the local best if (!boolOpt) // take the local best
theBest = _pop.best_element(); theBest = _pop.best_element();
unsigned int pSize = _pop.size(); unsigned int pSize = _pop.size();
distToBest.value().resize(pSize); distToBest.value().resize(pSize);
@ -109,24 +109,24 @@ class eoFDCFileSnapshot : public eoFileSnapshot // is an eoMonitor
{ {
public: public:
/** Ctor: in addition to the parameters of the ctor of an eoFileSnapshot /** Ctor: in addition to the parameters of the ctor of an eoFileSnapshot
we need here an eoFDCStat. The 2 vectors (distances to optimum we need here an eoFDCStat. The 2 vectors (distances to optimum
and fitnesses) are added to the monitor so they can be processed and fitnesses) are added to the monitor so they can be processed
later to a file - and eventually by gnuplot later to a file - and eventually by gnuplot
*/ */
eoFDCFileSnapshot(eoFDCStat<EOT> & _FDCstat, eoFDCFileSnapshot(eoFDCStat<EOT> & _FDCstat,
std::string _dirname = "tmpFDC", unsigned _frequency = 1, std::string _dirname = "tmpFDC", unsigned _frequency = 1,
std::string _filename = "FDC", std::string _delim = " "): std::string _filename = "FDC", std::string _delim = " "):
eoFileSnapshot(_dirname, _frequency, _filename, _delim), eoFileSnapshot(_dirname, _frequency, _filename, _delim),
FDCstat(_FDCstat) FDCstat(_FDCstat)
{ {
eoMonitor::add(FDCstat.theDist()); eoFileSnapshot::add(FDCstat.theDist());
eoMonitor::add(FDCstat.theFit()); eoFileSnapshot::add(FDCstat.theFit());
} }
/** just to be sure the add method is not called further /** just to be sure the add method is not called further
*/ */
virtual void add(const eoParam& _param) virtual void add(const eoParam& _param)
{ throw runtime_error("Trying to add stats to an eoFDCFileSnapshot"); } { throw runtime_error("eoFDCFileSnapshot::add(). Trying to add stats to an eoFDCFileSnapshot"); }
private: private:
eoFDCStat<EOT> & FDCstat; eoFDCStat<EOT> & FDCstat;

View file

@ -166,7 +166,7 @@ public :
{ {
if (!dynamic_cast<const eoValueParam<vector<double> >*>(&_param)) if (!dynamic_cast<const eoValueParam<vector<double> >*>(&_param))
{ {
throw logic_error("eoFileSnapshot: I can only monitor vectors of doubles, sorry"); throw logic_error(string("eoFileSnapshot: I can only monitor vectors of doubles, sorry. The offending parameter name = ") + _param.longName());
} }
eoMonitor::add(_param); eoMonitor::add(_param);
} }

View file

@ -138,16 +138,16 @@ public :
* @param _shortName Short name of the argument (Optional) * @param _shortName Short name of the argument (Optional)
* @param _required If it is a necessary parameter or not * @param _required If it is a necessary parameter or not
*/ */
eoValueParam (ValueType _defaultValue, eoValueParam (ValueType _defaultValue,
std::string _longName, std::string _longName,
std::string _description = "No description", std::string _description = "No description",
char _shortHand = 0, char _shortHand = 0,
bool _required = false) bool _required = false)
: eoParam(_longName, "", _description, _shortHand, _required), repValue(_defaultValue) : eoParam(_longName, "", _description, _shortHand, _required), repValue(_defaultValue)
{ {
eoParam::defValue(getValue()); eoParam::defValue(getValue());
} }
ValueType& value() { return repValue; } ValueType& value() { return repValue; }
ValueType value() const { return repValue; } ValueType value() const { return repValue; }
@ -157,9 +157,9 @@ public :
std::ostrstream os(buf, 1023); std::ostrstream os(buf, 1023);
os << repValue; os << repValue;
os << std::ends; os << std::ends;
return os.str(); return os.str();
} }
void setValue(std::string _value) void setValue(std::string _value)
{ {
std::istrstream is(_value.c_str()); std::istrstream is(_value.c_str());
@ -170,6 +170,16 @@ private :
ValueType repValue; ValueType repValue;
}; };
/*
Specialization for string
*/
template <>
std::string eoValueParam<std::string>::getValue(void) const
{
return repValue;
}
template <> template <>
void eoValueParam<bool>::setValue(std::string _value) void eoValueParam<bool>::setValue(std::string _value)
{ {
@ -290,9 +300,9 @@ void eoValueParam<std::vector<eoMinimizingFitness> >::setValue(std::string _valu
class eoContainerParam : public eoParam class eoContainerParam : public eoParam
{ {
public : public :
eoContainerParam (ContainerType& value, string _shortName, string _longName, eoContainerParam (ContainerType& value, string _shortName, string _longName,
string _default, string _default,
string _description, string _description,
bool _required, bool _required,
bool _change ) bool _change )
: value(_value), eoParam(_shortName, _longName, _description, _default, _required, _change) : value(_value), eoParam(_shortName, _longName, _description, _default, _required, _change)

View file

@ -33,11 +33,11 @@
The fitnesses of a whole population, as a vector The fitnesses of a whole population, as a vector
*/ */
template <class EOT, class FitT = typename EOT::Fitness> template <class EOT, class FitT = typename EOT::Fitness>
class eoScalarFitnessStat : public eoSortedStat<EOT, vector<FitT> > class eoScalarFitnessStat : public eoSortedStat<EOT, vector<double> >
{ {
public : public :
eoScalarFitnessStat(std::string _description = "FitnessES") : eoScalarFitnessStat(std::string _description = "FitnessES") :
eoSortedStat<EOT, vector<FitT> >(vector<FitT>(0), _description) {} eoSortedStat<EOT, vector<double> >(vector<double>(0), _description) {}
virtual void operator()(const vector<const EOT*>& _popPters) virtual void operator()(const vector<const EOT*>& _popPters)
{ {

View file

@ -64,7 +64,7 @@ void main_function(int argc, char **argv)
eoValueParam<string> loadNameParam("", "Load","A save file to restart from",'L'); eoValueParam<string> loadNameParam("", "Load","A save file to restart from",'L');
parser.processParam( loadNameParam, "Persistence" ); parser.processParam( loadNameParam, "Persistence" );
string loadName = loadNameParam.value(); string loadName = loadNameParam.value();
eoValueParam<unsigned int> maxGenParam(500, "maxGen", "Maximum number of generations",'G'); eoValueParam<unsigned int> maxGenParam(500, "maxGen", "Maximum number of generations",'G');
parser.processParam( maxGenParam, "Stopping criterion" ); parser.processParam( maxGenParam, "Stopping criterion" );
unsigned maxGen = maxGenParam.value(); unsigned maxGen = maxGenParam.value();
@ -196,7 +196,7 @@ void main_function(int argc, char **argv)
// generational replacement at the moment :-) // generational replacement at the moment :-)
eoGenerationalReplacement<Indi> replace; eoGenerationalReplacement<Indi> replace;
// want to add (weak) elitism? easy! // want to add (weak) elitism? easy!
// rename the eoGenerationalReplacement replace_main, // rename the eoGenerationalReplacement replace_main,
// then encapsulate it in the elitist replacement // then encapsulate it in the elitist replacement
// eoWeakElitistReplacement<Indi> replace(replace_main); // eoWeakElitistReplacement<Indi> replace(replace_main);
@ -285,7 +285,7 @@ void main_function(int argc, char **argv)
// when called by the checkpoint (i.e. at every generation) // when called by the checkpoint (i.e. at every generation)
checkpoint.add(monitor); checkpoint.add(monitor);
// the monitor will output a series of parameters: add them // the monitor will output a series of parameters: add them
monitor.add(generationCounter); monitor.add(generationCounter);
monitor.add(eval); // because now eval is an eoEvalFuncCounter! monitor.add(eval); // because now eval is an eoEvalFuncCounter!
monitor.add(bestStat); monitor.add(bestStat);
@ -302,7 +302,7 @@ void main_function(int argc, char **argv)
eoFileMonitor fileMonitor("stats.xg", " "); eoFileMonitor fileMonitor("stats.xg", " ");
// and an eoGnuplot1DMonitor will 1-print to a file, and 2- plot on screen // and an eoGnuplot1DMonitor will 1-print to a file, and 2- plot on screen
eoGnuplot1DMonitor gnuMonitor("best_average.xg",minimizing_fitness<Indi>()); eoGnuplot1DMonitor gnuMonitor("best_average.xg",minimizing_fitness<Indi>());
// the checkpoint mechanism can handle multiple monitors // the checkpoint mechanism can handle multiple monitors
checkpoint.add(fileMonitor); checkpoint.add(fileMonitor);
checkpoint.add(gnuMonitor); checkpoint.add(gnuMonitor);
@ -318,10 +318,10 @@ void main_function(int argc, char **argv)
// send a scaling command to gnuplot // send a scaling command to gnuplot
gnuMonitor.gnuplotCommand("set yrange [0:500]"); gnuMonitor.gnuplotCommand("set yrange [0:500]");
/*
// a specific plot monitor for FDC // a specific plot monitor for FDC
// first into a file (it adds everything ti itself // first into a file (it adds everything ti itself
eoFDCFileSnapshot<Indi> fdcFileSnapshot(fdcStat); eoFDCFileSnapshot<Indi> fdcFileSnapshot(fdcStat);
// then to a Gnuplot monitor // then to a Gnuplot monitor
eoGnuplot1DSnapshot fdcGnuplot(fdcFileSnapshot); eoGnuplot1DSnapshot fdcGnuplot(fdcFileSnapshot);
// and of coruse add them to the checkPoint // and of coruse add them to the checkPoint
@ -331,14 +331,14 @@ void main_function(int argc, char **argv)
// want to see how the fitness is spread? // want to see how the fitness is spread?
eoScalarFitnessStat<Indi> fitStat; eoScalarFitnessStat<Indi> fitStat;
checkpoint.add(fitStat); checkpoint.add(fitStat);
// a gnuplot-based monitor for snapshots: needs a dir name // a gnuplot-based monitor for snapshots: needs a dir name
// where to store the files // where to store the files
eoGnuplot1DSnapshot fitSnapshot("Fitnesses"); eoGnuplot1DSnapshot fitSnapshot("Fitnesses");
// add any stat that is a vector<double> to it // add any stat that is a vector<double> to it
fitSnapshot.add(fitStat); fitSnapshot.add(fitStat);
// and of course add it to the checkpoint // and of course add it to the checkpoint
checkpoint.add(fitSnapshot); checkpoint.add(fitSnapshot);
*/
// Last type of item the eoCheckpoint can handle: state savers: // Last type of item the eoCheckpoint can handle: state savers:
eoState outState; eoState outState;
// Register the algorithm into the state (so it has something to save!!) // Register the algorithm into the state (so it has something to save!!)