Changed stuff to make eoGeneration work
This commit is contained in:
parent
c853631219
commit
ba4ace6324
4 changed files with 50 additions and 28 deletions
|
|
@ -59,6 +59,10 @@
|
|||
#include <eoPopOps.h>
|
||||
#include <eoBitOp.h>
|
||||
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoEvalFuncPtr.h>
|
||||
#include <eoEvaluator.h>
|
||||
|
||||
#include <eoLottery.h>
|
||||
#include <eoBreeder.h>
|
||||
#include <eoInsertion.h>
|
||||
|
|
|
|||
|
|
@ -41,11 +41,11 @@ struct eoEvalFunc {
|
|||
#ifdef _MSC_VER
|
||||
typedef EOT::Fitness EOFitT;
|
||||
#else
|
||||
typedef typename Fitness::EOFitT EOFitT;
|
||||
typedef typename EOT::Fitness EOFitT;
|
||||
#endif
|
||||
|
||||
/// 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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -7,31 +27,34 @@
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoAlgo.h> // eoPop
|
||||
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGeneration
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoGeneration
|
||||
template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoGeneration(eoSelect<Chrom>& _select,
|
||||
eoTransform<Chrom>& _transform,
|
||||
eoMerge<Chrom>& _replace):
|
||||
select(_select), transform(_transform), replace(_replace) {}
|
||||
eoMerge<Chrom>& _replace,
|
||||
eoEvalFunc<Chrom>& _evaluator):
|
||||
select(_select), transform(_transform),
|
||||
replace(_replace), evaluator( _evaluator) {}
|
||||
|
||||
/// Apply one generation of evolution to the population.
|
||||
template<class Evaluator> void operator()(eoPop<Chrom>& pop,
|
||||
Evaluator evaluator)
|
||||
{
|
||||
eoPop<Chrom> breeders;
|
||||
|
||||
virtual void operator()(eoPop<Chrom>& pop) {
|
||||
eoPop<Chrom> breeders;
|
||||
select(pop, 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);
|
||||
}
|
||||
|
||||
|
|
@ -42,6 +65,7 @@ template<class Chrom> class eoGeneration
|
|||
eoSelect<Chrom>& select;
|
||||
eoTransform<Chrom>& transform;
|
||||
eoMerge<Chrom>& replace;
|
||||
eoEvalFunc<Chrom>& evaluator;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -7,23 +7,14 @@
|
|||
|
||||
#include <eo>
|
||||
|
||||
#include "binary_value.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
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()
|
||||
{
|
||||
const unsigned POP_SIZE = 8, CHROM_SIZE = 16;
|
||||
|
|
@ -59,16 +50,19 @@ main()
|
|||
|
||||
// replacement
|
||||
eoInclusion<Chrom> inclusion;
|
||||
|
||||
|
||||
// Evaluation
|
||||
eoEvalFuncPtr<Chrom> eval( binary_value );
|
||||
|
||||
// GA generation
|
||||
eoGeneration<Chrom> generation(lottery, breeder, inclusion);
|
||||
eoGeneration<Chrom> generation(lottery, breeder, inclusion, eval);
|
||||
|
||||
// evolution
|
||||
unsigned g = 0;
|
||||
do {
|
||||
try
|
||||
{
|
||||
generation(pop, binary_value);
|
||||
generation(pop);
|
||||
}
|
||||
catch (exception& e)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue