Modified crossover and mutation to account for the new interface (bool ...)
Added the genop.tmpl, template for general op, 3 cases, growing pop, shrinking pop through erase, thrinking pop through extra selector
This commit is contained in:
parent
83e883a158
commit
1662fd2a8b
3 changed files with 186 additions and 11 deletions
|
|
@ -22,13 +22,14 @@ public:
|
|||
* @param Indi1 The first parent
|
||||
* @param Indi2 The first parent
|
||||
*/
|
||||
void operator()(Indi& Indi1, Indi& Indi2)
|
||||
bool operator()(Indi& Indi1, Indi& Indi2)
|
||||
{
|
||||
// do whatever needs to be done
|
||||
|
||||
// DON'T FORGET if an individual is modified, to invalidate its fitness!!!
|
||||
Indi1.invalidate();
|
||||
Indi2.invalidate();
|
||||
// if at least one individual has been modified
|
||||
return true;
|
||||
// otherwise
|
||||
// return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
|
@ -56,11 +57,13 @@ public:
|
|||
* @param Indi1 The first parent
|
||||
* @param Indi2 The first parent - const
|
||||
*/
|
||||
void operator()(Indi& Indi1, const Indi& Indi2)
|
||||
bool operator()(Indi& Indi1, const Indi& Indi2)
|
||||
{
|
||||
// do whatever needs to be done
|
||||
// DON'T FORGET if an individual is modified, to invalidate its fitness!!!
|
||||
Indi1.invalidate();
|
||||
// if Indi1 has been modified
|
||||
return true;
|
||||
// otherwise
|
||||
// return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
|
|
|||
171
eo/tutorial/Templates/genop.tmpl
Normal file
171
eo/tutorial/Templates/genop.tmpl
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
Template for simple operators
|
||||
=============================
|
||||
|
||||
===========================================================================
|
||||
eoGen : general operator that takes any number of parents and generates
|
||||
====== any number of offspring
|
||||
|
||||
First, a GenOp that creates more offspring than there are parents
|
||||
-----------------------------------------------------------------
|
||||
|
||||
template<class Indi> class eoMyDerivedGrowGenOp: public eoGenOp<Indi>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* (Default) Constructor.
|
||||
*/
|
||||
eoMyDerivedGrowGenOp(paramType _anyParameter) :
|
||||
anyParameter(_anyParameter) {}
|
||||
|
||||
/// The class name. Used to display statistics
|
||||
string className() const { return "eoMyDerivedGrowGenOp"; }
|
||||
|
||||
/// The TOTAL number of offspring (including modified parents)
|
||||
unsigned max_production(void) { return NbOffspring; }
|
||||
|
||||
/**
|
||||
* eoGrowGen operator - eventually modifies the parents
|
||||
* BUT does generate more offspring
|
||||
*
|
||||
* @param _pop a POPULATOR (not a simple population)
|
||||
*/
|
||||
void apply(eoPopulator<EOT>& _plop)
|
||||
{
|
||||
EOT& parent1 = *_plop; // select the first parent
|
||||
++_plop; // advance once for each selected parents
|
||||
...
|
||||
EOT& parentN = *_plop; // select the last parent
|
||||
// don't advance after the last one: _plop always
|
||||
// points to the last that has already been treated
|
||||
|
||||
// apply operator to the parents (modifying them AND generating
|
||||
// new individuals ofs1, ofs2, ..., ofsN
|
||||
_plop.insert(ofs1);
|
||||
++_plop; // advance after each insertion
|
||||
...
|
||||
_plop.insert(ofsN);
|
||||
// don't advance after the last one: _plop always
|
||||
// points to the last that has already been treated
|
||||
|
||||
// oh right, and invalidate fitnesses of modified parents
|
||||
parent1.invalidate();
|
||||
...
|
||||
parentN.invalidate();
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
paramType anyParameter
|
||||
};
|
||||
|
||||
===========================================================================
|
||||
Now, a GenOp that creates less offspring than there are parents
|
||||
---------------------------------------------------------------
|
||||
First version, get parents from populator, and erase some of them
|
||||
-----------------------------------------------------------------
|
||||
|
||||
template<class Indi> class eoMyDerivedShrink1GenOp: public eoGenOp<Indi>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* (Default) Constructor.
|
||||
*/
|
||||
eoMyDerivedShrink1GenOp(paramType _anyParameter) :
|
||||
anyParameter(_anyParameter) {}
|
||||
|
||||
/// The class name. Used to display statistics
|
||||
string className() const { return "eoMyDerivedShrink1GenOp"; }
|
||||
|
||||
/// The TOTAL number of offspring (here = nb of remaining modified parents)
|
||||
unsigned max_production(void) { return NbLeftParents; }
|
||||
|
||||
/**
|
||||
* eoShrinkGen operator - modifies some of the parents
|
||||
* and kills the rest
|
||||
*
|
||||
* @param _pop a POPULATOR (not a simple population)
|
||||
*/
|
||||
void apply(eoPopulator<EOT>& _plop)
|
||||
{
|
||||
EOT& parent1 = *_plop; // select the first parent
|
||||
++_plop; // advance once for each selected parents
|
||||
...
|
||||
EOT& parentN = *_plop; // select the last parent
|
||||
// don't advance after the last one: _plop always
|
||||
// points to the last that has already been treated
|
||||
|
||||
// modify some of the parents (the first ones
|
||||
// as erase removes backwards)
|
||||
...
|
||||
|
||||
// then kill the ones that are now useless
|
||||
_plop.erase(); // as many times as necessary
|
||||
...
|
||||
// oh right, and invalidate fitnesses of remaining modified parents
|
||||
parent1.invalidate();
|
||||
...
|
||||
parentK.invalidate();
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
paramType anyParameter
|
||||
};
|
||||
|
||||
===========================================================================
|
||||
Now, a GenOp that creates less offspring than there are parents
|
||||
---------------------------------------------------------------
|
||||
Second version, get parents from an external selector (no erasing)
|
||||
------------------------------------------------------------------
|
||||
|
||||
template<class Indi> class eoMyDerivedShrink2GenOp: public eoGenOp<Indi>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* (Default) Constructor.
|
||||
*/
|
||||
eoMyDerivedShrink2GenOp(paramType _anyParameter) :
|
||||
anyParameter(_anyParameter) {}
|
||||
|
||||
/// The class name. Used to display statistics
|
||||
string className() const { return "eoMyDerivedShrink2GenOp"; }
|
||||
|
||||
/// The TOTAL number of offspring (here = nb of parents modified in place)
|
||||
unsigned max_production(void) { return NbLeftParents; }
|
||||
|
||||
/**
|
||||
* eoShrinkGen operator - modifies some parents in the populator
|
||||
* using extra "parents" selected from an external selector
|
||||
*
|
||||
* @param _pop a POPULATOR (not a simple population)
|
||||
*/
|
||||
void apply(eoPopulator<EOT>& _plop)
|
||||
{
|
||||
EOT& parent1 = *_plop; // select the first parent
|
||||
++_plop; // advance once for each selected parents
|
||||
...
|
||||
EOT& parentN = *_plop; // select the last parent you need
|
||||
// don't advance after the last one: _plop always
|
||||
// points to the last that has already been treated
|
||||
|
||||
// get extra parents - use private selector
|
||||
// _plop.source() is the eoPop<EOT> used by _plop to get parents
|
||||
// WARNING: you are not allowed to modify them (mandatory "const")
|
||||
const EOT& parentN+1 = sel(_plop.source());
|
||||
...
|
||||
const EOT& parentM = sel(_plop.source());
|
||||
|
||||
// modify (in place) the "true" parents
|
||||
// (i.e. parent1, ..., parentsN)
|
||||
...
|
||||
|
||||
// invalidate fitnesses of modified parents
|
||||
parent1.invalidate();
|
||||
...
|
||||
parentN.invalidate();
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
paramType anyParameter
|
||||
};
|
||||
|
|
@ -21,12 +21,13 @@ public:
|
|||
* eoMon crossover - modifies the parent
|
||||
* @param Indi The parent
|
||||
*/
|
||||
void operator()(Indi& Indi)
|
||||
bool operator()(Indi& Indi)
|
||||
{
|
||||
// do whatever needs to be done
|
||||
|
||||
// DON'T FORGET if an individual is modified, to invalidate its fitness!!!
|
||||
Indi.invalidate();
|
||||
// if Indi has been modified
|
||||
return true;
|
||||
// otherwise
|
||||
// return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
|
|
|||
Reference in a new issue