Added bounds to the real operators in make_XXX (and hence in t-eoReal)

This commit is contained in:
evomarc 2001-04-28 05:47:18 +00:00
commit a7042bffee
16 changed files with 330 additions and 181 deletions

View file

@ -48,7 +48,7 @@ template <class EOT>
eoPop<EOT>& do_make_pop(eoParser & _parser, eoState& _state, eoInit<EOT> & _init) eoPop<EOT>& do_make_pop(eoParser & _parser, eoState& _state, eoInit<EOT> & _init)
{ {
eoValueParam<uint32>& seedParam = _parser.createParam(uint32(time(0)), "seed", "Random number seed", 'S'); eoValueParam<uint32>& seedParam = _parser.createParam(uint32(time(0)), "seed", "Random number seed", 'S');
eoValueParam<unsigned>& popSize = _parser.createParam(unsigned(20), "PopSize", "Population Size", 'P', "initialization"); eoValueParam<unsigned>& popSize = _parser.createParam(unsigned(20), "popSize", "Population Size", 'P', "initialization");
// Either load or initialize // Either load or initialize
// create an empty pop and let the state handle the memory // create an empty pop and let the state handle the memory

View file

@ -6,14 +6,12 @@
INCLUDES = -I$(top_builddir)/src INCLUDES = -I$(top_builddir)/src
lib_LIBRARIES = libes.a lib_LIBRARIES = libes.a
libes_a_SOURCES = make_algo_scalar_real.cpp \ libes_a_SOURCES = make_algo_scalar_real.cpp make_checkpoint_real.cpp \
make_checkpoint_real.cpp \ make_continue_real.cpp make_genotype_real.cpp make_help.cpp \
make_continue_real.cpp \ make_op_real.cpp make_pop_real.cpp make_run_real.cpp \
make_genotype_real.cpp \ make_algo_scalar_es.cpp make_checkpoint_es.cpp \
make_help.cpp \ make_continue_es.cpp make_pop_es.cpp make_run_es.cpp
make_op_real.cpp \
make_pop_real.cpp \
make_run_real.cpp
CPPFLAGS = -Wall CPPFLAGS = -Wall
CXXFLAGS = -g CXXFLAGS = -g
libeoincdir = $(includedir)/eo/es libeoincdir = $(includedir)/eo/es

View file

@ -2,7 +2,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoRealOp.h // 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 This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -52,17 +52,37 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
* @param _epsilon the range for uniform nutation * @param _epsilon the range for uniform nutation
* @param _p_change the probability to change a given coordinate * @param _p_change the probability to change a given coordinate
*/ */
eoUniformMutation(const double& _epsilon, const double& _p_change = 1.0): eoUniformMutation(const unsigned _size, const double& _epsilon,
bounds(eoDummyVectorNoBounds), epsilon(_epsilon), p_change(_p_change) {} const double& _p_change = 1.0):
bounds(eoDummyVectorNoBounds), epsilon(_size, _epsilon),
p_change(_size, _p_change) {}
/** /**
* Constructor with bounds * Constructor with bounds
* @param _bounds an eoRealVectorBounds that contains the bounds * @param _bounds an eoRealVectorBounds that contains the bounds
* @param _epsilon the range for uniform nutation * @param _epsilon the range for uniform mutation - a double to be scaled
* @param _p_change the probability to change a given coordinate * @param _p_change the one probability to change all coordinates
*/ */
eoUniformMutation(eoRealVectorBounds & _bounds, eoUniformMutation(eoRealVectorBounds & _bounds,
const double& _epsilon, const double& _p_change = 1.0): 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<bounds.size(); i++)
if (bounds.isBounded(i))
epsilon[i] *= _epsilon*bounds.range(i);
}
/**
* Constructor with bounds
* @param _bounds an eoRealVectorBounds that contains the bounds
* @param _epsilon the VECTOR of ranges for uniform mutation
* @param _p_change the VECTOR of probabilities for each coordinates
*/
eoUniformMutation(eoRealVectorBounds & _bounds,
const vector<double>& _epsilon,
const vector<double>& _p_change):
bounds(_bounds), epsilon(_epsilon), p_change(_p_change) {} bounds(_bounds), epsilon(_epsilon), p_change(_p_change) {}
/// The class name. /// The class name.
@ -70,18 +90,22 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
/** /**
* Do it! * Do it!
* @param _eo The cromosome undergoing the mutation * @param _eo The indi undergoing the mutation
*/ */
bool operator()(EOT& _eo) bool operator()(EOT& _eo)
{ {
// sanity check ?
if (_eo.size() != bounds.size())
throw runtime_error("Invalid size of indi in eoUniformMutation");
bool hasChanged=false; bool hasChanged=false;
for (unsigned lieu=0; lieu<_eo.size(); lieu++) for (unsigned lieu=0; lieu<_eo.size(); lieu++)
{ {
if (rng.flip(p_change)) if (rng.flip(p_change[lieu]))
{ {
// check the bounds // check the bounds
double emin = _eo[lieu]-epsilon; double emin = _eo[lieu]-epsilon[lieu];
double emax = _eo[lieu]+epsilon; double emax = _eo[lieu]+epsilon[lieu];
if (bounds.isMinBounded(lieu)) if (bounds.isMinBounded(lieu))
emin = max(bounds.minimum(lieu), emin); emin = max(bounds.minimum(lieu), emin);
if (bounds.isMaxBounded(lieu)) if (bounds.isMaxBounded(lieu))
@ -95,8 +119,8 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
private: private:
eoRealVectorBounds & bounds; eoRealVectorBounds & bounds;
double epsilon; vector<double> epsilon;
double p_change; vector<double> p_change;
}; };
/** eoDetUniformMutation --> changes exactly k values of the vector /** eoDetUniformMutation --> changes exactly k values of the vector
@ -113,36 +137,63 @@ template<class EOT> class eoDetUniformMutation: public eoMonOp<EOT>
* @param _epsilon the range for uniform nutation * @param _epsilon the range for uniform nutation
* @param number of coordinate to modify * @param number of coordinate to modify
*/ */
eoDetUniformMutation(const double& _epsilon, const unsigned& _no = 1): eoDetUniformMutation(const unsigned _size, const double& _epsilon,
bounds(eoDummyVectorNoBounds), epsilon(_epsilon), no(_no) {} const unsigned& _no = 1):
bounds(eoDummyVectorNoBounds), epsilon(_size, _epsilon), no(_no) {}
/** /**
* Constructor with bounds * Constructor with bounds
* @param _bounds an eoRealVectorBounds that contains the 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 * @param number of coordinate to modify
*/ */
eoDetUniformMutation(eoRealVectorBounds & _bounds, eoDetUniformMutation(eoRealVectorBounds & _bounds,
const double& _epsilon, const unsigned& _no = 1): 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<bounds.size(); i++)
if (bounds.isBounded(i))
epsilon[i] *= _epsilon*bounds.range(i);
}
/**
* Constructor with bounds and full vector of epsilon
* @param _bounds an eoRealVectorBounds that contains the bounds
* @param _epsilon the VECTOR of ranges for uniform mutation
* @param number of coordinate to modify
*/
eoDetUniformMutation(eoRealVectorBounds & _bounds,
const vector<double>& _epsilon,
const unsigned& _no = 1):
bounds(_bounds), epsilon(_epsilon), no(_no)
{
// scale to the range - if any
for (unsigned i=0; i<bounds.size(); i++)
if (bounds.isBounded(i))
epsilon[i] *= _epsilon*bounds.range(i);
}
/// The class name. /// The class name.
string className() const { return "eoDetUniformMutation"; } string className() const { return "eoDetUniformMutation"; }
/** /**
* Do it! * Do it!
* @param _eo The cromosome undergoing the mutation * @param _eo The indi undergoing the mutation
*/ */
bool operator()(EOT& _eo) bool operator()(EOT& _eo)
{ {
// sanity check ?
if (_eo.size() != bounds.size())
throw runtime_error("Invalid size of indi in eoDetUniformMutation");
for (unsigned i=0; i<no; i++) for (unsigned i=0; i<no; i++)
{ {
unsigned lieu = rng.random(_eo.size()); unsigned lieu = rng.random(_eo.size());
// actually, we should test that we don't re-modify same variable! // actually, we should test that we don't re-modify same variable!
// check the bounds // check the bounds
double emin = _eo[lieu]-epsilon; double emin = _eo[lieu]-epsilon[lieu];
double emax = _eo[lieu]+epsilon; double emax = _eo[lieu]+epsilon[lieu];
if (bounds.isMinBounded(lieu)) if (bounds.isMinBounded(lieu))
emin = max(bounds.minimum(lieu), emin); emin = max(bounds.minimum(lieu), emin);
if (bounds.isMaxBounded(lieu)) if (bounds.isMaxBounded(lieu))
@ -155,7 +206,7 @@ template<class EOT> class eoDetUniformMutation: public eoMonOp<EOT>
private: private:
eoRealVectorBounds & bounds; eoRealVectorBounds & bounds;
double epsilon; vector<double> epsilon;
unsigned no; unsigned no;
}; };
@ -405,4 +456,3 @@ template<class EOT> class eoRealUxOver: public eoQuadOp<EOT>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
//@} //@}
#endif eoRealOp_h #endif eoRealOp_h

View file

@ -25,7 +25,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** This file contains all ***INSTANCIATED*** declarations of all components /** 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 * It should be included in the file that calls any of the corresponding fns
* *
* The corresponding ***INSTANCIATED*** definitions are contained in * The corresponding ***INSTANCIATED*** definitions are contained in
@ -33,7 +33,9 @@
* while the TEMPLATIZED code is define in the different make_XXX.h files * while the TEMPLATIZED code is define in the different make_XXX.h files
* either in hte src/do dir for representation independant functions, * either in hte src/do dir for representation independant functions,
* or in the src/es dir for representation dependent stuff. * 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, * Unlike most EO .h files, it does not (and should not) contain any code,
* just declarations * just declarations
@ -51,44 +53,77 @@
#include <eoGenOp.h> #include <eoGenOp.h>
#include <eoPop.h> #include <eoPop.h>
#include <es/eoReal.h> #include <es/eoEsSimple.h> // one Sigma per individual
#include <es/eoEsStdev.h> // one sigmal per object variable
#include <es/eoEsFull.h> // full correlation matrix per indi
//Representation dependent - rewrite everything anew for each representation //Representation dependent - rewrite everything anew for each representation
////////////////////////// //////////////////////////
/*
// the genotypes // the genotypes
eoInit<eoReal<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, double _d); eoInit<eoEsSimple<double> > & make_genotype(eoParameterLoader& _parser, eoState& _state, double _d);
eoInit<eoReal<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoMinimizingFitness _d); eoInit<eoEsSimple<eoMinimizingFitness> > & make_genotype(eoParameterLoader& _parser, eoState& _state, eoMinimizingFitness _d);
// the operators // the operators
eoGenOp<eoReal<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoReal<double> >& _init); eoGenOp<eoEsStdev<double> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoEsStdev<double> >& _init);
eoGenOp<eoReal<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoReal<eoMinimizingFitness> >& _init); eoGenOp<eoEsStdev<eoMinimizingFitness> >& make_op(eoParameterLoader& _parser, eoState& _state, eoInit<eoEsStdev<eoMinimizingFitness> >& _init);
*/
//Representation INdependent //Representation INdependent
//////////////////////////// ////////////////////////////
// you don't need to modify that part even if you use your own representation // you don't need to modify that part even if you use your own representation
// init pop // init pop
eoPop<eoReal<double> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoReal<double> >&); eoPop<eoEsSimple<double> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoEsSimple<double> >&);
eoPop<eoReal<eoMinimizingFitness> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoReal<eoMinimizingFitness> >&); eoPop<eoEsSimple<eoMinimizingFitness> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoEsSimple<eoMinimizingFitness> >&);
eoPop<eoEsStdev<double> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoEsStdev<double> >&);
eoPop<eoEsStdev<eoMinimizingFitness> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoEsStdev<eoMinimizingFitness> >&);
eoPop<eoEsFull<double> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoEsFull<double> >&);
eoPop<eoEsFull<eoMinimizingFitness> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoEsFull<eoMinimizingFitness> >&);
// the continue's // the continue's
eoContinue<eoReal<double> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoReal<double> > & _eval); eoContinue<eoEsSimple<double> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoEsSimple<double> > & _eval);
eoContinue<eoReal<eoMinimizingFitness> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoReal<eoMinimizingFitness> > & _eval); eoContinue<eoEsSimple<eoMinimizingFitness> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoEsSimple<eoMinimizingFitness> > & _eval);
eoContinue<eoEsStdev<double> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoEsStdev<double> > & _eval);
eoContinue<eoEsStdev<eoMinimizingFitness> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoEsStdev<eoMinimizingFitness> > & _eval);
eoContinue<eoEsFull<double> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoEsFull<double> > & _eval);
eoContinue<eoEsFull<eoMinimizingFitness> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoEsFull<eoMinimizingFitness> > & _eval);
// the checkpoint // the checkpoint
eoCheckPoint<eoReal<double> >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter<eoReal<double> >& _eval, eoContinue<eoReal<double> >& _continue); eoCheckPoint<eoEsSimple<double> >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter<eoEsSimple<double> >& _eval, eoContinue<eoEsSimple<double> >& _continue);
eoCheckPoint<eoReal<eoMinimizingFitness> >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter<eoReal<eoMinimizingFitness> >& _eval, eoContinue<eoReal<eoMinimizingFitness> >& _continue); eoCheckPoint<eoEsSimple<eoMinimizingFitness> >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter<eoEsSimple<eoMinimizingFitness> >& _eval, eoContinue<eoEsSimple<eoMinimizingFitness> >& _continue);
eoCheckPoint<eoEsStdev<double> >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter<eoEsStdev<double> >& _eval, eoContinue<eoEsStdev<double> >& _continue);
eoCheckPoint<eoEsStdev<eoMinimizingFitness> >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter<eoEsStdev<eoMinimizingFitness> >& _eval, eoContinue<eoEsStdev<eoMinimizingFitness> >& _continue);
eoCheckPoint<eoEsFull<double> >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter<eoEsFull<double> >& _eval, eoContinue<eoEsFull<double> >& _continue);
eoCheckPoint<eoEsFull<eoMinimizingFitness> >& make_checkpoint(eoParameterLoader& _parser, eoState& _state, eoEvalFuncCounter<eoEsFull<eoMinimizingFitness> >& _eval, eoContinue<eoEsFull<eoMinimizingFitness> >& _continue);
// the algo // the algo
eoAlgo<eoReal<double> >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc<eoReal<double> >& _eval, eoContinue<eoReal<double> >& _ccontinue, eoGenOp<eoReal<double> >& _op); eoAlgo<eoEsSimple<double> >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc<eoEsSimple<double> >& _eval, eoContinue<eoEsSimple<double> >& _ccontinue, eoGenOp<eoEsSimple<double> >& _op);
eoAlgo<eoEsSimple<eoMinimizingFitness> >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc<eoEsSimple<eoMinimizingFitness> >& _eval, eoContinue<eoEsSimple<eoMinimizingFitness> >& _ccontinue, eoGenOp<eoEsSimple<eoMinimizingFitness> >& _op);
eoAlgo<eoReal<eoMinimizingFitness> >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc<eoReal<eoMinimizingFitness> >& _eval, eoContinue<eoReal<eoMinimizingFitness> >& _ccontinue, eoGenOp<eoReal<eoMinimizingFitness> >& _op); eoAlgo<eoEsStdev<double> >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc<eoEsStdev<double> >& _eval, eoContinue<eoEsStdev<double> >& _ccontinue, eoGenOp<eoEsStdev<double> >& _op);
eoAlgo<eoEsStdev<eoMinimizingFitness> >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc<eoEsStdev<eoMinimizingFitness> >& _eval, eoContinue<eoEsStdev<eoMinimizingFitness> >& _ccontinue, eoGenOp<eoEsStdev<eoMinimizingFitness> >& _op);
eoAlgo<eoEsFull<double> >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc<eoEsFull<double> >& _eval, eoContinue<eoEsFull<double> >& _ccontinue, eoGenOp<eoEsFull<double> >& _op);
eoAlgo<eoEsFull<eoMinimizingFitness> >& make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc<eoEsFull<eoMinimizingFitness> >& _eval, eoContinue<eoEsFull<eoMinimizingFitness> >& _ccontinue, eoGenOp<eoEsFull<eoMinimizingFitness> >& _op);
// run // run
void run_ea(eoAlgo<eoReal<double> >& _ga, eoPop<eoReal<double> >& _pop); void run_ea(eoAlgo<eoEsSimple<double> >& _ga, eoPop<eoEsSimple<double> >& _pop);
void run_ea(eoAlgo<eoReal<eoMinimizingFitness> >& _ga, eoPop<eoReal<eoMinimizingFitness> >& _pop); void run_ea(eoAlgo<eoEsSimple<eoMinimizingFitness> >& _ga, eoPop<eoEsSimple<eoMinimizingFitness> >& _pop);
void run_ea(eoAlgo<eoEsStdev<double> >& _ga, eoPop<eoEsStdev<double> >& _pop);
void run_ea(eoAlgo<eoEsStdev<eoMinimizingFitness> >& _ga, eoPop<eoEsStdev<eoMinimizingFitness> >& _pop);
void run_ea(eoAlgo<eoEsFull<double> >& _ga, eoPop<eoEsFull<double> >& _pop);
void run_ea(eoAlgo<eoEsFull<eoMinimizingFitness> >& _ga, eoPop<eoEsFull<eoMinimizingFitness> >& _pop);
// end of parameter input (+ .status + help) // end of parameter input (+ .status + help)
// that one is not templatized, but is here for completeness // that one is not templatized, but is here for completeness

