From 835a14e8ff8a1951eb4ef5764418b5e4ac5b02d2 Mon Sep 17 00:00:00 2001 From: evomarc Date: Mon, 27 Dec 2004 08:01:29 +0000 Subject: [PATCH] Modified the way to set the initial values of the sigmas (and stdevs) so that they can be scaled to the range of each variable (by adding a "%" after the value in the parameter file) See also the examples in tutorial/Lesson4 --- eo/src/es/eoEsChromInit.h | 71 ++++++++++++++++++++++------------ eo/src/es/make_genotype_real.h | 33 ++++++++++++++-- 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/eo/src/es/eoEsChromInit.h b/eo/src/es/eoEsChromInit.h index a9b40e41..70e89772 100644 --- a/eo/src/es/eoEsChromInit.h +++ b/eo/src/es/eoEsChromInit.h @@ -58,8 +58,41 @@ class eoEsChromInit : public eoRealInitBounded public : typedef typename EOT::Fitness FitT; - eoEsChromInit(eoRealVectorBounds& _bounds, double _sigma = 0.3) : - eoRealInitBounded(_bounds), sigma(_sigma) {} + /** Ctor: @param + * eoRealVectorBounds& _bounds : bounds for uniform initialization + * double _sigma : initial value for the stddev + * bool _to_scale : wether sigma should be multiplied by the range of each variable + * added December 2004 - MS (together with the whole comment :-) + */ + eoEsChromInit(eoRealVectorBounds& _bounds, double _sigma = 0.3, bool _to_scale=false) : + eoRealInitBounded(_bounds) + { + // a bit of pre-computations, to ave time later (even if some are useless) + + // first, the case of one unique sigma + if (_to_scale) // sigma is scaled by the average range (if that means anything!) + { + double scaleUnique = 0; + for (unsigned i=0; i& result)// nothing to do here ... + void create_self_adapt(eoReal&)// nothing to do here ... { } // Adaptive mutation through a unique sigma void create_self_adapt(eoEsSimple& result) { - // sigma is scaled by the average range (if that means anything!) - result.stdev = sigma; + // pre-computed in the Ctor + result.stdev = uniqueSigma; } // Adaptive mutation through a std::vector of sigmas void create_self_adapt(eoEsStdev& result) { - unsigned theSize = eoRealInitBounded::size(); - result.stdevs.resize(theSize); - for (unsigned i = 0; i < theSize; ++i) - { - // should we scale sigmas to the corresponding object variable range? - result.stdevs[i] = sigma; - } + result.stdevs = lesSigmas; } // Adaptive mutation through a whole correlation matrix void create_self_adapt(eoEsFull& result) { - unsigned i, theSize = eoRealInitBounded::size(); - - result.stdevs.resize(theSize); - for (i = 0; i < theSize; ++i) - { - // should we scale sigmas to the corresponding object variable range? - result.stdevs[i] = sigma; - } - + // first the stdevs (pre-computed in the Ctor) + result.stdevs = lesSigmas; + unsigned int theSize = size(); // nb of rotation angles: N*(N-1)/2 (in general!) result.correlations.resize(theSize*(theSize - 1) / 2); - for (i = 0; i < result.correlations.size(); ++i) + for (unsigned i=0; i lesSigmas; // initial values in case of a vector fo sigmas }; #endif diff --git a/eo/src/es/make_genotype_real.h b/eo/src/es/make_genotype_real.h index 948e6962..9cf10301 100644 --- a/eo/src/es/make_genotype_real.h +++ b/eo/src/es/make_genotype_real.h @@ -27,6 +27,12 @@ #ifndef _make_genotype_h #define _make_genotype_h +#ifdef HAVE_SSTREAM +#include +#else +#include +#endif + #include #include #include @@ -72,14 +78,33 @@ eoEsChromInit & do_make_genotype(eoParser& _parser, eoState& _state, EOT) // now some initial value for sigmas - even if useless? // shoudl be used in Normal mutation - eoValueParam& sigmaParam = _parser.getORcreateParam(0.3, "sigmaInit", "Initial value for Sigma(s)", 's',"Genotype Initialization"); + std::string & sigmaString = _parser.getORcreateParam(std::string("0.3"), "sigmaInit", + "Initial value for Sigmas (with a '%' -> scaled by the range of each variable)", + 's',"Genotype Initialization").value(); + + // check for % + bool to_scale = false; // == no % + size_t pos = sigmaString.find('%'); + if (pos < sigmaString.size()) // found a % + { + to_scale = true; + sigmaString.resize(pos); // get rid of % + } + +#ifdef HAVE_SSTREAM + std::istringstream is(sigmaString); +#else + std::istrstream is(sigmaString.c_str()); +#endif + double sigma; + is >> sigma; // minimum check - if ( (sigmaParam.value() < 0) ) - throw std::runtime_error("Invalid sigma"); + if ( (sigma < 0) ) + throw std::runtime_error("Negative sigma in make_genotype"); eoEsChromInit * init = - new eoEsChromInit(boundsParam.value(), sigmaParam.value()); + new eoEsChromInit(boundsParam.value(), sigma, to_scale); // satore in state _state.storeFunctor(init); return *init;