From a7042bffee246a271b133b12424008b86c4b7264 Mon Sep 17 00:00:00 2001 From: evomarc Date: Sat, 28 Apr 2001 05:47:18 +0000 Subject: [PATCH] Added bounds to the real operators in make_XXX (and hence in t-eoReal) --- eo/src/do/make_pop.h | 2 +- eo/src/es/Makefile.am | 14 +- eo/src/es/eoRealOp.h | 90 ++++++++--- eo/src/es/es.h | 69 +++++++-- eo/src/es/make_algo_scalar_real.cpp | 3 - eo/src/es/make_checkpoint_real.cpp | 3 - eo/src/es/make_continue_real.cpp | 3 - eo/src/es/make_genotype.h | 4 +- eo/src/es/make_op.h | 231 +++++++++++++++++----------- eo/src/es/make_pop_real.cpp | 3 - eo/src/es/make_run_real.cpp | 3 - eo/src/utils/eoRealBounds.h | 26 ++-- eo/test/real_value.h | 8 +- eo/test/t-eoESFull.cpp | 44 +++++- eo/test/t-eoReal.cpp | 2 +- eo/tutorial/html/eoTutorial.html | 2 +- 16 files changed, 328 insertions(+), 179 deletions(-) diff --git a/eo/src/do/make_pop.h b/eo/src/do/make_pop.h index 2ab383094..0c2ecd75d 100644 --- a/eo/src/do/make_pop.h +++ b/eo/src/do/make_pop.h @@ -48,7 +48,7 @@ template eoPop& do_make_pop(eoParser & _parser, eoState& _state, eoInit & _init) { eoValueParam& seedParam = _parser.createParam(uint32(time(0)), "seed", "Random number seed", 'S'); - eoValueParam& popSize = _parser.createParam(unsigned(20), "PopSize", "Population Size", 'P', "initialization"); + eoValueParam& popSize = _parser.createParam(unsigned(20), "popSize", "Population Size", 'P', "initialization"); // Either load or initialize // create an empty pop and let the state handle the memory diff --git a/eo/src/es/Makefile.am b/eo/src/es/Makefile.am index 052e1ca6e..48c1b841b 100644 --- a/eo/src/es/Makefile.am +++ b/eo/src/es/Makefile.am @@ -6,14 +6,12 @@ INCLUDES = -I$(top_builddir)/src lib_LIBRARIES = libes.a -libes_a_SOURCES = make_algo_scalar_real.cpp \ - make_checkpoint_real.cpp \ - make_continue_real.cpp \ - make_genotype_real.cpp \ - make_help.cpp \ - make_op_real.cpp \ - make_pop_real.cpp \ - make_run_real.cpp +libes_a_SOURCES = make_algo_scalar_real.cpp make_checkpoint_real.cpp \ + make_continue_real.cpp make_genotype_real.cpp make_help.cpp \ + make_op_real.cpp make_pop_real.cpp make_run_real.cpp \ + make_algo_scalar_es.cpp make_checkpoint_es.cpp \ + make_continue_es.cpp make_pop_es.cpp make_run_es.cpp + CPPFLAGS = -Wall CXXFLAGS = -g libeoincdir = $(includedir)/eo/es diff --git a/eo/src/es/eoRealOp.h b/eo/src/es/eoRealOp.h index ed7ae3043..f18ac31b0 100644 --- a/eo/src/es/eoRealOp.h +++ b/eo/src/es/eoRealOp.h @@ -2,7 +2,7 @@ //----------------------------------------------------------------------------- // eoRealOp.h -// (c) EEAAX 2000 - Maarten Keijzer 2000 +// (c) Maarten Keijzer 2000 - 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 @@ -52,17 +52,37 @@ template class eoUniformMutation: public eoMonOp * @param _epsilon the range for uniform nutation * @param _p_change the probability to change a given coordinate */ - eoUniformMutation(const double& _epsilon, const double& _p_change = 1.0): - bounds(eoDummyVectorNoBounds), epsilon(_epsilon), p_change(_p_change) {} + eoUniformMutation(const unsigned _size, const double& _epsilon, + const double& _p_change = 1.0): + bounds(eoDummyVectorNoBounds), epsilon(_size, _epsilon), + p_change(_size, _p_change) {} /** * Constructor with bounds * @param _bounds an eoRealVectorBounds that contains the bounds - * @param _epsilon the range for uniform nutation - * @param _p_change the probability to change a given coordinate + * @param _epsilon the range for uniform mutation - a double to be scaled + * @param _p_change the one probability to change all coordinates */ eoUniformMutation(eoRealVectorBounds & _bounds, const double& _epsilon, const double& _p_change = 1.0): + bounds(_bounds), epsilon(_bounds.size(), _epsilon), + p_change(_bounds.size(), _p_change) + { + // scale to the range - if any + for (unsigned i=0; i& _epsilon, + const vector& _p_change): bounds(_bounds), epsilon(_epsilon), p_change(_p_change) {} /// The class name. @@ -70,18 +90,22 @@ template class eoUniformMutation: public eoMonOp /** * Do it! - * @param _eo The cromosome undergoing the mutation + * @param _eo The indi undergoing the mutation */ bool operator()(EOT& _eo) { + // sanity check ? + if (_eo.size() != bounds.size()) + throw runtime_error("Invalid size of indi in eoUniformMutation"); + bool hasChanged=false; for (unsigned lieu=0; lieu<_eo.size(); lieu++) { - if (rng.flip(p_change)) + if (rng.flip(p_change[lieu])) { // check the bounds - double emin = _eo[lieu]-epsilon; - double emax = _eo[lieu]+epsilon; + double emin = _eo[lieu]-epsilon[lieu]; + double emax = _eo[lieu]+epsilon[lieu]; if (bounds.isMinBounded(lieu)) emin = max(bounds.minimum(lieu), emin); if (bounds.isMaxBounded(lieu)) @@ -95,8 +119,8 @@ template class eoUniformMutation: public eoMonOp private: eoRealVectorBounds & bounds; - double epsilon; - double p_change; + vector epsilon; + vector p_change; }; /** eoDetUniformMutation --> changes exactly k values of the vector @@ -113,36 +137,63 @@ template class eoDetUniformMutation: public eoMonOp * @param _epsilon the range for uniform nutation * @param number of coordinate to modify */ - eoDetUniformMutation(const double& _epsilon, const unsigned& _no = 1): - bounds(eoDummyVectorNoBounds), epsilon(_epsilon), no(_no) {} + eoDetUniformMutation(const unsigned _size, const double& _epsilon, + const unsigned& _no = 1): + bounds(eoDummyVectorNoBounds), epsilon(_size, _epsilon), no(_no) {} /** * Constructor with bounds * @param _bounds an eoRealVectorBounds that contains the bounds - * @param _epsilon the range for uniform nutation + * @param _epsilon the range for uniform nutation (to be scaled if necessary) * @param number of coordinate to modify */ eoDetUniformMutation(eoRealVectorBounds & _bounds, const double& _epsilon, const unsigned& _no = 1): - bounds(_bounds), epsilon(_epsilon), no(_no) {} + bounds(_bounds), epsilon(_bounds.size(), _epsilon), no(_no) + { + // scale to the range - if any + for (unsigned i=0; i& _epsilon, + const unsigned& _no = 1): + bounds(_bounds), epsilon(_epsilon), no(_no) + { + // scale to the range - if any + for (unsigned i=0; i class eoDetUniformMutation: public eoMonOp private: eoRealVectorBounds & bounds; - double epsilon; + vector epsilon; unsigned no; }; @@ -405,4 +456,3 @@ template class eoRealUxOver: public eoQuadOp //----------------------------------------------------------------------------- //@} #endif eoRealOp_h - diff --git a/eo/src/es/es.h b/eo/src/es/es.h index cc1727e82..fff53fbe0 100644 --- a/eo/src/es/es.h +++ b/eo/src/es/es.h @@ -25,7 +25,7 @@ //----------------------------------------------------------------------------- /** This file contains all ***INSTANCIATED*** declarations of all components - * of the library for ***REAL_VALUED*** evolution inside EO. + * of the library for ***ES-like gnptype*** evolution inside EO. * It should be included in the file that calls any of the corresponding fns * * The corresponding ***INSTANCIATED*** definitions are contained in @@ -33,7 +33,9 @@ * while the TEMPLATIZED code is define in the different make_XXX.h files * either in hte src/do dir for representation independant functions, * or in the src/es dir for representation dependent stuff. - * Note that + * + * See also real.h for the similar declarations of eoReal genotypes + * i.e. ***without*** mutation parameters attached to individuals * * Unlike most EO .h files, it does not (and should not) contain any code, * just declarations @@ -51,44 +53,77 @@ #include #include -#include +#include // one Sigma per individual +#include // one sigmal per object variable +#include // full correlation matrix per indi //Representation dependent - rewrite everything anew for each representation ////////////////////////// +/* // the genotypes -eoInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, double _d); - eoInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoMinimizingFitness _d); +eoInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, double _d); + eoInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoMinimizingFitness _d); // the operators -eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit >& _init); -eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit >& _init); +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit >& _init); +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit >& _init); +*/ //Representation INdependent //////////////////////////// // you don't need to modify that part even if you use your own representation // init pop -eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit >&); -eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit >&); +eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit >&); +eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit >&); + +eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit >&); +eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit >&); + +eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit >&); +eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit >&); // the continue's -eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval); -eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval); +eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval); +eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval); + +eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval); +eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval); + +eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval); +eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval); // the checkpoint -eoCheckPoint >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue); -eoCheckPoint >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue); +eoCheckPoint >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue); +eoCheckPoint >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue); + +eoCheckPoint >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue); +eoCheckPoint >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue); + +eoCheckPoint >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue); +eoCheckPoint >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue); // the algo -eoAlgo >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _ccontinue, eoGenOp >& _op); +eoAlgo >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _ccontinue, eoGenOp >& _op); +eoAlgo >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _ccontinue, eoGenOp >& _op); -eoAlgo >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _ccontinue, eoGenOp >& _op); +eoAlgo >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _ccontinue, eoGenOp >& _op); +eoAlgo >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _ccontinue, eoGenOp >& _op); + +eoAlgo >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _ccontinue, eoGenOp >& _op); +eoAlgo >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _ccontinue, eoGenOp >& _op); // run -void run_ea(eoAlgo >& _ga, eoPop >& _pop); -void run_ea(eoAlgo >& _ga, eoPop >& _pop); +void run_ea(eoAlgo >& _ga, eoPop >& _pop); +void run_ea(eoAlgo >& _ga, eoPop >& _pop); + +void run_ea(eoAlgo >& _ga, eoPop >& _pop); +void run_ea(eoAlgo >& _ga, eoPop >& _pop); + +void run_ea(eoAlgo >& _ga, eoPop >& _pop); +void run_ea(eoAlgo >& _ga, eoPop >& _pop); // end of parameter input (+ .status + help) // that one is not templatized, but is here for completeness diff --git a/eo/src/es/make_algo_scalar_real.cpp b/eo/src/es/make_algo_scalar_real.cpp index d93afae3a..7c48b7fd3 100644 --- a/eo/src/es/make_algo_scalar_real.cpp +++ b/eo/src/es/make_algo_scalar_real.cpp @@ -33,9 +33,6 @@ * The corresponding ***INSTANCIATED DECLARATIONS*** are contained * in src/es/real.h * while the TEMPLATIZED code is define in make_algo_scalar.h in the src/do dir - * - * Unlike most EO .h files, it does not (and should not) contain any code, - * just declarations */ // The templatized code diff --git a/eo/src/es/make_checkpoint_real.cpp b/eo/src/es/make_checkpoint_real.cpp index 60a4a5cc1..d278caf8a 100644 --- a/eo/src/es/make_checkpoint_real.cpp +++ b/eo/src/es/make_checkpoint_real.cpp @@ -33,9 +33,6 @@ * The corresponding ***INSTANCIATED DECLARATIONS*** are contained * in src/es/real.h * while the TEMPLATIZED code is define in make_checkpoint.h in the src/do dir - * - * Unlike most EO .h files, it does not (and should not) contain any code, - * just declarations */ // The templatized code diff --git a/eo/src/es/make_continue_real.cpp b/eo/src/es/make_continue_real.cpp index e53f9ecec..52574024b 100644 --- a/eo/src/es/make_continue_real.cpp +++ b/eo/src/es/make_continue_real.cpp @@ -33,9 +33,6 @@ * The corresponding ***INSTANCIATED DECLARATIONS*** are contained * in src/es/real.h * while the TEMPLATIZED code is define in make_continue.h in the src/do dir - * - * Unlike most EO .h files, it does not (and should not) contain any code, - * just declarations */ // The templatized code diff --git a/eo/src/es/make_genotype.h b/eo/src/es/make_genotype.h index 31e645302..49e8738ba 100644 --- a/eo/src/es/make_genotype.h +++ b/eo/src/es/make_genotype.h @@ -53,10 +53,10 @@ template eoInit > & do_make_genotype(eoParameterLoader& _parser, eoState& _state, FitT) { // for eoReal, only thing needed is the size - eoValueParam& vecSize = _parser.createParam(unsigned(10), "VecSize", "The number of variables ", 'n',"initialization"); + eoValueParam& vecSize = _parser.createParam(unsigned(10), "vecSize", "The number of variables ", 'n',"initialization"); // to build an eoReal Initializer, we need bounds - eoValueParam& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "InitBounds", "Bounds for uniform initialization", 'B', "initialization"); + eoValueParam& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "initBounds", "Bounds for uniform initialization", 'B', "initialization"); eoParamParamType & ppBounds = boundsParam.value(); // pair > // transform into a vector diff --git a/eo/src/es/make_op.h b/eo/src/es/make_op.h index 9c5ab1ef8..18e77e49c 100644 --- a/eo/src/es/make_op.h +++ b/eo/src/es/make_op.h @@ -35,8 +35,9 @@ // combinations of simple eoOps (eoMonOp and eoQuadOp) #include -// the specialized GA stuff +// the specialized Real stuff #include +#include #include #include // also need the parser and param includes @@ -66,14 +67,56 @@ template eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit& _init) { - // this is a temporary version, while Maarten codes the full tree-structured - // general operator input + // First, decide whether the objective variables are bounded + eoValueParam& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "objectBounds", "Bounds for variables", 'B', "Genetic Operators"); + + // get initisalizer size == vector size + // eoRealInitBounded * realInit = (eoRealInitBounded*)(&_init); + // unsigned vecSize = realInit->theBounds().size(); + + // get vector size: safer??? + EOT eoTmp; + _init(eoTmp); + unsigned vecSize = eoTmp.size(); + + // the bounds pointer + eoRealVectorBounds * ptBounds; + if (_parser.isItThere(boundsParam)) // otherwise, no bounds + { + /////Warning: this code should probably be replaced by creating + ///// some eoValueParam with specific implementation + //// in eoParser.cpp. At the moemnt, it is there (cf also make_genotype + eoParamParamType & ppBounds = boundsParam.value(); // pair > + // transform into a vector + vector v; + vector::iterator it; + for (it=ppBounds.second.begin(); itc_str()); + double r; + is >> r; + v.push_back(r); + } + // now create the eoRealVectorBounds object + if (v.size() == 2) // a min and a max for all variables + ptBounds = new eoRealVectorBounds(vecSize, v[0], v[1]); + else // no time now + throw runtime_error("Sorry, only unique bounds for all variables implemented at the moment. Come back later"); + // we need to give ownership of this pointer to somebody + /////////// end of temporary code + } + else // no param for bounds was given + ptBounds = new eoRealVectorNoBounds(vecSize); // DON'T USE eoDummyVectorNoBounds + // as it does not have any dimension + + // 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(string("SGA"), "operator", "Description of the operator (SGA only now)", 'o', "Genetic Operators"); + eoValueParam& operatorParam = _parser.createParam(string("SGA"), "operator", "Description of the operator (SGA only now)", 'o', "Genetic Operators"); - if (operatorParam.value() != string("SGA")) - throw runtime_error("Sorry, only SGA-like operator available right now\n"); + if (operatorParam.value() != string("SGA")) + throw runtime_error("Sorry, only SGA-like operator available right now\n"); // now we read Pcross and Pmut, // the relative weights for all crossovers -> proportional choice @@ -81,119 +124,119 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators" ); - // minimum check - if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) ) - throw runtime_error("Invalid pCross"); + eoValueParam& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators" ); + // minimum check + if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) ) + throw runtime_error("Invalid pCross"); - eoValueParam& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators" ); - // minimum check - if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) ) - throw runtime_error("Invalid pMut"); + eoValueParam& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators" ); + // minimum check + if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) ) + throw runtime_error("Invalid pMut"); // the crossovers ///////////////// // the parameters - eoValueParam& segmentRateParam = _parser.createParam(double(1.0), "segmentRate", "Relative rate for segment crossover", 's', "Genetic Operators" ); - // minimum check - if ( (segmentRateParam.value() < 0) ) - throw runtime_error("Invalid segmentRate"); + eoValueParam& segmentRateParam = _parser.createParam(double(1.0), "segmentRate", "Relative rate for segment crossover", 's', "Genetic Operators" ); + // minimum check + if ( (segmentRateParam.value() < 0) ) + throw runtime_error("Invalid segmentRate"); - eoValueParam& arithmeticRateParam = _parser.createParam(double(2.0), "arithmeticRate", "Relative rate for arithmetic crossover", 'A', "Genetic Operators" ); - // minimum check - if ( (arithmeticRateParam.value() < 0) ) - throw runtime_error("Invalid arithmeticRate"); + eoValueParam& arithmeticRateParam = _parser.createParam(double(2.0), "arithmeticRate", "Relative rate for arithmetic crossover", 'A', "Genetic Operators" ); + // minimum check + if ( (arithmeticRateParam.value() < 0) ) + throw runtime_error("Invalid arithmeticRate"); // minimum check - bool bCross = true; - if (segmentRateParam.value()+arithmeticRateParam.value()==0) - { - cerr << "Warning: no crossover" << endl; - bCross = false; - } + bool bCross = true; + if (segmentRateParam.value()+arithmeticRateParam.value()==0) + { + cerr << "Warning: no crossover" << endl; + bCross = false; + } - // Create the CombinedQuadOp - eoPropCombinedQuadOp *ptCombinedQuadOp = NULL; - eoQuadOp *ptQuad = NULL; + // Create the CombinedQuadOp + eoPropCombinedQuadOp *ptCombinedQuadOp = NULL; + eoQuadOp *ptQuad = NULL; - if (bCross) - { - // segment crossover for bitstring - ptQuad = new eoSegmentCrossover; - _state.storeFunctor(ptQuad); - ptCombinedQuadOp = new eoPropCombinedQuadOp(*ptQuad, segmentRateParam.value()); + 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; - _state.storeFunctor(ptQuad); - ptCombinedQuadOp->add(*ptQuad, arithmeticRateParam.value()); + ptQuad = new eoArithmeticCrossover(*ptBounds); + _state.storeFunctor(ptQuad); + ptCombinedQuadOp->add(*ptQuad, arithmeticRateParam.value()); - // don't forget to store the CombinedQuadOp - _state.storeFunctor(ptCombinedQuadOp); - } + // don't forget to store the CombinedQuadOp + _state.storeFunctor(ptCombinedQuadOp); + } - // the mutations - ///////////////// - // the parameters - eoValueParam & epsilonParam = _parser.createParam(0.01, "epsilon", "Half-size of interval for Uniform Mutation", 'e', "Genetic Operators" ); - // minimum check - if ( (epsilonParam.value() < 0) ) - throw runtime_error("Invalid epsilon"); + // the mutations + ///////////////// + // the parameters + eoValueParam & epsilonParam = _parser.createParam(0.01, "epsilon", "Half-size of interval for Uniform Mutation", 'e', "Genetic Operators" ); + // minimum check + if ( (epsilonParam.value() < 0) ) + throw runtime_error("Invalid epsilon"); - eoValueParam & uniformMutRateParam = _parser.createParam(1.0, "uniformMutRate", "Relative rate for uniform mutation", 'u', "Genetic Operators" ); - // minimum check - if ( (uniformMutRateParam.value() < 0) ) - throw runtime_error("Invalid uniformMutRate"); + eoValueParam & uniformMutRateParam = _parser.createParam(1.0, "uniformMutRate", "Relative rate for uniform mutation", 'u', "Genetic Operators" ); + // minimum check + if ( (uniformMutRateParam.value() < 0) ) + throw runtime_error("Invalid uniformMutRate"); - eoValueParam & detMutRateParam = _parser.createParam(1.0, "detMutRate", "Relative rate for deterministic uniform mutation", 'd', "Genetic Operators" ); - // minimum check - if ( (detMutRateParam.value() < 0) ) - throw runtime_error("Invalid detMutRate"); + eoValueParam & detMutRateParam = _parser.createParam(1.0, "detMutRate", "Relative rate for deterministic uniform mutation", 'd', "Genetic Operators" ); + // minimum check + if ( (detMutRateParam.value() < 0) ) + throw runtime_error("Invalid detMutRate"); - eoValueParam & normalMutRateParam = _parser.createParam(1.0, "normalMutRate", "Relative rate for Gaussian mutation", 'd', "Genetic Operators" ); - // minimum check - if ( (normalMutRateParam.value() < 0) ) - throw runtime_error("Invalid normalMutRate"); - // and the sigma - eoValueParam & sigmaParam = _parser.createParam(1.0, "sigma", "Sigma (fixed) for Gaussian mutation", 'S', "Genetic Operators" ); - // minimum check - if ( (sigmaParam.value() < 0) ) - throw runtime_error("Invalid sigma"); + eoValueParam & normalMutRateParam = _parser.createParam(1.0, "normalMutRate", "Relative rate for Gaussian mutation", 'd', "Genetic Operators" ); + // minimum check + if ( (normalMutRateParam.value() < 0) ) + throw runtime_error("Invalid normalMutRate"); + // and the sigma + eoValueParam & sigmaParam = _parser.createParam(1.0, "sigma", "Sigma (fixed) for Gaussian mutation", 'S', "Genetic Operators" ); + // minimum check + if ( (sigmaParam.value() < 0) ) + throw runtime_error("Invalid sigma"); // minimum check - bool bMut = true; - if (uniformMutRateParam.value()+detMutRateParam.value()+normalMutRateParam.value()==0) - { - cerr << "Warning: no mutation" << endl; - bMut = false; - } - if (!bCross && !bMut) - throw runtime_error("No operator called in SGA operator definition!!!"); + bool bMut = true; + if (uniformMutRateParam.value()+detMutRateParam.value()+normalMutRateParam.value()==0) + { + cerr << "Warning: no mutation" << endl; + bMut = false; + } + if (!bCross && !bMut) + throw runtime_error("No operator called in SGA operator definition!!!"); // Create the CombinedMonOp - eoPropCombinedMonOp *ptCombinedMonOp = NULL; - eoMonOp *ptMon = NULL; + eoPropCombinedMonOp *ptCombinedMonOp = NULL; + eoMonOp *ptMon = NULL; - if (bMut) - { - // uniform mutation on all components: -// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon] - ptMon = new eoUniformMutation(epsilonParam.value()); - _state.storeFunctor(ptMon); - // create the CombinedMonOp - ptCombinedMonOp = new eoPropCombinedMonOp(*ptMon, uniformMutRateParam.value()); + if (bMut) + { + // uniform mutation on all components: + // offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon] + ptMon = new eoUniformMutation(*ptBounds, epsilonParam.value()); + _state.storeFunctor(ptMon); + // create the CombinedMonOp + ptCombinedMonOp = new eoPropCombinedMonOp(*ptMon, uniformMutRateParam.value()); // mutate exactly 1 component (uniformly) per individual - ptMon = new eoDetUniformMutation(epsilonParam.value()); - _state.storeFunctor(ptMon); - ptCombinedMonOp->add(*ptMon, detMutRateParam.value()); + ptMon = new eoDetUniformMutation(*ptBounds, epsilonParam.value()); + _state.storeFunctor(ptMon); + ptCombinedMonOp->add(*ptMon, detMutRateParam.value()); - // mutate all component using Gaussian mutation - ptMon = new eoNormalMutation(sigmaParam.value()); - _state.storeFunctor(ptMon); - ptCombinedMonOp->add(*ptMon, normalMutRateParam.value()); - _state.storeFunctor(ptCombinedMonOp); - } + // mutate all component using Gaussian mutation + ptMon = new eoNormalMutation(*ptBounds, sigmaParam.value()); + _state.storeFunctor(ptMon); + ptCombinedMonOp->add(*ptMon, normalMutRateParam.value()); + _state.storeFunctor(ptCombinedMonOp); + } // now build the eoGenOp: // to simulate SGA (crossover with proba pCross + mutation with proba pMut diff --git a/eo/src/es/make_pop_real.cpp b/eo/src/es/make_pop_real.cpp index 17a8a7b31..effb6245d 100644 --- a/eo/src/es/make_pop_real.cpp +++ b/eo/src/es/make_pop_real.cpp @@ -33,9 +33,6 @@ * The corresponding ***INSTANCIATED DECLARATIONS*** are contained * in src/es/real.h * while the TEMPLATIZED code is define in make_pop.h in the src/do dir - * - * Unlike most EO .h files, it does not (and should not) contain any code, - * just declarations */ // The templatized code diff --git a/eo/src/es/make_run_real.cpp b/eo/src/es/make_run_real.cpp index 4a1e11ffc..42f50bb29 100644 --- a/eo/src/es/make_run_real.cpp +++ b/eo/src/es/make_run_real.cpp @@ -33,9 +33,6 @@ * The corresponding ***INSTANCIATED DECLARATIONS*** are contained * in src/es/real.h * while the TEMPLATIZED code is define in make_run.h in the src/do dir - * - * Unlike most EO .h files, it does not (and should not) contain any code, - * just declarations */ // The templatized code diff --git a/eo/src/utils/eoRealBounds.h b/eo/src/utils/eoRealBounds.h index d0cd8df04..52bf836d2 100644 --- a/eo/src/utils/eoRealBounds.h +++ b/eo/src/utils/eoRealBounds.h @@ -169,6 +169,9 @@ public: } }; +// one object for all - see eoRealBounds.cpp +extern eoRealNoBounds eoDummyRealNoBounds; + /** * fully bounded eoRealBound == interval */ @@ -414,10 +417,9 @@ public: // virtual desctructor (to avoid warning?) virtual ~eoRealVectorBounds(){} - /** Default Ctor + /** Default Ctor. I don't like it, as it leaves NULL pointers around */ - eoRealVectorBounds() : - vector(0) {} + eoRealVectorBounds(unsigned _dim=0) : vector(_dim) {} /** Simple bounds = minimum and maximum (allowed) */ @@ -590,14 +592,19 @@ public: class eoRealVectorNoBounds: public eoRealVectorBounds { public: - // virtual desctructor (to avoid warining?) + // virtual desctructor (to avoid warning?) virtual ~eoRealVectorNoBounds(){} /** - Simple bounds = minimum and maximum (allowed) - */ - // Ctor: nothing to do! - eoRealVectorNoBounds(unsigned _dim=0) {} + * Ctor: nothing to do, but beware of dimension: call base class ctor + */ + eoRealVectorNoBounds(unsigned _dim=0) : eoRealVectorBounds(_dim) + { + // avoid NULL pointers, even though they shoudl (at the moment) never be used! + if (_dim) + for (unsigned i=0; i<_dim; i++) + operator[](i)=&eoDummyRealNoBounds; + } virtual bool isBounded(unsigned) {return false;} @@ -651,7 +658,6 @@ public: }; -// one object for all -extern eoRealNoBounds eoDummyRealNoBounds; +// one object for all - see eoRealBounds.cpp extern eoRealVectorNoBounds eoDummyVectorNoBounds; #endif diff --git a/eo/test/real_value.h b/eo/test/real_value.h index b176edfdb..8f3a0df59 100644 --- a/eo/test/real_value.h +++ b/eo/test/real_value.h @@ -2,17 +2,17 @@ //----------------------------------------------------------------------------- -/** Just a simple function that takes an eoEsBase and sets the fitnes +/** Just a simple function that takes an eoEsBase and sets the fitnes to sphere - @param _ind A floatingpoint vector + @param _ind vector */ double real_value(const std::vector& _ind) { - double sum = 0; /* compute in double format, even if return a float */ + double sum = 0; for (unsigned i = 0; i < _ind.size(); i++) sum += _ind[i] * _ind[i]; - return sum; + return sqrt(sum); } diff --git a/eo/test/t-eoESFull.cpp b/eo/test/t-eoESFull.cpp index 177b625bc..05d9c4219 100644 --- a/eo/test/t-eoESFull.cpp +++ b/eo/test/t-eoESFull.cpp @@ -27,7 +27,7 @@ typedef eoMinimizingFitness FitT; template void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _bounds, eoValueParam _load_name); -int main(int argc, char *argv[]) +int main_function(int argc, char *argv[]) { // Create the command-line parser eoParser parser( argc, argv, "Basic EA for vector with adaptive mutations"); @@ -83,6 +83,30 @@ int main(int argc, char *argv[]) return 0; } +// A main that catches the exceptions + +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + // rng.reseed(42); + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} + template void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _bounds, eoValueParam _load_name) { @@ -90,8 +114,8 @@ void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _ eoEvalFuncPtr&> eval( real_value ); // population parameters, unfortunately these can not be altered in the state file - eoValueParam mu = _parser.createParam(unsigned(50), "mu","Size of the population"); - eoValueParamlambda_rate = _parser.createParam(float(7.0), "lambda_rate", "Factor of children to produce"); + eoValueParam mu = _parser.createParam(unsigned(7), "mu","Size of the population"); + eoValueParamlambda_rate = _parser.createParam(double(7.0), "lambda_rate", "Factor of children to produce"); if (lambda_rate.value() < 1.0f) { @@ -133,11 +157,19 @@ void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _ checkpoint.add(monitor); checkpoint.add(average); + // only mutation (== with rate 1.0) + eoMonGenOp op(mutate); + + // the selection: sequential selection + eoSequentialSelect select; + // the general breeder (lambda is a rate -> true) + eoGeneralBreeder breed(select, op, lambda_rate.value(), true); - eoProportionalGOpSel opSel; - opSel.addOp(mutate, 1.0); + // the replacement - hard-coded Comma replacement + eoCommaReplacement replace; - eoEvolutionStrategy es(checkpoint, eval, opSel, lambda_rate.value(), eoEvolutionStrategy::comma_strategy()); + // now the eoEasyEA + eoEasyEA es(checkpoint, eval, breed, replace); es(pop); diff --git a/eo/test/t-eoReal.cpp b/eo/test/t-eoReal.cpp index efc67f4d2..36dc6cd94 100644 --- a/eo/test/t-eoReal.cpp +++ b/eo/test/t-eoReal.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include "real_value.h" #include diff --git a/eo/tutorial/html/eoTutorial.html b/eo/tutorial/html/eoTutorial.html index 765ee84d8..21db2ee4f 100644 --- a/eo/tutorial/html/eoTutorial.html +++ b/eo/tutorial/html/eoTutorial.html @@ -17,7 +17,7 @@ and to

EO Tutorial

-
Version 0.94 - Feb. 18 2001
+
Version 0.95 - Apr. 27 2001

Welcome to EO tutorial/on-line documentation.

Please note that this tutorial is not supposed