diff --git a/eo/app/gprop/mlp.h b/eo/app/gprop/mlp.h index 2667002a6..f029d114e 100644 --- a/eo/app/gprop/mlp.h +++ b/eo/app/gprop/mlp.h @@ -245,6 +245,9 @@ namespace mlp { load(is); } + /** Virtual destructor */ + virtual ~net() {}; + void load(istream &is) { unsigned num_inputs; unsigned num_outputs; @@ -253,7 +256,7 @@ namespace mlp { is >> num_inputs >> num_outputs >> num_hidden_layers; std::vector layer_sizes; - for (int i=0; i> layer_size; layer_sizes.push_back(layer_size); diff --git a/eo/configure.in b/eo/configure.in index 50e649501..7e920ccb3 100644 --- a/eo/configure.in +++ b/eo/configure.in @@ -28,28 +28,27 @@ AC_PROG_MAKE_SET AC_PROG_RANLIB AC_CHECK_PROG(DOXYGEN, doxygen, doxygen, true) -dnl Checks for header files. -AC_HEADER_STDC -AC_CHECK_HEADERS(inttypes.h) -AC_CHECK_HEADERS(limits.h) -AC_CHECK_HEADERS(values.h) -AC_CXX_HAVE_NUMERIC_LIMITS -AC_CXX_HAVE_SSTREAM +dnl Checks for compiler characteristics. AC_CXX_NAMESPACES +AC_TYPE_SIZE_T + +dnl Checks for header files. +AC_LANG(C) +AC_HEADER_STDC +AC_CHECK_HEADERS(limits.h) +AC_LANG(C++) +AC_CHECK_HEADERS(sstream, [], AC_MSG_ERROR([Need sstream C++ include.])) +AC_CHECK_HEADERS(stdint.h, [], AC_MSG_WARN([Need C99 standard header.])) +AC_CXX_HAVE_NUMERIC_LIMITS dnl Checks for typedefs, structures, and compiler characteristics. -AC_C_CONST -AC_C_INLINE -AC_CHECK_SIZEOF([unsigned long]) -AC_CHECK_TYPES(uint32_t) -AC_TYPE_SIZE_T +AC_CHECK_SIZEOF(unsigned long) +AC_CHECK_TYPES(uint32_t, [], AC_MSG_WARN([Need uint32_t from C99 standard.])) dnl Checks for libraries. AC_CHECK_LIB(m, cos) dnl Checks for library functions. -AC_CHECK_FUNCS(select) - dnl create makefiles AC_OUTPUT(Makefile \ diff --git a/eo/src/Makefile.am b/eo/src/Makefile.am index b609d3c41..cccb63b01 100644 --- a/eo/src/Makefile.am +++ b/eo/src/Makefile.am @@ -12,8 +12,6 @@ libeo_a_SOURCES = eoFunctorStore.cpp \ eoScalarFitnessAssembled.cpp -AM_CXXFLAGS = -I$(top_srcdir)/src - pkginclude_HEADERS = eo \ EO.h \ apply.h \ @@ -114,3 +112,6 @@ pkginclude_HEADERS = eo \ es.h \ ga.h +AM_CXXFLAGS = -I$(top_srcdir)/src + +AM_LIBS = -Lutils -leoutil diff --git a/eo/src/do/make_checkpoint_pareto.h b/eo/src/do/make_checkpoint_pareto.h index b79f314f7..6ca9bc23d 100644 --- a/eo/src/do/make_checkpoint_pareto.h +++ b/eo/src/do/make_checkpoint_pareto.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // make_checkpoint_pareto.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 @@ -32,11 +32,7 @@ #endif #include -#ifdef HAVE_SSTREAM #include -#else -#include -#endif #include "EO.h" #include "eoParetoFitness.h" @@ -55,7 +51,7 @@ template eoCheckPoint& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state, eoEvalFuncCounter& _eval, eoContinue& _continue) { // first, create a checkpoint from the eoContinue - and store in _state - eoCheckPoint & checkpoint = + eoCheckPoint & checkpoint = _state.storeFunctor(new eoCheckPoint(_continue)); /////// get number of obectives from Fitness - not very elegant @@ -72,9 +68,9 @@ eoCheckPoint& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state, // Create anyway a generation-counter parameter WARNING: not stored anywhere!!! eoValueParam *generationCounter = new eoValueParam(0, "Gen."); // Create an incrementor (sub-class of eoUpdater). - eoIncrementor & increment = + eoIncrementor & increment = _state.storeFunctor(new eoIncrementor(generationCounter->value()) ); - // Add it to the checkpoint, + // Add it to the checkpoint, checkpoint.add(increment); // dir for DISK output @@ -109,7 +105,7 @@ eoCheckPoint& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state, unsigned frequency = atoi(fPlot.first.c_str()); if (frequency) // something to plot { - unsigned nbPlot = fPlot.second.size(); + unsigned nbPlot = fPlot.second.size(); if ( nbPlot % 2 ) // odd! throw std::runtime_error("Odd number of front description in make_checkpoint_pareto"); @@ -124,32 +120,18 @@ eoCheckPoint& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state, unsigned obj2 = atoi(fPlot.second[i+1].c_str()); eoMOFitnessStat* fStat; if (!bStat[obj1]) { // not already there: create it -#ifdef HAVE_SSTREAM std::ostringstream os; - os << "Obj. " << obj1 << std::ends; + os << "Obj. " << obj1 << std::ends; fStat = new eoMOFitnessStat(obj1, os.str().c_str()); -#else - char s[1024]; - std::ostrstream os(s, 1022); - os << "Obj. " << obj1 << std::ends; - fStat = new eoMOFitnessStat(obj1, s); -#endif _state.storeFunctor(fStat); bStat[obj1]=true; theStats[obj1]=fStat; checkpoint.add(*fStat); } if (!bStat[obj2]) { // not already there: create it -#ifdef HAVE_SSTREAM std::ostringstream os; - os << "Obj. " << obj2 << std::ends; + os << "Obj. " << obj2 << std::ends; fStat = new eoMOFitnessStat(obj2, os.str().c_str()); -#else - char s[1024]; - std::ostrstream os2(s, 1022); - os2 << "Obj. " << obj2 << std::ends; - fStat = new eoMOFitnessStat(obj2, s); -#endif _state.storeFunctor(fStat); bStat[obj2]=true; theStats[obj2]=fStat; @@ -157,26 +139,18 @@ eoCheckPoint& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state, } // then the fileSnapshots -#ifdef HAVE_SSTREAM std::ostringstream os; - os << "Front." << obj1 << "." << obj2 << "." << std::ends; + os << "Front." << obj1 << "." << obj2 << "." << std::ends; eoFileSnapshot& snapshot = _state.storeFunctor( new eoFileSnapshot(dirName, frequency, os.str().c_str())); -#else - char s3[1024]; - std::ostrstream os3(s3, 1022); - os3 << "Front." << obj1 << "." << obj2 << "." << std::ends; - eoFileSnapshot & snapshot = _state.storeFunctor( - new eoFileSnapshot(dirName, frequency, s3 ) ); -#endif checkpoint.add(snapshot); - + snapshot.add(*theStats[obj1]); snapshot.add(*theStats[obj2]); // and create the gnuplotter from the fileSnapshot #if !defined(NO_GNUPLOT) - if (boolGnuplot) + if (boolGnuplot) { eoGnuplot1DSnapshot & plotSnapshot = _state.storeFunctor(new eoGnuplot1DSnapshot(snapshot)); @@ -204,8 +178,8 @@ eoCheckPoint& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state, // do we want an eoStdoutMonitor? bool needStdoutMonitor = printPop ; // only this one at the moment - // The Stdout monitor will print parameters to the screen ... - if ( needStdoutMonitor ) + // The Stdout monitor will print parameters to the screen ... + if ( needStdoutMonitor ) { eoStdoutMonitor & monitor = _state.storeFunctor(new eoStdoutMonitor(false)); @@ -241,12 +215,12 @@ eoCheckPoint& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state, #else std::string stmp = dirName + "/generations"; #endif - eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp); + eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp); _state.storeFunctor(stateSaver1); checkpoint.add(*stateSaver1); } - // save state every T seconds + // save state every T seconds eoValueParam& saveTimeIntervalParam = _parser.createParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" ); if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0) { @@ -259,7 +233,7 @@ eoCheckPoint& do_make_checkpoint_pareto(eoParser& _parser, eoState& _state, #else std::string stmp = dirName + "/time"; #endif - eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp); + eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp); _state.storeFunctor(stateSaver2); checkpoint.add(*stateSaver2); } diff --git a/eo/src/eoSequentialSelect.h b/eo/src/eoSequentialSelect.h index adb65327e..8abbf4eaa 100644 --- a/eo/src/eoSequentialSelect.h +++ b/eo/src/eoSequentialSelect.h @@ -28,12 +28,13 @@ #define eoSequential_h /** Contains the following classes: - * - eoSequentialSelect, returns all individuals one by one, + * - eoSequentialSelect, returns all individuals one by one, * either sorted or shuffled * - eoEliteSequentialSelect, returns all indivisuals one by one * starting with best, continuing shuffled (see G3 engine) */ +#include #include #include @@ -129,7 +130,7 @@ template class eoEliteSequentialSelect: public eoSelectOne const EOT *ptmp = eoPters[0]; eoPters[0]=best; eoPters[ibest] = ptmp; - // exit after setting current + // exit after setting current current=0; } diff --git a/eo/src/eoVariableLengthCrossover.h b/eo/src/eoVariableLengthCrossover.h index 5e0b1653a..f6e72f9da 100644 --- a/eo/src/eoVariableLengthCrossover.h +++ b/eo/src/eoVariableLengthCrossover.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // eoVariableLengthCrossover.h // (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 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 @@ -55,7 +55,7 @@ public: eoUniformAtomExchange(double _rate=0.5):rate(_rate){} /** randomize: fill the mask: the exchange will be simulated first - * to see if sizes are OK, so it must be repeatable : + * to see if sizes are OK, so it must be repeatable : * the mask has to be a private data, cannot be computed on the fly */ void randomize(unsigned _size1, unsigned _size2) @@ -154,17 +154,10 @@ public : /** the inherited className */ virtual std::string className() const - { -#ifdef HAVE_SSTREAM + { std::ostringstream os; - os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")"; - return os.str() -#else - char s[1024]; - std::ostrstream os(s, 1022); - os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")" << std::ends; - return std::string(s); -#endif + os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")"; + return os.str() } private: @@ -172,7 +165,7 @@ private: eoAtomExchange & atomExchange; }; -/** Crossover using an AtomCrossover. Probably irrelevant in Variable Length - +/** Crossover using an AtomCrossover. Probably irrelevant in Variable Length - see eoFlOrBinOp.h and eoFlOrQuadOp.h for the similar Fixed Length operators */ template @@ -201,7 +194,7 @@ public : } virtual std::string className() const - { + { return "eoInnerExchangeQuadOp(" + op.className() + ")"; } diff --git a/eo/src/eoVariableLengthMutation.h b/eo/src/eoVariableLengthMutation.h index 2a17da685..585fc3767 100644 --- a/eo/src/eoVariableLengthMutation.h +++ b/eo/src/eoVariableLengthMutation.h @@ -142,16 +142,9 @@ public : virtual std::string className() const { -#ifdef HAVE_SSTREAM std::ostringstream os; os << "eoVlDelMutation("< -/** -\ingroup EvolutionStrategies +/** Initialize Mutation operator - eoESMutationInit. Proxy class that is used for initializing the mutation - operator. It provides an interface between eoEsMutate and the abstract - parameterLoader. It also provides the names for the parameters in this - class as virtual protected member functions. +@ingroup EvolutionStrategies - If you have more than a single ES in a project that need different - names in the configuration files, you might consider overriding this class - to change the names. +Proxy class that is used for initializing the mutation operator. It provides an +interface between eoEsMutate and the abstract parameterLoader. It also provides +the names for the parameters in this class as virtual protected member +functions. - @see eoEsMutate +If you have more than a single ES in a project that need different names in the +configuration files, you might consider overriding this class to change the +names. + +@see eoEsMutate */ class eoEsMutationInit { - public : +public : + /** Constructor + + @param _parser Parser to read parameters from. + @param _section Parser section for \tau-parameters. + */ eoEsMutationInit(eoParser& _parser, std::string _section="ES mutation parameters" ) : - parser(_parser), repSection(_section), - TauLclParam(0), TauGlbParam(0), TauBetaParam(0) {} + parser(_parser), repSection(_section), + TauLclParam(0), TauGlbParam(0), TauBetaParam(0) {} - // because we have virtual function - size - virtual ~eoEsMutationInit(){} + /** Virtual destructor */ + virtual ~eoEsMutationInit() {} + /** local \tau */ double TauLcl(void) { if (TauLclParam == 0) @@ -63,10 +74,10 @@ class eoEsMutationInit "Local Tau (before normalization)", TauLclShort(), section()); } - return TauLclParam->value(); } + /** global tau */ double TauGlb(void) { if (TauGlbParam == 0) @@ -75,10 +86,10 @@ class eoEsMutationInit "Global Tau (before normalization)", TauGlbShort(), section()); } - return TauGlbParam->value(); } + /** correlation's tau */ double TauBeta(void) { if (TauBetaParam == 0) @@ -86,14 +97,12 @@ class eoEsMutationInit TauBetaParam = &parser.getORcreateParam(0.0873, TauBetaName(), "Beta", TauBetaShort(), section()); } - return TauBetaParam->value(); } protected : - virtual std::string section(void) - { return repSection; } + virtual std::string section(void) { return repSection; } virtual std::string TauLclName(void) const { return "TauLoc"; } virtual char TauLclShort(void) const { return 'l'; } @@ -104,13 +113,13 @@ class eoEsMutationInit virtual std::string TauBetaName(void) const { return "Beta"; } virtual char TauBetaShort(void) const { return 'b'; } - private : +private: - eoParser& parser; - std::string repSection; - eoValueParam* TauLclParam; - eoValueParam* TauGlbParam; - eoValueParam* TauBetaParam; + eoParser& parser; + std::string repSection; + eoValueParam* TauLclParam; + eoValueParam* TauGlbParam; + eoValueParam* TauBetaParam; }; #endif diff --git a/eo/src/es/make_genotype_real.h b/eo/src/es/make_genotype_real.h index b92be7bf4..5d7668086 100644 --- a/eo/src/es/make_genotype_real.h +++ b/eo/src/es/make_genotype_real.h @@ -27,23 +27,13 @@ #ifndef _make_genotype_h #define _make_genotype_h -#ifdef HAVE_CONFIG_H -#include -#endif - -#ifdef HAVE_SSTREAM -#include -#else -#include -#endif - #include +#include #include "es/eoReal.h" #include "es/eoEsChromInit.h" -#include "utils/eoRealVectorBounds.h" - // also need the parser and param includes #include "utils/eoParser.h" +#include "utils/eoRealVectorBounds.h" #include "utils/eoState.h" @@ -102,11 +92,7 @@ eoEsChromInit & do_make_genotype(eoParser& _parser, eoState& _state, EOT) to_scale = true; sigmaString.resize(pos); } -#ifdef HAVE_SSTREAM std::istringstream is(sigmaString); -#else - std::istrstream is(sigmaString.c_str()); -#endif double sigma; is >> sigma; // minimum check diff --git a/eo/src/gp/eoParseTree.h b/eo/src/gp/eoParseTree.h index ee9c94a7f..23fa32f40 100644 --- a/eo/src/gp/eoParseTree.h +++ b/eo/src/gp/eoParseTree.h @@ -27,33 +27,29 @@ #ifndef eoParseTree_h #define eoParseTree_h -#ifdef HAVE_CONFIG_H -#include -#endif - #include - #include #include +#include #include #include -#include using namespace gp_parse_tree; -/** -\defgroup ParseTree +/** @defgroup ParseTree - Various functions for tree-based Genetic Programming -*/ - -/** eoParseTree : implementation of parse-tree for genetic programming -\class eoParseTree eoParseTree.h gp/eoParseTree.h -\ingroup ParseTree +Various functions for tree-based Genetic Programming */ + +/** Implementation of parse-tree for genetic programming + +@class eoParseTree eoParseTree.h gp/eoParseTree.h + +@ingroup ParseTree +*/ template class eoParseTree : public EO, public parse_tree { diff --git a/eo/src/pyeo/PyEO.cpp b/eo/src/pyeo/PyEO.cpp index 9ccd0c9a9..a6828f039 100644 --- a/eo/src/pyeo/PyEO.cpp +++ b/eo/src/pyeo/PyEO.cpp @@ -1,6 +1,6 @@ /* PyEO - + Copyright (C) 2003 Maarten Keijzer This program is free software; you can redistribute it and/or modify @@ -18,35 +18,31 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include + #include "PyEO.h" #include -#ifdef HAVE_SSTREAM -#include -#else -#include -#endif - using namespace std; //using namespace boost::python; // static member, needs to be instantiated somewhere std::vector PyFitness::objective_info; - + bool PyFitness::dominates(const PyFitness& oth) const -{ +{ bool dom = false; for (unsigned i = 0; i < nObjectives(); ++i) { int objective = objective_info[i]; - + if (objective == 0) // ignore continue; bool maxim = objective > 0; - + double aval = maxim? (*this)[i] : -(*this)[i]; double bval = maxim? oth[i] : -oth[i]; @@ -103,17 +99,9 @@ struct pyPop_pickle_suite : boost::python::pickle_suite template boost::python::str to_string(T& _p) { -#ifdef HAVE_SSTREAM std::ostringstream os; _p.printOn(os); return boost::python::str(os.str().c_str()); -#else - std::ostrstream os; - _p.printOn(os); - os << ends; - std::string s(os.str()); - return boost::python::str(s.c_str()); -#endif } void pop_sort(eoPop& pop) { pop.sort(); } @@ -129,11 +117,11 @@ PyEO& pop_getitem(eoPop& pop, boost::python::object key) boost::python::extract x(key); if (!x.check()) throw index_error("Slicing not allowed"); - + int i = x(); - + if (static_cast(i) >= pop.size()) - { + { throw index_error("Index out of bounds"); } return pop[i]; @@ -144,14 +132,14 @@ void pop_setitem(eoPop& pop, boost::python::object key, PyEO& value) boost::python::extract x(key); if (!x.check()) throw index_error("Slicing not allowed"); - + int i = x(); - + if (static_cast(i) >= pop.size()) - { + { throw index_error("Index out of bounds"); } - + pop[i] = value; } @@ -179,7 +167,7 @@ BOOST_PYTHON_MODULE(PyEO) using namespace boost::python; boost::python::register_exception_translator(&translate_index_error); - + boost::python::class_("EO") .add_property("fitness", &PyEO::getFitness, &PyEO::setFitness) .add_property("genome", &PyEO::getGenome, &PyEO::setGenome) @@ -204,8 +192,8 @@ BOOST_PYTHON_MODULE(PyEO) .def_pickle(pyPop_pickle_suite()) ; - - // Other definitions in different compilation units, + + // Other definitions in different compilation units, // this to avoid having g++ to choke on the load random_numbers(); valueParam(); diff --git a/eo/src/pyeo/pickle.h b/eo/src/pyeo/pickle.h index 496a28726..c4219016c 100644 --- a/eo/src/pyeo/pickle.h +++ b/eo/src/pyeo/pickle.h @@ -1,6 +1,6 @@ /* PyEO - + Copyright (C) 2003 Maarten Keijzer This program is free software; you can redistribute it and/or modify @@ -24,30 +24,22 @@ #include #include -#ifdef HAVE_SSTREAM #include -#else -#include -#endif /** Implements pickle support for eoPersistent derivatives */ template struct T_pickle_suite : boost::python::pickle_suite { - static + static std::string print_to_string(const T& t) { -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - std::ostrstream os; -#endif t.printOn(os); os << std::ends; return os.str(); } - + static boost::python::tuple getstate(const T& t) { @@ -59,11 +51,7 @@ struct T_pickle_suite : boost::python::pickle_suite void setstate(T& t, boost::python::tuple pickled) { std::string s = boost::python::extract(pickled[0]); -#ifdef HAVE_SSTREAM std::istringstream is(s); -#else - std::istrstream is(s.c_str(), s.size()); -#endif t.readFrom(is); } }; diff --git a/eo/src/pyeo/random_numbers.cpp b/eo/src/pyeo/random_numbers.cpp index fb01724bf..bd0b29198 100644 --- a/eo/src/pyeo/random_numbers.cpp +++ b/eo/src/pyeo/random_numbers.cpp @@ -1,6 +1,6 @@ /* PyEO - + Copyright (C) 2003 Maarten Keijzer This program is free software; you can redistribute it and/or modify @@ -23,12 +23,7 @@ using namespace boost::python; -#ifdef HAVE_SSTREAM #include -#else -#include -#endif - #include using namespace boost::python; @@ -43,11 +38,7 @@ double normal(eoRng& rng) { return rng.normal(); } std::string rng_to_string(const eoRng& _rng) { -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - std::ostrstream os; -#endif _rng.printOn(os); os << std::ends; return os.str(); @@ -56,11 +47,7 @@ std::string rng_to_string(const eoRng& _rng) void rng_from_string(eoRng& _rng, std::string s) { -#ifdef HAVE_SSTREAM std::istringstream is(s); -#else - std::istrstream is(s.c_str(), s.size()); -#endif _rng.readFrom(is); } @@ -91,7 +78,7 @@ int spin(eoRng& _rng, numeric::array values, double total) } double chance = _rng.uniform() * total; - + int i = 0; while (chance >= 0.0) chance -= extract(values[i++]); @@ -115,6 +102,6 @@ void random_numbers() .def("roulette_wheel", spin) .def_pickle(RNG_pickle_suite()) ; - + def("rng", get_rng, return_value_policy()); } diff --git a/eo/src/utils/Makefile.am b/eo/src/utils/Makefile.am index 5c041cb7d..873fe482a 100644 --- a/eo/src/utils/Makefile.am +++ b/eo/src/utils/Makefile.am @@ -1,8 +1,9 @@ ## Makefile.am for eo/src/utils -lib_LIBRARIES = libeoutils.a +lib_LIBRARIES = libeoutils.a -libeoutils_a_SOURCES = eoParser.cpp \ +libeoutils_a_SOURCES = eoData.cpp \ + eoParser.cpp \ eoRNG.cpp \ eoState.cpp \ eoUpdater.cpp \ @@ -12,8 +13,6 @@ libeoutils_a_SOURCES = eoParser.cpp \ eoIntBounds.cpp \ make_help.cpp -AM_CXXFLAGS = -I$(top_srcdir)/src - utilsincludedir = $(pkgincludedir)/utils utilsinclude_HEADERS = checkpointing \ @@ -50,3 +49,6 @@ utilsinclude_HEADERS = checkpointing \ pipecom.h \ rnd_generators.h \ selectors.h + +AM_CXXFLAGS = -I$(top_srcdir)/src + diff --git a/eo/src/utils/eoData.cpp b/eo/src/utils/eoData.cpp new file mode 100644 index 000000000..d43f2719b --- /dev/null +++ b/eo/src/utils/eoData.cpp @@ -0,0 +1,30 @@ +// Copyright (C) 2005 Jochen Küpper +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; see the file License. if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + + +#include "eoData.h" + +#ifdef HAVE_NUMERIC_LIMITS +int MAXINT = numeric_limits::max(); +#else +#include +int MAXINT = INT_MAX; +#endif + + +// Local Variables: +// c-file-style: "Stroustrup" +// fill-column: 80 +// End: diff --git a/eo/src/utils/eoData.h b/eo/src/utils/eoData.h index 5fb9cb30d..8e973c222 100644 --- a/eo/src/utils/eoData.h +++ b/eo/src/utils/eoData.h @@ -25,61 +25,31 @@ #ifndef EODATA_H #define EODATA_H -//----------------------------------------------------------------------------- - -#include // std::vector -#include // set -#include // std::string - - -#ifdef _MSC_VER - #include // MAXDOUBLE - #define MAXFLOAT numeric_limits::max() - #define MINFLOAT numeric_limits::min() - #define MAXDOUBLE numeric_limits::max() - #define MAXINT numeric_limits::max() -#else - #include - #include +#ifdef MAXINT +#undef MAXINT #endif - -#if !defined(_WIN32) && !defined(__CYGWIN__) && !(defined(__APPLE__) || defined(MACOSX)) && !defined(__FreeBSD__) - #include -#endif - -// for cygwin and windows (and possibly MacOsX) -#ifndef MINFLOAT - #define MINFLOAT ( (float)1e-127 ) -#endif -#ifndef MAXFLOAT - #define MAXFLOAT ( (float)1e127 ) -#endif -#ifndef MAXINT - #define MAXINT 2147483647 -#endif -#ifndef MAXDOUBLE - #define MAXDOUBLE (double)1.79769313486231570e+308 -#endif +extern int MAXINT; #ifndef _MSC_VER #include #define _isnan isnan #endif + //----------------------------------------------------------------------------- // some defines to make things easier to get at first sight -// tuning the amount of output using a boolean argument: +// tuning the amount of output using a boolean argument: // true should always mean more output #define eo_verbose true #define eo_no_verbose false -// to be used in selection / replacement procedures to indicate whether +// to be used in selection / replacement procedures to indicate whether // the argument (rate, a double) shoudl be treated as a rate (number=rate*popSize) // or as an absolute integer (number=rate regardless of popsize). // the default value shoudl ALWAYS be true (eo_as_a_rate). // -// this construct is mandatory because in some cases you might not know the -// population size that will enter the replacement for instance - so you +// this construct is mandatory because in some cases you might not know the +// population size that will enter the replacement for instance - so you // cannot simply have a pre-computed (double) rate of 1/popSize #define eo_is_a_rate true #define eo_is_an_integer false diff --git a/eo/src/utils/eoFileSnapshot.h b/eo/src/utils/eoFileSnapshot.h index 97a8e6f71..5dff408af 100644 --- a/eo/src/utils/eoFileSnapshot.h +++ b/eo/src/utils/eoFileSnapshot.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // eoFileSnapshot.h // (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 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 @@ -27,10 +27,6 @@ #ifndef _eoFileSnapshot_h #define _eoFileSnapshot_h -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include @@ -39,15 +35,15 @@ /** - Prints snapshots of fitnesses to a (new) file every N generations + Prints snapshots of fitnesses to a (new) file every N generations -Assumes that the parameters that are passed to the monitor +Assumes that the parameters that are passed to the monitor (method add in eoMonitor.h) are eoValueParam > of same size. -A dir is created and one file per snapshot is created there - +A dir is created and one file per snapshot is created there - so you can later generate a movie! -TODO: The counter is handled internally, but this should be changed +TODO: The counter is handled internally, but this should be changed so that you can pass e.g. an evalcounter (minor) I failed to templatize everything so that it can handle eoParam > @@ -103,17 +99,9 @@ public : */ void setCurrentFileName() { -#ifdef HAVE_SSTREAM - std::ostringstream oscount; - oscount << counter; -#else - char buff[255]; - std::ostrstream oscount(buff, 254); - oscount << counter; - oscount << std::ends; -#endif - - currentFileName = dirname + "/" + filename + oscount.str(); + std::ostringstream oscount; + oscount << counter; + currentFileName = dirname + "/" + filename + oscount.str(); } /** The operator(void): opens the std::ostream and calls the write method diff --git a/eo/src/utils/eoGnuplot.h b/eo/src/utils/eoGnuplot.h index 1d6d25eff..5758a3ec9 100644 --- a/eo/src/utils/eoGnuplot.h +++ b/eo/src/utils/eoGnuplot.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // eoGnuplot1DMonitor.h // (c) Marc Schoenauer, 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 @@ -26,10 +26,6 @@ #ifndef _eoGnuplot_H #define _eoGnuplot_H -#ifdef HAVE_CONFIG_H -#include -#endif - #include /** @@ -52,13 +48,13 @@ class eoGnuplot { public: // Ctor - eoGnuplot(std::string _title, std::string _extra = std::string("")) : + eoGnuplot(std::string _title, std::string _extra = std::string("")) : firstTime(true) { // opens pipe with Gnuplot initGnuPlot(_title, _extra); } - + // Dtor virtual ~eoGnuplot() { // close - the gnuplot windows if pipe was correctly opened @@ -109,24 +105,13 @@ private: inline void eoGnuplot::initGnuPlot(std::string _title, std::string _extra) ///////////////////////////////////////////////////////// { -#ifdef HAVE_SSTREAM std::ostringstream os; os << "250x150-0+" << numWindow*170; -#else - char snum[255]; - std::ostrstream os(snum, 254); - os << "250x150-0+" << numWindow*170 << std::ends; -#endif - numWindow++; char *args[6]; args[0] = strdup( "gnuplot" ); args[1] = strdup( "-geometry" ); -#ifdef HAVE_SSTREAM args[2] = strdup( os.str().c_str()); -#else - args[2] = strdup( os.str() ); -#endif args[3] = strdup( "-title" ); args[4] = strdup( _title.c_str() ); args[5] = 0; @@ -154,7 +139,7 @@ inline void eoGnuplot::initGnuPlot(std::string _title, std::string _extra) * Created......: Mon Mar 13 13:50:11 1995 * Description..: Communication par pipe bidirectionnel avec un autre process * - * Ident........: $Id: eoGnuplot.h,v 1.12 2005-09-07 17:09:19 maartenkeijzer Exp $ + * Ident........: $Id: eoGnuplot.h,v 1.13 2005-09-28 21:49:26 kuepper Exp $ * ---------------------------------------------------------------------- */ diff --git a/eo/src/utils/eoGnuplot1DMonitor.h b/eo/src/utils/eoGnuplot1DMonitor.h index 8e01413e5..b3358e094 100644 --- a/eo/src/utils/eoGnuplot1DMonitor.h +++ b/eo/src/utils/eoGnuplot1DMonitor.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // eoGnuplot1DMonitor.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 @@ -28,10 +28,6 @@ #ifndef _eoGnuplot1DMonitor_H #define _eoGnuplot1DMonitor_H -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include @@ -93,10 +89,10 @@ inline eoMonitor& eoGnuplot1DMonitor::operator() (void) FirstPlot(); firstTime = false; } - else + else { if( gpCom ) { - PipeComSend( gpCom, "replot\n" ); + PipeComSend( gpCom, "replot\n" ); } } return *this; @@ -110,13 +106,7 @@ inline void eoGnuplot1DMonitor::FirstPlot() { throw std::runtime_error("Must have some stats to plot!\n"); } -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - char buff[1024]; - std::ostrstream os(buff, 1024); -#endif - os << "plot"; for (unsigned i=1; i -#endif - -#include -#ifdef HAVE_SSTREAM #include -#endif +#include #include #include @@ -57,7 +51,7 @@ This class plots through gnuplot the eoStat given as argument /** eoGnuplot1DSnapshot plots stats through gnuplot - * assumes that the same file is re-written every so and so, + * assumes that the same file is re-written every so and so, * and plots it from scratch everytime it's called */ class eoGnuplot1DSnapshot: public eoFileSnapshot, public eoGnuplot @@ -107,14 +101,8 @@ class eoGnuplot1DSnapshot: public eoFileSnapshot, public eoGnuplot virtual void handleBounds(eoRealVectorBounds & _bounds) { -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - // use strstream and not std::stringstream until strstream is in all distributions - char buf[1024]; - std::ostrstream os(buf, 1023); -#endif - // std::ostrstream os; + // std::ostrstream os; os << "set autoscale\nset yrange [" ; if (_bounds.isMinBounded(0)) os << _bounds.minimum(0); @@ -140,25 +128,13 @@ inline eoMonitor& eoGnuplot1DSnapshot::operator() (void) eoFileSnapshot::operator()(); // sends plot order to gnuplot -#ifdef HAVE_SSTREAM //std::string buff; // need local memory std::ostringstream os; -#else - char buff[1024]; - std::ostrstream os(buff, 1024); -#endif - os << "set title 'Gen. " << getCounter() << "'; plot '" // mk: had to use getFilename().c_str(), because it seems the string(stream) lib is screwed in gcc3.2 << getFileName().c_str() << "' notitle with points ps " << pointSize; os << std::endl; - -#ifdef HAVE_SSTREAM PipeComSend( gpCom, os.str().c_str()); -#else - os << std::ends; - PipeComSend( gpCom, buff ); -#endif return (*this); } diff --git a/eo/src/utils/eoHowMany.h b/eo/src/utils/eoHowMany.h index d59321b34..39faa8b98 100644 --- a/eo/src/utils/eoHowMany.h +++ b/eo/src/utils/eoHowMany.h @@ -4,7 +4,7 @@ // eoHowMany_h.h // Base class for choosing a number of guys to apply something from a popsize // (c) Marc Schoenauer, 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 @@ -30,11 +30,11 @@ * Typically, is used in selection / replacement procedures, e.g. * the number of offspring from the number of parents, or * the number of survivors for an eoReduce functor, ... - * - * Such construct is very useful because in some cases you might not know the - * population size that will enter the replacement. For instance, you - * cannot simply have a pre-computed (double) rate of 1/popSize - * if you want to select or kill just 1 guy. Using an eoHowMany + * + * Such construct is very useful because in some cases you might not know the + * population size that will enter the replacement. For instance, you + * cannot simply have a pre-computed (double) rate of 1/popSize + * if you want to select or kill just 1 guy. Using an eoHowMany * allows one to modify the population size without touching anything else. * * There are 4 possible way to compute the return value from the argument: @@ -49,11 +49,11 @@ * It has 2 private members, a double and an integer to cover all cases * * Example use: in eoGeneralBreeder.h - * Example reading from parser: in + * Example reading from parser: in * do/make_algo_scalar.h line 141 - * MS 10/04/2002: - * Added the possibility to have a negative number - + * MS 10/04/2002: + * Added the possibility to have a negative number - * when treated as a number: returns then (size - combien) * Should not modify anything when a positive number is passed in the ctor * @@ -64,20 +64,12 @@ * It is an eoPersistent because we need to be able to use eoParamValue */ -#ifdef HAVE_CONFIG_H -#include -#endif - -#ifdef HAVE_SSTREAM #include -#else -#include -#endif class eoHowMany : public eoPersistent { public: - /** Original Ctor from direct rate + bool + /** Original Ctor from direct rate + bool @param rate the rate, OR the integer to store, depending on 2nd arg. @param _interpret_as_rate to tell whether the rate actually is a rate */ @@ -102,18 +94,18 @@ public: } } - /** Ctor from an int - both from int and unsigned int are needed + /** Ctor from an int - both from int and unsigned int are needed * to avoid ambiguity with the Ctor from a double */ eoHowMany(int _combien) : rate(0.0), combien(_combien) {} - /** Ctor from an unsigned int - both from int and unsigned int are needed + /** Ctor from an unsigned int - both from int and unsigned int are needed * to avoid ambiguity with the Ctor from a double */ eoHowMany(unsigned int _combien) : rate(0.0), combien(_combien) {} /// Virtual dtor. They are needed in virtual class hierarchies. virtual ~eoHowMany() {} - /** Does what it was designed for + /** Does what it was designed for * - combien==0 : return rate*_size * - else * - combien>0 : return combien (regardless of _size) @@ -135,7 +127,7 @@ public: return unsigned(combien); } - virtual void printOn(std::ostream& _os) const + virtual void printOn(std::ostream& _os) const { if (combien == 0) _os << 100*rate << "% "; @@ -145,7 +137,7 @@ public: } - virtual void readFrom(std::istream& _is) + virtual void readFrom(std::istream& _is) { std::string value; _is >> value; @@ -163,12 +155,8 @@ public: interpret_as_rate = true; _value.resize(pos); // get rid of % } - -#ifdef HAVE_SSTREAM + std::istringstream is(_value); -#else - std::istrstream is(_value.c_str()); -#endif is >> rate; // now store if (interpret_as_rate) @@ -193,7 +181,7 @@ public: combien = -combien; return (*this); } - + private : double rate; int combien; diff --git a/eo/src/utils/eoIntBounds.cpp b/eo/src/utils/eoIntBounds.cpp index 3ac29bdcd..59350240b 100644 --- a/eo/src/utils/eoIntBounds.cpp +++ b/eo/src/utils/eoIntBounds.cpp @@ -8,12 +8,7 @@ #endif #include - -#ifdef HAVE_SSTREAM #include -#else -#include -#endif #include "eoIntBounds.h" diff --git a/eo/src/utils/eoParam.h b/eo/src/utils/eoParam.h index 7fe537c56..5bf1dd0a4 100644 --- a/eo/src/utils/eoParam.h +++ b/eo/src/utils/eoParam.h @@ -27,24 +27,13 @@ #ifndef eoParam_h #define eoParam_h -#ifdef HAVE_CONFIG_H -#include -#endif - -//----------------------------------------------------------------------------- -#include // for floor -#include - -#ifdef HAVE_SSTREAM -#include -#else -#include -#endif - -#include -#include // for GCC 3.2 +#include +#include #include -#include // for specializations +#include +#include +#include +#include /** @@ -189,26 +178,15 @@ public : std::string getValue(void) const { -#ifdef HAVE_SSTREAM std::ostringstream os; os << repValue; -#else - char buf[1024]; - std::ostrstream os(buf, 1023); - os << repValue; - os << std::ends; -#endif return os.str(); } void setValue(const std::string& _value) { -#ifdef HAVE_SSTREAM std::istringstream is(_value); -#else - std::istrstream is(_value.c_str()); -#endif is >> repValue; } @@ -235,11 +213,7 @@ inline void eoValueParam::setValue(const std::string& _value) repValue = true; return; } -#ifdef HAVE_SSTREAM std::istringstream is(_value); -#else - std::istrstream is(_value.c_str()); -#endif is >> repValue; } @@ -249,14 +223,8 @@ template <> inline std::string eoValueParam >::getValue(void) const { // use own buffer as MSVC's buffer leaks! -#ifdef HAVE_SSTREAM std::ostringstream os; os << repValue.first << ' ' << repValue.second; -#else - char buff[1024]; - std::ostrstream os(buff, 1024); - os << repValue.first << ' ' << repValue.second << std::ends; -#endif return os.str(); } @@ -264,11 +232,7 @@ inline std::string eoValueParam >::getValue(void) cons template <> inline void eoValueParam >::setValue(const std::string& _value) { -#ifdef HAVE_SSTREAM std::istringstream is(_value); -#else - std::istrstream is(_value.c_str()); -#endif is >> repValue.first; is >> repValue.second; } @@ -279,21 +243,13 @@ inline void eoValueParam >::setValue(const std::string template <> inline std::string eoValueParam > >::getValue(void) const { -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - std::ostrstream os; -#endif 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, " ")); } - -#ifndef HAVE_SSTREAM - os << std::ends; -#endif return os.str(); } @@ -301,11 +257,7 @@ inline std::string eoValueParam > >::getValue(vo template <> inline void eoValueParam > >::setValue(const std::string& _value) { -#ifdef HAVE_SSTREAM std::istringstream is(_value); -#else - std::istrstream is(_value.c_str()); -#endif unsigned i,j,sz; is >> sz; repValue.resize(sz); @@ -328,16 +280,9 @@ inline void eoValueParam > >::setValue(const std template <> inline std::string eoValueParam >::getValue(void) const { -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - std::ostrstream os; -#endif os << repValue.size() << ' '; std::copy(repValue.begin(), repValue.end(), std::ostream_iterator(os, " ")); -#ifndef HAVE_SSTREAM - os << std::ends; -#endif return os.str(); } @@ -345,11 +290,7 @@ inline std::string eoValueParam >::getValue(void) const template <> inline void eoValueParam >::setValue(const std::string& _value) { -#ifdef HAVE_SSTREAM std::istringstream is(_value); -#else - std::istrstream is(_value.c_str()); -#endif unsigned sz; is >> sz; repValue.resize(sz); @@ -362,16 +303,9 @@ inline void eoValueParam >::setValue(const std::string& _val template <> inline std::string eoValueParam >::getValue(void) const { -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - std::ostrstream os; -#endif os << repValue.size() << ' '; std::copy(repValue.begin(), repValue.end(), std::ostream_iterator(os, " ")); -#ifndef HAVE_SSTREAM - os<< std::ends; -#endif return os.str(); } @@ -380,11 +314,7 @@ inline std::string eoValueParam >::getValue(voi template <> inline void eoValueParam >::setValue(const std::string& _value) { -#ifdef HAVE_SSTREAM std::istringstream is(_value); -#else - std::istrstream is(_value.c_str()); -#endif unsigned sz; is >> sz; repValue.resize(sz); diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index ad4d04d49..ccf9dd611 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -90,11 +90,7 @@ eoParser::eoParser ( unsigned _argc, char **_argv , string _programDescription, } } // now read arguments on command-line -#ifdef HAVE_SSTREAM stringstream stream; -#else - strstream stream; -#endif for (i = 1; i < _argc; ++i) { stream << _argv[i] << '\n'; diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index b3fd7e30f..612497db2 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -24,7 +24,7 @@ */ //----------------------------------------------------------------------------- /** -CVS Info: $Date: 2005-09-13 10:24:50 $ $Version$ $Author: kuepper $ +CVS Info: $Date: 2005-09-28 21:49:26 $ $Version$ $Author: kuepper $ */ #ifndef eoParser_h #define eoParser_h @@ -222,11 +222,7 @@ public: { eoValueParam& param = createParam(_defaultValue, _longName, _description, _shortHand, _section, _required); -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - std::ostrstream os; -#endif os << _defaultValue; if(isItThere(param)) { param.setValue(os.str()); diff --git a/eo/src/utils/eoPopStat.h b/eo/src/utils/eoPopStat.h index a4d1117b5..8e89bf118 100644 --- a/eo/src/utils/eoPopStat.h +++ b/eo/src/utils/eoPopStat.h @@ -35,10 +35,6 @@ that can be used to dump to the screen #ifndef _eoPopStat_h #define _eoPopStat_h -#ifdef HAVE_CONFIG_H -#include -#endif - #include @@ -69,7 +65,6 @@ public: Adds a \n before so it does not get mixed up with the rest of the stats that are written by the monitor it is probably used from. */ -#ifdef HAVE_SSTREAM void operator()(const eoPop& _pop) { value() = "\n# ====== pop dump =====\n"; @@ -83,24 +78,6 @@ void operator()(const eoPop& _pop) value() += os.str(); } } -#else -void operator()(const eoPop& _pop) -{ - char buffer[1023]; // about one k of space per member - value() = "\n# ====== pop dump =====\n"; - unsigned howmany=combien?combien:_pop.size(); - value() += "\n"; // otherwise, possible mix-up with other stats - for (unsigned i = 0; i < howmany; ++i) - { - std::ostrstream os(buffer, 1022); // leave space for emergency terminate - os << _pop[i] << std::endl << std::ends; - - // paranoid: - buffer[1022] = '\0'; - value() += buffer; - } -} -#endif private: unsigned combien; @@ -136,7 +113,6 @@ public: the rest of the stats that are written by the monitor it is probably used from. */ -#ifdef HAVE_SSTREAM void operator()(const std::vector& _pop) { value() = ""; // empty @@ -150,23 +126,6 @@ public: value() += os.str(); } } -#else - void operator()(const std::vector& _pop) - { - char buffer[1023]; // about one K of space per member - value() = ""; // empty - unsigned howMany=combien?combien:_pop.size(); - for (unsigned i = 0; i < howMany; ++i) - { - std::ostrstream os(buffer, 1022); // leave space for emergency terminate - os << *_pop[i] << std::endl << std::ends; - - // paranoid: - buffer[1022] = '\0'; - value() += buffer; - } - } -#endif private: unsigned combien; }; diff --git a/eo/src/utils/eoRNG.h b/eo/src/utils/eoRNG.h index 6919b6adb..99063b065 100644 --- a/eo/src/utils/eoRNG.h +++ b/eo/src/utils/eoRNG.h @@ -1,10 +1,9 @@ -/* -* Random number generator adapted from (see comments below) -* -* The random number generator is modified into a class -* by Maarten Keijzer (mak@dhi.dk). Also added the Box-Muller -* transformation to generate normal deviates. -* +/** Random number generator adapted from (see comments below) + + The random number generator is modified into a class + by Maarten Keijzer (mak@dhi.dk). Also added the Box-Muller + transformation to generate normal deviates. + 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 @@ -22,142 +21,118 @@ Contact: todos@geneura.ugr.es, http://geneura.ugr.es */ -/* ************ DOCUMENTATION IN ORIGINAL FILE *********************/ - -// This is the ``Mersenne Twister'' random number generator MT19937, which -// generates pseudorandom integers uniformly distributed in 0..(2^32 - 1) -// starting from any odd seed in 0..(2^32 - 1). This version is a recode -// by Shawn Cokus (Cokus@math.washington.edu) on March 8, 1998 of a version by -// Takuji Nishimura (who had suggestions from Topher Cooper and Marc Rieffel in -// July-August 1997). -// -// Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC Alpha -// running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to -// generate 300 million random numbers; after recoding: 24.0 sec. for the same -// (i.e., 46.5% of original time), so speed is now about 12.5 million random -// number generations per second on this machine. -// -// According to the URL -// (and paraphrasing a bit in places), the Mersenne Twister is ``designed -// with consideration of the flaws of various existing generators,'' has -// a period of 2^19937 - 1, gives a sequence that is 623-dimensionally -// equidistributed, and ``has passed many std::stringent tests, including the -// die-hard test of G. Marsaglia and the load test of P. Hellekalek and -// S. Wegenkittl.'' It is efficient in memory usage (typically using 2506 -// to 5012 bytes of static data, depending on data type sizes, and the code -// is quite short as well). It generates random numbers in batches of 624 -// at a time, so the caching and pipelining of modern systems is exploited. -// It is also divide- and mod-free. -// -// This library is free software; you can redistribute it and/or modify it -// under the terms of the GNU Library 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 Library General Public License for more details. You should have -// received a copy of the GNU Library 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. -// -// The code as Shawn received it included the following notice: -// -// Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When -// you use this, send an e-mail to with -// an appropriate reference to your work. -// -// It would be nice to CC: when you write. -// - // // uint32_t must be an unsigned integer type capable of holding at least 32 // bits; exactly 32 should be fastest, but 64 is better on an Alpha with // GCC at -O3 optimization so try your options and see what's best for you // -/* ************ END DOCUMENTATION IN ORIGINAL FILE *********************/ - #ifndef EO_RANDOM_NUMBER_GENERATOR #define EO_RANDOM_NUMBER_GENERATOR -#ifdef HAVE_CONFIG_H -#include -#endif -#ifdef HAVE_INTTYPES_H -#include -#endif +#include +#include +#include "eoPersistent.h" +#include "eoObject.h" -#include "../eoPersistent.h" -#include "../eoObject.h" +/** Random Number Generator -// TODO: check for various compilers if this is exactly 32 bits -// Unfortunately MSVC's preprocessor does not comprehend sizeof() -// so neat preprocessing tricks will not work +@class eoRng eoRNG.h utils/eoRNG.h -#if(! (defined HAVE_UINT32_T)) -#if(SIZEOF_UNSIGNED_LONG == 4) -typedef unsigned long uint32_t; -#else -#error Need to provide a type for uint32_t in eoRNG.h. -#endif -#endif +eoRng is a persistent class that uses the ``Mersenne Twister'' random +number generator MT19937 for generating random numbers. The various +member functions implement useful functions for evolutionary +algorithms. Included are: rand(), random(), flip() and normal(). + +

DOCUMENTATION IN ORIGINAL FILE

+ +This is the ``Mersenne Twister'' random number generator MT19937, which +generates pseudorandom integers uniformly distributed in 0..(2^32 - 1) starting +from any odd seed in 0..(2^32 - 1). This version is a recode by Shawn Cokus +(Cokus@math.washington.edu) on March 8, 1998 of a version by Takuji Nishimura +(who had suggestions from Topher Cooper and Marc Rieffel in July-August 1997). + +Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC Alpha +running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to +generate 300 million random numbers; after recoding: 24.0 sec. for the same +(i.e., 46.5% of original time), so speed is now about 12.5 million random number +generations per second on this machine. + +According to the URL (and +paraphrasing a bit in places), the Mersenne Twister is ``designed with +consideration of the flaws of various existing generators,'' has a period of +2^19937 - 1, gives a sequence that is 623-dimensionally equidistributed, and +``has passed many std::stringent tests, including the die-hard test of G. +Marsaglia and the load test of P. Hellekalek and S. Wegenkittl.'' It is +efficient in memory usage (typically using 2506 to 5012 bytes of static data, +depending on data type sizes, and the code is quite short as well). It generates +random numbers in batches of 624 at a time, so the caching and pipelining of +modern systems is exploited. It is also divide- and mod-free. + +The code as Shawn received it included the following notice: +- Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When you use this, + send an e-mail to with an appropriate reference to + your work. +- It would be nice to CC: when you write. -//----------------------------------------------------------------------------- -// eoRng -//----------------------------------------------------------------------------- -/** -\class eoRng eoRNG.h utils/eoRNG.h -eoRng is a persistent class that uses the ``Mersenne Twister'' random number generator MT19937 -for generating random numbers. The various member functions implement useful functions -for evolutionary algorithms. Included are: rand(), random(), flip() and normal(). +

Portability

-Note for people porting EO to other platforms: please make sure that the typedef +Note for people porting EO to other platforms: please make sure that the type uint32_t in the file eoRng.h is exactly 32 bits long. It may be longer, but not shorter. If it is longer, file compatibility between EO on different platforms may be broken. */ -class eoRng : public eoObject, public eoPersistent +class eoRng : public eoObject, public eoPersistent { public : - /** - ctor takes a random seed; if you want another seed, use reseed - @see reseed to see why the parameter to initialize is doubled - */ - eoRng(uint32_t s) : state(0), next(0), left(-1), cached(false), N(624), M(397), K(0x9908B0DFU) { - state = new uint32_t[N+1]; - initialize(2*s); - } + /** Constructor - ~eoRng(void) - { - delete [] state; - } + @param s Random seed; if you want another seed, use reseed. - /** - * Re-initializes the Random Number Generator. - * WARNING: after Jeroen Eggermont noticed that - * initialize does not differentiate between odd and even numbers, - * the argument to reseed is now doubled before being passed on. - * - * Manually divide the seed by 2 if you want to re-run old runs - * - * MS. 5 Oct. 2001 - */ - void reseed(uint32_t s) - { - initialize(2*s); - } + @see reseed for details on usage of the seeding value. + */ + eoRng(uint32_t s) + : state(0), next(0), left(-1), cached(false), N(624), M(397), K(0x9908B0DFU) + { + state = new uint32_t[N+1]; + initialize(2*s); + } - /** - Re-initializes the Random Number Generator - old version - */ - void oldReseed(uint32_t s) - { - initialize(s); - } + ~eoRng(void) + { + delete [] state; + } + + /** Re-initializes the Random Number Generator. + + WARNING: Jeroen Eggermont noticed that initialize does + not differentiate between odd and even numbers, therefore the argument to + reseed is now doubled before being passed on. + + Manually divide the seed by 2 if you want to re-run old runs + + @version MS. 5 Oct. 2001 + */ + void reseed(uint32_t s) + { + initialize(2*s); + } + + /** Re-initializes the Random Number Generator + + This is the traditional seeding procedure. + + @see reseed for details on usage of the seeding value. + + @version old version + */ + void oldReseed(uint32_t s) + { + initialize(s); + } /** uniform(m = 1.0) returns a random double in the range [0, m) @@ -218,44 +193,52 @@ public : */ uint32_t rand(); - /** - rand_max() the maximum returned by rand() - */ - uint32_t rand_max(void) const { return (uint32_t) 0xffffffff; } + /** + rand_max() the maximum returned by rand() + */ + uint32_t rand_max(void) const { return uint32_t(0xffffffff); } - /** - roulette_wheel(vec, total = 0) does a roulette wheel selection - on the input std::vector vec. If the total is not supplied, it is - calculated. It returns an integer denoting the selected argument. - */ - template - int roulette_wheel(const std::vector& vec, T total = 0) - { - if (total == 0) - { // count - for (unsigned i = 0; i < vec.size(); ++i) - total += vec[i]; - } + /** + roulette_wheel(vec, total = 0) does a roulette wheel selection + on the input std::vector vec. If the total is not supplied, it is + calculated. It returns an integer denoting the selected argument. + */ + template + int roulette_wheel(const std::vector& vec, TYPE total = 0) + { + if (total == 0) + { // count + for (unsigned i = 0; i < vec.size(); ++i) + total += vec[i]; + } + double fortune = uniform() * total; + int i = 0; + while (fortune > 0) + { + fortune -= vec[i++]; + } + return --i; + }; - double fortune = uniform() * total; - int i = 0; - while (fortune > 0) - { - fortune -= vec[i++]; - } + /** Randomly select element from vector. - return --i; - } + @return Uniformly chosen element from the vector. + */ + template + const TYPE& choice(const std::vector& vec) const + { return vec[random(vec.size())]; } - /** - * choice(vec), returns a uniformly chosen element from the vector - */ - template - const T& choice(const std::vector& vec) { return vec[random(vec.size())]; } - template - T& choice(std::vector& vec) { return vec[random(vec.size())]; } + /** Randomly select element from vector. + + @overload + + @return Uniformly chosen element from the vector. + */ + template + TYPE& choice(std::vector& vec) + { return vec[random(vec.size())]; } /// void printOn(std::ostream& _os) const @@ -474,3 +457,10 @@ namespace eo { #endif + +// Local Variables: +// coding: iso-8859-1 +// mode: C++ +// c-file-style: "Stroustrup" +// fill-column: 80 +// End: diff --git a/eo/src/utils/eoRealBounds.cpp b/eo/src/utils/eoRealBounds.cpp index 0b3faaac7..c090edb01 100644 --- a/eo/src/utils/eoRealBounds.cpp +++ b/eo/src/utils/eoRealBounds.cpp @@ -8,11 +8,7 @@ #endif #include -#ifdef HAVE_SSTREAM #include -#else -#include -#endif #include "eoRealBounds.h" #include "eoRealVectorBounds.h" @@ -37,11 +33,7 @@ bool remove_leading(std::string & _s, const std::string _delim) double read_double(std::string _s) { -#ifdef HAVE_SSTREAM std::istringstream is(_s); -#else - std::istrstream is(_s.c_str()); -#endif double r; is >> r; return r; @@ -49,11 +41,7 @@ double read_double(std::string _s) long int read_int(std::string _s) { -#ifdef HAVE_SSTREAM std::istringstream is(_s); -#else - std::istrstream is(_s.c_str()); -#endif long int i; is >> i; return i; diff --git a/eo/src/utils/eoState.cpp b/eo/src/utils/eoState.cpp index 09447339d..c5997fe19 100644 --- a/eo/src/utils/eoState.cpp +++ b/eo/src/utils/eoState.cpp @@ -8,11 +8,7 @@ #include #include -#ifdef HAVE_SSTREAM #include -#else -#include -#endif #include "eoState.h" #include "eoObject.h" @@ -32,7 +28,7 @@ void removeComment(string& str, string comment) } } -bool is_section(const string& str, string& name) +bool is_section(const string& str, string& name) { string::size_type pos = str.find("\\section{"); @@ -58,12 +54,12 @@ eoState::~eoState(void) delete ownedObjects[i]; } } - + void eoState::registerObject(eoPersistent& registrant) { string name = createObjectName(dynamic_cast(®istrant)); - - pair res = objectMap.insert(make_pair(name, ®istrant)); + + pair res = objectMap.insert(make_pair(name, ®istrant)); if (res.second == true) { @@ -84,7 +80,7 @@ void eoState::load(const string& _filename) string str = "Could not open file " + _filename; throw runtime_error(str); } - + load(is); } @@ -94,7 +90,7 @@ void eoState::load(std::istream& is) string name; getline(is, str); - + if (is.fail()) { string str = "Error while reading stream"; @@ -118,7 +114,7 @@ void eoState::load(std::istream& is) } else { - + eoPersistent* object = it->second; // now we have the object, get lines, remove comments etc. @@ -135,11 +131,7 @@ void eoState::load(std::istream& is) removeComment(str, getCommentString()); fullstring += str + "\n"; } -#ifdef HAVE_SSTREAM istringstream the_stream(fullstring); -#else - istrstream the_stream(fullstring.c_str(), fullstring.size()); -#endif object->readFrom(the_stream); } } @@ -180,32 +172,21 @@ string eoState::createObjectName(eoObject* obj) { if (obj == 0) { -#ifdef HAVE_SSTREAM ostringstream os; -#else - ostrstream os; -#endif os << objectMap.size(); return os.str(); } // else - + string name = obj->className(); ObjectMap::const_iterator it = objectMap.find(name); unsigned count = 1; while (it != objectMap.end()) { -#ifdef HAVE_SSTREAM ostringstream os; os << obj->className().c_str() << count++; -#else - ostrstream os; - os << obj->className().c_str() << count++ << ends; -#endif - name = os.str(); - it = objectMap.find(name); } diff --git a/eo/src/utils/eoUpdater.cpp b/eo/src/utils/eoUpdater.cpp index b39ff0732..fa6ca109c 100644 --- a/eo/src/utils/eoUpdater.cpp +++ b/eo/src/utils/eoUpdater.cpp @@ -6,11 +6,7 @@ #include #endif -#ifdef HAVE_SSTREAM #include -#else -#include -#endif #include #include @@ -24,28 +20,17 @@ void eoTimedStateSaver::operator()(void) if (now >= last_time + interval) { last_time = now; - -#ifdef HAVE_SSTREAM ostringstream os; os << prefix << (now - first_time) << '.' << extension; -#else - ostrstream os; - os << prefix << (now - first_time) << '.' << extension << ends; -#endif state.save(os.str()); } } void eoCountedStateSaver::doItNow(void) { -#ifdef HAVE_SSTREAM ostringstream os; - os << prefix << counter << '.' << extension; -#else - ostrstream os; - os << prefix << counter << '.' << extension << ends; -#endif - state.save(os.str()); + os << prefix << counter << '.' << extension; + state.save(os.str()); } void eoCountedStateSaver::operator()(void) diff --git a/eo/test/t-eoGenOp.cpp b/eo/test/t-eoGenOp.cpp index b57703997..45a790db8 100644 --- a/eo/test/t-eoGenOp.cpp +++ b/eo/test/t-eoGenOp.cpp @@ -26,6 +26,9 @@ /** test program for the general operator - millenium version! * uses dummy individuals */ + +#include + #include #include #include @@ -176,13 +179,7 @@ void init(eoPop & _pop, unsigned _pSize) } for (unsigned i=0; i<_pSize; i++) { -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - char s[255]; - std::ostrstream os(s, 254); -#endif - os << i << std::ends; _pop[i] = Dummy(os.str()); _pop[i].fitness(i); diff --git a/eo/test/t-eoSharing.cpp b/eo/test/t-eoSharing.cpp index 031276af8..a68820bed 100644 --- a/eo/test/t-eoSharing.cpp +++ b/eo/test/t-eoSharing.cpp @@ -8,13 +8,9 @@ #endif #include +#include #include #include -#ifdef HAVE_SSTREAM -#include -#else -#include -#endif // general #include diff --git a/eo/test/t-eoVector.cpp b/eo/test/t-eoVector.cpp index 87c6cdda8..768178c3f 100644 --- a/eo/test/t-eoVector.cpp +++ b/eo/test/t-eoVector.cpp @@ -30,11 +30,6 @@ #include #include -#ifdef HAVE_SSTREAM -#include -#else -#include -#endif #include #include // eoVector diff --git a/eo/test/t-eobin.cpp b/eo/test/t-eobin.cpp index 8342b3411..7c27e001f 100644 --- a/eo/test/t-eobin.cpp +++ b/eo/test/t-eobin.cpp @@ -27,11 +27,7 @@ #endif #include // std::cout -#ifdef HAVE_SSTREAM #include -#else -#include // ostrstream, istrstream -#endif #include // general EO #include // bitstring representation & operators @@ -65,23 +61,14 @@ void main_function() std::cout << "chrom: " << chrom << std::endl << "chrom2: " << chrom2 << std::endl; -#ifdef HAVE_SSTREAM std::ostringstream os; -#else - char buff[1024]; - std::ostrstream os(buff, 1024); -#endif os << chrom; -#ifdef HAVE_SSTREAM std::istringstream is(os.str()); -#else - std::istrstream is(os.str()); -#endif is >> chrom2; chrom.fitness(binary_value(chrom2)); - + std::cout << "\nTesting reading, writing\n"; std::cout << "chrom: " << chrom << "\nchrom2: " << chrom2 << '\n'; - + std::fill(chrom.begin(), chrom.end(), false); std::cout << "--------------------------------------------------" << std::endl << "eoMonOp's aplied to .......... " << chrom << std::endl; @@ -95,7 +82,7 @@ void main_function() eoOneBitFlip bitflip; bitflip(chrom); chrom.fitness(binary_value(chrom)); std::cout << "after eoBitFlip .............. " << chrom << std::endl; - + eoBitMutation mutation(0.5); mutation(chrom); chrom.fitness(binary_value(chrom)); std::cout << "after eoBinMutation(0.5) ..... " << chrom << std::endl; @@ -115,15 +102,15 @@ void main_function() std::fill(chrom.begin(), chrom.end(), false); chrom.fitness(binary_value(chrom)); std::fill(chrom2.begin(), chrom2.end(), true); chrom2.fitness(binary_value(chrom2)); std::cout << "--------------------------------------------------" - << std::endl << "eoBinOp's aplied to ... " + << std::endl << "eoBinOp's aplied to ... " << chrom << " " << chrom2 << std::endl; eo1PtBitXover xover; std::fill(chrom.begin(), chrom.end(), false); std::fill(chrom2.begin(), chrom2.end(), true); - xover(chrom, chrom2); + xover(chrom, chrom2); chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2)); - std::cout << "eoBinCrossover ........ " << chrom << " " << chrom2 << std::endl; + std::cout << "eoBinCrossover ........ " << chrom << " " << chrom2 << std::endl; for (i = 1; i < SIZE; i++) { @@ -132,8 +119,8 @@ void main_function() std::fill(chrom2.begin(), chrom2.end(), true); nxover(chrom, chrom2); chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2)); - std::cout << "eoBinNxOver(" << i << ") ........ " - << chrom << " " << chrom2 << std::endl; + std::cout << "eoBinNxOver(" << i << ") ........ " + << chrom << " " << chrom2 << std::endl; } for (i = 1; i < SIZE / 2; i++) @@ -144,8 +131,8 @@ void main_function() std::fill(chrom2.begin(), chrom2.end(), true); gxover(chrom, chrom2); chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2)); - std::cout << "eoBinGxOver(" << i << ", " << j << ") ..... " - << chrom << " " << chrom2 << std::endl; + std::cout << "eoBinGxOver(" << i << ", " << j << ") ..... " + << chrom << " " << chrom2 << std::endl; } // test SGA algorithm @@ -206,7 +193,7 @@ void main_function() //----------------------------------------------------------------------------- // For MSVC memory lead detection #ifdef _MSC_VER -#include +#include #endif int main() diff --git a/eo/tutorial/Lesson1/FirstBitGA.cpp b/eo/tutorial/Lesson1/FirstBitGA.cpp index f12344b71..ac53607ae 100644 --- a/eo/tutorial/Lesson1/FirstBitGA.cpp +++ b/eo/tutorial/Lesson1/FirstBitGA.cpp @@ -10,16 +10,8 @@ #include #endif -// standard includes -#include // runtime_error -#include // cout -#ifdef HAVE_SSTREAM -#include -#else -#include // ostrstream, istrstream -#endif - -// the general include for eo +#include +#include #include #include @@ -27,7 +19,7 @@ // Use functions from namespace std using namespace std; -// REPRESENTATION +// REPRESENTATION //----------------------------------------------------------------------------- // define your individuals typedef eoBit Indi; // A bitstring with fitness double @@ -63,7 +55,7 @@ void main_function(int argc, char **argv) ////////////////////////// // Random seed ////////////////////////// - //reproducible random seed: if you don't change SEED above, + //reproducible random seed: if you don't change SEED above, // you'll aways get the same result, NOT a random run rng.reseed(SEED); @@ -136,21 +128,21 @@ void main_function(int argc, char **argv) ///////////////////////////////////// // stop after MAX_GEN generations eoGenContinue continuator(MAX_GEN); - + // GENERATION ///////////////////////////////////////// // the algorithm //////////////////////////////////////// // standard Generational GA requires as parameters // selection, evaluation, crossover and mutation, stopping criterion - - eoSGA gga(select, xover, CROSS_RATE, mutation, MUT_RATE, + + eoSGA gga(select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator); // Apply algo to pop - that's it! gga(pop); - + // OUTPUT // Print (sorted) intial population pop.sort(); diff --git a/eo/tutorial/Lesson1/FirstRealGA.cpp b/eo/tutorial/Lesson1/FirstRealGA.cpp index 392aff4aa..09db59e61 100644 --- a/eo/tutorial/Lesson1/FirstRealGA.cpp +++ b/eo/tutorial/Lesson1/FirstRealGA.cpp @@ -9,16 +9,9 @@ #include #endif -// standard includes -#include // runtime_error -#include // cout -#ifdef HAVE_SSTREAM +#include +#include #include -#else -#include // ostrstream, istrstream -#endif - -// the general include for eo #include #include @@ -34,7 +27,7 @@ using namespace std; // EVAL //----------------------------------------------------------------------------- // a simple fitness function that computes the euclidian norm of a real vector -// @param _indi A real-valued individual +// @param _indi A real-valued individual double real_value(const Indi & _indi) { @@ -62,7 +55,7 @@ void main_function(int argc, char **argv) ////////////////////////// // Random seed ////////////////////////// - //reproducible random seed: if you don't change SEED above, + //reproducible random seed: if you don't change SEED above, // you'll aways get the same result, NOT a random run rng.reseed(SEED); @@ -121,7 +114,7 @@ void main_function(int argc, char **argv) eoSegmentCrossover xover; // MUTATION // offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon] - eoUniformMutation mutation(EPSILON); + eoUniformMutation mutation(EPSILON); // STOP // CHECKPOINT @@ -130,21 +123,21 @@ void main_function(int argc, char **argv) ///////////////////////////////////// // stop after MAX_GEN generations eoGenContinue continuator(MAX_GEN); - + // GENERATION ///////////////////////////////////////// // the algorithm //////////////////////////////////////// // standard Generational GA requires // selection, evaluation, crossover and mutation, stopping criterion - - eoSGA gga(select, xover, CROSS_RATE, mutation, MUT_RATE, + + eoSGA gga(select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator); // Apply algo to pop - that's it! gga(pop); - + // OUTPUT // Print (sorted) intial population pop.sort(); diff --git a/eo/tutorial/Lesson1/exercise1.3.cpp b/eo/tutorial/Lesson1/exercise1.3.cpp index c7bf389a0..abd62a0b9 100644 --- a/eo/tutorial/Lesson1/exercise1.3.cpp +++ b/eo/tutorial/Lesson1/exercise1.3.cpp @@ -12,11 +12,6 @@ // standard includes #include #include -#ifdef HAVE_SSTREAM -#include -#else -#include -#endif // the general include for eo #include @@ -57,7 +52,7 @@ void main_function(int argc, char **argv) ////////////////////////// // Random seed ////////////////////////// - //reproducible random seed: if you don't change SEED above, + //reproducible random seed: if you don't change SEED above, // you'll aways get the same result, NOT a random run rng.reseed(SEED); @@ -96,10 +91,10 @@ void main_function(int argc, char **argv) // selection and replacement //////////////////////////////////// - // solution solution solution: uncomment one of the following, + // solution solution solution: uncomment one of the following, // comment out the eoDetTournament lines - // The well-known roulette + // The well-known roulette // eoProportionalSelect select; // could also use stochastic binary tournament selection @@ -121,7 +116,7 @@ void main_function(int argc, char **argv) ///////////////////////////////////// // stop after MAX_GEN generations eoGenContinue continuator(MAX_GEN); - + ////////////////////////////////////// // The variation operators @@ -136,14 +131,14 @@ void main_function(int argc, char **argv) //////////////////////////////////////// // standard Generational GA requires as parameters // selection, evaluation, crossover and mutation, stopping criterion - - eoSGA gga(select, xover, CROSS_RATE, mutation, MUT_RATE, + + eoSGA gga(select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator); // Apply algo to pop - that's it! gga(pop); - + // Print (sorted) intial population pop.sort(); cout << "FINAL Population\n" << pop << endl; diff --git a/eo/tutorial/Lesson2/FirstBitEA.cpp b/eo/tutorial/Lesson2/FirstBitEA.cpp index 928e60196..13da78957 100644 --- a/eo/tutorial/Lesson2/FirstBitEA.cpp +++ b/eo/tutorial/Lesson2/FirstBitEA.cpp @@ -2,7 +2,7 @@ // FirstBitEA.cpp //----------------------------------------------------------------------------- //* -// Still an instance of a VERY simple Bitstring Genetic Algorithm +// Still an instance of a VERY simple Bitstring Genetic Algorithm // (see FirstBitGA.cpp) but now with Breeder - and Combined Ops // //----------------------------------------------------------------------------- @@ -11,13 +11,8 @@ #endif // standard includes -#include // runtime_error +#include // runtime_error #include // cout -#ifdef HAVE_SSTREAM -#include -#else -#include // ostrstream, istrstream -#endif // the general include for eo #include @@ -68,7 +63,7 @@ void main_function(int argc, char **argv) ////////////////////////// // Random seed ////////////////////////// - //reproducible random seed: if you don't change SEED above, + //reproducible random seed: if you don't change SEED above, // you'll aways get the same result, NOT a random run rng.reseed(SEED); @@ -112,9 +107,9 @@ void main_function(int argc, char **argv) eoSelectPerc select(selectOne);// by default rate==1 // REPLACE - // And we now have the full slection/replacement - though with + // And we now have the full slection/replacement - though with // no replacement (== generational replacement) at the moment :-) - eoGenerationalReplacement replace; + eoGenerationalReplacement replace; // OPERATORS ////////////////////////////////////// @@ -136,7 +131,7 @@ void main_function(int argc, char **argv) // standard bit-flip mutation for bitstring eoBitMutation mutationBitFlip(P_MUT_PER_BIT); // mutate exactly 1 bit per individual - eoDetBitFlip mutationOneBit; + eoDetBitFlip mutationOneBit; // Combine them with relative rates eoPropCombinedMonOp mutation(mutationBitFlip, bitFlipRate); mutation.add(mutationOneBit, oneBitRate, true); @@ -159,20 +154,20 @@ void main_function(int argc, char **argv) eoCombinedContinue continuator(genCont); continuator.add(steadyCont); continuator.add(fitCont); - + // GENERATION ///////////////////////////////////////// // the algorithm //////////////////////////////////////// - // Easy EA requires + // Easy EA requires // selection, transformation, eval, replacement, and stopping criterion eoEasyEA gga(continuator, eval, select, transform, replace); // Apply algo to pop - that's it! cout << "\n Here we go\n\n"; gga(pop); - + // OUTPUT // Print (sorted) intial population pop.sort(); diff --git a/eo/tutorial/Lesson2/FirstRealEA.cpp b/eo/tutorial/Lesson2/FirstRealEA.cpp index 91793a1b1..8901c3818 100644 --- a/eo/tutorial/Lesson2/FirstRealEA.cpp +++ b/eo/tutorial/Lesson2/FirstRealEA.cpp @@ -2,7 +2,7 @@ // FirstRealEA.cpp //----------------------------------------------------------------------------- //* -// Still an instance of a VERY simple Real-coded Genetic Algorithm +// Still an instance of a VERY simple Real-coded Genetic Algorithm // (see FirstBitGA.cpp) but now with Breeder - and Combined Ops // //----------------------------------------------------------------------------- @@ -11,13 +11,8 @@ #endif // standard includes -#include // runtime_error +#include // runtime_error #include // cout -#ifdef HAVE_SSTREAM -#include -#else -#include // ostrstream, istrstream -#endif // the general include for eo #include @@ -26,7 +21,7 @@ // REPRESENTATION //----------------------------------------------------------------------------- // define your individuals -typedef eoReal Indi; +typedef eoReal Indi; // Use functions from namespace std using namespace std; @@ -69,7 +64,7 @@ void main_function(int argc, char **argv) ////////////////////////// // Random seed ////////////////////////// - //reproducible random seed: if you don't change SEED above, + //reproducible random seed: if you don't change SEED above, // you'll aways get the same result, NOT a random run rng.reseed(SEED); @@ -90,7 +85,7 @@ void main_function(int argc, char **argv) eoInitFixedLength random(VEC_SIZE, uGen); // Initialization of the population eoPop pop(POP_SIZE, random); - + // and evaluate it in one loop apply(eval, pop); // STL syntax @@ -112,9 +107,9 @@ void main_function(int argc, char **argv) eoSelectPerc select(selectOne);// by default rate==1 // REPLACE - // And we now have the full slection/replacement - though with + // And we now have the full slection/replacement - though with // no replacement (== generational replacement) at the moment :-) - eoGenerationalReplacement replace; + eoGenerationalReplacement replace; // OPERATORS ////////////////////////////////////// @@ -131,11 +126,11 @@ void main_function(int argc, char **argv) // MUTATION // offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon] - eoUniformMutation mutationU(EPSILON); + eoUniformMutation mutationU(EPSILON); // k (=1) coordinates of parents are uniformly modified - eoDetUniformMutation mutationD(EPSILON); + eoDetUniformMutation mutationD(EPSILON); // all coordinates of parents are normally modified (stDev SIGMA) - eoNormalMutation mutationN(SIGMA); + eoNormalMutation mutationN(SIGMA); // Combine them with relative weights eoPropCombinedMonOp mutation(mutationU, uniformMutRate); mutation.add(mutationD, detMutRate); @@ -156,7 +151,7 @@ void main_function(int argc, char **argv) eoCombinedContinue continuator(genCont); continuator.add(steadyCont); continuator.add(fitCont); - + // The operators are encapsulated into an eoTRansform object eoSGATransform transform(xover, P_CROSS, mutation, P_MUT); @@ -165,14 +160,14 @@ void main_function(int argc, char **argv) // the algorithm //////////////////////////////////////// - // Easy EA requires + // Easy EA requires // selection, transformation, eval, replacement, and stopping criterion eoEasyEA gga(continuator, eval, select, transform, replace); // Apply algo to pop - that's it! cout << "\n Here we go\n\n"; gga(pop); - + // OUTPUT // Print (sorted) intial population pop.sort(); diff --git a/eo/tutorial/Lesson2/exercise2.3.cpp b/eo/tutorial/Lesson2/exercise2.3.cpp index 20867a0fc..dc39e5d9f 100644 --- a/eo/tutorial/Lesson2/exercise2.3.cpp +++ b/eo/tutorial/Lesson2/exercise2.3.cpp @@ -2,7 +2,7 @@ // FirstBitEA.cpp //----------------------------------------------------------------------------- //* -// Still an instance of a VERY simple Bitstring Genetic Algorithm +// Still an instance of a VERY simple Bitstring Genetic Algorithm // (see FirstBitGA.cpp) but now with Breeder - and Combined Ops // //----------------------------------------------------------------------------- @@ -11,13 +11,8 @@ #endif // standard includes -#include // runtime_error +#include // runtime_error #include // cout -#ifdef HAVE_SSTREAM -#include -#else -#include // ostrstream, istrstream -#endif // the general include for eo #include @@ -68,7 +63,7 @@ void main_function(int argc, char **argv) ////////////////////////// // Random seed ////////////////////////// - //reproducible random seed: if you don't change SEED above, + //reproducible random seed: if you don't change SEED above, // you'll aways get the same result, NOT a random run rng.reseed(SEED); @@ -108,7 +103,7 @@ void main_function(int argc, char **argv) // SELECT // The robust tournament selection eoDetTournamentSelect selectOne(T_SIZE); // T_SIZE in [2,POP_SIZE] - // solution solution solution solution solution solution solution + // solution solution solution solution solution solution solution // modify the nb offspring / rate in the constructor. 2 ways: // second arg treated as integer eoSelectMany select(selectOne,2, eo_is_an_integer); @@ -116,10 +111,10 @@ void main_function(int argc, char **argv) // eoSelectMany select(selectOne,0.1); // REPLACE - // solution solution solution solution solution solution solution + // solution solution solution solution solution solution solution // eoCommaReplacement keeps the best among offspring // eoPlusReplacement keeps the best among parents + offspring - // eoCommaReplacement replace; + // eoCommaReplacement replace; eoPlusReplacement replace; // OPERATORS @@ -142,7 +137,7 @@ void main_function(int argc, char **argv) // standard bit-flip mutation for bitstring eoBitMutation mutationBitFlip(P_MUT_PER_BIT); // mutate exactly 1 bit per individual - eoDetBitFlip mutationOneBit; + eoDetBitFlip mutationOneBit; // Combine them with relative rates eoPropCombinedMonOp mutation(mutationBitFlip, bitFlipRate); mutation.add(mutationOneBit, oneBitRate, true); @@ -165,19 +160,19 @@ void main_function(int argc, char **argv) eoCombinedContinue continuator(genCont); continuator.add(steadyCont); continuator.add(fitCont); - + // GENERATION ///////////////////////////////////////// // the algorithm //////////////////////////////////////// - // Easy EA requires + // Easy EA requires // selection, transformation, eval, replacement, and stopping criterion eoEasyEA gga(continuator, eval, select, transform, replace); // Apply algo to pop - that's it! gga(pop); - + // OUTPUT // Print (sorted) intial population pop.sort(); diff --git a/eo/tutorial/Lesson3/SecondBitEA.cpp b/eo/tutorial/Lesson3/SecondBitEA.cpp index 0a250c674..0507bc5c3 100644 --- a/eo/tutorial/Lesson3/SecondBitEA.cpp +++ b/eo/tutorial/Lesson3/SecondBitEA.cpp @@ -14,11 +14,6 @@ #include #include // cout #include // runtime_error -#ifdef HAVE_SSTREAM -#include -#else -#include // ostrstream, istrstream -#endif // the general include for eo #include diff --git a/eo/tutorial/Lesson3/SecondRealEA.cpp b/eo/tutorial/Lesson3/SecondRealEA.cpp index 48d50b6ca..2ae69fd81 100644 --- a/eo/tutorial/Lesson3/SecondRealEA.cpp +++ b/eo/tutorial/Lesson3/SecondRealEA.cpp @@ -15,12 +15,7 @@ // standard includes #include #include // cout -#include // runtime_error -#ifdef HAVE_SSTREAM -#include -#else -#include // ostrstream, istrstream -#endif +#include // runtime_error // the general include for eo #include @@ -29,7 +24,7 @@ // REPRESENTATION //----------------------------------------------------------------------------- // define your individuals -typedef eoReal Indi; +typedef eoReal Indi; // Use functions from namespace std using namespace std; @@ -50,18 +45,18 @@ void main_function(int argc, char **argv) //----------------------------------------------------------------------------- // instead of having all values of useful parameters as constants, read them: // either on the command line (--option=value or -o=value) -// or in a parameter file (same syntax, order independent, -// # = usual comment character +// or in a parameter file (same syntax, order independent, +// # = usual comment character // or in the environment (TODO) // First define a parser from the command-line arguments eoParser parser(argc, argv); - + // For each parameter, you can in on single line // define the parameter, read it through the parser, and assign it - + unsigned seed = parser.createParam(unsigned(time(0)), "seed", "Random number seed", 'S').value(); // will be in default section General - + // description of genotype unsigned vecSize = parser.createParam(unsigned(8), "vecSize", "Genotype size",'V', "Representation" ).value(); @@ -72,7 +67,7 @@ void main_function(int argc, char **argv) // init and stop string loadName = parser.createParam(string(""), "Load","A save file to restart from",'L', "Persistence" ).value(); - + unsigned maxGen = parser.createParam(unsigned(100), "maxGen", "Maximum number of generations",'G', "Stopping criterion" ).value(); unsigned minGen = parser.createParam(unsigned(100), "minGen", "Minimum number of generations",'g', "Stopping criterion" ).value(); @@ -91,7 +86,7 @@ void main_function(int argc, char **argv) // internal parameters for the mutations double EPSILON = parser.createParam(double(0.01), "EPSILON", "Width for uniform mutation", '\0', "Genetic Operators" ).value(); - + double SIGMA = parser.createParam(double(0.3), "SIGMA", "Sigma for normal mutation", '\0', "Genetic Operators" ).value(); // relative rates for mutations @@ -101,7 +96,7 @@ void main_function(int argc, char **argv) double normalMutRate = parser.createParam(double(1), "normalMutRate", "Relative rate for normal mutation", '\0', "Genetic Operators" ).value(); - // the name of the "status" file where all actual parameter values will be saved + // the name of the "status" file where all actual parameter values will be saved string str_status = parser.ProgramName() + ".status"; // default value string statusName = parser.createParam(str_status, "status","Status file",'S', "Persistence" ).value(); @@ -142,11 +137,11 @@ void main_function(int argc, char **argv) // eventually with different parameters inState.registerObject(rng); inState.registerObject(pop); - + if (loadName != "") { inState.load(loadName); // load the pop and the rng - // the fitness is read in the file: + // the fitness is read in the file: // do only evaluate the pop if the fitness has changed } else @@ -156,10 +151,10 @@ void main_function(int argc, char **argv) // based on boolean_generator class (see utils/rnd_generator.h) eoUniformGenerator uGen(-1.0, 1.0); eoInitFixedLength random(vecSize, uGen); - + // Init pop from the randomizer: need to use the append function - pop.append(popSize, random); - // and evaluate pop (STL syntax) + pop.append(popSize, random); + // and evaluate pop (STL syntax) apply(eval, pop); } // end of initializatio of the population @@ -181,9 +176,9 @@ void main_function(int argc, char **argv) eoSelectPerc select(selectOne);// by default rate==1 // REPLACE - // And we now have the full slection/replacement - though with + // And we now have the full slection/replacement - though with // no replacement (== generational replacement) at the moment :-) - eoGenerationalReplacement replace; + eoGenerationalReplacement replace; // OPERATORS ////////////////////////////////////// @@ -200,11 +195,11 @@ void main_function(int argc, char **argv) // MUTATION // offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon] - eoUniformMutation mutationU(EPSILON); + eoUniformMutation mutationU(EPSILON); // k (=1) coordinates of parents are uniformly modified - eoDetUniformMutation mutationD(EPSILON); + eoDetUniformMutation mutationD(EPSILON); // all coordinates of parents are normally modified (stDev SIGMA) - eoNormalMutation mutationN(SIGMA); + eoNormalMutation mutationN(SIGMA); // Combine them with relative weights eoPropCombinedMonOp mutation(mutationU, uniformMutRate); mutation.add(mutationD, detMutRate); @@ -223,27 +218,27 @@ void main_function(int argc, char **argv) eoCombinedContinue continuator(genCont); continuator.add(steadyCont); continuator.add(fitCont); - - + + // CHECKPOINT - // but now you want to make many different things every generation + // but now you want to make many different things every generation // (e.g. statistics, plots, ...). // the class eoCheckPoint is dedicated to just that: - // Declare a checkpoint (from a continuator: an eoCheckPoint + // Declare a checkpoint (from a continuator: an eoCheckPoint // IS AN eoContinue and will be called in the loop of all algorithms) eoCheckPoint checkpoint(continuator); - + // Create a counter parameter eoValueParam generationCounter(0, "Gen."); - - // Create an incrementor (sub-class of eoUpdater). Note that the - // parameter's value is passed by reference, + + // Create an incrementor (sub-class of eoUpdater). Note that the + // parameter's value is passed by reference, // so every time the incrementer is updated (every generation), // the data in generationCounter will change. eoIncrementor increment(generationCounter.value()); - // Add it to the checkpoint, + // Add it to the checkpoint, // so the counter is updated (here, incremented) every generation checkpoint.add(increment); @@ -259,11 +254,11 @@ void main_function(int argc, char **argv) // The Stdout monitor will print parameters to the screen ... eoStdoutMonitor monitor(false); - + // when called by the checkpoint (i.e. at every generation) 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(eval); // because now eval is an eoEvalFuncCounter! monitor.add(bestStat); @@ -271,7 +266,7 @@ void main_function(int argc, char **argv) // A file monitor: will print parameters to ... a File, yes, you got it! eoFileMonitor fileMonitor("stats.xg", " "); - + // the checkpoint mechanism can handle multiple monitors checkpoint.add(fileMonitor); @@ -289,9 +284,9 @@ void main_function(int argc, char **argv) // and feed the state to state savers // save state every 100th generation - eoCountedStateSaver stateSaver1(20, outState, "generation"); - // save state every 1 seconds - eoTimedStateSaver stateSaver2(1, outState, "time"); + eoCountedStateSaver stateSaver1(20, outState, "generation"); + // save state every 1 seconds + eoTimedStateSaver stateSaver2(1, outState, "time"); // Don't forget to add the two savers to the checkpoint checkpoint.add(stateSaver1); @@ -303,13 +298,13 @@ void main_function(int argc, char **argv) // the algorithm //////////////////////////////////////// - // Easy EA requires + // Easy EA requires // stopping criterion, eval, selection, transformation, replacement eoEasyEA gga(checkpoint, eval, select, transform, replace); // Apply algo to pop - that's it! gga(pop); - + // OUTPUT // Print (sorted) intial population pop.sort(); diff --git a/eo/tutorial/Lesson3/exercise3.1.cpp b/eo/tutorial/Lesson3/exercise3.1.cpp index 55a89a249..50531e094 100644 --- a/eo/tutorial/Lesson3/exercise3.1.cpp +++ b/eo/tutorial/Lesson3/exercise3.1.cpp @@ -14,11 +14,6 @@ #include #include // cout #include // runtime_error -#ifdef HAVE_SSTREAM -#include -#else -#include // ostrstream, istrstream -#endif // the general include for eo #include diff --git a/eo/tutorial/Lesson4/Makefile.am b/eo/tutorial/Lesson4/Makefile.am index 168b4a42b..7b1c2dcce 100644 --- a/eo/tutorial/Lesson4/Makefile.am +++ b/eo/tutorial/Lesson4/Makefile.am @@ -1,4 +1,3 @@ - noinst_PROGRAMS = BitEA RealEA ESEA @@ -15,9 +14,13 @@ noinst_HEADERS = binary_value.h \ extra_DIST = Makefile.simple -LDADD = -L$(top_builddir)/src -L$(top_builddir)/src/es \ - -L$(top_builddir)/src/ga -L$(top_builddir)/src/utils +AM_CXXFLAGS = -I$(top_srcdir)/src -LIBS = -lga -les -leoutils -leo +LIBEO = $(top_builddir)/src/libeo.a +LIBES = $(top_builddir)/src/es/libes.a +LIBGA = $(top_builddir)/src/ga/libga.a +LIBUTILS = $(top_builddir)/src/utils/libeoutils.a -INCLUDES = -I$(top_srcdir)/src +DEPS = $(LIBEO) $(LIBUTILS) $(LIBES) $(LIBGA) + +LIBS = $(LIBES) $(LIBGA) $(LIBEO) $(LIBUTILS) diff --git a/eo/tutorial/Lesson5/Makefile.am b/eo/tutorial/Lesson5/Makefile.am index 65027f15a..110cfb087 100644 --- a/eo/tutorial/Lesson5/Makefile.am +++ b/eo/tutorial/Lesson5/Makefile.am @@ -17,6 +17,12 @@ noinst_HEADERS = eoOneMax.h \ extra_DIST = Makefile.simple AM_CXXFLAGS = -I$(top_srcdir)/src -LDADD = -L$(top_builddir)/src -L$(top_builddir)/src/ga -L$(top_builddir)/src/utils -LIBS = -lga -leoutils -leo +LIBEO = $(top_builddir)/src/libeo.a +LIBES = $(top_builddir)/src/es/libes.a +LIBGA = $(top_builddir)/src/ga/libga.a +LIBUTILS = $(top_builddir)/src/utils/libeoutils.a + +DEPS = $(LIBEO) $(LIBUTILS) $(LIBES) $(LIBGA) + +LIBS = $(LIBES) $(LIBGA) $(LIBEO) $(LIBUTILS) diff --git a/eo/tutorial/Lesson5/eoOneMaxMutation.h b/eo/tutorial/Lesson5/eoOneMaxMutation.h index a5e4df2c4..f98e74824 100644 --- a/eo/tutorial/Lesson5/eoOneMaxMutation.h +++ b/eo/tutorial/Lesson5/eoOneMaxMutation.h @@ -14,14 +14,14 @@ Template for simple mutation operators #include -/** +/** * Always write a comment in this format before class definition * if you want the class to be documented by Doxygen * * THere is NO ASSUMPTION on the class GenoypeT. * In particular, it does not need to derive from EO */ -template +template class eoOneMaxMutation: public eoMonOp { public: @@ -30,7 +30,7 @@ public: */ // START eventually add or modify the anyVariable argument eoOneMaxMutation() - // eoOneMaxMutation( varType _anyVariable) : anyVariable(_anyVariable) + // eoOneMaxMutation( varType _anyVariable) : anyVariable(_anyVariable) // END eventually add or modify the anyVariable argument { // START Code of Ctor of an eoOneMaxEvalFunc object @@ -44,7 +44,7 @@ public: * modifies the parent * @param _genotype The parent genotype (will be modified) */ - bool operator()(GenotypeT & _genotype) + bool operator()(GenotypeT & _genotype) { bool isModified; // START code for mutation of the _genotype object