Modified the tempaltes to take into account

- the last modifications of the eopopulator class
- the include files (were totally missing in the old templates)
- JJ's demand for one class - one file :-)
This commit is contained in:
evomarc 2001-04-03 17:14:08 +00:00
commit 7ec7a856e5
6 changed files with 301 additions and 7 deletions

View file

@ -0,0 +1,45 @@
/*
Template for simple binary crossover operators
==============================================
Binary crossover operators modify the first parent only,
based on the second
*/
#ifndef eoMyDerivedBinOp_H
#define eoMyDerivedBinOp_H
#include <eoOp.h>
template<class Indi>
class eoMyDerivedBinOp: public eoBinOp<Indi>
{
public:
/**
* (Default) Constructor.
*/
eoMyDerivedBinOp(paramType _anyParameter) :
anyParameter(_anyParameter) {}
/// The class name. Used to display statistics
string className() const { return "eoMyDerivedBinOp"; }
/**
* eoBin crossover - modifies first parent only
* @param Indi1 The first parent
* @param Indi2 The second parent - const
*/
bool operator()(Indi& Indi1, const Indi& Indi2)
{
// do whatever needs to be done
// if Indi1 has been modified
return true;
// otherwise
// return false;
}
protected:
paramType anyParameter
};
#endif

View file

@ -0,0 +1,69 @@
/*
Template for general operators
===============================
i.e. that takes any number of parents and generates any number of offspring
a GenOp that creates less offspring than there are parents
Second version, get parents using an external eoSelectOne
*/
#ifndef eoLessOffspringExternalSelectorGenOp_H
#define eoLessOffspringExternalSelectorGenOp_H
#include <eoGenOp.h>
template<class Indi>
class eoLessOffspringExternalSelectorGenOp: public eoGenOp<Indi>
{
public:
/**
* (Default) Constructor.
*/
eoLessOffspringExternalSelectorGenOp(eoSelectOne<EOT> & _sel, paramType _anyParameter) :
sel(_sel), anyParameter(_anyParameter) {}
/// The class name. Used to display statistics
string className() const { return "eoLessOffspringExternalSelectorGenOp"; }
/// 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)
{
// First, select as many parents as you will have offspring
EOT& parent1 = *_plop; // select the first parent
++_plop; // advance once for each selected parents
...
EOT& parentN = *_plop; // say you want N offspring
// 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& parentN+K = sel(_plop.source());
// modify (in place) the "true" parents
// (i.e. parent1, ..., parentsN)
...
// invalidate fitnesses of modified parents
parent1.invalidate();
...
parentN.invalidate();
}
protected:
eoSelectOne<EOT> & sel;
paramType anyParameter
};
#endif

View file

@ -0,0 +1,64 @@
/*
Template for general operators
===============================
i.e. that takes any number of parents and generates any number of offspring
a GenOp that creates less offspring than there are parents
First version, get parents from populator using the ibbedded select() method
*/
#ifndef eoLessOffspringSameSelectorGenOp_H
#define eoLessOffspringSameSelectorGenOp_H
#include <eoGenOp.h>
template<class Indi>
class eoLessOffspringSameSelectorGenOp: public eoGenOp<Indi>
{
public:
/**
* (Default) Constructor.
*/
eoLessOffspringSameSelectorGenOp(paramType _anyParameter) :
anyParameter(_anyParameter) {}
/// The class name. Used to display statistics
string className() const { return "eoLessOffspringSameSelectorGenOp"; }
/// The TOTAL number of offspring (here = nb of remaining modified parents)
unsigned max_production(void) { return NbLeftParents; }
/**
* eoLesOffspringSameSelectorGenOp operator -
* gets extra parents from the populator
*
* @param _pop a POPULATOR (not a simple population)
*/
void apply(eoPopulator<EOT>& _plop)
{
// First, select as many parents as you will have offspring
EOT& parent1 = *_plop; // select the first parent
++_plop; // advance once for each selected parents
...
EOT& parentN = *_plop; // say you want N offspring
// Now select extra parents from the populator
EOT& parentN+1 = _plop.select();
...
EOT& parentN+K = _plop.select();
// modify the first N parents
...
// oh right, and invalidate their fitnesses
parent1.invalidate();
...
parentN.invalidate();
}
protected:
paramType anyParameter
};
#endif

View file

@ -0,0 +1,65 @@
/*
Template for general operators
===============================
i.e. that takes any number of parents and generates any number of offspring
Here, a GenOp that creates more (or same number of) offspring
than there are parents
*/
#ifndef eoMoreOffspringGenOp_H
#define eoMoreOffspringGenOp_H
#include <eoGenOp.h>
template<class Indi>
class eoMoreOffspringGenOp: public eoGenOp<Indi>
{
public:
/**
* (Default) Constructor.
*/
eoMoreOffspringGenOp(paramType _anyParameter) :
anyParameter(_anyParameter) {}
/// The class name. Used to display statistics
string className() const { return "eoMoreOffspringGenOp"; }
/// The TOTAL number of offspring (including modified parents)
unsigned max_production(void) { return NbOffspring; }
/**
* eoMoreOffspringGenOp 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; // advance before each insertion
_plop.insert(ofs1);
...
++_plop; // advance before each insertion
_plop.insert(ofsN);
// oh right, and invalidate fitnesses of modified parents
parent1.invalidate();
...
parentN.invalidate();
}
protected:
paramType anyParameter
};
#endif

View file

@ -1,11 +1,16 @@
Template for simple operators
=============================
/*
Template for simple mutation operators
======================================
*/
===========================================================================
eoMon : mutation operators
======
#ifndef eoMyDerivedMonOp_H
#define eoMyDerivedMonOp_H
template<class Indi> class eoMyDerivedMonOp: public eoMonOp<Indi>
#include <eoOp.h>
template<class Indi>
class eoMyDerivedMonOp: public eoMonOp<Indi>
{
public:
/**
@ -18,7 +23,7 @@ public:
string className() const { return "eoMyDerivedMonOp"; }
/**
* eoMon crossover - modifies the parent
* modifies the parent
* @param Indi The parent
*/
bool operator()(Indi& Indi)
@ -34,3 +39,4 @@ protected:
paramType anyParameter
};
#endif

View file

@ -0,0 +1,45 @@
/*
Template for simple quadratic crossover operators
=================================================
Quadratic crossover operators modify the both parents
*/
#ifndef eoMyDerivedQuadOp_H
#define eoMyDerivedQuadOp_H
#include <eoOp.h>
template<class Indi>
class eoMyDerivedQuadOp: public eoQuadOp<Indi>
{
public:
/**
* (Default) Constructor.
*/
eoMyDerivedQuadOp(paramType _anyParameter) :
anyParameter(_anyParameter) {}
/// The class name. Used to display statistics
string className() const { return "eoMyDerivedQuadOp"; }
/**
* eoQuad crossover - modifies both parents
* @param Indi1 The first parent
* @param Indi2 The second parent
*/
bool operator()(Indi& Indi1, Indi& Indi2)
{
// do whatever needs to be done
// if at least one individual has been modified - no way to distinguish
return true;
// otherwise
// return false;
}
protected:
paramType anyParameter
};
#endif