Adding Evolution Strategies:

src/es/eoEsGlobalXover.h and src/es/eoEsLocalXover.h for crossover
src/es/make_XXX_es.h for user-input
test/t-eoEsAll.cpp to test

However, an old bug appeared: className was not const in eoGenOp (and derived classes)
so I had to change that throughtout the hierarchy
This commit is contained in:
evomarc 2001-05-02 10:42:32 +00:00
commit 10064ad967
28 changed files with 471 additions and 180 deletions

View file

@ -63,7 +63,7 @@ class eoGenOp : public eoOp<EOT>, public eoUF<eoPopulator<EOT> &, void>
*/
virtual unsigned max_production(void) = 0;
virtual string className() = 0;
virtual string className() const = 0;
void operator()(eoPopulator<EOT>& _pop)
{
_pop.reserve(max_production());
@ -93,7 +93,7 @@ class eoMonGenOp : public eoGenOp<EOT>
(*_it).invalidate(); // look how simple
}
string className() {return op.className();}
virtual string className() const {return op.className();}
private :
eoMonOp<EOT>& op;
};
@ -120,7 +120,7 @@ class eoBinGenOp : public eoGenOp<EOT>
if (op(a, b))
a.invalidate();
}
string className() {return op.className();}
virtual string className() const {return op.className();}
private :
eoBinOp<EOT>& op;
@ -141,7 +141,7 @@ class eoSelBinGenOp : public eoGenOp<EOT>
if (op(*_pop, sel(_pop.source())))
(*_pop).invalidate();
}
string className() {return op.className();}
virtual string className() const {return op.className();}
private :
eoBinOp<EOT>& op;
@ -170,7 +170,7 @@ class eoQuadGenOp : public eoGenOp<EOT>
b.invalidate();
}
}
string className() {return op.className();}
virtual string className() const {return op.className();}
private :
eoQuadOp<EOT>& op;

View file

@ -82,7 +82,7 @@ class eoGeneralBreeder: public eoBreed<EOT>
}
/// The class name.
string className() const { return "eoGeneralBreeder"; }
virtual string className() const { return "eoGeneralBreeder"; }
private:
eoSelectOne<EOT>& select;

View file

@ -67,7 +67,7 @@ class eoOpContainer : public eoGenOp<EOT>
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<EOT>
while (!_pop.exhausted());
}
}
virtual string className() {return "SequentialOp";}
virtual string className() const {return "SequentialOp";}
private :
@ -148,7 +148,7 @@ class eoProportionalOp : public eoOpContainer<EOT>
catch(eoPopulator<EOT>::OutOfIndividuals&)
{}
}
virtual string className() {return "ProportionalOp";}
virtual string className() const {return "ProportionalOp";}
};

View file

@ -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

View file

