Finalized Checkpointing, see t-eoCheckpointing for a test

This commit is contained in:
mac 2000-03-31 10:02:18 +00:00
commit 9bcf9d95f8
12 changed files with 558 additions and 11 deletions

View file

@ -0,0 +1,6 @@
#include <utils/eoUpdater.h>
#include <utils/eoMonitor.h>
#include <utils/eoFileMonitor.h>
#include <utils/eoCheckPoint.h>
#include <utils/eoStat.h>

View file

@ -0,0 +1,75 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoCheckPoint.h
// (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2000
/*
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@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef _eoCheckPoint_h
#define _eoCheckPoint_h
#include <eoTerm.h>
template <class EOT> class eoStatBase;
class eoMonitor;
class eoUpdater;
template <class EOT>
class eoCheckPoint : public eoTerm<EOT>
{
public :
eoCheckPoint(eoTerm<EOT>& _term) : term(_term) {}
bool operator()(const eoPop<EOT>& _pop);
void add(eoStatBase<EOT>& _stat) { stats.push_back(&_stat); }
void add(eoMonitor& _mon) { monitors.push_back(&_mon); }
void add(eoUpdater& _upd) { updaters.push_back(&_upd); }
std::string className(void) const { return "eoCheckPoint"; }
private :
eoTerm<EOT>& term;
std::vector<eoStatBase<EOT>*> stats;
std::vector<eoMonitor*> monitors;
std::vector<eoUpdater*> updaters;
};
template <class EOT>
bool eoCheckPoint<EOT>::operator()(const eoPop<EOT>& _pop)
{
unsigned i;
for (i = 0; i < stats.size(); ++i)
(*stats[i])(_pop);
for (i = 0; i < updaters.size(); ++i)
(*updaters[i])();
for (i = 0; i < monitors.size(); ++i)
(*monitors[i])();
return term(_pop);
}
#endif

View file

@ -0,0 +1,58 @@
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <utils/eoFileMonitor.h>
#include <utils/compatibility.h>
#include <utils/eoParam.h>
using namespace std;
void eoFileMonitor::operator()(void)
{
if (firsttime)
{
firsttime = false;
// create file
ofstream os(filename.c_str());
if (!os)
{
string str = "eoFileMonitor: Could not open " + filename;
throw runtime_error(str);
}
iterator it = begin();
os << (*it)->longName();
++it;
for (; it != end(); ++it)
{
os << ',' << (*it)->longName();
}
}
// ok, now the real saving. append to file
ofstream os(filename.c_str(), ios_base::app);
if (!os)
{
string str = "eoFileMonitor: Could not append to " + filename;
throw runtime_error(str);
}
iterator it = begin();
os << '\n' << (*it)->getValue();
for(++it; it != end(); ++it)
{
os << ',' << (*it)->getValue();
}
// and we're there
}

View file

@ -0,0 +1,47 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoFileMonitor.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
/*
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@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef _eoFileMonitor_h
#define _eoFileMonitor_h
#include <string>
#include <utils/eoMonitor.h>
#include <eoObject.h>
class eoFileMonitor : public eoMonitor
{
public :
eoFileMonitor(std::string _filename, std::string _delim = ",") : filename(_filename), delim(_delim), firsttime(true) {}
void operator()(void);
private :
std::string filename;
std::string delim;
bool firsttime;
};
#endif

53
eo/src/utils/eoMonitor.h Normal file
View file

@ -0,0 +1,53 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoParam.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
/*
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@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef _eoMonitor_h
#define _eoMonitor_h
#include <vector>
class eoParam;
/**
The abstract monitor class is a vector of parameter pointers. Use
either push_back a pointer or add a reference to a parameter.
Derived classes will then implement the operator()(void) which
will stream or pipe the current values of the parameters to wherever you
want it streamed or piped to.
*/
class eoMonitor : public std::vector<const eoParam*>
{
public :
virtual ~eoMonitor() {}
virtual void operator()(void) = 0;
void add(const eoParam& _param) { push_back(&_param); }
};
#endif

View file

