diff --git a/eo/src/eoGenOp.h b/eo/src/eoGenOp.h index 4a0f3ac7..66e0100d 100644 --- a/eo/src/eoGenOp.h +++ b/eo/src/eoGenOp.h @@ -63,7 +63,7 @@ class eoGenOp : public eoOp, public eoUF &, void> */ virtual unsigned max_production(void) = 0; - virtual string className() = 0; + virtual string className() const = 0; void operator()(eoPopulator& _pop) { _pop.reserve(max_production()); @@ -93,7 +93,7 @@ class eoMonGenOp : public eoGenOp (*_it).invalidate(); // look how simple } - string className() {return op.className();} + virtual string className() const {return op.className();} private : eoMonOp& op; }; @@ -120,7 +120,7 @@ class eoBinGenOp : public eoGenOp if (op(a, b)) a.invalidate(); } - string className() {return op.className();} + virtual string className() const {return op.className();} private : eoBinOp& op; @@ -141,7 +141,7 @@ class eoSelBinGenOp : public eoGenOp if (op(*_pop, sel(_pop.source()))) (*_pop).invalidate(); } - string className() {return op.className();} + virtual string className() const {return op.className();} private : eoBinOp& op; @@ -170,7 +170,7 @@ class eoQuadGenOp : public eoGenOp b.invalidate(); } } - string className() {return op.className();} + virtual string className() const {return op.className();} private : eoQuadOp& op; diff --git a/eo/src/eoGeneralBreeder.h b/eo/src/eoGeneralBreeder.h index 563d23a8..dfb05f8f 100644 --- a/eo/src/eoGeneralBreeder.h +++ b/eo/src/eoGeneralBreeder.h @@ -82,7 +82,7 @@ class eoGeneralBreeder: public eoBreed } /// The class name. - string className() const { return "eoGeneralBreeder"; } + virtual string className() const { return "eoGeneralBreeder"; } private: eoSelectOne& select; diff --git a/eo/src/eoOpContainer.h b/eo/src/eoOpContainer.h index a2c9f850..29c62a56 100644 --- a/eo/src/eoOpContainer.h +++ b/eo/src/eoOpContainer.h @@ -67,7 +67,7 @@ class eoOpContainer : public eoGenOp max_to_produce = max(max_to_produce,ops.back()->max_production()); } - virtual string className() = 0; + virtual string className() const = 0; protected : @@ -122,7 +122,7 @@ class eoSequentialOp : public eoOpContainer while (!_pop.exhausted()); } } - virtual string className() {return "SequentialOp";} + virtual string className() const {return "SequentialOp";} private : @@ -148,7 +148,7 @@ class eoProportionalOp : public eoOpContainer catch(eoPopulator::OutOfIndividuals&) {} } - virtual string className() {return "ProportionalOp";} + virtual string className() const {return "ProportionalOp";} }; diff --git a/eo/src/es/Makefile.am b/eo/src/es/Makefile.am index 3ff39207..8d94aae2 100644 --- a/eo/src/es/Makefile.am +++ b/eo/src/es/Makefile.am @@ -10,7 +10,8 @@ libes_a_SOURCES = make_algo_scalar_real.cpp make_checkpoint_real.cpp \ make_continue_real.cpp make_genotype_real.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 + make_continue_es.cpp make_genotype_es.cpp \ + make_op_es.cpp make_pop_es.cpp make_run_es.cpp CPPFLAGS = -Wall CXXFLAGS = -g diff --git a/eo/src/es/eoEsChromInit.h b/eo/src/es/eoEsChromInit.h index 895cbe8b..90720d3d 100644 --- a/eo/src/es/eoEsChromInit.h +++ b/eo/src/es/eoEsChromInit.h @@ -27,13 +27,10 @@ #ifndef _eoEsChromInit_H #define _eoEsChromInit_H -#include +#include #include #include #include -#include - -#include #ifndef M_PI #define M_PI 3.1415926535897932384626433832795 @@ -44,90 +41,85 @@ Random Es-chromosome initializer (therefore derived from eoInit) - This class can initialize three types of Es's: + This class can initialize four types of real-valued genotypes + thanks to tempate specialization of private method create - eoEsSimple - eoEsStdev - eoEsFull + eoReal just an eoVector + eoEsSimple + one self-adapting single sigma for all variables + eoEsStdev a whole vector of self-adapting sigmas + eoEsFull a full self-adapting correlation matrix - @see eoEsSimple eoEsStdev eoEsFull eoInit + @see eoReal eoEsSimple eoEsStdev eoEsFull eoInit */ template -class eoEsChromInit : public eoInit +class eoEsChromInit : public eoRealInitBounded { public : - typedef typename EOT::Fitness FitT; + typedef typename EOT::Fitness FitT; - eoEsChromInit(eoRealVectorBounds& _bounds) : bounds(_bounds) - {} + eoEsChromInit(eoRealVectorBounds& _bounds, double _sigma = 0.3) : + eoRealInitBounded(_bounds), sigma(_sigma) {} - void operator()(EOT& _eo) - { - create(_eo); - } + void operator()(EOT& _eo) + { + eoRealInitBounded::operator()(_eo); + create_self_adapt(_eo); + _eo.invalidate(); // was MISSING!!!! + } + + // accessor to sigma + double sigmaInit() {return sigma;} private : - eoEsSimple& create(eoEsSimple& result) - { - result.resize(bounds.size()); + // No adaptive mutation at all + void create_self_adapt(eoReal& result)// security check :-) + { + throw runtime_error("We should not be in create_self_adapt(eoReal)!"); + } - bounds.uniform(result); + // 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; + } - result.stdev = 0.3*bounds.averageRange(); // 0.3 should be read as a parameter + // Adaptive mutation through a 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; + } + } - return result; - } + // Adaptive mutation through a whole correlation matrix + void create_self_adapt(eoEsFull& result) + { + unsigned theSize = eoRealInitBounded::size(); - eoEsStdev create(eoEsStdev& result) - { - unsigned chromSize = bounds.size(); - result.resize(chromSize); - result.stdevs.resize(chromSize); - - bounds.uniform(result); - for (unsigned i = 0; i < chromSize; ++i) - { - // uniformly in [0.2,0.4) - // scaled by the range of the object variable) - // Just a guess (anyone a better idea?) - result.stdevs[i] = bounds.range(i) * (0.2 + 0.2*eo::rng.uniform()); - } - - return result; - } - - eoEsFull create(eoEsFull& result) - { - unsigned chromSize = bounds.size(); - result.resize(chromSize); - result.stdevs.resize(chromSize); - - unsigned i; - - for (i = 0; i < chromSize; ++i) - { - double length = bounds.maximum(i) - bounds.minimum(i); - result[i] = bounds.minimum(i) + rng.uniform(length); + 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; + } - // Just a guess at the stdevs (anyone a better idea?) - result.stdevs[i] = rng.uniform(length); - } - - // nb of rotation angles: N*(N-1)/2 (in general!) - result.correlations.resize(chromSize*(chromSize - 1) / 2); - - for (i = 0; i < result.correlations.size(); ++i) - { - // And another random guess for random initialization - result.correlations[i] = rng.uniform(2 * M_PI) - M_PI; - } + // nb of rotation angles: N*(N-1)/2 (in general!) + result.correlations.resize(theSize*(theSize - 1) / 2); + for (unsigned i = 0; i < result.correlations.size(); ++i) + { + // uniform in [-PI, PI) + result.correlations[i] = rng.uniform(2 * M_PI) - M_PI; + } + } - return result; - } - - eoRealVectorBounds& bounds; + double sigma; // initial value for sigmas }; #endif diff --git a/eo/src/es/eoEsFull.h b/eo/src/es/eoEsFull.h index f4f2710a..1f6eb734 100644 --- a/eo/src/es/eoEsFull.h +++ b/eo/src/es/eoEsFull.h @@ -44,7 +44,7 @@ class eoEsFull : public eoVector eoEsFull(void) : eoVector() {} - std::string className(void) const { return "eoEsFull"; } + virtual std::string className(void) const { return "eoEsFull"; } void printOn(std::ostream& os) const { diff --git a/eo/src/es/eoEsGlobalXover.h b/eo/src/es/eoEsGlobalXover.h new file mode 100644 index 00000000..ce016b56 --- /dev/null +++ b/eo/src/es/eoEsGlobalXover.h @@ -0,0 +1,139 @@ +/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoEsGlobalXover.h : ES global crossover +// (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 + 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser 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 + + Contact: marc.schoenauer@polytechnique.fr http://eeaax.cmap.polytchnique.fr/ + */ +//----------------------------------------------------------------------------- + + +#ifndef _eoEsGlobalXover_H +#define _eoEsGlobalXover_H + +#include + +#include +#include +#include + +#include +// needs a selector - here random +#include + +/** Gloabl crossover operator for ES genotypes. + * Uses some Atom crossovers to handle both the object variables + * and the mutation strategy parameters + */ +template +class eoEsGlobalXover: public eoGenOp +{ +public: + typedef typename EOT::Fitness FitT; + + /** + * (Default) Constructor. + */ + eoEsGlobalXover(eoBinOp & _crossObj, eoBinOp & _crossMut) : + crossObj(_crossObj), crossMut(_crossMut) {} + + /// The class name. Used to display statistics + virtual string className() const { return "eoEsGlobalXover"; } + + /// The TOTAL number of offspring (here = nb of parents modified in place) + unsigned max_production(void) { return 1; } + + /** + * modifies one parents in the populator + * using 2 new parents for each component! + * + * @param _pop a POPULATOR (not a simple population) + */ + void apply(eoPopulator& _plop) + { + // First, select as many parents as you will have offspring + EOT& parent = *_plop; // select the first parent + + // first, the object variables + for (unsigned i=0; i used by _plop to get parents + const EOT& realParent1 = sel(_plop.source()); + const EOT& realParent2 = sel(_plop.source()); + parent[i] = realParent1[i]; + crossObj(parent[i], realParent2[i]); // apply eoBinOp + } + // then the self-adaptation parameters + cross_self_adapt(parent, _plop.source()); + // dont' forget to invalidate + parent.invalidate(); + } + +private: + + // the method to cross slef-adaptation parameters: need to specialize + + void cross_self_adapt(eoEsSimple & _parent, const eoPop >& _pop) + { + const EOT& realParent1 = sel(_pop); + const EOT& realParent2 = sel(_pop); + _parent.stdev = realParent1.stdev; + crossMut(_parent.stdev, realParent2.stdev); // apply eoBinOp + } + + void cross_self_adapt(eoEsStdev & _parent, const eoPop >& _pop) + { + for (unsigned i=0; i<_parent.size(); i++) + { + const EOT& realParent1 = sel(_pop); + const EOT& realParent2 = sel(_pop); + _parent.stdevs[i] = realParent1.stdevs[i]; + crossMut(_parent.stdevs[i], realParent2.stdevs[i]); // apply eoBinOp + } + } + + void cross_self_adapt(eoEsFull & _parent, const eoPop >& _pop) + { + unsigned i; + // the StDev + for (i=0; i<_parent.size(); i++) + { + const EOT& realParent1 = sel(_pop); + const EOT& realParent2 = sel(_pop); + _parent.stdevs[i] = realParent1.stdevs[i]; + crossMut(_parent.stdevs[i], realParent2.stdevs[i]); // apply eoBinOp + } + // the roataion angles + for (i=0; i<_parent.correlations.size(); i++) + { + const EOT& realParent1 = sel(_pop); + const EOT& realParent2 = sel(_pop); + _parent.correlations[i] = realParent1.correlations[i]; + crossMut(_parent.correlations[i], realParent2.correlations[i]); // apply eoBinOp + } + + } + + // the data + eoRandomSelect sel; + eoBinOp & crossObj; + eoBinOp & crossMut; +}; + +#endif diff --git a/eo/src/es/eoEsLocalXover.h b/eo/src/es/eoEsLocalXover.h new file mode 100644 index 00000000..bcf1c855 --- /dev/null +++ b/eo/src/es/eoEsLocalXover.h @@ -0,0 +1,124 @@ +/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoEsLocalXover.h : ES global crossover +// (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 + 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser 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 + + Contact: marc.schoenauer@polytechnique.fr http://eeaax.cmap.polytchnique.fr/ + */ +//----------------------------------------------------------------------------- + + +#ifndef _eoEsLocalXover_H +#define _eoEsLocalXover_H + +#include + +#include +#include +#include + +#include +// needs a selector - here random +#include + +/** Local (i.e. std eoBinOp) crossover operator for ES genotypes. + * Uses some Atom crossovers to handle both the object variables + * and the mutation strategy parameters + * It is an eoGenOp and not an eoBinOp for consistency with the global version + */ +template +class eoEsLocalXover: public eoGenOp +{ +public: + typedef typename EOT::Fitness FitT; + + /** + * (Default) Constructor. + */ + eoEsLocalXover(eoBinOp & _crossObj, eoBinOp & _crossMut) : + crossObj(_crossObj), crossMut(_crossMut) {} + + /// The class name. Used to display statistics + virtual string className() const { return "eoEsLocalXover"; } + + /// The TOTAL number of offspring (here = nb of parents modified in place) + unsigned max_production(void) { return 1; } + + /** + * modifies one parents in the populator + * using a second parent + * + * @param _pop a POPULATOR (not a simple population) + */ + void apply(eoPopulator& _plop) + { + // First, select as many parents as you will have offspring + EOT& parent1 = *_plop; // select the first parent + const EOT& parent2 = sel(_plop.source()); // and the second, randomly + + // first, the object variables + for (unsigned i=0; i & _parent1, const eoEsSimple & _parent2) + { + crossMut(_parent1.stdev, _parent2.stdev); // apply eoBinOp + } + + void cross_self_adapt(eoEsStdev & _parent1, const eoEsStdev & _parent2) + { + for (unsigned i=0; i<_parent1.size(); i++) + { + crossMut(_parent1.stdevs[i], _parent2.stdevs[i]); // apply eoBinOp + } + } + + void cross_self_adapt(eoEsFull & _parent1, const eoEsFull & _parent2) + { + unsigned i; + // the StDev + for (i=0; i<_parent1.size(); i++) + { + crossMut(_parent1.stdevs[i], _parent2.stdevs[i]); // apply eoBinOp + } + // the roataion angles + for (i=0; i<_parent1.correlations.size(); i++) + { + crossMut(_parent1.correlations[i], _parent2.correlations[i]); // apply eoBinOp + } + + } + + // the data + eoRandomSelect sel; + eoBinOp & crossObj; + eoBinOp & crossMut; +}; + +#endif diff --git a/eo/src/es/eoEsMutate.h b/eo/src/es/eoEsMutate.h index bee67f2a..d6568a90 100644 --- a/eo/src/es/eoEsMutate.h +++ b/eo/src/es/eoEsMutate.h @@ -122,11 +122,11 @@ public: virtual bool operator()( eoEsStdev& _eo ) { - double global = exp(TauGlb * rng.normal()); + double global = TauGlb * rng.normal(); for (unsigned i = 0; i < _eo.size(); i++) { double stdev = _eo.stdevs[i]; - stdev *= global * exp(TauLcl * rng.normal()); + stdev *= exp( global + TauLcl * rng.normal() ); if (stdev < stdev_eps) stdev = stdev_eps; @@ -135,7 +135,7 @@ public: _eo[i] += stdev * rng.normal(); } - bounds.foldsInBounds(_eo); + bounds.foldsInBounds(_eo); return true; } @@ -158,12 +158,12 @@ public: * First: mutate standard deviations (as above). */ - double global = exp(TauGlb * rng.normal()); + double global = TauGlb * rng.normal(); unsigned i; for (i = 0; i < _eo.size(); i++) { double stdev = _eo.stdevs[i]; - stdev *= global * exp(TauLcl * rng.normal()); + stdev *= exp( global + TauLcl*rng.normal() ); if (stdev < stdev_eps) stdev = stdev_eps; @@ -229,7 +229,8 @@ public: { unsigned size = bounds.size(); TauLcl = _init.TauLcl(); - TauLcl /= sqrt((double) size); + TauLcl /= sqrt(2*(double) size); + cout << "Init: tau local " << TauLcl << endl; } void init(eoEsStdev, eoEsMutationInit& _init) @@ -242,12 +243,14 @@ public: // renormalization TauLcl /= sqrt( 2.0 * sqrt( (double)size ) ); TauGlb /= sqrt( 2.0 * ( (double) size ) ); + cout << "Init: tau local " << TauLcl << " et global " << TauGlb << endl; } void init(eoEsFull, eoEsMutationInit& _init) { init(eoEsStdev(), _init); TauBeta = _init.TauBeta(); + cout << "Init: tau local " << TauLcl << " et global " << TauGlb << endl; } // the data @@ -264,14 +267,5 @@ public: template const double eoEsMutate::stdev_eps = 1.0e-40; -/* - * Correlated mutations in ESs, according to the following - * sources: - * H.-P. Schwefel: Internal Report of KFA Juelich, KFA-STE-IB-3/80 - * p. 43, 1980 - * G. Rudolph: Globale Optimierung mit parallelen Evolutions- - * strategien, Diploma Thesis, University of Dortmund, 1990 - */ - #endif diff --git a/eo/src/es/eoEsMutationInit.h b/eo/src/es/eoEsMutationInit.h index 58f683e5..9baa340f 100644 --- a/eo/src/es/eoEsMutationInit.h +++ b/eo/src/es/eoEsMutationInit.h @@ -47,13 +47,19 @@ class eoEsMutationInit { public : - eoEsMutationInit(eoParameterLoader& _parser) : parser(_parser), TauLclParam(0), TauGlbParam(0), TauBetaParam(0) {} + eoEsMutationInit(eoParameterLoader& _parser, + std::string _section="ES mutation parameters" ) : + parser(_parser), repSection(_section), + TauLclParam(0), TauGlbParam(0), TauBetaParam(0) {} + + // because we have virtual function - size + virtual ~eoEsMutationInit(){} double TauLcl(void) { if (TauLclParam == 0) { - TauLclParam = &parser.createParam(1.0, TauLclName(), "Local Tau", TauLclShort(), section()); + TauLclParam = &parser.createParam(1.0, TauLclName(), "Local Tau (before normalization)", TauLclShort(), section()); } return TauLclParam->value(); @@ -63,7 +69,7 @@ class eoEsMutationInit { if (TauGlbParam == 0) { - TauGlbParam = &parser.createParam(1.0, TauGlbName(), "Global Tau", TauGlbShort(), section()); + TauGlbParam = &parser.createParam(1.0, TauGlbName(), "Global Tau (before normalization)", TauGlbShort(), section()); } return TauGlbParam->value(); @@ -82,7 +88,7 @@ class eoEsMutationInit protected : virtual std::string section(void) - { return "Parameters of ES mutation (before renormalization)"; } + { return repSection; } virtual std::string TauLclName(void) const { return "TauLcL"; } virtual char TauLclShort(void) const { return 'l'; } @@ -95,11 +101,11 @@ class eoEsMutationInit private : - eoParameterLoader& parser; - - eoValueParam* TauLclParam; - eoValueParam* TauGlbParam; - eoValueParam* TauBetaParam; + eoParameterLoader& parser; + std::string repSection; + eoValueParam* TauLclParam; + eoValueParam* TauGlbParam; + eoValueParam* TauBetaParam; }; #endif diff --git a/eo/src/es/eoEsSimple.h b/eo/src/es/eoEsSimple.h index c7ba1e6c..9ec29040 100644 --- a/eo/src/es/eoEsSimple.h +++ b/eo/src/es/eoEsSimple.h @@ -52,7 +52,7 @@ public : eoEsSimple(void) : eoVector() {} - std::string className(void) const { return "eoEsSimple"; } + virtual std::string className(void) const { return "eoEsSimple"; } void printOn(std::ostream& os) const { diff --git a/eo/src/es/eoEsStdev.h b/eo/src/es/eoEsStdev.h index 9c7b3831..fd43476d 100644 --- a/eo/src/es/eoEsStdev.h +++ b/eo/src/es/eoEsStdev.h @@ -45,7 +45,7 @@ class eoEsStdev : public eoVector eoEsStdev(void) : eoVector() {} - std::string className(void) const { return "eoEsStdev"; } + virtual std::string className(void) const { return "eoEsStdev"; } void printOn(std::ostream& os) const { diff --git a/eo/src/es/eoEsStdevXOver.h b/eo/src/es/eoEsStdevXOver.h index 1e0aa388..aac1e9a6 100644 --- a/eo/src/es/eoEsStdevXOver.h +++ b/eo/src/es/eoEsStdevXOver.h @@ -48,7 +48,7 @@ public : eoQuadOp > & _stdDevXOver) : objectXOver(_objectXOver), stdDevXOver(_stdDevXOver) {} - std::string className(void) const { return "eoEsStdevXOver"; } + virtual std::string className(void) const { return "eoEsStdevXOver"; } bool operator()(EOT & _eo1, EOT & _eo2) { diff --git a/eo/src/es/eoGenericRealOp.h b/eo/src/es/eoGenericRealOp.h index d037df8d..ac3eadd6 100644 --- a/eo/src/es/eoGenericRealOp.h +++ b/eo/src/es/eoGenericRealOp.h @@ -75,7 +75,7 @@ template class eoGenericUniformMutation: public eoMonOp bounds(_bounds), epsilon(_epsilon), p_change(_p_change) {} /// The class name. - string className() const { return "eoGenericUniformMutation"; } + virtual string className() const { return "eoGenericUniformMutation"; } /** * Do it! @@ -140,7 +140,7 @@ template class eoGenericDetUniformMutation: bounds(_bounds), epsilon(_epsilon), no(_no) {} /// The class name. - string className() const { return "eoGenericDetUniformMutation"; } + virtual string className() const { return "eoGenericDetUniformMutation"; } /** * Do it! @@ -208,7 +208,7 @@ template class eoGenericSegmentCrossover: public eoQuadOp bounds(_bounds), alpha(_alpha), range(1+2*_alpha) {} /// The class name. - string className() const { return "eoGenericSegmentCrossover"; } + virtual string className() const { return "eoGenericSegmentCrossover"; } /** * segment crossover - modifies both parents @@ -307,7 +307,7 @@ template class eoGenericArithmeticCrossover: } /// The class name. - string className() const { return "eoGenericArithmeticCrossover"; } + virtual string className() const { return "eoGenericArithmeticCrossover"; } /** * arithmetical crossover - modifies both parents @@ -383,7 +383,7 @@ template class eoGenericRealUxOver: public eoQuadOp } /// The class name. - string className() const { return "eoRealUxOver"; } + virtual string className() const { return "eoRealUxOver"; } /** * Uniform crossover for real vectors diff --git a/eo/src/es/eoNormalMutation.h b/eo/src/es/eoNormalMutation.h index 2c4718c4..4722a081 100644 --- a/eo/src/es/eoNormalMutation.h +++ b/eo/src/es/eoNormalMutation.h @@ -69,7 +69,7 @@ template class eoNormalMutation: public eoMonOp sigma(_sigma), bounds(_bounds), p_change(_p_change) {} /// The class name. - string className() const { return "eoNormalMutation"; } + virtual string className() const { return "eoNormalMutation"; } /** * Do it! diff --git a/eo/src/es/eoReal.h b/eo/src/es/eoReal.h index 9143f278..b239fecf 100644 --- a/eo/src/es/eoReal.h +++ b/eo/src/es/eoReal.h @@ -46,7 +46,7 @@ template class eoReal: public eoVector eoVector(size, value) {} /// My class name. - string className() const + virtual string className() const { return "eoReal"; } diff --git a/eo/src/es/eoRealInitBounded.h b/eo/src/es/eoRealInitBounded.h index bc9136b3..f35a20b4 100644 --- a/eo/src/es/eoRealInitBounded.h +++ b/eo/src/es/eoRealInitBounded.h @@ -33,21 +33,30 @@ #include #include -template -class eoRealInitBounded : public eoInit > +/** Simple initialization for any EOT that derives from vector + * uniformly in some bounds + */ +template +class eoRealInitBounded : public eoInit { public: /** Ctor - from eoRealVectorBounds */ - eoRealInitBounded(eoRealVectorBounds & _bounds):bounds(_bounds) {} + eoRealInitBounded(eoRealVectorBounds & _bounds):bounds(_bounds) + { + if (!bounds.isBounded()) + throw runtime_error("Needs bounded bounds to initialize a vector"); + } /** simply passes the argument to the uniform method of the bounds */ - virtual void operator()(eoReal& _eo) + virtual void operator()(EOT & _eo) { - bounds.uniform(_eo); // fills _eo with uniform values in bounds + bounds.uniform(_eo); // resizes, and fills uniformly in bounds + _eo.invalidate(); // was MISSING!!!! } /** accessor to the bounds */ - const eoRealVectorBounds & theBounds() {return bounds;} + virtual eoRealVectorBounds & theBounds() {return bounds;} + virtual unsigned size(){return bounds.size();} private: eoRealVectorBounds & bounds; @@ -55,5 +64,5 @@ class eoRealInitBounded : public eoInit > //----------------------------------------------------------------------------- //@} -#endif eoRealOp_h +#endif diff --git a/eo/src/es/eoRealOp.h b/eo/src/es/eoRealOp.h index 99cda3af..18271092 100644 --- a/eo/src/es/eoRealOp.h +++ b/eo/src/es/eoRealOp.h @@ -87,7 +87,7 @@ template class eoUniformMutation: public eoMonOp p_change(_p_change) {} /// The class name. - string className() const { return "eoUniformMutation"; } + virtual string className() const { return "eoUniformMutation"; } /** * Do it! @@ -191,7 +191,7 @@ template class eoDetUniformMutation: public eoMonOp } /// The class name. - string className() const { return "eoDetUniformMutation"; } + virtual string className() const { return "eoDetUniformMutation"; } /** * Do it! @@ -273,7 +273,7 @@ template class eoSegmentCrossover: public eoQuadOp bounds(_bounds), alpha(_alpha), range(1+2*_alpha) {} /// The class name. - string className() const { return "eoSegmentCrossover"; } + virtual string className() const { return "eoSegmentCrossover"; } /** * segment crossover - modifies both parents @@ -371,7 +371,7 @@ template class eoArithmeticCrossover: public eoQuadOp } /// The class name. - string className() const { return "eoArithmeticCrossover"; } + virtual string className() const { return "eoArithmeticCrossover"; } /** * arithmetical crossover - modifies both parents @@ -448,7 +448,7 @@ template class eoRealUxOver: public eoQuadOp } /// The class name. - string className() const { return "eoRealUxOver"; } + virtual string className() const { return "eoRealUxOver"; } /** * Uniform crossover for real vectors diff --git a/eo/src/es/make_es.h b/eo/src/es/make_es.h index fff53fbe..d4c45394 100644 --- a/eo/src/es/make_es.h +++ b/eo/src/es/make_es.h @@ -57,18 +57,31 @@ #include // one sigmal per object variable #include // full correlation matrix per indi +// include all similar declaration for eoReal - i.e. real-valued genotyes +// without self-adaptation +#include + //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); +eoRealInitBounded > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsSimple _eo); +eoRealInitBounded > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsSimple _eo); + +eoRealInitBounded > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsStdev _eo); +eoRealInitBounded > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsStdev _eo); + +eoRealInitBounded > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsFull _eo); +eoRealInitBounded > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsFull _eo); + + // 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, eoRealInitBounded >& _init); +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded >& _init); +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded >& _init); +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded >& _init); +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded >& _init); +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded >& _init); //Representation INdependent //////////////////////////// diff --git a/eo/src/es/make_genotype_real.cpp b/eo/src/es/make_genotype_real.cpp index 29775d24..b5ea0374 100644 --- a/eo/src/es/make_genotype_real.cpp +++ b/eo/src/es/make_genotype_real.cpp @@ -52,11 +52,11 @@ /// The following function merely call the templatized do_* functions -eoInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal _eo) +eoEsChromInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal _eo) { return do_make_genotype(_parser, _state, _eo); } -eoInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal _eo) +eoEsChromInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal _eo) { return do_make_genotype(_parser, _state, _eo); } diff --git a/eo/src/es/make_genotype_real.h b/eo/src/es/make_genotype_real.h index 236f1e12..823b509b 100644 --- a/eo/src/es/make_genotype_real.h +++ b/eo/src/es/make_genotype_real.h @@ -28,7 +28,7 @@ #define _make_genotype_h #include -#include +#include // also need the parser and param includes #include #include @@ -58,7 +58,7 @@ */ template -eoInit & do_make_genotype(eoParameterLoader& _parser, eoState& _state, EOT) +eoEsChromInit & do_make_genotype(eoParameterLoader& _parser, eoState& _state, EOT) { // the fitness type typedef typename EOT::Fitness FitT; @@ -88,8 +88,16 @@ eoInit & do_make_genotype(eoParameterLoader& _parser, eoState& _state, EOT) 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 - eoRealInitBounded * init = - new eoRealInitBounded(*ptBounds); + // now some initial value for sigmas - even if useless? + // shoudl be used in Normal mutation + eoValueParam& sigmaParam = _parser.createParam(0.3, "sigmaInit", "Initial value for Sigma(s)", 's',"initialization"); + + // minimum check + if ( (sigmaParam.value() < 0) ) + throw runtime_error("Invalid sigma"); + + eoEsChromInit * init = + new eoEsChromInit(*ptBounds, sigmaParam.value()); // satore in state _state.storeFunctor(init); return *init; diff --git a/eo/src/es/make_op_real.cpp b/eo/src/es/make_op_real.cpp index 7273de9c..b392193d 100644 --- a/eo/src/es/make_op_real.cpp +++ b/eo/src/es/make_op_real.cpp @@ -37,18 +37,18 @@ */ // Templatized code -#include +#include /// The following function merely call the templatized do_* functions above // oeprators //////////// -eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit >& _init) +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded >& _init) { return do_make_op(_parser, _state, _init); } -eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit >& _init) +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded >& _init) { return do_make_op(_parser, _state, _init); } diff --git a/eo/src/es/make_real.h b/eo/src/es/make_real.h index 3786b87c..f858e8a5 100644 --- a/eo/src/es/make_real.h +++ b/eo/src/es/make_real.h @@ -53,18 +53,19 @@ #include #include +#include #include //Representation dependent - rewrite everything anew for each representation ////////////////////////// // the genotypes -eoInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal _eo); - eoInit > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal _eo); +eoRealInitBounded > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal _eo); +eoRealInitBounded > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal _eo); // 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, eoRealInitBounded >& _init); +eoGenOp >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded >& _init); //Representation INdependent //////////////////////////// diff --git a/eo/src/ga/make_op.h b/eo/src/ga/make_op.h index 05af2eb9..4fc7968c 100644 --- a/eo/src/ga/make_op.h +++ b/eo/src/ga/make_op.h @@ -72,7 +72,7 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit& 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', "Variation Operators"); if (operatorParam.value() != string("SGA")) throw runtime_error("Only SGA-like operator available roght now\n"); @@ -83,12 +83,12 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators" ); + eoValueParam& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Variation 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" ); + eoValueParam& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Variation Operators" ); // minimum check if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) ) throw runtime_error("Invalid pMut"); @@ -96,17 +96,17 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit& onePointRateParam = _parser.createParam(double(1.0), "onePointRate", "Relative rate for one point crossover", '1', "Genetic Operators" ); + eoValueParam& onePointRateParam = _parser.createParam(double(1.0), "onePointRate", "Relative rate for one point crossover", '1', "Variation Operators" ); // minimum check if ( (onePointRateParam.value() < 0) ) throw runtime_error("Invalid onePointRate"); - eoValueParam& twoPointsRateParam = _parser.createParam(double(1.0), "twoPointRate", "Relative rate for two point crossover", '2', "Genetic Operators" ); + eoValueParam& twoPointsRateParam = _parser.createParam(double(1.0), "twoPointRate", "Relative rate for two point crossover", '2', "Variation Operators" ); // minimum check if ( (twoPointsRateParam.value() < 0) ) throw runtime_error("Invalid twoPointsRate"); - eoValueParam& uRateParam = _parser.createParam(double(2.0), "uRate", "Relative rate for uniform crossover", 'U', "Genetic Operators" ); + eoValueParam& uRateParam = _parser.createParam(double(2.0), "uRate", "Relative rate for uniform crossover", 'U', "Variation Operators" ); // minimum check if ( (uRateParam.value() < 0) ) throw runtime_error("Invalid uRate"); @@ -143,17 +143,17 @@ eoGenOp & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit & pMutPerBitParam = _parser.createParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators" ); + eoValueParam & pMutPerBitParam = _parser.createParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b', "Variation Operators" ); // minimum check if ( (pMutPerBitParam.value() < 0) || (pMutPerBitParam.value() > 0.5) ) throw runtime_error("Invalid pMutPerBit"); - eoValueParam & bitFlipRateParam = _parser.createParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 's', "Genetic Operators" ); + eoValueParam & bitFlipRateParam = _parser.createParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 's', "Variation Operators" ); // minimum check if ( (bitFlipRateParam.value() < 0) ) throw runtime_error("Invalid bitFlipRate"); - eoValueParam & oneBitRateParam = _parser.createParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'd', "Genetic Operators" ); + eoValueParam & oneBitRateParam = _parser.createParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'd', "Variation Operators" ); // minimum check if ( (oneBitRateParam.value() < 0) ) throw runtime_error("Invalid oneBitRate"); diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index 6197c15a..66899e8e 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -14,19 +14,13 @@ CXXFLAGS = -g -Wall ############################################################################### check_PROGRAMS = t-eoParetoFitness t-eoPareto t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing t-eoSSGA \ - t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoReal t-eoVector + t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoReal t-eoVector t-eoESAll TESTS=run_tests t-eoVector t-eoRandom t-eoSSGA t-eoPareto t-eoParetoFitness # removing temporarily t-eoESFull # noinst_PROGRAMS = t-eofitness t-eobin t-eoStateAndParser t-eoCheckpointing t-eoExternalEO t-eoESFull t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoReal ############################################################################### -#t_eoESFull_SOURCES = t-eoESFull.cpp real_value.h -#t_eoESFull_DEPENDENCIES = $(DEPS) -#t_eoESFull_LDFLAGS = -lm -#t_eoESFull_LDADD = $(LDADDS) -############################################################################### - t_eoRandom_SOURCES = t-eoRandom.cpp t_eoRandom_DEPENDENCIES = $(DEPS) t_eoRandom_LDADD = $(LDADDS) @@ -122,6 +116,12 @@ t_eoReal_LDADD = $(top_builddir)/src/es/libes.a $(LDADDS) ############################################################################### +t_eoESAll_SOURCES = t-eoESAll.cpp real_value.h +t_eoESAll_DEPENDENCIES = $(DEPS) $(top_builddir)/src/es/libes.a +t_eoESAll_LDFLAGS = -lm +t_eoESAll_LDADD = $(top_builddir)/src/es/libes.a $(LDADDS) +############################################################################### + t_eoSSGA_SOURCES = t-eoSSGA.cpp binary_value.h t_eoSSGA_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a t_eoSSGA_LDFLAGS = -lm diff --git a/eo/test/t-eoGA.cpp b/eo/test/t-eoGA.cpp index 7d073d65..9fa1fd66 100644 --- a/eo/test/t-eoGA.cpp +++ b/eo/test/t-eoGA.cpp @@ -36,7 +36,6 @@ int main(int argc, char* argv[]) // initialize the population - and evaluate // yes, this is representation indepedent once you have an eoInit eoPop& pop = make_pop(parser, state, init); - apply(eval, pop); // stopping criteria eoContinue & term = make_continue(parser, state, eval); @@ -52,6 +51,9 @@ int main(int argc, char* argv[]) //// GO /////// + // evaluate intial population AFTER help and status in case it takes time + apply(eval, pop); + // print it out cout << "Initial Population\n"; pop.sortedPrintOn(cout); cout << endl; diff --git a/eo/test/t-eoGenOp.cpp b/eo/test/t-eoGenOp.cpp index d885b7cb..6591ada9 100644 --- a/eo/test/t-eoGenOp.cpp +++ b/eo/test/t-eoGenOp.cpp @@ -59,7 +59,7 @@ class monop : public eoMonOp _eo.fitness(_eo.fitness()+pSize); return false; } - string className() {return sig;} + string className() const {return sig;} private: string sig; }; @@ -74,13 +74,13 @@ class binop: public eoBinOp _eo1.fitness(_eo1.fitness()+f); return false; } - string className() {return "binop";} + string className() const {return "binop";} }; class quadop: public eoQuadOp { public : - string className() {return "quadop";} + string className() const {return "quadop";} bool operator()(EOT& a, EOT& b) { EOT oi = a; @@ -98,7 +98,7 @@ class quadop: public eoQuadOp class quadClone: public eoQuadOp { public : - string className() {return "quadclone";} + string className() const {return "quadclone";} bool operator()(EOT& , EOT& ) {return false;} }; @@ -120,7 +120,7 @@ class one2threeOp : public eoGenOp // :-) eo.s = "v(" + eo.s + ", 0)"; // only now change the thing // oh right, and invalidate fitnesses } - virtual string className() {return "one2threeOp";} + virtual string className() const {return "one2threeOp";} }; @@ -136,7 +136,7 @@ class two2oneOp : public eoGenOp // :-) eo.s = "221(" + eo.s + ", " + eo2.s + ")"; // oh right, and invalidate fitnesses } - virtual string className() {return "two2oneOp";} + virtual string className() const {return "two2oneOp";} }; class three2threeOp : public eoGenOp // :-) @@ -159,7 +159,7 @@ class three2threeOp : public eoGenOp // :-) // oh right, and invalidate fitnesses cout << "les enfants: a=" << eo1 << " et b=" << eo2 << " et c=" << eo3 << endl; } - virtual string className() {return "three2threeOp";} + virtual string className() const {return "three2threeOp";} }; diff --git a/eo/test/t-eoReal.cpp b/eo/test/t-eoReal.cpp index 42d80a0c..09e7ced5 100644 --- a/eo/test/t-eoReal.cpp +++ b/eo/test/t-eoReal.cpp @@ -26,7 +26,7 @@ int main(int argc, char* argv[]) eoEvalFuncCounter eval(mainEval); // the genotype - through a genotype initializer - eoInit& init = make_genotype(parser, state, EOT()); + eoRealInitBounded& init = make_genotype(parser, state, EOT()); // Build the variation operator (any seq/prop construct) eoGenOp& op = make_op(parser, state, init); @@ -37,14 +37,13 @@ int main(int argc, char* argv[]) // initialize the population - and evaluate // yes, this is representation indepedent once you have an eoInit eoPop& pop = make_pop(parser, state, init); - apply(eval, pop); // stopping criteria eoContinue & term = make_continue(parser, state, eval); // output eoCheckPoint & checkpoint = make_checkpoint(parser, state, eval, term); // algorithm (need the operator!) - eoAlgo& ga = make_algo_scalar(parser, state, eval, checkpoint, op); + eoAlgo& ea = make_algo_scalar(parser, state, eval, checkpoint, op); ///// End of construction of the algorith ///////////////////////////////////////// @@ -53,11 +52,14 @@ int main(int argc, char* argv[]) //// GO /////// + // evaluate intial population AFTER help and status in case it takes time + apply(eval, pop); + // print it out cout << "Initial Population\n"; pop.sortedPrintOn(cout); cout << endl; - run_ea(ga, pop); // run the ga + run_ea(ea, pop); // run the ea cout << "Final Population\n"; pop.sortedPrintOn(cout);