diff --git a/eo/src/utils/checkpointing b/eo/src/utils/checkpointing index c8fe220f..8af4df76 100644 --- a/eo/src/utils/checkpointing +++ b/eo/src/utils/checkpointing @@ -12,3 +12,4 @@ #include #include #include +#include diff --git a/eo/src/utils/eoFileSnapshot.h b/eo/src/utils/eoFileSnapshot.h index 2ba1a428..910b73fe 100644 --- a/eo/src/utils/eoFileSnapshot.h +++ b/eo/src/utils/eoFileSnapshot.h @@ -56,9 +56,9 @@ public : typedef vector vDouble; typedef eoValueParam > vDoubleParam; - eoFileSnapshot(std::string _dirname, unsigned _frequency = 1, + eoFileSnapshot(std::string _dirname, unsigned _frequency = 1, std::string _filename = "gen", std::string _delim = " "): - dirname(_dirname), frequency(_frequency), + dirname(_dirname), frequency(_frequency), filename(_filename), delim(_delim), counter(0), boolChanged(true) { string s = "test -d " + dirname; @@ -69,16 +69,16 @@ public : // now make sure there is a dir without any genXXX file in it if (res) // no dir present { - s = string("mkdir ")+dirname; + s = string("mkdir ")+dirname; } else { - s = string("/bin/rm ")+dirname+ "/" + filename + "*"; + s = string("/bin/rm ")+dirname+ "/" + filename + "*"; } system(s.c_str()); // all done } - + /** accessor: has something changed (for gnuplot subclass) */ virtual bool hasChanged() {return boolChanged;} @@ -94,7 +94,7 @@ public : char buff[255]; ostrstream oscount(buff, 254); oscount << counter; - oscount << std::ends; + oscount << std::ends; currentFileName = dirname + "/" + filename + oscount.str(); } @@ -112,13 +112,13 @@ public : boolChanged = true; setCurrentFileName(); ofstream os(currentFileName.c_str()); - + if (!os) { string str = "eoFileSnapshot: Could not open " + currentFileName; throw runtime_error(str); } - + return operator()(os); } @@ -126,7 +126,7 @@ public : */ eoMonitor& operator()(std::ostream& _os) { - const eoValueParam > * ptParam = + const eoValueParam > * ptParam = static_cast >* >(vec[0]); const vector v = ptParam->value(); @@ -160,6 +160,17 @@ public : { return dirname;} virtual const string baseFileName() // the title for eoGnuPlot { return filename;} + + /// add checks whether it is a vector of doubles + void add(const eoParam& _param) + { + if (!dynamic_cast >*>(&_param)) + { + throw logic_error("eoFileSnapshot: I can only monitor vectors of doubles, sorry"); + } + eoMonitor::add(_param); + } + private : std::string dirname; unsigned frequency; diff --git a/eo/src/utils/eoMOFitnessStat.h b/eo/src/utils/eoMOFitnessStat.h new file mode 100644 index 00000000..2c6b105e --- /dev/null +++ b/eo/src/utils/eoMOFitnessStat.h @@ -0,0 +1,73 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoFitnessStat.h +// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000, 2001 +/* + 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 _eoFitnessStat_h +#define _eoFitnessStat_h + +#include + +/** + The fitnesses of a whole population, as a vector +*/ +template +class eoFitnessStat : public eoSortedStat > +{ +public : + eoFitnessStat(std::string _description = "AllFitnesses") : + eoSortedStat >(vector(0), _description) {} + + virtual void operator()(const vector& _popPters) + { + value().resize(_popPters.size()); + for (unsigned i=0; i<_popPters.size(); i++) + value()[i] = _popPters[i]->fitness(); + } +}; + + +/** For multi-objective fitness, we need to translate a stat > + into a vector, so each objective gets a seperate stat +*/ +template +class eoMOFitnessStat : public eoSortedStat > +{ +public : + /** Ctor: say what component you want + */ + eoMOFitnessStat(unsigned _objective, std::string _description = "MO-Fitness") : + eoSortedStat >(vector(0), _description), + objective(_objective) {} + + virtual void operator()(const vector& _popPters) + { + value().resize(_popPters.size()); + for (unsigned i=0; i<_popPters.size(); i++) + value()[i] = _popPters[i]->fitness()[objective]; + } +private: + unsigned int objective; // The objective we're storing +}; +#endif diff --git a/eo/src/utils/eoMonitor.h b/eo/src/utils/eoMonitor.h index a71d154b..1af48728 100644 --- a/eo/src/utils/eoMonitor.h +++ b/eo/src/utils/eoMonitor.h @@ -36,8 +36,8 @@ 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 + 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. */ @@ -46,7 +46,12 @@ class eoMonitor : public eoF public : virtual void lastCall() {} - virtual void add(const eoParam& _param) { vec.push_back(&_param); } + + /** + Adds a parameter to the monitor. It is virtual so you can do some type checking + in derived classes if you must. + */ + virtual void add(const eoParam& _param) { vec.push_back(&_param); } protected : diff --git a/eo/src/utils/eoParam.h b/eo/src/utils/eoParam.h index 954a8611..26db8527 100644 --- a/eo/src/utils/eoParam.h +++ b/eo/src/utils/eoParam.h @@ -73,7 +73,7 @@ public: * Pure virtual function to set the value */ virtual void setValue(std::string _value) = 0 ; - + /** * Returns the short name. */ @@ -111,7 +111,7 @@ private: std::string repLongName; std::string repDefault; std::string repDescription; - char repShortHand; + char repShortHand; bool repRequired; }; @@ -187,7 +187,7 @@ void eoValueParam::setValue(std::string _value) /// Because MSVC does not support partial specialization, the pair is a double, not a T template <> std::string eoValueParam >::getValue(void) const -{ +{ // use own buffer as MSVC's buffer leaks! char buff[1024]; std::ostrstream os(buff, 1024); @@ -204,6 +204,43 @@ void eoValueParam >::setValue(std::string _value) is >> repValue.second; } +/// Because MSVC does not support partial specialization, the vector is a vector of doubles, not a T +template <> +std::string eoValueParam > >::getValue(void) const +{ + std::ostrstream os; + os << repValue.size() << ' '; + for (unsigned i = 0; i < repValue.size(); ++i) + { + os << repValue[i].size() << ' '; + std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator(os, " ")); + } + + os << std::ends; + return os.str(); +} + +/// Because MSVC does not support partial specialization, the vector is a vector of doubles, not a T +template <> +void eoValueParam > >::setValue(std::string _value) +{ + std::istrstream is(_value.c_str()); + unsigned sz; + is >> sz; + repValue.resize(sz); + + for (unsigned i = 0; i < repValue.size(); ++i) + { + unsigned sz2; + is >> sz2; + repValue[i].resize(sz2); + for (unsigned j = 0; j < sz2; ++j) + { + is >> repValue[i][j]; + } + } +} + /// Because MSVC does not support partial specialization, the vector is a double, not a T template <> std::string eoValueParam >::getValue(void) const