@ -27,13 +27,10 @@
#ifndef _eoEsChromInit_H
#define _eoEsChromInit_H
#include <utils/eoRealBounds.h>
#include <es/eoRealInitBounded.h>
#include <es/eoEsSimple.h>
#include <es/eoEsStdev.h>
#include <es/eoEsFull.h>
#include <utils/eoRNG.h>
#include <eoInit.h>
#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<double>
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 EOT>
class eoEsChromInit : public eoInit<EOT>
class eoEsChromInit : public eoRealInitBounded<EOT>
{
public :
typedef typename EOT::Fitness FitT;
typedef typename EOT::Fitness FitT;
eoEsChromInit(eoRealVectorBounds& _bounds) : bounds(_bounds)
{}
eoEsChromInit(eoRealVectorBounds& _bounds, double _sigma = 0.3) :
eoRealInitBounded<EOT>(_bounds), sigma(_sigma) {}
void operator()(EOT& _eo)
{
create(_eo);
}
void operator()(EOT& _eo)
{
eoRealInitBounded<EOT>::operator()(_eo);
create_self_adapt(_eo);
_eo.invalidate(); // was MISSING!!!!
}
// accessor to sigma
double sigmaInit() {return sigma;}
private :
eoEsSimple<FitT>& create(eoEsSimple<FitT>& result)
{
result.resize(bounds.size());
// No adaptive mutation at all
void create_self_adapt(eoReal<FitT>& 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<FitT>& 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<FitT>& result)
{
unsigned theSize = eoRealInitBounded<EOT>::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<FitT>& result)
{
unsigned theSize = eoRealInitBounded<EOT>::size();
eoEsStdev<FitT> create(eoEsStdev<FitT>& 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<FitT> create(eoEsFull<FitT>& 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

View file

@ -44,7 +44,7 @@ class eoEsFull : public eoVector<Fit, double>
eoEsFull(void) : eoVector<Fit, double>() {}
std::string className(void) const { return "eoEsFull"; }
virtual std::string className(void) const { return "eoEsFull"; }
void printOn(std::ostream& os) const
{

139
eo/src/es/eoEsGlobalXover.h Normal file
View file

@ -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 <utils/eoRNG.h>
#include <es/eoEsSimple.h>
#include <es/eoEsStdev.h>
#include <es/eoEsFull.h>
#include <eoGenOp.h>
// needs a selector - here random
#include <eoRandomSelect.h>
/** Gloabl crossover operator for ES genotypes.
* Uses some Atom crossovers to handle both the object variables
* and the mutation strategy parameters
*/
template<class EOT>
class eoEsGlobalXover: public eoGenOp<EOT>
{
public:
typedef typename EOT::Fitness FitT;
/**
* (Default) Constructor.
*/
eoEsGlobalXover(eoBinOp<double> & _crossObj, eoBinOp<double> & _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<EOT>& _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<parent.size(); i++)
{
// get extra parents - use private selector
// _plop.source() is the eoPop<EOT> 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<FitT> & _parent, const eoPop<eoEsSimple<FitT> >& _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<FitT> & _parent, const eoPop<eoEsStdev<FitT> >& _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<FitT> & _parent, const eoPop<eoEsFull<FitT> >& _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<EOT> sel;
eoBinOp<double> & crossObj;
eoBinOp<double> & crossMut;
};
#endif

124
eo/src/es/eoEsLocalXover.h Normal file
View file

@ -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 <utils/eoRNG.h>
#include <es/eoEsSimple.h>
#include <es/eoEsStdev.h>
#include <es/eoEsFull.h>
#include <eoGenOp.h>
// needs a selector - here random
#include <eoRandomSelect.h>
/** 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 EOT>
class eoEsLocalXover: public eoGenOp<EOT>
{
public:
typedef typename EOT::Fitness FitT;
/**
* (Default) Constructor.
*/
eoEsLocalXover(eoBinOp<double> & _crossObj, eoBinOp<double> & _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<EOT>& _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.size(); i++)
{
crossObj(parent1[i], parent2[i]); // apply eoBinOp
}
// then the self-adaptation parameters
cross_self_adapt(parent1, parent2);
// dont' forget to invalidate
parent1.invalidate();
}
private:
// the method to cross slef-adaptation parameters: need to specialize
void cross_self_adapt(eoEsSimple<FitT> & _parent1, const eoEsSimple<FitT> & _parent2)
{
crossMut(_parent1.stdev, _parent2.stdev); // apply eoBinOp
}
void cross_self_adapt(eoEsStdev<FitT> & _parent1, const eoEsStdev<FitT> & _parent2)
{
for (unsigned i=0; i<_parent1.size(); i++)
{
crossMut(_parent1.stdevs[i], _parent2.stdevs[i]); // apply eoBinOp
}
}
void cross_self_adapt(eoEsFull<FitT> & _parent1, const eoEsFull<FitT> & _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<EOT> sel;
eoBinOp<double> & crossObj;
eoBinOp<double> & crossMut;
};
#endif

View file

@ -122,11 +122,11 @@ public:
virtual bool operator()( eoEsStdev<FitT>& _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<eoEsSimple>: tau local " << TauLcl << endl;
}
void init(eoEsStdev<FitT>, eoEsMutationInit& _init)
@ -242,12 +243,14 @@ public:
// renormalization
TauLcl /= sqrt( 2.0 * sqrt( (double)size ) );
TauGlb /= sqrt( 2.0 * ( (double) size ) );
cout << "Init<eoStDev>: tau local " << TauLcl << " et global " << TauGlb << endl;
}
void init(eoEsFull<FitT>, eoEsMutationInit& _init)
{
init(eoEsStdev<FitT>(), _init);
TauBeta = _init.TauBeta();
cout << "Init<eoEsFull>: tau local " << TauLcl << " et global " << TauGlb << endl;
}
// the data
@ -264,14 +267,5 @@ public:
template <class EOT>
const double eoEsMutate<EOT>::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

View file

@ -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<double>* TauLclParam;
eoValueParam<double>* TauGlbParam;
eoValueParam<double>* TauBetaParam;
eoParameterLoader& parser;
std::string repSection;
eoValueParam<double>* TauLclParam;
eoValueParam<double>* TauGlbParam;
eoValueParam<double>* TauBetaParam;
};
#endif

View file

@ -52,7 +52,7 @@ public :
eoEsSimple(void) : eoVector<Fit, double>() {}
std::string className(void) const { return "eoEsSimple"; }
virtual std::string className(void) const { return "eoEsSimple"; }
void printOn(std::ostream& os) const
{

View file

@ -45,7 +45,7 @@ class eoEsStdev : public eoVector<Fit, double>
eoEsStdev(void) : eoVector<Fit, double>() {}
std::string className(void) const { return "eoEsStdev"; }
virtual std::string className(void) const { return "eoEsStdev"; }
void printOn(std::ostream& os) const
{

View file

@ -48,7 +48,7 @@ public :
eoQuadOp<vector<double> > & _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)
{

View file

@ -75,7 +75,7 @@ template<class EOT> class eoGenericUniformMutation: public eoMonOp<EOT>
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 EOT> 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 EOT> class eoGenericSegmentCrossover: public eoQuadOp<EOT>
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 EOT> 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 EOT> class eoGenericRealUxOver: public eoQuadOp<EOT>
}
/// The class name.
string className() const { return "eoRealUxOver"; }
virtual string className() const { return "eoRealUxOver"; }
/**
* Uniform crossover for real vectors

View file

@ -69,7 +69,7 @@ template<class EOT> class eoNormalMutation: public eoMonOp<EOT>
sigma(_sigma), bounds(_bounds), p_change(_p_change) {}
/// The class name.
string className() const { return "eoNormalMutation"; }
virtual string className() const { return "eoNormalMutation"; }
/**
* Do it!

View file

@ -46,7 +46,7 @@ template <class FitT> class eoReal: public eoVector<FitT, double>
eoVector<FitT, double>(size, value) {}
/// My class name.
string className() const
virtual string className() const
{
return "eoReal";
}

View file

@ -33,21 +33,30 @@
#include <es/eoReal.h>
#include <utils/eoRealBounds.h>
template <class FitT>
class eoRealInitBounded : public eoInit<eoReal<FitT> >
/** Simple initialization for any EOT that derives from vector<double>
* uniformly in some bounds
*/
template <class EOT>
class eoRealInitBounded : public eoInit<EOT>
{
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<double>");
}
/** simply passes the argument to the uniform method of the bounds */
virtual void operator()(eoReal<FitT>& _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<eoReal<FitT> >
//-----------------------------------------------------------------------------
//@}
#endif eoRealOp_h
#endif

View file

@ -87,7 +87,7 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
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 EOT> class eoDetUniformMutation: public eoMonOp<EOT>
}
/// The class name.
string className() const { return "eoDetUniformMutation"; }
virtual string className() const { return "eoDetUniformMutation"; }
/**
* Do it!
@ -273,7 +273,7 @@ template<class EOT> class eoSegmentCrossover: public eoQuadOp<EOT>
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 EOT> class eoArithmeticCrossover: public eoQuadOp<EOT>
}
/// 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 EOT> class eoRealUxOver: public eoQuadOp<EOT>
}
/// The class name.
string className() const { return "eoRealUxOver"; }
virtual string className() const { return "eoRealUxOver"; }
/**
* Uniform crossover for real vectors

View file

@ -57,18 +57,31 @@
#include <es/eoEsStdev.h> // one sigmal per object variable
#include <es/eoEsFull.h> // full correlation matrix per indi
// include all similar declaration for eoReal - i.e. real-valued genotyes
// without self-adaptation
#include <es/make_real.h>
//Representation dependent - rewrite everything anew for each representation
//////////////////////////
/*
// the genotypes
eoInit<eoEsSimple<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, double _d);
eoInit<eoEsSimple<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoMinimizingFitness _d);
eoRealInitBounded<eoEsSimple<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsSimple<double> _eo);
eoRealInitBounded<eoEsSimple<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsSimple<eoMinimizingFitness> _eo);
eoRealInitBounded<eoEsStdev<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsStdev<double> _eo);
eoRealInitBounded<eoEsStdev<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsStdev<eoMinimizingFitness> _eo);
eoRealInitBounded<eoEsFull<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsFull<double> _eo);
eoRealInitBounded<eoEsFull<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoEsFull<eoMinimizingFitness> _eo);
// the operators
eoGenOp<eoEsStdev<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoEsStdev<double> >& _init);
eoGenOp<eoEsStdev<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoEsStdev<eoMinimizingFitness> >& _init);
*/
eoGenOp<eoEsSimple<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoEsSimple<double> >& _init);
eoGenOp<eoEsSimple<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoEsSimple<eoMinimizingFitness> >& _init);
eoGenOp<eoEsStdev<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoEsStdev<double> >& _init);
eoGenOp<eoEsStdev<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoEsStdev<eoMinimizingFitness> >& _init);
eoGenOp<eoEsFull<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoEsFull<double> >& _init);
eoGenOp<eoEsFull<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoEsFull<eoMinimizingFitness> >& _init);
//Representation INdependent
////////////////////////////