@ -30,6 +30,7 @@
//-----------------------------------------------------------------------------
#include <string>
#include <strstream>
#include <vector>
/**
eoParam: Base class for monitoring and parsing parameters
@ -41,7 +42,7 @@ public:
/** Empty constructor - called from outside any parser
*/
eoParam ()
: repLongName(""), repDescription(""), repDefault(""),
: repLongName(""), repDefault(""), repDescription(""),
repShortHand(0), repRequired(false){}
/**
@ -54,9 +55,9 @@ public:
*/
eoParam (std::string _longName, std::string _default,
std::string _description, char _shortName = 0, bool _required = false)
: repShortHand(_shortName), repLongName(_longName),
repDescription(_description ), repDefault(_default),
repRequired( _required) {}
: repLongName(_longName), repDefault(_default),
repDescription(_description ),
repShortHand(_shortName), repRequired( _required) {}
/**
* Virtual destructor is needed.
@ -64,7 +65,7 @@ public:
virtual ~eoParam () {};
/**
* Pure virtual function to get the value out.
* Pure virtual function to get the value out.
*/
virtual std::string getValue ( void ) const = 0;
@ -118,6 +119,9 @@ private:
eoValueParam<ValueType>: templatized derivation of eoParam. Can be used to contain
any scalar value type. It makes use of std::strstream to get and set values. This
should be changed to std::stringstream when that class is available in g++.
Note also that there is a template specialization for pair<double, double> and
for vector<double>. These stream their contents delimited with whitespace.
*/
template <class ValueType>
@ -136,10 +140,10 @@ public :
*/
eoValueParam (ValueType _defaultValue,
std::string _longName,
std::string _description,
std::string _description = "No description",
char _shortHand = 0,
bool _required = false)
: repValue(_defaultValue), eoParam(_longName, "", _description, _shortHand, _required)
: eoParam(_longName, "", _description, _shortHand, _required), repValue(_defaultValue)
{
eoParam::defValue(getValue());
}
@ -165,6 +169,46 @@ private :
ValueType repValue;
};
/// Because MSVC does not support partial specialization, the pair is a double, not a T
template <>
std::string eoValueParam<std::pair<double, double> >::getValue(void) const
{
std::ostrstream os;
os << repValue.first << ' ' << repValue.second << std::ends;
return os.str();
}
/// Because MSVC does not support partial specialization, the pair is a double, not a T
template <>
void eoValueParam<std::pair<double, double> >::setValue(std::string _value)
{
std::istrstream is(_value.c_str());
is >> repValue.first;
is >> repValue.second;
}
/// Because MSVC does not support partial specialization, the vector is a double, not a T
template <>
std::string eoValueParam<std::vector<double> >::getValue(void) const
{
std::ostrstream os;
os << repValue.size() << ' ';
std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<double>(os, " "));
os << std::ends;
return os.str();
}
/// Because MSVC does not support partial specialization, the vector is a double, not a T
template <>
void eoValueParam<std::vector<double> >::setValue(std::string _value)
{
std::istrstream is(_value.c_str());
unsigned sz;
is >> sz;
repValue.resize(sz);
std::copy(std::istream_iterator<double>(is), std::istream_iterator<double>(), repValue.begin());
}
/*template <class ContainerType>
class eoContainerParam : public eoParam
{

View file

@ -29,7 +29,7 @@ std::ostream& printSectionHeader(std::ostream& os, std::string section)
eoParameterLoader::~eoParameterLoader()
{
for (int i = 0; i < ownedParams.size(); ++i)
for (unsigned i = 0; i < ownedParams.size(); ++i)
{
delete ownedParams[i];
}

122
eo/src/utils/eoStat.h Normal file
View file

@ -0,0 +1,122 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoStat.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
/*
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@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef _eoStat_h
#define _eoStat_h
#include <utils/eoParam.h>
#include <eoPop.h>
template <class EOT>
class eoStatBase
{
public :
virtual ~eoStatBase(){}
virtual void operator()(const eoPop<EOT>& _pop) = 0;
};
template <class EOT, class T>
class eoStat : public eoValueParam<T>, public eoStatBase<EOT>
{
public :
eoStat(T _value, std::string _description) : eoValueParam<T>(_value, _description) {}
virtual void operator()(const eoPop<EOT>& _pop) = 0;
};
#include <numeric>
/**
Average fitness of a population, fitness needs to be scalar.
*/
template <class EOT>
class eoAverageStat : public eoStat<EOT, double>
{
public :
eoAverageStat(std::string _description = "AverageFitness") : eoStat<EOT, double>(0.0, _description) {}
static double sumFitness(double _sum, const EOT& eot)
{
_sum += _eot.fitness();
return _sum;
}
eoAverageStat(double _value, std::string _desc) : eoStat<EOT, double>(_value, _desc) {}
virtual void operator()(const eoPop<EOT>& _pop)
{
double v = std::accumulate(_pop.begin(), _pop.end(), 0.0, eoAverageStat::sumFitness);
value() = v / pop.size();
}
};
template <class EOT>
class eoSecondMomentStats : public eoStat<EOT, std::pair<double, double> >
{
public :
typedef std::pair<double, double> SquarePair;
eoSecondMomentStats(std::string _description = "Average & Stdev") : eoStat<EOT, SquarePair>(std::make_pair(0.0,0.0), _description) {}
static SquarePair sumOfSquares(SquarePair _sq, const EOT& _eo)
{
double fitness = _eo.fitness();
_sq.first += fitness;
_sq.second += fitness * fitness;
return _sq;
}
virtual void operator()(const eoPop<EOT>& _pop)
{
SquarePair result = std::accumulate(_pop.begin(), _pop.end(), std::make_pair(0.0, 0.0), eoSecondMomentStats::sumOfSquares);
double n = _pop.size();
value().first = result.first / n; // average
value().second = sqrt( (result.second - value().first) / (n - 1.0)); // stdev
}
};
/*
template <class EOT>
class eoStdevStat : public eoStat<EOT, double >
{
public :
typedef typename eoSecondMomentStats<EOT>::SquarePair SquarePair;
eoStdevStat(std::string _description = "Stdev") : eoStat<EOT, double>(0.0, _description) {}
virtual void operator()(const eoPop<EOT>& _pop)
{
SquarePair result = std::accumulate(pop.begin(), pop.end(), std::make_pair(0.0, 0.0), eoSecondMomentStats::sumOfSquares);
double n = pop.size();
value() = sqrt( (result.second - (result.first / n)) / (n - 1.0)); // stdev
}
};
*/
#endif

