From 12c6bdf058db11c726fa57605fc93845cba8a617 Mon Sep 17 00:00:00 2001 From: evomarc Date: Tue, 28 Nov 2000 17:17:19 +0000 Subject: [PATCH] Added the class eoSGATransform: I break the one file / one class rule a lot, but both are supposed to be used mainly in the tutorial, not in real life :-) --- eo/src/eoSGA.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/eo/src/eoSGA.h b/eo/src/eoSGA.h index cb55fe4f..5f3edc44 100644 --- a/eo/src/eoSGA.h +++ b/eo/src/eoSGA.h @@ -109,4 +109,65 @@ private : eoEvalFunc& eval; }; +/////////////////////////////////////////////////////////////////////////////// +// class eoSGATransform +/////////////////////////////////////////////////////////////////////////////// +#include // vector +#include +#include + +/***************************************************************************** + * eoSGATransform: transforms a population using genetic operators. + * It does it exactly as class eoSGA, i.e. only accepts + * quadratic crossover and unary mutation + * It is here mainly for tutorial reasons + *****************************************************************************/ +template class eoSGATransform : public eoTransform +{ + public: + + /// Default constructor. + eoSGATransform(eoQuadraticOp& _cross, float _crate, + eoMonOp& _mutate, float _mrate) + : cross(_cross), + crossoverRate(_crate), + mutate(_mutate), + mutationRate(_mrate) {} + + + /** + * Transforms a population. + * @param pop The population to be transformed. + */ + void operator()(eoPop& _pop) + { + unsigned i; + + for (i=0; i<_pop.size()/2; i++) + { + if ( rng.flip(crossoverRate) ) + { + // this crossover generates 2 offspring from two parents + cross(_pop[2*i], _pop[2*i+1]); + } + } + + for (i=0; i < _pop.size(); i++) + { + if (rng.flip(mutationRate) ) + { + mutate(_pop[i]); + } + + } + }; + + private: + eoQuadraticOp& cross; + float crossoverRate; + eoMonOp& mutate; + float mutationRate; +}; + + #endif