diff --git a/eo/src/do/make_continue.h b/eo/src/do/make_continue.h index 0ae6a907..7dbce4a2 100644 --- a/eo/src/do/make_continue.h +++ b/eo/src/do/make_continue.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // make_continue.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 @@ -94,7 +94,10 @@ eoContinue & do_make_continue(eoParser& _parser, eoState& _state, eoEvalFu } // Same thing with Eval - but here default value is 0 - eoValueParam& maxEvalParam = _parser.createParam((unsigned long)0, "maxEval", "Maximum number of evaluations (0 = none)",'E',"Stopping criterion"); + eoValueParam& maxEvalParam + = _parser.getORcreateParam((unsigned long)0, "maxEval", + "Maximum number of evaluations (0 = none)", + 'E', "Stopping criterion"); if (maxEvalParam.value()) // positive: -> define and store { diff --git a/eo/src/es/eoEsMutationInit.h b/eo/src/es/eoEsMutationInit.h index 79472138..3d9446a9 100644 --- a/eo/src/es/eoEsMutationInit.h +++ b/eo/src/es/eoEsMutationInit.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // eoEsMutationInit.h // (c) GeNeura Team, 1998 - 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 @@ -34,11 +34,11 @@ 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 + parameterLoader. It also provides the names for the parameters in this class as virtual protected member functions. - + 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 + names in the configuration files, you might consider overriding this class to change the names. @see eoEsMutate @@ -47,9 +47,9 @@ class eoEsMutationInit { public : - eoEsMutationInit(eoParser& _parser, - std::string _section="ES mutation parameters" ) : - parser(_parser), repSection(_section), + eoEsMutationInit(eoParser& _parser, + std::string _section="ES mutation parameters" ) : + parser(_parser), repSection(_section), TauLclParam(0), TauGlbParam(0), TauBetaParam(0) {} // because we have virtual function - size @@ -59,7 +59,9 @@ class eoEsMutationInit { if (TauLclParam == 0) { - TauLclParam = &parser.createParam(1.0, TauLclName(), "Local Tau (before normalization)", TauLclShort(), section()); + TauLclParam = &parser.getORcreateParam(1.0, TauLclName(), + "Local Tau (before normalization)", + TauLclShort(), section()); } return TauLclParam->value(); @@ -69,7 +71,9 @@ class eoEsMutationInit { if (TauGlbParam == 0) { - TauGlbParam = &parser.createParam(1.0, TauGlbName(), "Global Tau (before normalization)", TauGlbShort(), section()); + TauGlbParam = &parser.getORcreateParam(1.0, TauGlbName(), + "Global Tau (before normalization)", + TauGlbShort(), section()); } return TauGlbParam->value(); @@ -79,25 +83,26 @@ class eoEsMutationInit { if (TauBetaParam == 0) { - TauBetaParam = &parser.createParam(0.0873, TauBetaName(), "Beta", TauBetaShort(), section()); + TauBetaParam = &parser.getORcreateParam(0.0873, TauBetaName(), + "Beta", TauBetaShort(), section()); } return TauBetaParam->value(); } protected : - - virtual std::string section(void) + + virtual std::string section(void) { return repSection; } virtual std::string TauLclName(void) const { return "TauLoc"; } virtual char TauLclShort(void) const { return 'l'; } - - virtual std::string TauGlbName(void) const { return "TauGlob"; } - virtual char TauGlbShort(void) const { return 'g'; } - virtual std::string TauBetaName(void) const { return "Beta"; } - virtual char TauBetaShort(void) const { return 'b'; } + virtual std::string TauGlbName(void) const { return "TauGlob"; } + virtual char TauGlbShort(void) const { return 'g'; } + + virtual std::string TauBetaName(void) const { return "Beta"; } + virtual char TauBetaShort(void) const { return 'b'; } private : diff --git a/eo/src/es/make_genotype_real.cpp b/eo/src/es/make_genotype_real.cpp index bce60a84..d5ad238d 100644 --- a/eo/src/es/make_genotype_real.cpp +++ b/eo/src/es/make_genotype_real.cpp @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // make_genotype_real.cpp // (c) Maarten Keijzer, Marc Schoenauer 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,41 +27,47 @@ #ifdef _MSC_VER // to avoid long name warnings #pragma warning(disable:4786) -#endif +#endif /** This file contains ***INSTANCIATED DEFINITIONS*** of eoReal Init fns * It should be included in the file that calls any of the corresponding fns * Compiling this file allows one to generate part of the library (i.e. object * files that you just need to link with your own main and fitness code). * - * The corresponding ***INSTANCIATED DECLARATIONS*** are contained + * The corresponding ***INSTANCIATED DECLARATIONS*** are contained * in src/es/make_real.h * while the TEMPLATIZED code is define in make_genotype_real.h * - * It is instanciated in src/es/make_genotype_real.cpp - + * It is instanciated in src/es/make_genotype_real.cpp - * and incorporated in the ga/libga.a * - * It returns an eoInit that can later be used to initialize + * It returns an eoInit that can later be used to initialize * the population (see make_pop.h). * * 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 ctor of EOT, resulting in most cases in an EOT + * 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*** */ // the templatized code #include -/// The following function merely call the templatized do_* functions +/// The following functions merely call the templatized do_* functions +eoRealInitBounded > & make_genotype(eoParser& _parser, + eoState& _state, + eoReal _eo) +{ + return do_make_genotype(_parser, _state, _eo); +} -eoRealInitBounded > & make_genotype(eoParser& _parser, eoState& _state, eoReal _eo) + + +eoRealInitBounded > & make_genotype(eoParser& _parser, + eoState& _state, + eoReal _eo) { - return do_make_genotype(_parser, _state, _eo); -} -eoRealInitBounded > & make_genotype(eoParser& _parser, eoState& _state, eoReal _eo) -{ - return do_make_genotype(_parser, _state, _eo); + return do_make_genotype(_parser, _state, _eo); } diff --git a/eo/src/es/make_op.h b/eo/src/es/make_op.h index 846c9908..a19dc4b0 100644 --- a/eo/src/es/make_op.h +++ b/eo/src/es/make_op.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // make_op.h - the real-valued version // (c) Maarten Keijzer, Marc Schoenauer 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 @@ -56,19 +56,21 @@ * This is why the template is the complete EOT even though only the fitness * is actually templatized here: the following only applies to bitstrings * - * Note : the last parameter is an eoInit: if some operator needs some info + * Note : the last parameter is an eoInit: if some operator needs some info * about the gneotypes, the init has it all (e.g. bounds, ...) - * Simply do + * Simply do * EOT myEO; * _init(myEO); * and myEO is then an ACTUAL object */ - template eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit& _init) { // First, decide whether the objective variables are bounded - eoValueParam& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "objectBounds", "Bounds for variables (unbounded if absent)", 'B', "Genetic Operators"); + eoValueParam& boundsParam + = _parser.getORcreateParam(eoParamParamType("(0,1)"), "objectBounds", + "Bounds for variables (unbounded if absent)", + 'B', "Genetic Operators"); // get initisalizer size == std::vector size // eoRealInitBounded * realInit = (eoRealInitBounded*)(&_init); @@ -83,7 +85,7 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit with specific implementation //// in eoParser.cpp. At the moemnt, it is there (cf also make_genotype eoParamParamType & ppBounds = boundsParam.value(); // std::pair > @@ -98,7 +100,7 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit& operatorParam = _parser.createParam(std::string("SGA"), "operator", "Description of the operator (SGA only now)", 'o', "Genetic Operators"); + eoValueParam& operatorParam + = _parser.getORcreateParam(std::string("SGA"), "operator", + "Description of the operator (SGA only now)", + 'o', "Genetic Operators"); if (operatorParam.value() != std::string("SGA")) throw std::runtime_error("Sorry, only SGA-like operator available right now\n"); - // now we read Pcross and Pmut, + // now we read Pcross and Pmut, // the relative weights for all crossovers -> proportional choice // the relative weights for all mutations -> proportional choice - // and create the eoGenOp that is exactly + // and create the eoGenOp that is exactly // crossover with pcross + mutation with pmut - eoValueParam& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators" ); + eoValueParam& pCrossParam + = _parser.getORcreateParam(0.6, "pCross", "Probability of Crossover", + 'C', "Genetic Operators" ); // minimum check if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) ) throw std::runtime_error("Invalid pCross"); - eoValueParam& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators" ); + eoValueParam& pMutParam + = _parser.getORcreateParam(0.1, "pMut", "Probability of Mutation", + 'M', "Genetic Operators" ); // minimum check if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) ) throw std::runtime_error("Invalid pMut"); @@ -137,12 +146,18 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit& segmentRateParam = _parser.createParam(double(1.0), "segmentRate", "Relative rate for segment crossover", 's', "Genetic Operators" ); + eoValueParam& segmentRateParam + = _parser.getORcreateParam(double(1.0), "segmentRate", + "Relative rate for segment crossover", + 's', "Genetic Operators" ); // minimum check if ( (segmentRateParam.value() < 0) ) throw std::runtime_error("Invalid segmentRate"); - eoValueParam& arithmeticRateParam = _parser.createParam(double(2.0), "arithmeticRate", "Relative rate for arithmetic crossover", 'A', "Genetic Operators" ); + eoValueParam& arithmeticRateParam + = _parser.getORcreateParam(double(2.0), "arithmeticRate", + "Relative rate for arithmetic crossover", + 'A', "Genetic Operators" ); // minimum check if ( (arithmeticRateParam.value() < 0) ) throw std::runtime_error("Invalid arithmeticRate"); @@ -154,23 +169,23 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit *ptCombinedQuadOp = NULL; eoQuadOp *ptQuad = NULL; - if (bCross) + if (bCross) { // segment crossover for bitstring - pass it the bounds ptQuad = new eoSegmentCrossover(*ptBounds); _state.storeFunctor(ptQuad); ptCombinedQuadOp = new eoPropCombinedQuadOp(*ptQuad, segmentRateParam.value()); - + // arithmetic crossover ptQuad = new eoArithmeticCrossover(*ptBounds); _state.storeFunctor(ptQuad); ptCombinedQuadOp->add(*ptQuad, arithmeticRateParam.value()); - + // don't forget to store the CombinedQuadOp _state.storeFunctor(ptCombinedQuadOp); } @@ -178,27 +193,40 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit & epsilonParam = _parser.createParam(0.01, "epsilon", "Half-size of interval for Uniform Mutation", 'e', "Genetic Operators" ); + eoValueParam & epsilonParam + = _parser.getORcreateParam(0.01, "epsilon", "Half-size of interval for Uniform Mutation", + 'e', "Genetic Operators" ); // minimum check if ( (epsilonParam.value() < 0) ) throw std::runtime_error("Invalid epsilon"); - eoValueParam & uniformMutRateParam = _parser.createParam(1.0, "uniformMutRate", "Relative rate for uniform mutation", 'u', "Genetic Operators" ); + eoValueParam & uniformMutRateParam + = _parser.getORcreateParam(1.0, "uniformMutRate", + "Relative rate for uniform mutation", 'u', "Genetic Operators" ); // minimum check if ( (uniformMutRateParam.value() < 0) ) throw std::runtime_error("Invalid uniformMutRate"); - - eoValueParam & detMutRateParam = _parser.createParam(1.0, "detMutRate", "Relative rate for deterministic uniform mutation", 'd', "Genetic Operators" ); + + eoValueParam & detMutRateParam + = _parser.getORcreateParam(1.0, "detMutRate", + "Relative rate for deterministic uniform mutation", + 'd', "Genetic Operators" ); // minimum check if ( (detMutRateParam.value() < 0) ) throw std::runtime_error("Invalid detMutRate"); - eoValueParam & normalMutRateParam = _parser.createParam(1.0, "normalMutRate", "Relative rate for Gaussian mutation", 'd', "Genetic Operators" ); + eoValueParam & normalMutRateParam + = _parser.getORcreateParam(1.0, "normalMutRate", + "Relative rate for Gaussian mutation", + 'd', "Genetic Operators" ); // minimum check if ( (normalMutRateParam.value() < 0) ) throw std::runtime_error("Invalid normalMutRate"); // and the sigma - eoValueParam & sigmaParam = _parser.createParam(1.0, "sigma", "Sigma (fixed) for Gaussian mutation", 'S', "Genetic Operators" ); + eoValueParam & sigmaParam + = _parser.getORcreateParam(1.0, "sigma", + "Sigma (fixed) for Gaussian mutation", + 'S', "Genetic Operators" ); // minimum check if ( (sigmaParam.value() < 0) ) throw std::runtime_error("Invalid sigma"); @@ -225,14 +253,14 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit(*ptMon, uniformMutRateParam.value()); - + // mutate exactly 1 component (uniformly) per individual - ptMon = new eoDetUniformMutation(*ptBounds, epsilonParam.value()); + ptMon = new eoDetUniformMutation(*ptBounds, epsilonParam.value()); _state.storeFunctor(ptMon); ptCombinedMonOp->add(*ptMon, detMutRateParam.value()); - + // mutate all component using Gaussian mutation - ptMon = new eoNormalMutation(*ptBounds, sigmaParam.value()); + ptMon = new eoNormalMutation(*ptBounds, sigmaParam.value()); _state.storeFunctor(ptMon); ptCombinedMonOp->add(*ptMon, normalMutRateParam.value()); _state.storeFunctor(ptCombinedMonOp); @@ -242,7 +270,7 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< unsigned vecSize = _init.size(); // First, decide whether the objective variables are bounded - eoValueParam& boundsParam = _parser.createParam(eoRealVectorBounds(vecSize,eoDummyRealNoBounds), "objectBounds", "Bounds for variables", 'B', "Variation Operators"); + eoValueParam& boundsParam + = _parser.getORcreateParam(eoRealVectorBounds(vecSize,eoDummyRealNoBounds), + "objectBounds", "Bounds for variables", 'B', "Variation Operators"); - // now we read Pcross and Pmut, - eoValueParam& operatorParam = _parser.createParam(std::string("SGA"), "operator", "Description of the operator (SGA only now)", 'o', "Variation Operators"); + // now we read Pcross and Pmut, + eoValueParam& operatorParam + = _parser.getORcreateParam(std::string("SGA"), "operator", + "Description of the operator (SGA only now)", + 'o', "Variation Operators"); if (operatorParam.value() != std::string("SGA")) throw std::runtime_error("Sorry, only SGA-like operator available right now\n"); - // now we read Pcross and Pmut, - // and create the eoGenOp that is exactly + // now we read Pcross and Pmut, + // and create the eoGenOp that is exactly // crossover with pcross + mutation with pmut - eoValueParam& pCrossParam = _parser.createParam(1.0, "pCross", "Probability of Crossover", 'C', "Variation Operators" ); + eoValueParam& pCrossParam + = _parser.getORcreateParam(1.0, "pCross", "Probability of Crossover", + 'C', "Variation Operators" ); // minimum check if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) ) throw std::runtime_error("Invalid pCross"); - eoValueParam& pMutParam = _parser.createParam(1.0, "pMut", "Probability of Mutation", 'M', "Variation Operators" ); + eoValueParam& pMutParam + = _parser.getORcreateParam(1.0, "pMut", "Probability of Mutation", + 'M', "Variation Operators" ); // minimum check if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) ) throw std::runtime_error("Invalid pMut"); @@ -100,10 +109,19 @@ eoGenOp & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< // crossover ///////////// // ES crossover - eoValueParam& crossTypeParam = _parser.createParam(std::string("global"), "crossType", "Type of ES recombination (global or standard)", 'C', "Variation Operators"); - - eoValueParam& crossObjParam = _parser.createParam(std::string("discrete"), "crossObj", "Recombination of object variables (discrete, intermediate or none)", 'O', "Variation Operators"); - eoValueParam& crossStdevParam = _parser.createParam(std::string("intermediate"), "crossStdev", "Recombination of mutation strategy parameters (intermediate, discrete or none)", 'S', "Variation Operators"); + eoValueParam& crossTypeParam + = _parser.getORcreateParam(std::string("global"), "crossType", + "Type of ES recombination (global or standard)", + 'C', "Variation Operators"); + + eoValueParam& crossObjParam + = _parser.getORcreateParam(std::string("discrete"), "crossObj", + "Recombination of object variables (discrete, intermediate or none)", + 'O', "Variation Operators"); + eoValueParam& crossStdevParam + = _parser.getORcreateParam(std::string("intermediate"), "crossStdev", + "Recombination of mutation strategy parameters (intermediate, discrete or none)", + 'S', "Variation Operators"); // The pointers: first the atom Xover eoBinOp *ptObjAtomCross = NULL; @@ -128,7 +146,7 @@ eoGenOp & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< ptStdevAtomCross = new eoBinCloneOp; else throw std::runtime_error("Invalid mutation strategy parameter crossover type"); - // and build the indi Xover + // and build the indi Xover if (crossTypeParam.value() == std::string("global")) ptCross = new eoEsGlobalXover(*ptObjAtomCross, *ptStdevAtomCross); else if (crossTypeParam.value() == std::string("standard")) @@ -145,15 +163,15 @@ eoGenOp & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< _state.storeFunctor(ptStdevAtomCross); _state.storeFunctor(ptCross); - // mutation + // mutation ///////////// // Ok, time to set up the self-adaptive mutation // Proxy for the mutation parameters eoEsMutationInit mutateInit(_parser, "Variation Operators"); - + eoEsMutate & mut = _state.storeFunctor( - new eoEsMutate(mutateInit, boundsParam.value())); + new eoEsMutate(mutateInit, boundsParam.value())); // now the general op - a sequential application of crossover and mutatation // no need to first have crossover combined with a clone as it is an diff --git a/eo/src/es/make_op_real.h b/eo/src/es/make_op_real.h index 11138821..64f37883 100644 --- a/eo/src/es/make_op_real.h +++ b/eo/src/es/make_op_real.h @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- // make_op.h - the real-valued version // (c) Maarten Keijzer, Marc Schoenauer 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 @@ -56,9 +56,9 @@ * This is why the template is the complete EOT even though only the fitness * is actually templatized here: the following only applies to bitstrings * - * Note : the last parameter is an eoInit: if some operator needs some info + * Note : the last parameter is an eoInit: if some operator needs some info * about the gneotypes, the init has it all (e.g. bounds, ...) - * Simply do + * Simply do * EOT myEO; * _init(myEO); * and myEO is then an ACTUAL object @@ -71,29 +71,40 @@ eoGenOp & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< unsigned vecSize = _init.size(); // First, decide whether the objective variables are bounded - eoValueParam& boundsParam = _parser.createParam(eoRealVectorBounds(vecSize,eoDummyRealNoBounds), "objectBounds", "Bounds for variables", 'B', "Variation Operators"); + eoValueParam& boundsParam + = _parser.getORcreateParam(eoRealVectorBounds(vecSize,eoDummyRealNoBounds), "objectBounds", + "Bounds for variables", 'B', "Variation Operators"); - // this is a temporary version(!), + // this is a temporary version(!), // while Maarten codes the full tree-structured general operator input // BTW we must leave that simple version available somehow, as it is the one // that 90% people use! - eoValueParam& operatorParam = _parser.createParam(std::string("SGA"), "operator", "Description of the operator (SGA only now)", 'o', "Variation Operators"); + eoValueParam& operatorParam + = _parser.getORcreateParam(std::string("SGA"), "operator", + "Description of the operator (SGA only now)", + 'o', "Variation Operators"); if (operatorParam.value() != std::string("SGA")) throw std::runtime_error("Sorry, only SGA-like operator available right now\n"); - // now we read Pcross and Pmut, + // now we read Pcross and Pmut, // the relative weights for all crossovers -> proportional choice // the relative weights for all mutations -> proportional choice - // and create the eoGenOp that is exactly + // and create the eoGenOp that is exactly // crossover with pcross + mutation with pmut - eoValueParam& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Variation Operators" ); + eoValueParam& pCrossParam + = _parser.getORcreateParam(0.6, "pCross", + "Probability of Crossover", + 'C', "Variation Operators" ); // minimum check if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) ) throw std::runtime_error("Invalid pCross"); - eoValueParam& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Variation Operators" ); + eoValueParam& pMutParam + = _parser.getORcreateParam(0.1, "pMut", + "Probability of Mutation", + 'M', "Variation Operators" ); // minimum check if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) ) throw std::runtime_error("Invalid pMut"); @@ -101,23 +112,35 @@ eoGenOp & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< // the crossovers ///////////////// // the parameters - eoValueParam& alphaParam = _parser.createParam(double(0.0), "alpha", "Bound for factor of linear recombinations", 'a', "Variation Operators" ); + eoValueParam& alphaParam + = _parser.getORcreateParam(double(0.0), "alpha", + "Bound for factor of linear recombinations", + 'a', "Variation Operators" ); // minimum check if ( (alphaParam.value() < 0) ) throw std::runtime_error("Invalid BLX coefficient alpha"); - eoValueParam& segmentRateParam = _parser.createParam(double(1.0), "segmentRate", "Relative rate for segment crossover", 's', "Variation Operators" ); + eoValueParam& segmentRateParam + = _parser.getORcreateParam(double(1.0), "segmentRate", + "Relative rate for segment crossover", + 's', "Variation Operators" ); // minimum check if ( (segmentRateParam.value() < 0) ) throw std::runtime_error("Invalid segmentRate"); - eoValueParam& hypercubeRateParam = _parser.createParam(double(1.0), "hypercubeRate", "Relative rate for hypercube crossover", 'A', "Variation Operators" ); + eoValueParam& hypercubeRateParam + = _parser.getORcreateParam(double(1.0), "hypercubeRate", + "Relative rate for hypercube crossover", + 'A', "Variation Operators" ); // minimum check if ( (hypercubeRateParam.value() < 0) ) throw std::runtime_error("Invalid hypercubeRate"); - eoValueParam& uxoverRateParam = _parser.createParam(double(1.0), "uxoverRate", "Relative rate for uniform crossover", 'A', "Variation Operators" ); + eoValueParam& uxoverRateParam + = _parser.getORcreateParam(double(1.0), "uxoverRate", + "Relative rate for uniform crossover", + 'A', "Variation Operators" ); // minimum check if ( (uxoverRateParam.value() < 0) ) throw std::runtime_error("Invalid uxoverRate"); @@ -129,28 +152,28 @@ eoGenOp & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< std::cerr << "Warning: no crossover" << std::endl; bCross = false; } - + // Create the CombinedQuadOp eoPropCombinedQuadOp *ptCombinedQuadOp = NULL; eoQuadOp *ptQuad = NULL; - if (bCross) + if (bCross) { // segment crossover for bitstring - pass it the bounds ptQuad = new eoSegmentCrossover(boundsParam.value(), alphaParam.value()); _state.storeFunctor(ptQuad); ptCombinedQuadOp = new eoPropCombinedQuadOp(*ptQuad, segmentRateParam.value()); - + // hypercube crossover ptQuad = new eoHypercubeCrossover(boundsParam.value(), alphaParam.value()); _state.storeFunctor(ptQuad); ptCombinedQuadOp->add(*ptQuad, hypercubeRateParam.value()); - + // uniform crossover ptQuad = new eoRealUXover(); _state.storeFunctor(ptQuad); ptCombinedQuadOp->add(*ptQuad, uxoverRateParam.value()); - + // don't forget to store the CombinedQuadOp _state.storeFunctor(ptCombinedQuadOp); } @@ -158,29 +181,46 @@ eoGenOp & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< // the mutations ///////////////// // the parameters - eoValueParam & epsilonParam = _parser.createParam(0.01, "epsilon", "Half-size of interval for Uniform Mutation", 'e', "Variation Operators" ); + eoValueParam & epsilonParam + = _parser.getORcreateParam(0.01, "epsilon", + "Half-size of interval for Uniform Mutation", + 'e', "Variation Operators" ); // minimum check if ( (epsilonParam.value() < 0) ) throw std::runtime_error("Invalid epsilon"); - eoValueParam & uniformMutRateParam = _parser.createParam(1.0, "uniformMutRate", "Relative rate for uniform mutation", 'u', "Variation Operators" ); + eoValueParam & uniformMutRateParam + = _parser.getORcreateParam(1.0, "uniformMutRate", + "Relative rate for uniform mutation", + 'u', "Variation Operators" ); // minimum check if ( (uniformMutRateParam.value() < 0) ) throw std::runtime_error("Invalid uniformMutRate"); - - eoValueParam & detMutRateParam = _parser.createParam(1.0, "detMutRate", "Relative rate for deterministic uniform mutation", 'd', "Variation Operators" ); + + eoValueParam & detMutRateParam + = _parser.getORcreateParam(1.0, "detMutRate", + "Relative rate for deterministic uniform mutation", + 'd', "Variation Operators" ); // minimum check if ( (detMutRateParam.value() < 0) ) throw std::runtime_error("Invalid detMutRate"); - eoValueParam & normalMutRateParam = _parser.createParam(1.0, "normalMutRate", "Relative rate for Gaussian mutation", 'd', "Variation Operators" ); + eoValueParam & normalMutRateParam + = _parser.getORcreateParam(1.0, "normalMutRate", + "Relative rate for Gaussian mutation", 'd', "Variation Operators" ); // minimum check if ( (normalMutRateParam.value() < 0) ) throw std::runtime_error("Invalid normalMutRate"); - eoValueParam & sigmaParam = _parser.createParam(0.3, "sigma", "Sigma (fixed) for Gaussian mutation", 's', "Variation Operators" ); + eoValueParam & sigmaParam + = _parser.getORcreateParam(0.3, "sigma", + "Sigma (fixed) for Gaussian mutation", + 's', "Variation Operators" ); - eoValueParam & pNormalParam = _parser.createParam(1.0, "pNormal", "Proba. to change each variable for Gaussian mutation", 's', "Variation Operators" ); + eoValueParam & pNormalParam + = _parser.getORcreateParam(1.0, "pNormal", + "Proba. to change each variable for Gaussian mutation", + 's', "Variation Operators" ); // minimum check bool bMut = true; @@ -204,14 +244,14 @@ eoGenOp & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< _state.storeFunctor(ptMon); // create the CombinedMonOp ptCombinedMonOp = new eoPropCombinedMonOp(*ptMon, uniformMutRateParam.value()); - + // mutate exactly 1 component (uniformly) per individual - ptMon = new eoDetUniformMutation(boundsParam.value(), epsilonParam.value()); + ptMon = new eoDetUniformMutation(boundsParam.value(), epsilonParam.value()); _state.storeFunctor(ptMon); ptCombinedMonOp->add(*ptMon, detMutRateParam.value()); - + // mutate all component using Gaussian mutation - ptMon = new eoNormalVecMutation(boundsParam.value(), sigmaParam.value(), pNormalParam.value()); + ptMon = new eoNormalVecMutation(boundsParam.value(), sigmaParam.value(), pNormalParam.value()); _state.storeFunctor(ptMon); ptCombinedMonOp->add(*ptMon, normalMutRateParam.value()); _state.storeFunctor(ptCombinedMonOp); @@ -221,7 +261,7 @@ eoGenOp & do_make_op(eoParser& _parser, eoState& _state, eoRealInitBounded< // to simulate SGA (crossover with proba pCross + mutation with proba pMut // we must construct // a sequential combination of - // with proba 1, a proportional combination of + // with proba 1, a proportional combination of // a QuadCopy and our crossover // with proba pMut, our mutation diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index 518570be..ad4d04d4 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -123,11 +123,10 @@ void eoParser::processParam(eoParam& param, std::string section) { // this param enters the parser: add the prefix to the long name if (prefix != "") - { + { param.setLongName(prefix+param.longName()); section = prefix + section; // and to section - } - + } doRegisterParam(param); // plainly register it params.insert(make_pair(section, ¶m)); } @@ -139,9 +138,7 @@ void eoParser::doRegisterParam(eoParam& param) const string msg = "Required parameter: " + param.longName() + " missing"; messages.push_back(msg); } - pair value = getValue(param); - if (value.first) { param.setValue(value.second); @@ -162,9 +159,7 @@ pair eoParser::getValue(eoParam& _param) const return result; } } - map::const_iterator it = longNameMap.find(_param.longName()); - if (it != longNameMap.end()) { result.second = it->second; @@ -172,7 +167,6 @@ pair eoParser::getValue(eoParam& _param) const return result; } // else (TODO: check environment, just long names) - return result; } @@ -188,72 +182,72 @@ void eoParser::updateParameters() const void eoParser::readFrom(istream& is) { - string str; - // we must avoid processing \section{xxx} if xxx is NOT "Parser" - bool processing = true; - while (is >> str) + string str; + // we must avoid processing \section{xxx} if xxx is NOT "Parser" + bool processing = true; + while (is >> str) { - if (str.find(string("\\section{"))==0) // found section begin - processing = (str.find(string("Parser"))= 2) + if (str.size() >= 2) { - if (str[2] == '=') + if (str[2] == '=') { - if (str.size() >= 3) - value = string(str.begin() + 3, str.end()); + if (str.size() >= 3) + value = string(str.begin() + 3, str.end()); } - else + else { - value = string(str.begin() + 2, str.end()); + value = string(str.begin() + 2, str.end()); } } - shortNameMap[str[1]] = value; + shortNameMap[str[1]] = value; } } } - } + } - updateParameters(); + updateParameters(); } void eoParser::printOn(ostream& os) const diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index 2f549d36..e8ebc0e4 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -24,7 +24,7 @@ */ //----------------------------------------------------------------------------- /** -CVS Info: $Date: 2005-08-29 07:50:50 $ $Version$ $Author: kuepper $ +CVS Info: $Date: 2005-09-12 17:53:24 $ $Version$ $Author: kuepper $ */ #ifndef eoParser_h #define eoParser_h @@ -73,29 +73,27 @@ public : * @param _required If it is a necessary parameter or not */ template - eoValueParam& createParam - (ValueType _defaultValue, - std::string _longName, - std::string _description, - char _shortHand = 0, - std::string _section = "", - bool _required = false) - { - eoValueParam* p = new eoValueParam(_defaultValue, - _longName, - _description, - _shortHand, - _required); - ownedParams.push_back(p); - processParam(*p, _section); - return *p; - } + eoValueParam& createParam(ValueType _defaultValue, + std::string _longName, + std::string _description, + char _shortHand = 0, + std::string _section = "", + bool _required = false) + { + eoValueParam* p = new eoValueParam(_defaultValue, + _longName, + _description, + _shortHand, + _required); + ownedParams.push_back(p); + processParam(*p, _section); + return *p; + } private : std::vector ownedParams; - }; @@ -198,7 +196,10 @@ public: - /** Make sure parameter has specific value + /** Set parameter value or create parameter + + This makes sure that the specified parameter has the given value. + If the parameter does not exist yet, it is created. This requires that operator<< is defined for ValueType. */ @@ -207,18 +208,19 @@ public: std::string _description, char _shortHand = 0, std::string _section = "", bool _required = false) { - eoParam *param = getParamWithLongName(_longName); - if(0 == param) { - createParam(_defaultValue, _longName, _description, - _shortHand, _section, _required); - } else { + eoValueParam& param = createParam(_defaultValue, _longName, _description, + _shortHand, _section, _required); #ifdef HAVE_SSTREAM - std::ostringstream os; + std::ostringstream os; #else - std::ostrstream os; + std::ostrstream os; #endif - os << _defaultValue; - dynamic_cast *>(param)->setValue(os.str()); + os << _defaultValue; + if(isItThere(param)) { + param.setValue(os.str()); + } else { + longNameMap[_longName] = os.str(); + shortNameMap[_shortHand] = os.str(); } } diff --git a/eo/src/utils/eoRealBounds.cpp b/eo/src/utils/eoRealBounds.cpp index cd59620b..0b3faaac 100644 --- a/eo/src/utils/eoRealBounds.cpp +++ b/eo/src/utils/eoRealBounds.cpp @@ -1,7 +1,7 @@ #ifdef _MSC_VER // to avoid long name warnings #pragma warning(disable:4786) -#endif +#endif #ifdef HAVE_CONFIG_H #include @@ -18,7 +18,7 @@ #include "eoRealVectorBounds.h" -// the global dummy bounds +// the global dummy bounds // (used for unbounded variables when bounds are required) eoRealNoBounds eoDummyRealNoBounds; eoRealVectorNoBounds eoDummyVectorNoBounds(0); @@ -74,7 +74,7 @@ eoRealVectorBounds::eoRealVectorBounds(const eoRealVectorBounds & _b): // the readFrom method of eoRealVectorNoBounds: // only calls the readFrom(string) - for param reading -void eoRealVectorBounds::readFrom(std::istream& _is) +void eoRealVectorBounds::readFrom(std::istream& _is) { std::string value; _is >> value; @@ -95,7 +95,7 @@ void eoRealVectorBounds::readFrom(std::string _value) ownedBounds.resize(0); factor.resize(0); resize(0); - + // now read std::string delim(",; "); while (_value.size()>0) @@ -129,7 +129,7 @@ void eoRealVectorBounds::readFrom(std::string _value) std::string sBounds = _value.substr(posDeb+1, posFin-posDeb-1); // and remove from original string _value = _value.substr(posFin+1); - + remove_leading(sBounds, delim); size_t posDelim = sBounds.find_first_of(delim); if (posDelim >= sBounds.size()) @@ -193,10 +193,10 @@ void eoRealVectorBounds::adjust_size(unsigned _dim) } } -/** the constructor for eoGeneralRealBound - from a string - * very similar to the eoRealVectorBounds::readFrom above +/** the constructor for eoGeneralRealBound - from a string + * very similar to the eoRealVectorBounds::readFrom above * but was written much later so the readFrom does not call this one - * as it should do + * as it should do */ eoRealBounds* eoGeneralRealBounds::getBoundsFromString(std::string _value) { @@ -208,7 +208,7 @@ eoRealBounds* eoGeneralRealBounds::getBoundsFromString(std::string _value) // look for opening char size_t posDeb = _value.find_first_of(beginOrClose); // allow ]a,b] - if (posDeb >= _value.size()) // nothing left to read + if (posDeb >= _value.size()) // nothing left to read throw std::runtime_error("Syntax error in eoGeneralRealBounds Ctor"); // ending char: next {}() after posDeb @@ -220,7 +220,7 @@ eoRealBounds* eoGeneralRealBounds::getBoundsFromString(std::string _value) std::string sBounds = _value.substr(posDeb+1, posFin-posDeb-1); // and remove from original string _value = _value.substr(posFin+1); - + remove_leading(sBounds, delim); size_t posDelim = sBounds.find_first_of(delim); if (posDelim >= sBounds.size()) @@ -260,7 +260,7 @@ eoRealBounds* eoGeneralRealBounds::getBoundsFromString(std::string _value) if (maxBound <= minBound) throw std::runtime_error("Syntax error in eoGeneralRealBounds Ctor"); locBound = new eoRealInterval(minBound, maxBound); - } + } else if (!minBounded && !maxBounded) // no bound at all locBound = new eoRealNoBounds; else if (!minBounded && maxBounded)