View file

@ -52,11 +52,11 @@
/// The following function merely call the templatized do_* functions
eoInit<eoReal<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal<double> _eo)
eoEsChromInit<eoReal<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal<double> _eo)
{
return do_make_genotype(_parser, _state, _eo);
}
eoInit<eoReal<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal<eoMinimizingFitness> _eo)
eoEsChromInit<eoReal<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal<eoMinimizingFitness> _eo)
{
return do_make_genotype(_parser, _state, _eo);
}

View file

@ -28,7 +28,7 @@
#define _make_genotype_h
#include <es/eoReal.h>
#include <eoRealInitBounded.h>
#include <eoEsChromInit.h>
// also need the parser and param includes
#include <utils/eoParser.h>
#include <utils/eoState.h>
@ -58,7 +58,7 @@
*/
template <class EOT>
eoInit<EOT> & do_make_genotype(eoParameterLoader& _parser, eoState& _state, EOT)
eoEsChromInit<EOT> & do_make_genotype(eoParameterLoader& _parser, eoState& _state, EOT)
{
// the fitness type
typedef typename EOT::Fitness FitT;
@ -88,8 +88,16 @@ eoInit<EOT> & 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<FitT> * init =
new eoRealInitBounded<FitT>(*ptBounds);
// now some initial value for sigmas - even if useless?
// shoudl be used in Normal mutation
eoValueParam<double>& 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<EOT> * init =
new eoEsChromInit<EOT>(*ptBounds, sigmaParam.value());
// satore in state
_state.storeFunctor(init);
return *init;

View file

@ -37,18 +37,18 @@
*/
// Templatized code
#include <es/make_op.h>
#include <es/make_op_real.h>
/// The following function merely call the templatized do_* functions above
// oeprators
////////////
eoGenOp<eoReal<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoReal<double> >& _init)
eoGenOp<eoReal<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoReal<double> >& _init)
{
return do_make_op(_parser, _state, _init);
}
eoGenOp<eoReal<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoReal<eoMinimizingFitness> >& _init)
eoGenOp<eoReal<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoReal<eoMinimizingFitness> >& _init)
{
return do_make_op(_parser, _state, _init);
}

