From d7693131a600e49ec1bd4694d93f2ec201445353 Mon Sep 17 00:00:00 2001 From: evomarc Date: Tue, 26 Dec 2000 08:33:48 +0000 Subject: [PATCH] Moved eoSGATransform into a separate file and added the dynamic version (where arguments can be passed by value or by reference). Modified eo accordingly --- eo/src/eo | 27 +------ eo/src/eoSGA.h | 61 --------------- eo/src/eoSGATransform.h | 163 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 85 deletions(-) create mode 100644 eo/src/eoSGATransform.h diff --git a/eo/src/eo b/eo/src/eo index 62c8de89..eb0d8f07 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -30,20 +30,8 @@ // some defines to make things easier to get at first sight -// tunigni the amount of output using a boolean argument: -// true should always mean more output #define eo_verbose true #define eo_no_verbose false -// to be used in selection / replacement procedures to indicate whether -// the argument (rate, a double) shoudl be treated as a rate (number=rate*popSize) -// or as an absolute integer (number=rate regardless of popsize). -// the default value shoudl ALWAYS be true (eo_as_a_rate). -// -// this construct is mandatory because in some cases you might not know the -// population size that will enter the replacement for instance - so you -// cannot simply have a pre-computed (double) rate of 1/popSize -#define eo_is_a_rate true -#define eo_is_an_integer false //----------------------------------------------------------------------------- #include @@ -98,29 +86,20 @@ #include #include -// Selection +// Selection and reproduction stuff #include #include #include #include #include -#include -#include -#include - -// Replacement -// #include -#include -#include -#include - -// Variation #include #include #include #include +#include + //#include //#include //#include diff --git a/eo/src/eoSGA.h b/eo/src/eoSGA.h index 5f3edc44..cb55fe4f 100644 --- a/eo/src/eoSGA.h +++ b/eo/src/eoSGA.h @@ -109,65 +109,4 @@ 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 diff --git a/eo/src/eoSGATransform.h b/eo/src/eoSGATransform.h new file mode 100644 index 00000000..96a6e39e --- /dev/null +++ b/eo/src/eoSGATransform.h @@ -0,0 +1,163 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoSGA.h +// (c) Marc.Schoenauer 2000 - Maarten Keijzer 2000 +/* + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mak@dhi.dk + */ +//----------------------------------------------------------------------------- + +#ifndef _eoSGATransform_h +#define _eoSGATransform_h + +#include +#include + +/////////////////////////////////////////////////////////////////////////////// +// 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, double _cProba, + eoMonOp& _mutate, double _mProba) + : cross(_cross), + crossoverProba(_cProba), + mutate(_mutate), + mutationProba(_mProba) {} + + + /** + * 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(crossoverProba) ) + { + // 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(mutationProba) ) + { + mutate(_pop[i]); + } + + } + }; + + private: + eoQuadraticOp& cross; + double crossoverProba; + eoMonOp& mutate; + double mutationProba; +}; + +/***************************************************************************** + * eoSDynGATransform: transforms a population using genetic operators. + * It is the Dynamic version of the above eoSGATransform + * i.e. the operators probabilities can be passed as an eoValueParam, + * and hence can be modified from outside + * It is here mainly for tutorial reasons + *****************************************************************************/ +template class eoDynSGATransform : public eoTransform +{ + public: + + /// Default constructor - receives values + eoDynSGATransform(eoQuadraticOp& _cross, double _cProba, + eoMonOp& _mutate, double _mProba) + : cross(_cross), + crossoverProbaHolder(_cProba), crossoverProba(crossoverProbaHolder), + mutate(_mutate), + mutationProbaHolder(_mProba), mutationProba(mutationProbaHolder) {} + + /// This constructor receives references + // these will usually be some eoValueParam.value() + // the ...Holder will bever be used in this object + eoDynSGATransform(eoQuadraticOp& _cross, double& _cProbaRef, + eoMonOp& _mutate, double& _mProbaRef) + : cross(_cross), + crossoverProbaHolder(0), crossoverProba(_cProbaRef), + mutate(_mutate), + mutationProbaHolder(0), mutationProba(_mProbaRef) {} + + + /** + * 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(crossoverProba) ) + { + // 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(mutationProba) ) + { + mutate(_pop[i]); + } + + } + }; + + private: + // difference with eoSGATransform: the operator probabilities + // they can be passed by reference or by value. + // hence we need here to use a reference, and to eventually store a value + eoQuadraticOp& cross; + double crossoverProbaHolder; // the value, used only if ctor gets a value + double& crossoverProba; // the reference, to be used in operator() + eoMonOp& mutate; + double mutationProbaHolder; // the value, used only if ctor gets a value + double& mutationProba; // the reference, to be used in operator() +}; + + +#endif