Back to Lesson 1 - Tutorial main page - Algorithm-Based - Component-Based page - Programming hints - EO documentation

# eoSGA.h

`//-----------------------------------------------------------------------------`
`// eoSGA.h`
`//-----------------------------------------------------------------------------`
`#ifndef _eoSGA_h`
`#define _eoSGA_h`
`#include <eoOp.h>`
`#include <eoContinue.h>`
`#include <eoPop.h>`
`#include <eoSelectOne.h>`
`#include <eoSelectPerc.h>`
`#include <eoEvalFunc.h>`
`#include <eoAlgo.h>`
`#include <apply.h>`
`/** The Simple Genetic Algorithm, following Holland and Goldberg`
`* Needs a selector (class eoSelectOne) a crossover (eoQuadratic,`
`* i.e. a 2->2 operator) and a mutation with their respective rates,`
`* of course an evaluation function (eoEvalFunc) and a continuator`
`* (eoContinue) which gives the stopping criterion. Performs full`
`* generational replacement.`
`*/`
`template <class EOT>`
`class eoSGA : public eoAlgo<EOT>`
`{`
`public :`
`// added this second ctor as I didn't like the ordering of the parameters`
`// in the one above. Any objection :-) MS`
`eoSGA(`
`eoSelectOne<EOT>& _select,`
`eoQuadraticOp<EOT>& _cross, float _crate,`
`eoMonOp<EOT>& _mutate, float _mrate,`
`eoEvalFunc<EOT>& _eval,`
`eoContinue<EOT>& _cont)`
`: cont(_cont),`
`mutate(_mutate),`
`mutationRate(_mrate),`
`cross(_cross),`
`crossoverRate(_crate),`
`select(_select),`
`eval(_eval) {}`

`void operator()(eoPop<EOT>& _pop)`
`{`
`eoPop<EOT> offspring;`
`do {`
`select(_pop, offspring);`
`unsigned i;`
`for (i=0; i<_pop.size()/2; i++)`
`{ // generates 2 offspring from two parents`
`if ( rng.flip(crossoverRate) )`
`{`
`cross(offspring[2*i], offspring[2*i+1]);`
`}`
`}`
`for (i=0; i < _pop.size(); i++)`
`{`
`if (rng.flip(mutationRate) )`
`{`
`mutate(offspring[i]);`
`}`
`}`
`_pop.swap(offspring);`
`apply<EOT>(eval, _pop);`
`} while (cont(_pop));`
`}`

`private :` `eoContinue<EOT>& cont;` `eoMonOp<EOT>& mutate;` `float mutationRate;` `eoQuadraticOp<EOT>& cross;` `float crossoverRate;` `eoSelectPerc<EOT> select;` `eoEvalFunc<EOT>& eval;`

`};` `#endif`

Back to Lesson 1 - Tutorial main page - Algorithm-Based - Component-Based page - Programming hints - EO documentation

Last modified: Sun Nov 19 19:36:21 2000