View file

@ -53,18 +53,19 @@
#include <eoGenOp.h>
#include <eoPop.h>
#include <es/eoRealInitBounded.h>
#include <es/eoReal.h>
//Representation dependent - rewrite everything anew for each representation
//////////////////////////
// the genotypes
eoInit<eoReal<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal<double> _eo);
eoInit<eoReal<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal<eoMinimizingFitness> _eo);
eoRealInitBounded<eoReal<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal<double> _eo);
eoRealInitBounded<eoReal<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoReal<eoMinimizingFitness> _eo);
// the operators
eoGenOp<eoReal<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoReal<double> >& _init);
eoGenOp<eoReal<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoReal<eoMinimizingFitness> >& _init);
eoGenOp<eoReal<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoReal<double> >& _init);
eoGenOp<eoReal<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoRealInitBounded<eoReal<eoMinimizingFitness> >& _init);
//Representation INdependent
////////////////////////////

View file

@ -72,7 +72,7 @@ eoGenOp<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EO
// general operator input
// BTW we must leave that simple version available somehow, as it is the one
// that 90% people use!
eoValueParam<string>& operatorParam = _parser.createParam(string("SGA"), "operator", "Description of the operator (SGA only now)", 'o', "Genetic Operators");
eoValueParam<string>& 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<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EO
// and create the eoGenOp that is exactly
// crossover with pcross + mutation with pmut
eoValueParam<double>& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators" );
eoValueParam<double>& 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<double>& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators" );
eoValueParam<double>& 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<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EO
// the crossovers
/////////////////
// the parameters
eoValueParam<double>& onePointRateParam = _parser.createParam(double(1.0), "onePointRate", "Relative rate for one point crossover", '1', "Genetic Operators" );
eoValueParam<double>& 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<double>& twoPointsRateParam = _parser.createParam(double(1.0), "twoPointRate", "Relative rate for two point crossover", '2', "Genetic Operators" );
eoValueParam<double>& 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<double>& uRateParam = _parser.createParam(double(2.0), "uRate", "Relative rate for uniform crossover", 'U', "Genetic Operators" );
eoValueParam<double>& 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<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EO
// the mutations
/////////////////
// the parameters
eoValueParam<double> & pMutPerBitParam = _parser.createParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators" );
eoValueParam<double> & 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<double> & bitFlipRateParam = _parser.createParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 's', "Genetic Operators" );
eoValueParam<double> & 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<double> & oneBitRateParam = _parser.createParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'd', "Genetic Operators" );
eoValueParam<double> & 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");