View file

@ -33,9 +33,6 @@
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained * The corresponding ***INSTANCIATED DECLARATIONS*** are contained
* in src/es/real.h * in src/es/real.h
* while the TEMPLATIZED code is define in make_algo_scalar.h in the src/do dir * 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 // The templatized code

View file

@ -33,9 +33,6 @@
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained * The corresponding ***INSTANCIATED DECLARATIONS*** are contained
* in src/es/real.h * in src/es/real.h
* while the TEMPLATIZED code is define in make_checkpoint.h in the src/do dir * 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 // The templatized code

View file

@ -33,9 +33,6 @@
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained * The corresponding ***INSTANCIATED DECLARATIONS*** are contained
* in src/es/real.h * in src/es/real.h
* while the TEMPLATIZED code is define in make_continue.h in the src/do dir * 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 // The templatized code

View file

@ -53,10 +53,10 @@ template <class FitT>
eoInit<eoReal<FitT> > & do_make_genotype(eoParameterLoader& _parser, eoState& _state, FitT) eoInit<eoReal<FitT> > & do_make_genotype(eoParameterLoader& _parser, eoState& _state, FitT)
{ {
// for eoReal, only thing needed is the size // for eoReal, only thing needed is the size
eoValueParam<unsigned>& vecSize = _parser.createParam(unsigned(10), "VecSize", "The number of variables ", 'n',"initialization"); eoValueParam<unsigned>& vecSize = _parser.createParam(unsigned(10), "vecSize", "The number of variables ", 'n',"initialization");
// to build an eoReal Initializer, we need bounds // to build an eoReal Initializer, we need bounds
eoValueParam<eoParamParamType>& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "InitBounds", "Bounds for uniform initialization", 'B', "initialization"); eoValueParam<eoParamParamType>& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "initBounds", "Bounds for uniform initialization", 'B', "initialization");
eoParamParamType & ppBounds = boundsParam.value(); // pair<string,vector<string> > eoParamParamType & ppBounds = boundsParam.value(); // pair<string,vector<string> >
// transform into a vector<double> // transform into a vector<double>

