This repository has been archived on 2026-03-28. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
eodev/eo/src/eoEasyEA.h
evomarc 963683996d Adding eoPopEvalFunc, that handles evaluation globally: it receives
two populations, parents and offspring, and does whatever necessary.
The subclass eoPopLoopEval does the simple loop on the offspring.

eoEasyEA was subsequently modified to handle an eoPopEval passed in Ctor,
but also to encapsulate an eoEvalFunc into an eoPopLoopEval tranparently.
2001-07-04 04:44:30 +00:00

210 lines
6.4 KiB
C++

// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoEasyEA.h
// (c) GeNeura Team, 1998
/*
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
*/
//-----------------------------------------------------------------------------
#ifndef _eoEasyEA_h
#define _eoEasyEA_h
//-----------------------------------------------------------------------------
#include <apply.h>
#include <eoAlgo.h>
#include <eoEvalFunc.h>
#include <eoContinue.h>
#include <eoSelect.h>
#include <eoTransform.h>
#include <eoBreed.h>
#include <eoMergeReduce.h>
#include <eoReplacement.h>
/** eoEasyEA:
An easy-to-use evolutionary algorithm; you can use any chromosome,
and any selection transformation, merging and evaluation
algorithms; you can even change in runtime parameters of those
sub-algorithms
Change (MS, July 3. 2001):
Replaced the eoEvalFunc by an eoPopEvalFunc: this immediately
allows many useful constructs, such as co-evolution (e.g. game players),
parisian approach (the solution to the problem is the whole population)
or simple distribution of evaluations on a cluster.
In case an eoEvalFunc is passed, it is embedded on an eoPopLoopEval
This makes things a little uglier (required an additional "dummy" member
Note: it looks ugly only because we wanted to authorize many different
constructors. Please only look at the operator() and there shall be light
*/
template<class EOT> class eoEasyEA: public eoAlgo<EOT>
{
public:
/** Ctor taking a breed and merge */
eoEasyEA(
eoContinue<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoReplacement<EOT>& _replace
) : continuator(_continuator),
loopEval(_eval),
popEval(loopEval),
selectTransform(dummySelect, dummyTransform),
breed(_breed),
mergeReduce(dummyMerge, dummyReduce),
replace(_replace)
{}
/** NEW Ctor taking a breed and merge and an eoPopEval */
eoEasyEA(
eoContinue<EOT>& _continuator,
eoPopEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoReplacement<EOT>& _replace
) : continuator(_continuator),
loopEval(dummyEval),
popEval(_eval),
selectTransform(dummySelect, dummyTransform),
breed(_breed),
mergeReduce(dummyMerge, dummyReduce),
replace(_replace)
{}
/// Ctor eoBreed, eoMerge and eoReduce.
eoEasyEA(
eoContinue<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoMerge<EOT>& _merge,
eoReduce<EOT>& _reduce
) : continuator(_continuator),
loopEval(_eval),
popEval(loopEval),
selectTransform(dummySelect, dummyTransform),
breed(_breed),
mergeReduce(_merge, _reduce),
replace(mergeReduce)
{}
/// Ctor eoSelect, eoTransform, and eoReplacement
eoEasyEA(
eoContinue<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoSelect<EOT>& _select,
eoTransform<EOT>& _transform,
eoReplacement<EOT>& _replace
) : continuator(_continuator),
loopEval(_eval),
popEval(loopEval),
selectTransform(_select, _transform),
breed(selectTransform),
mergeReduce(dummyMerge, dummyReduce),
replace(_replace)
{}
/// Ctor eoSelect, eoTransform, eoMerge and eoReduce.
eoEasyEA(
eoContinue<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoSelect<EOT>& _select,
eoTransform<EOT>& _transform,
eoMerge<EOT>& _merge,
eoReduce<EOT>& _reduce
) : continuator(_continuator),
loopEval(_eval),
popEval(loopEval),
selectTransform(_select, _transform),
breed(selectTransform),
mergeReduce(_merge, _reduce),
replace(mergeReduce)
{}
/// Apply a few generation of evolution to the population.
virtual void operator()(eoPop<EOT>& _pop)
{
eoPop<EOT> offspring;
do
{
try
{
unsigned pSize = _pop.size();
offspring.clear(); // new offspring
breed(_pop, offspring);
popEval(_pop, offspring); // eval of parents + offspring if necessary
replace(_pop, offspring); // after replace, the new pop. is in _pop
if (pSize > _pop.size())
throw runtime_error("Population shrinking!");
else if (pSize < _pop.size())
throw runtime_error("Population growing!");
}
catch (exception& e)
{
string s = e.what();
s.append( " in eoEasyEA");
throw runtime_error( s );
}
} while ( continuator( _pop ) );
}
private:
// If selectTransform needs not be used, dummySelect and dummyTransform are used
// to instantiate it.
class eoDummySelect : public eoSelect<EOT>
{ public : void operator()(const eoPop<EOT>&, eoPop<EOT>&) {} } dummySelect;
class eoDummyTransform : public eoTransform<EOT>
{ public : void operator()(eoPop<EOT>&) {} } dummyTransform;
class eoDummyEval : public eoEvalFunc<EOT>
{public: void operator()(EOT &) {} } dummyEval;
eoContinue<EOT>& continuator;
eoPopLoopEval<EOT> loopEval;
eoPopEvalFunc<EOT>& popEval;
eoSelectTransform<EOT> selectTransform;
eoBreed<EOT>& breed;
// If mergeReduce needs not be used, dummyMerge and dummyReduce are used
// to instantiate it.
eoNoElitism<EOT> dummyMerge;
eoTruncate<EOT> dummyReduce;
eoMergeReduce<EOT> mergeReduce;
eoReplacement<EOT>& replace;
};
//-----------------------------------------------------------------------------
#endif eoSelectTransformReduce_h