This repository has been archived on 2026-03-28. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
eodev/eo/tutorial/Templates/genop.tmpl
2001-02-17 06:47:31 +00:00

172 lines
5.6 KiB
Cheetah

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(eoSelectOne<EOT> & _sel, paramType _anyParameter) :
sel(_sel), 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:
eoSelectoIne<EOT> & sel;
paramType anyParameter
};