Re-introduced the old ctors for the mutations (for backward compatibility

with the tutorial :-(
This commit is contained in:
evomarc 2001-04-29 05:03:05 +00:00
commit 4f05bfaaf1
2 changed files with 67 additions and 41 deletions

View file

@ -46,16 +46,16 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
{ {
public: public:
/** /**
* (Default) Constructor. * Constructor without bounds == unbounded variables :-)
* The bounds are initialized with the global object that says: no bounds. * not very clean, but who's doing unbounded optimization anyway?
* and it's there mostly for backward compatibility
* *
* @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 unsigned _size, const double& _epsilon, eoUniformMutation(const double& _epsilon, const double& _p_change = 1.0):
const double& _p_change = 1.0): homogeneous(true), bounds(eoDummyVectorNoBounds), epsilon(1, _epsilon),
bounds(eoDummyVectorNoBounds), epsilon(_size, _epsilon), p_change(1, _p_change) {}
p_change(_size, _p_change) {}
/** /**
* Constructor with bounds * Constructor with bounds
@ -65,7 +65,7 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
*/ */
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), homogeneous(false), bounds(_bounds), epsilon(_bounds.size(), _epsilon),
p_change(_bounds.size(), _p_change) p_change(_bounds.size(), _p_change)
{ {
// scale to the range - if any // scale to the range - if any
@ -83,7 +83,8 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
eoUniformMutation(eoRealVectorBounds & _bounds, eoUniformMutation(eoRealVectorBounds & _bounds,
const vector<double>& _epsilon, const vector<double>& _epsilon,
const vector<double>& _p_change): const vector<double>& _p_change):
bounds(_bounds), epsilon(_epsilon), p_change(_p_change) {} homogeneous(false), bounds(_bounds), epsilon(_epsilon),
p_change(_p_change) {}
/// The class name. /// The class name.
string className() const { return "eoUniformMutation"; } string className() const { return "eoUniformMutation"; }
@ -93,6 +94,18 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
* @param _eo The indi undergoing the mutation * @param _eo The indi undergoing the mutation
*/ */
bool operator()(EOT& _eo) bool operator()(EOT& _eo)
{
bool hasChanged=false;
if (homogeneous) // implies no bounds object
for (unsigned lieu=0; lieu<_eo.size(); lieu++)
{
if (rng.flip(p_change[0]))
{
_eo[0] += 2*epsilon[0]*rng.uniform()-epsilon[0];
hasChanged = true;
}
}
else
{ {
// sanity check ? // sanity check ?
if (_eo.size() != bounds.size()) if (_eo.size() != bounds.size())
@ -100,7 +113,6 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
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[lieu])) if (rng.flip(p_change[lieu]))
{ {
// check the bounds // check the bounds
@ -118,9 +130,10 @@ template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
} }
private: private:
bool homogeneous; // == no bounds passed in the ctor
eoRealVectorBounds & bounds; eoRealVectorBounds & bounds;
vector<double> epsilon; vector<double> epsilon; // the ranges for mutation
vector<double> p_change; vector<double> p_change; // the proba that each variable is modified
}; };
/** eoDetUniformMutation --> changes exactly k values of the vector /** eoDetUniformMutation --> changes exactly k values of the vector
@ -133,13 +146,15 @@ template<class EOT> class eoDetUniformMutation: public eoMonOp<EOT>
{ {
public: public:
/** /**
* (Default) Constructor. * (Default) Constructor for homogeneous genotype
* it's there mostly for backward compatibility
*
* @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 unsigned _size, const double& _epsilon, eoDetUniformMutation(const double& _epsilon, const unsigned& _no = 1):
const unsigned& _no = 1): homogeneous(true), bounds(eoDummyVectorNoBounds),
bounds(eoDummyVectorNoBounds), epsilon(_size, _epsilon), no(_no) {} epsilon(1, _epsilon), no(_no) {}
/** /**
* Constructor with bounds * Constructor with bounds
@ -149,7 +164,8 @@ template<class EOT> class eoDetUniformMutation: public eoMonOp<EOT>
*/ */
eoDetUniformMutation(eoRealVectorBounds & _bounds, eoDetUniformMutation(eoRealVectorBounds & _bounds,
const double& _epsilon, const unsigned& _no = 1): const double& _epsilon, const unsigned& _no = 1):
bounds(_bounds), epsilon(_bounds.size(), _epsilon), no(_no) homogeneous(false), bounds(_bounds),
epsilon(_bounds.size(), _epsilon), no(_no)
{ {
// scale to the range - if any // scale to the range - if any
for (unsigned i=0; i<bounds.size(); i++) for (unsigned i=0; i<bounds.size(); i++)
@ -166,7 +182,7 @@ template<class EOT> class eoDetUniformMutation: public eoMonOp<EOT>
eoDetUniformMutation(eoRealVectorBounds & _bounds, eoDetUniformMutation(eoRealVectorBounds & _bounds,
const vector<double>& _epsilon, const vector<double>& _epsilon,
const unsigned& _no = 1): const unsigned& _no = 1):
bounds(_bounds), epsilon(_epsilon), no(_no) homogeneous(false), bounds(_bounds), epsilon(_epsilon), no(_no)
{ {
// scale to the range - if any // scale to the range - if any
for (unsigned i=0; i<bounds.size(); i++) for (unsigned i=0; i<bounds.size(); i++)
@ -182,6 +198,15 @@ template<class EOT> class eoDetUniformMutation: public eoMonOp<EOT>
* @param _eo The indi undergoing the mutation * @param _eo The indi undergoing the mutation
*/ */
bool operator()(EOT& _eo) bool operator()(EOT& _eo)
{
if (homogeneous)
for (unsigned i=0; i<no; i++)
{
unsigned lieu = rng.random(_eo.size());
// actually, we should test that we don't re-modify same variable!
_eo[lieu] = 2*epsilon[0]*rng.uniform()-epsilon[0];
}
else
{ {
// sanity check ? // sanity check ?
if (_eo.size() != bounds.size()) if (_eo.size() != bounds.size())
@ -200,13 +225,14 @@ template<class EOT> class eoDetUniformMutation: public eoMonOp<EOT>
emax = min(bounds.maximum(lieu), emax); emax = min(bounds.maximum(lieu), emax);
_eo[lieu] = emin + (emax-emin)*rng.uniform(); _eo[lieu] = emin + (emax-emin)*rng.uniform();
} }
}
return true; return true;
} }
private: private:
bool homogeneous; // == no bounds passed in the ctor
eoRealVectorBounds & bounds; eoRealVectorBounds & bounds;
vector<double> epsilon; vector<double> epsilon; // the ranges of mutation
unsigned no; unsigned no;
}; };

View file

@ -68,7 +68,7 @@ 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)
{ {
// First, decide whether the objective variables are bounded // First, decide whether the objective variables are bounded
eoValueParam<eoParamParamType>& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "objectBounds", "Bounds for variables", 'B', "Genetic Operators"); eoValueParam<eoParamParamType>& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "objectBounds", "Bounds for variables (unbounded if absent)", 'B', "Genetic Operators");
// get initisalizer size == vector size // get initisalizer size == vector size
// eoRealInitBounded<EOT> * realInit = (eoRealInitBounded<EOT>*)(&_init); // eoRealInitBounded<EOT> * realInit = (eoRealInitBounded<EOT>*)(&_init);