View file

@ -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

View file

@ -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<EOT>& pop = make_pop(parser, state, init);
apply(eval, pop);
// stopping criteria
eoContinue<EOT> & 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;

View file

@ -59,7 +59,7 @@ class monop : public eoMonOp<EOT>
_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<EOT>
_eo1.fitness(_eo1.fitness()+f);
return false;
}
string className() {return "binop";}
string className() const {return "binop";}
};
class quadop: public eoQuadOp<EOT>
{
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<EOT>
class quadClone: public eoQuadOp<EOT>
{
public :
string className() {return "quadclone";}
string className() const {return "quadclone";}
bool operator()(EOT& , EOT& ) {return false;}
};
@ -120,7 +120,7 @@ class one2threeOp : public eoGenOp<EOT> // :-)
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<EOT> // :-)
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<EOT> // :-)
@ -159,7 +159,7 @@ class three2threeOp : public eoGenOp<EOT> // :-)
// 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";}
};

View file

@ -26,7 +26,7 @@ int main(int argc, char* argv[])
eoEvalFuncCounter<EOT> eval(mainEval);
// the genotype - through a genotype initializer
eoInit<EOT>& init = make_genotype(parser, state, EOT());
eoRealInitBounded<EOT>& init = make_genotype(parser, state, EOT());
// Build the variation operator (any seq/prop construct)
eoGenOp<EOT>& 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<EOT>& pop = make_pop(parser, state, init);
apply(eval, pop);
// stopping criteria
eoContinue<EOT> & term = make_continue(parser, state, eval);
// output
eoCheckPoint<EOT> & checkpoint = make_checkpoint(parser, state, eval, term);
// algorithm (need the operator!)
eoAlgo<EOT>& ga = make_algo_scalar(parser, state, eval, checkpoint, op);
eoAlgo<EOT>& 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);