diff --git a/eo/src/utils/checkpointing b/eo/src/utils/checkpointing new file mode 100644 index 00000000..f13ac85a --- /dev/null +++ b/eo/src/utils/checkpointing @@ -0,0 +1,6 @@ + +#include +#include +#include +#include +#include \ No newline at end of file diff --git a/eo/src/utils/eoCheckPoint.h b/eo/src/utils/eoCheckPoint.h new file mode 100644 index 00000000..537b60cd --- /dev/null +++ b/eo/src/utils/eoCheckPoint.h @@ -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 + +template class eoStatBase; +class eoMonitor; +class eoUpdater; + +template +class eoCheckPoint : public eoTerm +{ +public : + + eoCheckPoint(eoTerm& _term) : term(_term) {} + + bool operator()(const eoPop& _pop); + + void add(eoStatBase& _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& term; + std::vector*> stats; + std::vector monitors; + std::vector updaters; +}; + +template +bool eoCheckPoint::operator()(const eoPop& _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 \ No newline at end of file diff --git a/eo/src/utils/eoFileMonitor.cpp b/eo/src/utils/eoFileMonitor.cpp new file mode 100644 index 00000000..f0c0047c --- /dev/null +++ b/eo/src/utils/eoFileMonitor.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +#include +#include +#include + +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 +} + diff --git a/eo/src/utils/eoFileMonitor.h b/eo/src/utils/eoFileMonitor.h new file mode 100644 index 00000000..6263a8ec --- /dev/null +++ b/eo/src/utils/eoFileMonitor.h @@ -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 + +#include +#include + +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 \ No newline at end of file diff --git a/eo/src/utils/eoMonitor.h b/eo/src/utils/eoMonitor.h new file mode 100644 index 00000000..3348403c --- /dev/null +++ b/eo/src/utils/eoMonitor.h @@ -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 + +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 +{ +public : + + virtual ~eoMonitor() {} + + virtual void operator()(void) = 0; + + void add(const eoParam& _param) { push_back(&_param); } +}; + +#endif \ No newline at end of file diff --git a/eo/src/utils/eoParam.h b/eo/src/utils/eoParam.h index 1fe4dd27..fed8a525 100644 --- a/eo/src/utils/eoParam.h +++ b/eo/src/utils/eoParam.h @@ -30,6 +30,7 @@ //----------------------------------------------------------------------------- #include #include +#include /** 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: 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 and + for vector. These stream their contents delimited with whitespace. */ template @@ -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 >::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 >::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 >::getValue(void) const +{ + std::ostrstream os; + os << repValue.size() << ' '; + std::copy(repValue.begin(), repValue.end(), std::ostream_iterator(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 >::setValue(std::string _value) +{ + std::istrstream is(_value.c_str()); + unsigned sz; + is >> sz; + repValue.resize(sz); + std::copy(std::istream_iterator(is), std::istream_iterator(), repValue.begin()); +} + /*template class eoContainerParam : public eoParam { diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index 3058bfba..6fd16b2e 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -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]; } diff --git a/eo/src/utils/eoStat.h b/eo/src/utils/eoStat.h new file mode 100644 index 00000000..0ae64113 --- /dev/null +++ b/eo/src/utils/eoStat.h @@ -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 +#include + +template +class eoStatBase +{ +public : + virtual ~eoStatBase(){} + + virtual void operator()(const eoPop& _pop) = 0; +}; + +template +class eoStat : public eoValueParam, public eoStatBase +{ +public : + eoStat(T _value, std::string _description) : eoValueParam(_value, _description) {} + + virtual void operator()(const eoPop& _pop) = 0; +}; + +#include + +/** + Average fitness of a population, fitness needs to be scalar. +*/ +template +class eoAverageStat : public eoStat +{ +public : + eoAverageStat(std::string _description = "AverageFitness") : eoStat(0.0, _description) {} + + static double sumFitness(double _sum, const EOT& eot) + { + _sum += _eot.fitness(); + return _sum; + } + + eoAverageStat(double _value, std::string _desc) : eoStat(_value, _desc) {} + + virtual void operator()(const eoPop& _pop) + { + double v = std::accumulate(_pop.begin(), _pop.end(), 0.0, eoAverageStat::sumFitness); + + value() = v / pop.size(); + } +}; + +template +class eoSecondMomentStats : public eoStat > +{ +public : + typedef std::pair SquarePair; + eoSecondMomentStats(std::string _description = "Average & Stdev") : eoStat(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& _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 eoStdevStat : public eoStat +{ +public : + typedef typename eoSecondMomentStats::SquarePair SquarePair; + + eoStdevStat(std::string _description = "Stdev") : eoStat(0.0, _description) {} + + virtual void operator()(const eoPop& _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 diff --git a/eo/src/utils/eoState.cpp b/eo/src/utils/eoState.cpp index e1e2dd4c..5a14e743 100644 --- a/eo/src/utils/eoState.cpp +++ b/eo/src/utils/eoState.cpp @@ -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::iterator it = creationOrder.begin(); it != creationOrder.end(); ++it) + for (vector::const_iterator it = creationOrder.begin(); it != creationOrder.end(); ++it) { os << "\\section{" << (*it)->first << "}\n"; (*it)->second->printOn(os); diff --git a/eo/src/utils/eoState.h b/eo/src/utils/eoState.h index e505f55f..fd50cc9b 100644 --- a/eo/src/utils/eoState.h +++ b/eo/src/utils/eoState.h @@ -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); diff --git a/eo/src/utils/eoUpdater.cpp b/eo/src/utils/eoUpdater.cpp new file mode 100644 index 00000000..9bcdc1f8 --- /dev/null +++ b/eo/src/utils/eoUpdater.cpp @@ -0,0 +1,36 @@ +#ifdef _MSC_VER +#pragma warning(disable:4786) +#endif + + +#include + +#include +#include + +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()); + } +}; + diff --git a/eo/src/utils/eoUpdater.h b/eo/src/utils/eoUpdater.h new file mode 100644 index 00000000..f877bf11 --- /dev/null +++ b/eo/src/utils/eoUpdater.h @@ -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 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 + +/** +*/ +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 \ No newline at end of file