From d7b935b6455d1d70d17bb992db8663f43d6fe2ab Mon Sep 17 00:00:00 2001 From: kuepper Date: Thu, 16 Nov 2006 12:35:46 +0000 Subject: [PATCH] * make_genotype_real.h (eoEsChromInit): Rewrite vecSigmaInit-handling: If sigmaInit is relative (%), do not read vecSigmaInit. Otherwise always use vecSigmaInit with default all values of sigmaInit. * eoParser.h (eoParser::getORcreateParam): Make this a real if-then-else clause around ptParam (found or not). * eoParam.h (eoValueParam::setValue): Document. (eoValueParam >::setValue): Allow delimiters ',' and ';'. A plain ' ' does not work, as it is not correctly read by eoParser::readFrom. --- eo/ChangeLog | 8 +++++ eo/src/es/ChangeLog | 15 +++++++++ eo/src/es/eoEsChromInit.h | 8 +++-- eo/src/es/make_genotype_real.h | 61 +++++++++++++--------------------- eo/src/utils/ChangeLog | 15 +++++++++ eo/src/utils/eoParam.h | 22 +++++++++++- eo/src/utils/eoParser.cpp | 1 - eo/src/utils/eoParser.h | 11 +++--- 8 files changed, 95 insertions(+), 46 deletions(-) create mode 100644 eo/src/es/ChangeLog create mode 100644 eo/src/utils/ChangeLog diff --git a/eo/ChangeLog b/eo/ChangeLog index e69de29bb..d40835e6b 100644 --- a/eo/ChangeLog +++ b/eo/ChangeLog @@ -0,0 +1,8 @@ + + + + * Local Variables: + * coding: iso-8859-1 + * mode: flyspell + * fill-column: 80 + * End: diff --git a/eo/src/es/ChangeLog b/eo/src/es/ChangeLog new file mode 100644 index 000000000..e9b31f6ae --- /dev/null +++ b/eo/src/es/ChangeLog @@ -0,0 +1,15 @@ +2006-11-16 Jochen Küpper + + * make_genotype_real.h (eoEsChromInit): Rewrite vecSigmaInit-handling: + If sigmaInit is relative (%), do not read vecSigmaInit. Otherwise + always use vecSigmaInit with default all values of sigmaInit. + + * eoEsChromInit.h (class eoEsChromInit): Take const-reference to sigma-vector. + + + + * Local Variables: + * coding: iso-8859-1 + * mode: flyspell + * fill-column: 80 + * End: diff --git a/eo/src/es/eoEsChromInit.h b/eo/src/es/eoEsChromInit.h index aec08438b..a64dfe376 100644 --- a/eo/src/es/eoEsChromInit.h +++ b/eo/src/es/eoEsChromInit.h @@ -24,6 +24,7 @@ Contact: http://eodev.sourceforge.net #ifndef _eoEsChromInit_H #define _eoEsChromInit_H +#include #include #include @@ -106,9 +107,12 @@ public: @param _bounds bounds for uniform initialization @param _sigma initial value for the stddev */ - eoEsChromInit(eoRealVectorBounds& _bounds, std::vector _vecSigma) + eoEsChromInit(eoRealVectorBounds& _bounds, const std::vector& _vecSigma) : eoRealInitBounded(_bounds), uniqueSigma(_vecSigma[0]), vecSigma(_vecSigma) - {} + { + assert(_bounds.size() == size()); + assert(_vecSigma.size() == size()); + } void operator()(EOT& _eo) diff --git a/eo/src/es/make_genotype_real.h b/eo/src/es/make_genotype_real.h index a2077793e..95d372c46 100644 --- a/eo/src/es/make_genotype_real.h +++ b/eo/src/es/make_genotype_real.h @@ -53,7 +53,7 @@ It returns an eoInit tha can later be used to initialize the population It uses a parser (to get user parameters) and a state (to store the memory) the last argument is to disambiguate the call upon different instanciations. -WARNING: that last argument will generally be the result of calling the default +@warning: that last argument will generally be the result of calling the default ctor of EOT, resulting in most cases in an EOT that is ***not properly initialized*** */ @@ -68,7 +68,7 @@ eoEsChromInit & do_make_genotype(eoParser& _parser, eoState& _state, EOT) eoValueParam& vecSize = _parser.getORcreateParam(unsigned(10), "vecSize", "The number of variables ", - 'n',"Genotype Initialization"); + 'n', "Genotype Initialization"); // to build an eoReal Initializer, we need bounds: [-1,1] by default eoValueParam& boundsParam = _parser.getORcreateParam(eoRealVectorBounds(vecSize.value(), -1, 1), @@ -80,43 +80,30 @@ eoEsChromInit & do_make_genotype(eoParser& _parser, eoState& _state, EOT) eoValueParam& sigmaParam = _parser.getORcreateParam(std::string("0.3"), "sigmaInit", "Initial value for Sigmas (with a '%' -> scaled by the range of each variable)", - 's',"Genotype Initialization"); - // check if there is a vecSigmaInit - eoParam *vecSigmaParam = _parser.getParamWithLongName("vecSigmaInit"); - if(vecSigmaParam) { - eoValueParam >& vecSigmaParam - = _parser.getORcreateParam(std::vector(vecSize.value(), 0.3), - "vecSigmaInit", "Initial value for Sigma(s)", - 'V',"Genotype Initialization"); - init = new eoEsChromInit(boundsParam.value(), vecSigmaParam.value()); - } else { - // now some initial value for sigmas - even if useless? - // should be used in Normal mutation - eoValueParam& sigmaParam - = _parser.getORcreateParam(std::string("0.3"), "sigmaInit", - "Initial value for Sigmas " - "(with '%' scaled by the range of each variable)", - 's',"Genotype Initialization"); - // check for % - bool to_scale = false; - size_t pos = sigmaParam.value().find('%'); - if(pos < sigmaParam.value().size()) - { - // found a % - use scaling and get rid of '%' - to_scale = true; - sigmaParam.value().resize(pos); - } - std::istringstream is(sigmaParam.value()); - double sigma; - is >> sigma; - // minimum check - if(sigma < 0) - throw std::runtime_error("Negative sigma in make_genotype"); + 's', "Genotype Initialization"); + // check for % + bool to_scale = false; + size_t pos = sigmaParam.value().find('%'); + if(pos < sigmaParam.value().size()) { + // found a % - use scaling and get rid of '%' + to_scale = true; + sigmaParam.value().resize(pos); + } + std::istringstream is(sigmaParam.value()); + double sigma; + is >> sigma; + // minimum check + if(sigma < 0) + throw std::runtime_error("Negative sigma in make_genotype"); + if(to_scale) init = new eoEsChromInit(boundsParam.value(), sigma, to_scale); + else { // define parameter - _parser.getORcreateParam(std::vector(vecSize.value(), 0.3), - "vecSigmaInit", "Initial value for Sigma(s)", - 'V',"Genotype Initialization"); + eoValueParam >& vecSigmaParam + = _parser.getORcreateParam(std::vector(vecSize.value(), sigma), "vecSigmaInit", + "Initial value for Sigmas (only used when initSigma is not scaled)", + 'S', "Genotype Initialization"); + init = new eoEsChromInit(boundsParam.value(), vecSigmaParam.value()); } // store in state _state.storeFunctor(init); diff --git a/eo/src/utils/ChangeLog b/eo/src/utils/ChangeLog new file mode 100644 index 000000000..c59a72c13 --- /dev/null +++ b/eo/src/utils/ChangeLog @@ -0,0 +1,15 @@ +2006-11-16 Jochen Küpper + + * eoParser.h (eoParser::getORcreateParam): Make this a real if-then-else + clause around ptParam (found or not). + + * eoParam.h (eoValueParam::setValue): Document. + (eoValueParam >::setValue): Allow delimiters ',' and + ';'. A plain ' ' does not work, as it is not correctly read by + eoParser::readFrom. + + * Local Variables: + * coding: iso-8859-1 + * mode: flyspell + * fill-column: 80 + * End: diff --git a/eo/src/utils/eoParam.h b/eo/src/utils/eoParam.h index 5bf1dd0a4..27ebd1110 100644 --- a/eo/src/utils/eoParam.h +++ b/eo/src/utils/eoParam.h @@ -184,6 +184,19 @@ public : } + /** @brief Set value according to the speciied string + + For scalar types the textual represenation is typically quite + straigtforward. + + For vector we expect a list of numbers, where the first is + an unsigned integer taken as the length ot the vector and then + successively the vector elements. Vector elements can be separated + by ',', ';', or ' '. Note, however, that eoParser does not deal + correctly with parameter values contianing spaces (' '). + + @param _value Textual representation of the new value + */ void setValue(const std::string& _value) { std::istringstream is(_value); @@ -290,11 +303,18 @@ inline std::string eoValueParam >::getValue(void) const template <> inline void eoValueParam >::setValue(const std::string& _value) { + static const std::string delimiter(",;"); std::istringstream is(_value); unsigned sz; is >> sz; repValue.resize(sz); - std::copy(std::istream_iterator(is), std::istream_iterator(), repValue.begin()); + for(unsigned i=0; i> c; + } while((std::string::npos != delimiter.find(c)) && (! is.eof())); + is >> repValue[i]; + } } // The std::vector diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index ccf9dd611..8ef2346a5 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -218,7 +218,6 @@ void eoParser::readFrom(istream& is) string name(str.begin() + 2, equalLocation); longNameMap[name] = value; - } else // it should be a char { diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index 1f69f3100..2df6c1ab7 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -180,13 +180,14 @@ public: eoParam* ptParam = getParamWithLongName(_longName); if (ptParam) { // found - eoValueParam* ptTypedParam = - dynamic_cast*>(ptParam); + eoValueParam* ptTypedParam( + dynamic_cast*>(ptParam)); return *ptTypedParam; + } else { + // not found -> create it + return createParam(_defaultValue, _longName, _description, + _shortHand, _section, _required); } - // not found -> create it - return createParam (_defaultValue, _longName, _description, - _shortHand, _section, _required); }