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

@ -35,8 +35,9 @@
// combinations of simple eoOps (eoMonOp and eoQuadOp)
#include <eoProportionalCombinedOp.h>
// the specialized GA stuff
// the specialized Real stuff
#include <es/eoReal.h>
#include <es/eoRealInitBounded.h>
#include <es/eoRealOp.h>
#include <es/eoNormalMutation.h>
// also need the parser and param includes
@ -66,14 +67,56 @@
template <class EOT>
eoGenOp<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EOT>& _init)
{
// this is a temporary version, while Maarten codes the full tree-structured
// general operator input
// First, decide whether the objective variables are bounded
eoValueParam<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
// 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");
if (operatorParam.value() != string("SGA"))
throw runtime_error("Sorry, only SGA-like operator available right now\n");
if (operatorParam.value() != string("SGA"))
throw runtime_error("Sorry, only SGA-like operator available right now\n");
// now we read Pcross and Pmut,
// the relative weights for all crossovers -> proportional choice
@ -81,119 +124,119 @@ eoGenOp<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" );
// minimum check
if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) )
throw runtime_error("Invalid pCross");
eoValueParam<double>& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators" );
// minimum check
if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) )
throw runtime_error("Invalid pCross");
eoValueParam<double>& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators" );
// minimum check
if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) )
throw runtime_error("Invalid pMut");
eoValueParam<double>& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators" );
// minimum check
if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) )
throw runtime_error("Invalid pMut");
// the crossovers
/////////////////
// the parameters
eoValueParam<double>& segmentRateParam = _parser.createParam(double(1.0), "segmentRate", "Relative rate for segment crossover", 's', "Genetic Operators" );
// minimum check
if ( (segmentRateParam.value() < 0) )
throw runtime_error("Invalid segmentRate");
eoValueParam<double>& segmentRateParam = _parser.createParam(double(1.0), "segmentRate", "Relative rate for segment crossover", 's', "Genetic Operators" );
// minimum check
if ( (segmentRateParam.value() < 0) )
throw runtime_error("Invalid segmentRate");
eoValueParam<double>& arithmeticRateParam = _parser.createParam(double(2.0), "arithmeticRate", "Relative rate for arithmetic crossover", 'A', "Genetic Operators" );
// minimum check
if ( (arithmeticRateParam.value() < 0) )
throw runtime_error("Invalid arithmeticRate");
eoValueParam<double>& arithmeticRateParam = _parser.createParam(double(2.0), "arithmeticRate", "Relative rate for arithmetic crossover", 'A', "Genetic Operators" );
// minimum check
if ( (arithmeticRateParam.value() < 0) )
throw runtime_error("Invalid arithmeticRate");
// minimum check
bool bCross = true;
if (segmentRateParam.value()+arithmeticRateParam.value()==0)
{
cerr << "Warning: no crossover" << endl;
bCross = false;
}
bool bCross = true;
if (segmentRateParam.value()+arithmeticRateParam.value()==0)
{
cerr << "Warning: no crossover" << endl;
bCross = false;
}
// Create the CombinedQuadOp
eoPropCombinedQuadOp<EOT> *ptCombinedQuadOp = NULL;
eoQuadOp<EOT> *ptQuad = NULL;
// Create the CombinedQuadOp
eoPropCombinedQuadOp<EOT> *ptCombinedQuadOp = NULL;
eoQuadOp<EOT> *ptQuad = NULL;
if (bCross)
{
// segment crossover for bitstring
ptQuad = new eoSegmentCrossover<EOT>;
_state.storeFunctor(ptQuad);
ptCombinedQuadOp = new eoPropCombinedQuadOp<EOT>(*ptQuad, segmentRateParam.value());
if (bCross)
{
// segment crossover for bitstring - pass it the bounds
ptQuad = new eoSegmentCrossover<EOT>(*ptBounds);
_state.storeFunctor(ptQuad);
ptCombinedQuadOp = new eoPropCombinedQuadOp<EOT>(*ptQuad, segmentRateParam.value());
// arithmetic crossover
ptQuad = new eoArithmeticCrossover<EOT>;
_state.storeFunctor(ptQuad);
ptCombinedQuadOp->add(*ptQuad, arithmeticRateParam.value());
ptQuad = new eoArithmeticCrossover<EOT>(*ptBounds);
_state.storeFunctor(ptQuad);
ptCombinedQuadOp->add(*ptQuad, arithmeticRateParam.value());
// don't forget to store the CombinedQuadOp
_state.storeFunctor(ptCombinedQuadOp);
}
// don't forget to store the CombinedQuadOp
_state.storeFunctor(ptCombinedQuadOp);
}
// the mutations
/////////////////
// the parameters
eoValueParam<double> & epsilonParam = _parser.createParam(0.01, "epsilon", "Half-size of interval for Uniform Mutation", 'e', "Genetic Operators" );
// minimum check
if ( (epsilonParam.value() < 0) )
throw runtime_error("Invalid epsilon");
// the mutations
/////////////////
// the parameters
eoValueParam<double> & epsilonParam = _parser.createParam(0.01, "epsilon", "Half-size of interval for Uniform Mutation", 'e', "Genetic Operators" );
// minimum check
if ( (epsilonParam.value() < 0) )
throw runtime_error("Invalid epsilon");
eoValueParam<double> & uniformMutRateParam = _parser.createParam(1.0, "uniformMutRate", "Relative rate for uniform mutation", 'u', "Genetic Operators" );
// minimum check
if ( (uniformMutRateParam.value() < 0) )
throw runtime_error("Invalid uniformMutRate");
eoValueParam<double> & uniformMutRateParam = _parser.createParam(1.0, "uniformMutRate", "Relative rate for uniform mutation", 'u', "Genetic Operators" );
// minimum check
if ( (uniformMutRateParam.value() < 0) )
throw runtime_error("Invalid uniformMutRate");
eoValueParam<double> & detMutRateParam = _parser.createParam(1.0, "detMutRate", "Relative rate for deterministic uniform mutation", 'd', "Genetic Operators" );
// minimum check
if ( (detMutRateParam.value() < 0) )
throw runtime_error("Invalid detMutRate");
eoValueParam<double> & detMutRateParam = _parser.createParam(1.0, "detMutRate", "Relative rate for deterministic uniform mutation", 'd', "Genetic Operators" );
// minimum check
if ( (detMutRateParam.value() < 0) )
throw runtime_error("Invalid detMutRate");
eoValueParam<double> & normalMutRateParam = _parser.createParam(1.0, "normalMutRate", "Relative rate for Gaussian mutation", 'd', "Genetic Operators" );
// minimum check
if ( (normalMutRateParam.value() < 0) )
throw runtime_error("Invalid normalMutRate");
// and the sigma
eoValueParam<double> & sigmaParam = _parser.createParam(1.0, "sigma", "Sigma (fixed) for Gaussian mutation", 'S', "Genetic Operators" );
// minimum check
if ( (sigmaParam.value() < 0) )
throw runtime_error("Invalid sigma");
eoValueParam<double> & normalMutRateParam = _parser.createParam(1.0, "normalMutRate", "Relative rate for Gaussian mutation", 'd', "Genetic Operators" );
// minimum check
if ( (normalMutRateParam.value() < 0) )
throw runtime_error("Invalid normalMutRate");
// and the sigma
eoValueParam<double> & sigmaParam = _parser.createParam(1.0, "sigma", "Sigma (fixed) for Gaussian mutation", 'S', "Genetic Operators" );
// minimum check
if ( (sigmaParam.value() < 0) )
throw runtime_error("Invalid sigma");
// minimum check
bool bMut = true;
if (uniformMutRateParam.value()+detMutRateParam.value()+normalMutRateParam.value()==0)
{
cerr << "Warning: no mutation" << endl;
bMut = false;
}
if (!bCross && !bMut)
throw runtime_error("No operator called in SGA operator definition!!!");
bool bMut = true;
if (uniformMutRateParam.value()+detMutRateParam.value()+normalMutRateParam.value()==0)
{
cerr << "Warning: no mutation" << endl;
bMut = false;
}
if (!bCross && !bMut)
throw runtime_error("No operator called in SGA operator definition!!!");
// Create the CombinedMonOp
eoPropCombinedMonOp<EOT> *ptCombinedMonOp = NULL;
eoMonOp<EOT> *ptMon = NULL;
eoPropCombinedMonOp<EOT> *ptCombinedMonOp = NULL;
eoMonOp<EOT> *ptMon = NULL;
if (bMut)
{
// uniform mutation on all components:
// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
ptMon = new eoUniformMutation<EOT>(epsilonParam.value());
_state.storeFunctor(ptMon);
// create the CombinedMonOp
ptCombinedMonOp = new eoPropCombinedMonOp<EOT>(*ptMon, uniformMutRateParam.value());
if (bMut)
{
// uniform mutation on all components:
// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
ptMon = new eoUniformMutation<EOT>(*ptBounds, epsilonParam.value());
_state.storeFunctor(ptMon);
// create the CombinedMonOp
ptCombinedMonOp = new eoPropCombinedMonOp<EOT>(*ptMon, uniformMutRateParam.value());
// mutate exactly 1 component (uniformly) per individual
ptMon = new eoDetUniformMutation<EOT>(epsilonParam.value());
_state.storeFunctor(ptMon);
ptCombinedMonOp->add(*ptMon, detMutRateParam.value());
ptMon = new eoDetUniformMutation<EOT>(*ptBounds, epsilonParam.value());
_state.storeFunctor(ptMon);
ptCombinedMonOp->add(*ptMon, detMutRateParam.value());
// mutate all component using Gaussian mutation
ptMon = new eoNormalMutation<EOT>(sigmaParam.value());
_state.storeFunctor(ptMon);
ptCombinedMonOp->add(*ptMon, normalMutRateParam.value());
_state.storeFunctor(ptCombinedMonOp);
}
// mutate all component using Gaussian mutation
ptMon = new eoNormalMutation<EOT>(*ptBounds, sigmaParam.value());
_state.storeFunctor(ptMon);
ptCombinedMonOp->add(*ptMon, normalMutRateParam.value());
_state.storeFunctor(ptCombinedMonOp);
}
// now build the eoGenOp:
// to simulate SGA (crossover with proba pCross + mutation with proba pMut