Changed stuff to make eoGeneration work

This commit is contained in:
jmerelo 1999-10-08 09:51:40 +00:00
commit ba4ace6324
4 changed files with 50 additions and 28 deletions

View file

@ -59,6 +59,10 @@
#include <eoPopOps.h> #include <eoPopOps.h>
#include <eoBitOp.h> #include <eoBitOp.h>
#include <eoEvalFunc.h>
#include <eoEvalFuncPtr.h>
#include <eoEvaluator.h>
#include <eoLottery.h> #include <eoLottery.h>
#include <eoBreeder.h> #include <eoBreeder.h>
#include <eoInsertion.h> #include <eoInsertion.h>

View file

@ -41,11 +41,11 @@ struct eoEvalFunc {
#ifdef _MSC_VER #ifdef _MSC_VER
typedef EOT::Fitness EOFitT; typedef EOT::Fitness EOFitT;
#else #else
typedef typename Fitness::EOFitT EOFitT; typedef typename EOT::Fitness EOFitT;
#endif #endif
/// Effectively applies the evaluation function to an EO or urEO /// Effectively applies the evaluation function to an EO or urEO
virtual EOFitT evaluate( EOT & _eo ) const = 0; virtual void operator() ( EOT & _eo ) const = 0;
}; };
#endif #endif

View file

@ -1,5 +1,25 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoGeneration.h // eoOp.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 eoGeneration_h #ifndef eoGeneration_h
@ -7,31 +27,34 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <eoPop.h> // eoPop #include <eoAlgo.h> // eoPop
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge #include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoGeneration // eoGeneration
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template<class Chrom> class eoGeneration template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
{ {
public: public:
/// Constructor. /// Constructor.
eoGeneration(eoSelect<Chrom>& _select, eoGeneration(eoSelect<Chrom>& _select,
eoTransform<Chrom>& _transform, eoTransform<Chrom>& _transform,
eoMerge<Chrom>& _replace): eoMerge<Chrom>& _replace,
select(_select), transform(_transform), replace(_replace) {} eoEvalFunc<Chrom>& _evaluator):
select(_select), transform(_transform),
replace(_replace), evaluator( _evaluator) {}
/// Apply one generation of evolution to the population. /// Apply one generation of evolution to the population.
template<class Evaluator> void operator()(eoPop<Chrom>& pop, virtual void operator()(eoPop<Chrom>& pop) {
Evaluator evaluator)
{
eoPop<Chrom> breeders; eoPop<Chrom> breeders;
select(pop, breeders); select(pop, breeders);
transform(breeders); transform(breeders);
for_each(breeders.begin(), breeders.end(), evaluator); eoPop<Chrom>::iterator i;
// Can't use foreach here since foreach takes the
// parameter by reference
for ( i = breeders.begin(); i != breeders.end(); i++)
evaluator(*i);
replace(breeders, pop); replace(breeders, pop);
} }
@ -42,6 +65,7 @@ template<class Chrom> class eoGeneration
eoSelect<Chrom>& select; eoSelect<Chrom>& select;
eoTransform<Chrom>& transform; eoTransform<Chrom>& transform;
eoMerge<Chrom>& replace; eoMerge<Chrom>& replace;
eoEvalFunc<Chrom>& evaluator;
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View file

@ -7,23 +7,14 @@
#include <eo> #include <eo>
#include "binary_value.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
typedef eoBin<float> Chrom; typedef eoBin<float> Chrom;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void binary_value(Chrom& chrom)
{
float sum = 0;
for (unsigned i = 0; i < chrom.size(); i++)
if (chrom[i])
sum += pow(2, chrom.size() - i - 1);
chrom.fitness(sum);
}
//-----------------------------------------------------------------------------
main() main()
{ {
const unsigned POP_SIZE = 8, CHROM_SIZE = 16; const unsigned POP_SIZE = 8, CHROM_SIZE = 16;
@ -60,15 +51,18 @@ main()
// replacement // replacement
eoInclusion<Chrom> inclusion; eoInclusion<Chrom> inclusion;
// Evaluation
eoEvalFuncPtr<Chrom> eval( binary_value );
// GA generation // GA generation
eoGeneration<Chrom> generation(lottery, breeder, inclusion); eoGeneration<Chrom> generation(lottery, breeder, inclusion, eval);
// evolution // evolution
unsigned g = 0; unsigned g = 0;
do { do {
try try
{ {
generation(pop, binary_value); generation(pop);
} }
catch (exception& e) catch (exception& e)
{ {