View file

@ -115,7 +115,7 @@ void eoState::load(const string& _filename)
}
void eoState::save(const string& filename)
void eoState::save(const string& filename) const
{ // saves in order of insertion
ofstream os(filename.c_str());
@ -125,7 +125,7 @@ void eoState::save(const string& filename)
throw runtime_error(msg);
}
for (vector<ObjectMap::iterator>::iterator it = creationOrder.begin(); it != creationOrder.end(); ++it)
for (vector<ObjectMap::iterator>::const_iterator it = creationOrder.begin(); it != creationOrder.end(); ++it)
{
os << "\\section{" << (*it)->first << "}\n";
(*it)->second->printOn(os);

View file

@ -71,7 +71,7 @@ public :
*
* @param _filename the name of the file to save into
*/
void save(const std::string& _filename);
void save(const std::string& _filename) const;
private :
std::string createObjectName(eoObject* obj);

View file

@ -0,0 +1,36 @@
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include <strstream>
#include <utils/eoState.h>
#include <utils/eoUpdater.h>
using namespace std;
void eoTimedStateSaver::operator()(void)
{
time_t now = time(0);
if (now >= last_time + interval)
{
last_time = now;
ostrstream os;
os << prefix << (now - first_time) << '.' << extension << ends;
state.save(os.str());
}
}
void eoCountedStateSaver::operator()(void)
{
if (++counter % interval == 0)
{
ostrstream os;
os << prefix << counter << '.' << extension << ends;
state.save(os.str());
}
};

106
eo/src/utils/eoUpdater.h Normal file
View file

@ -0,0 +1,106 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoUpdater.h
// (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2000
/*
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@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef _eoUpdater_h
#define _eoUpdater_h
/**
eoUpdater is a generic procudere for updating whatever you want.
It is about as abstract as you can get.
*/
class eoUpdater
{
public :
/// virtual classes have virtual dtors
virtual ~eoUpdater(void) {}
/**
does the work, what it is is quite undefined
*/
virtual void operator()(void) = 0;
};
/**
*/
template <class T>
class eoIncrementor : public eoUpdater
{public :
eoIncrementor(T& _counter, T _stepsize = 1) : counter(_counter), stepsize(_stepsize) {}
virtual void operator()()
{
counter += stepsize;
}
private:
T& counter;
T stepsize;
};
#include <time.h>
/**
*/
class eoTimedStateSaver : public eoUpdater
{
public :
eoTimedStateSaver(time_t _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav") : state(_state),
interval(_interval), last_time(time(0)), first_time(time(0)),
prefix(_prefix), extension(_extension) {}
void operator()(void);
private :
const eoState& state;
const time_t interval;
time_t last_time;
const time_t first_time;
const std::string prefix;
const std::string extension;
};
/**
*/
class eoCountedStateSaver : public eoUpdater
{
public :
eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav")
: state(_state), interval(_interval), counter(0),
prefix(_prefix), extension(_extension) {}
void operator()(void);
private :
const eoState& state;
const unsigned interval;
unsigned counter;
const std::string prefix;
const std::string extension;
};
#endif