Moved eoSGATransform into a separate file and added the dynamic version

(where arguments can be passed by value or by reference).
Modified eo accordingly
This commit is contained in:
evomarc 2000-12-26 08:33:48 +00:00
commit d7693131a6
3 changed files with 166 additions and 85 deletions

View file

@ -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 <utils/eoData.h>
@ -98,29 +86,20 @@
#include <eoSteadyFitContinue.h>
#include <eoFitContinue.h>
// Selection
// Selection and reproduction stuff
#include <eoSelectOne.h>
#include <eoSelectRandom.h>
#include <eoDetTournament.h>
#include <eoProportional.h>
#include <eoStochTournament.h>
#include <eoSelectPerc.h>
#include <eoSelectNumber.h>
#include <eoSelectMany.h>
// Replacement
// #include <eoReplacement.h>
#include <eoMergeReduce.h>
#include <eoReduceMerge.h>
#include <eoSurviveAndDie.h>
// Variation
#include <eoProportionalCombinedOp.h>
#include <eoProportionalOpSel.h>
#include <eoProportionalGOpSel.h>
#include <eoSequentialGOpSel.h>
#include <eoSGATransform.h>
//#include <eoLottery.h>
//#include <eoBreeder.h>
//#include <eoInsertion.h>

View file

@ -109,65 +109,4 @@ private :
eoEvalFunc<EOT>& eval;
};
///////////////////////////////////////////////////////////////////////////////
// class eoSGATransform
///////////////////////////////////////////////////////////////////////////////
#include <vector> // vector
#include <utils/eoRNG.h>
#include <eoTransform.h>
/*****************************************************************************
* 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 EOT> class eoSGATransform : public eoTransform<EOT>
{
public:
/// Default constructor.
eoSGATransform(eoQuadraticOp<EOT>& _cross, float _crate,
eoMonOp<EOT>& _mutate, float _mrate)
: cross(_cross),
crossoverRate(_crate),
mutate(_mutate),
mutationRate(_mrate) {}
/**
* Transforms a population.
* @param pop The population to be transformed.
*/
void operator()(eoPop<EOT>& _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<EOT>& cross;
float crossoverRate;
eoMonOp<EOT>& mutate;
float mutationRate;
};
#endif

163
eo/src/eoSGATransform.h Normal file
View file

@ -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 <eoOp.h>
#include <eoPop.h>
///////////////////////////////////////////////////////////////////////////////
// class eoSGATransform
///////////////////////////////////////////////////////////////////////////////
#include <vector> // vector
#include <utils/eoRNG.h>
#include <eoTransform.h>
/*****************************************************************************
* 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 EOT> class eoSGATransform : public eoTransform<EOT>
{
public:
/// Default constructor.
eoSGATransform(eoQuadraticOp<EOT>& _cross, double _cProba,
eoMonOp<EOT>& _mutate, double _mProba)
: cross(_cross),
crossoverProba(_cProba),
mutate(_mutate),
mutationProba(_mProba) {}
/**
* Transforms a population.
* @param pop The population to be transformed.
*/
void operator()(eoPop<EOT>& _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<EOT>& cross;
double crossoverProba;
eoMonOp<EOT>& 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 EOT> class eoDynSGATransform : public eoTransform<EOT>
{
public:
/// Default constructor - receives values
eoDynSGATransform(eoQuadraticOp<EOT>& _cross, double _cProba,
eoMonOp<EOT>& _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<double>.value()
// the ...Holder will bever be used in this object
eoDynSGATransform(eoQuadraticOp<EOT>& _cross, double& _cProbaRef,
eoMonOp<EOT>& _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<EOT>& _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<EOT>& cross;
double crossoverProbaHolder; // the value, used only if ctor gets a value
double& crossoverProba; // the reference, to be used in operator()
eoMonOp<EOT>& mutate;
double mutationProbaHolder; // the value, used only if ctor gets a value
double& mutationProba; // the reference, to be used in operator()
};
#endif