View file

@ -35,8 +35,9 @@
// combinations of simple eoOps (eoMonOp and eoQuadOp) // combinations of simple eoOps (eoMonOp and eoQuadOp)
#include <eoProportionalCombinedOp.h> #include <eoProportionalCombinedOp.h>
// the specialized GA stuff // the specialized Real stuff
#include <es/eoReal.h> #include <es/eoReal.h>
#include <es/eoRealInitBounded.h>
#include <es/eoRealOp.h> #include <es/eoRealOp.h>
#include <es/eoNormalMutation.h> #include <es/eoNormalMutation.h>
// also need the parser and param includes // also need the parser and param includes
@ -66,8 +67,50 @@
template <class EOT> template <class EOT>
eoGenOp<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EOT>& _init) eoGenOp<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EOT>& _init)
{ {
// this is a temporary version, while Maarten codes the full tree-structured // First, decide whether the objective variables are bounded
// general operator input eoValueParam<eoParamParamType>& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "objectBounds", "Bounds for variables", 'B', "Genetic Operators");
// get initisalizer size == vector size
// eoRealInitBounded<EOT> * realInit = (eoRealInitBounded<EOT>*)(&_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<eoRealVectorBounds> with specific implementation
//// in eoParser.cpp. At the moemnt, it is there (cf also make_genotype
eoParamParamType & ppBounds = boundsParam.value(); // pair<string,vector<string> >
// transform into a vector<double>
vector<double> v;
vector<string>::iterator it;
for (it=ppBounds.second.begin(); it<ppBounds.second.end(); it++)
{
istrstream is(it->c_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 // BTW we must leave that simple version available somehow, as it is the one
// that 90% people use! // 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', "Genetic Operators");
@ -118,13 +161,13 @@ eoGenOp<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EO
if (bCross) if (bCross)
{ {
// segment crossover for bitstring // segment crossover for bitstring - pass it the bounds
ptQuad = new eoSegmentCrossover<EOT>; ptQuad = new eoSegmentCrossover<EOT>(*ptBounds);
_state.storeFunctor(ptQuad); _state.storeFunctor(ptQuad);
ptCombinedQuadOp = new eoPropCombinedQuadOp<EOT>(*ptQuad, segmentRateParam.value()); ptCombinedQuadOp = new eoPropCombinedQuadOp<EOT>(*ptQuad, segmentRateParam.value());
// arithmetic crossover // arithmetic crossover
ptQuad = new eoArithmeticCrossover<EOT>; ptQuad = new eoArithmeticCrossover<EOT>(*ptBounds);
_state.storeFunctor(ptQuad); _state.storeFunctor(ptQuad);
ptCombinedQuadOp->add(*ptQuad, arithmeticRateParam.value()); ptCombinedQuadOp->add(*ptQuad, arithmeticRateParam.value());
@ -177,19 +220,19 @@ eoGenOp<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EO
if (bMut) if (bMut)
{ {
// uniform mutation on all components: // uniform mutation on all components:
// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon] // offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
ptMon = new eoUniformMutation<EOT>(epsilonParam.value()); ptMon = new eoUniformMutation<EOT>(*ptBounds, epsilonParam.value());
_state.storeFunctor(ptMon); _state.storeFunctor(ptMon);
// create the CombinedMonOp // create the CombinedMonOp
ptCombinedMonOp = new eoPropCombinedMonOp<EOT>(*ptMon, uniformMutRateParam.value()); ptCombinedMonOp = new eoPropCombinedMonOp<EOT>(*ptMon, uniformMutRateParam.value());
// mutate exactly 1 component (uniformly) per individual // mutate exactly 1 component (uniformly) per individual
ptMon = new eoDetUniformMutation<EOT>(epsilonParam.value()); ptMon = new eoDetUniformMutation<EOT>(*ptBounds, epsilonParam.value());
_state.storeFunctor(ptMon); _state.storeFunctor(ptMon);
ptCombinedMonOp->add(*ptMon, detMutRateParam.value()); ptCombinedMonOp->add(*ptMon, detMutRateParam.value());
// mutate all component using Gaussian mutation // mutate all component using Gaussian mutation
ptMon = new eoNormalMutation<EOT>(sigmaParam.value()); ptMon = new eoNormalMutation<EOT>(*ptBounds, sigmaParam.value());
_state.storeFunctor(ptMon); _state.storeFunctor(ptMon);
ptCombinedMonOp->add(*ptMon, normalMutRateParam.value()); ptCombinedMonOp->add(*ptMon, normalMutRateParam.value());
_state.storeFunctor(ptCombinedMonOp); _state.storeFunctor(ptCombinedMonOp);

View file

@ -33,9 +33,6 @@
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained * The corresponding ***INSTANCIATED DECLARATIONS*** are contained
* in src/es/real.h * in src/es/real.h
* while the TEMPLATIZED code is define in make_pop.h in the src/do dir * 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 // The templatized code

View file

@ -33,9 +33,6 @@
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained * The corresponding ***INSTANCIATED DECLARATIONS*** are contained
* in src/es/real.h * in src/es/real.h
* while the TEMPLATIZED code is define in make_run.h in the src/do dir * 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 // The templatized code

View file

@ -169,6 +169,9 @@ public:
} }
}; };
// one object for all - see eoRealBounds.cpp
extern eoRealNoBounds eoDummyRealNoBounds;
/** /**
* fully bounded eoRealBound == interval * fully bounded eoRealBound == interval
*/ */
@ -414,10 +417,9 @@ public:
// virtual desctructor (to avoid warning?) // virtual desctructor (to avoid warning?)
virtual ~eoRealVectorBounds(){} virtual ~eoRealVectorBounds(){}
/** Default Ctor /** Default Ctor. I don't like it, as it leaves NULL pointers around
*/ */
eoRealVectorBounds() : eoRealVectorBounds(unsigned _dim=0) : vector<eoRealBounds *>(_dim) {}
vector<eoRealBounds *>(0) {}
/** Simple bounds = minimum and maximum (allowed) /** Simple bounds = minimum and maximum (allowed)
*/ */
@ -590,14 +592,19 @@ public:
class eoRealVectorNoBounds: public eoRealVectorBounds class eoRealVectorNoBounds: public eoRealVectorBounds
{ {
public: public:
// virtual desctructor (to avoid warining?) // virtual desctructor (to avoid warning?)
virtual ~eoRealVectorNoBounds(){} virtual ~eoRealVectorNoBounds(){}
/** /**
Simple bounds = minimum and maximum (allowed) * Ctor: nothing to do, but beware of dimension: call base class ctor
*/ */
// Ctor: nothing to do! eoRealVectorNoBounds(unsigned _dim=0) : eoRealVectorBounds(_dim)
eoRealVectorNoBounds(unsigned _dim=0) {} {
// 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;} virtual bool isBounded(unsigned) {return false;}
@ -651,7 +658,6 @@ public:
}; };
// one object for all // one object for all - see eoRealBounds.cpp
extern eoRealNoBounds eoDummyRealNoBounds;
extern eoRealVectorNoBounds eoDummyVectorNoBounds; extern eoRealVectorNoBounds eoDummyVectorNoBounds;
#endif #endif

View file

@ -2,17 +2,17 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Just a simple function that takes an eoEsBase<float> and sets the fitnes /** Just a simple function that takes an eoEsBase<double> and sets the fitnes
to sphere to sphere
@param _ind A floatingpoint vector @param _ind vector<double>
*/ */
double real_value(const std::vector<double>& _ind) double real_value(const std::vector<double>& _ind)
{ {
double sum = 0; /* compute in double format, even if return a float */ double sum = 0;
for (unsigned i = 0; i < _ind.size(); i++) for (unsigned i = 0; i < _ind.size(); i++)
sum += _ind[i] * _ind[i]; sum += _ind[i] * _ind[i];
return sum; return sqrt(sum);
} }

View file

@ -27,7 +27,7 @@ typedef eoMinimizingFitness FitT;
template <class EOT> template <class EOT>
void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _bounds, eoValueParam<string> _load_name); void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _bounds, eoValueParam<string> _load_name);
int main(int argc, char *argv[]) int main_function(int argc, char *argv[])
{ {
// Create the command-line parser // Create the command-line parser
eoParser parser( argc, argv, "Basic EA for vector<float> with adaptive mutations"); eoParser parser( argc, argv, "Basic EA for vector<float> with adaptive mutations");
@ -83,6 +83,30 @@ int main(int argc, char *argv[])
return 0; 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 <class EOT> template <class EOT>
void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _bounds, eoValueParam<string> _load_name) void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _bounds, eoValueParam<string> _load_name)
{ {
@ -90,8 +114,8 @@ void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _
eoEvalFuncPtr<EOT, double, const vector<double>&> eval( real_value ); eoEvalFuncPtr<EOT, double, const vector<double>&> eval( real_value );
// population parameters, unfortunately these can not be altered in the state file // population parameters, unfortunately these can not be altered in the state file
eoValueParam<unsigned> mu = _parser.createParam(unsigned(50), "mu","Size of the population"); eoValueParam<unsigned> mu = _parser.createParam(unsigned(7), "mu","Size of the population");
eoValueParam<float>lambda_rate = _parser.createParam(float(7.0), "lambda_rate", "Factor of children to produce"); eoValueParam<double>lambda_rate = _parser.createParam(double(7.0), "lambda_rate", "Factor of children to produce");
if (lambda_rate.value() < 1.0f) if (lambda_rate.value() < 1.0f)
{ {
@ -133,11 +157,19 @@ void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _
checkpoint.add(monitor); checkpoint.add(monitor);
checkpoint.add(average); checkpoint.add(average);
// only mutation (== with rate 1.0)
eoMonGenOp<EOT> op(mutate);
eoProportionalGOpSel<EOT> opSel; // the selection: sequential selection
opSel.addOp(mutate, 1.0); eoSequentialSelect<EOT> select;
// the general breeder (lambda is a rate -> true)
eoGeneralBreeder<EOT> breed(select, op, lambda_rate.value(), true);
eoEvolutionStrategy<EOT> es(checkpoint, eval, opSel, lambda_rate.value(), eoEvolutionStrategy<EOT>::comma_strategy()); // the replacement - hard-coded Comma replacement
eoCommaReplacement<EOT> replace;
// now the eoEasyEA
eoEasyEA<EOT> es(checkpoint, eval, breed, replace);
es(pop); es(pop);

View file

@ -1,6 +1,6 @@
#include <iostream> #include <iostream>
#include <es/es.h> #include <es/real.h>
#include "real_value.h" #include "real_value.h"
#include <apply.h> #include <apply.h>

View file

@ -17,7 +17,7 @@ and to</font></b></center>
<h1> <h1>
<font color="#FF0000">EO Tutorial</font></h1></center> <font color="#FF0000">EO Tutorial</font></h1></center>
<center><font color="#FF0000">Version 0.94 - Feb. 18 2001</font></center> <center><font color="#FF0000">Version 0.95 - Apr. 27 2001</font></center>
<p>Welcome to EO tutorial/on-line documentation. <p>Welcome to EO tutorial/on-line documentation.
<p>Please note that <b><font color="#FF6600">this tutorial is not supposed <p>Please note that <b><font color="#FF6600">this tutorial is not supposed