From ba4ace6324a4d7071d3373325955d5bef076d324 Mon Sep 17 00:00:00 2001 From: jmerelo Date: Fri, 8 Oct 1999 09:51:40 +0000 Subject: [PATCH] Changed stuff to make eoGeneration work --- eo/src/eo | 4 ++++ eo/src/eoEvalFunc.h | 6 ++--- eo/src/eoGeneration.h | 46 +++++++++++++++++++++++++++++--------- eo/test/t-eogeneration.cpp | 22 +++++++----------- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/eo/src/eo b/eo/src/eo index 8d180d543..21bd61b69 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -59,6 +59,10 @@ #include #include +#include +#include +#include + #include #include #include diff --git a/eo/src/eoEvalFunc.h b/eo/src/eoEvalFunc.h index 1d6f98192..6d5fc4cb2 100644 --- a/eo/src/eoEvalFunc.h +++ b/eo/src/eoEvalFunc.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 \ No newline at end of file +#endif diff --git a/eo/src/eoGeneration.h b/eo/src/eoGeneration.h index ac062bb26..e8beb9c35 100644 --- a/eo/src/eoGeneration.h +++ b/eo/src/eoGeneration.h @@ -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 +#include // eoPop #include // eoSelect, eoTranform, eoMerge //----------------------------------------------------------------------------- // eoGeneration //----------------------------------------------------------------------------- -template class eoGeneration +template class eoGeneration: public eoAlgo { public: /// Constructor. eoGeneration(eoSelect& _select, eoTransform& _transform, - eoMerge& _replace): - select(_select), transform(_transform), replace(_replace) {} + eoMerge& _replace, + eoEvalFunc& _evaluator): + select(_select), transform(_transform), + replace(_replace), evaluator( _evaluator) {} /// Apply one generation of evolution to the population. - template void operator()(eoPop& pop, - Evaluator evaluator) - { - eoPop breeders; - + virtual void operator()(eoPop& pop) { + eoPop breeders; select(pop, breeders); transform(breeders); - for_each(breeders.begin(), breeders.end(), evaluator); + eoPop::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 eoGeneration eoSelect& select; eoTransform& transform; eoMerge& replace; + eoEvalFunc& evaluator; }; //----------------------------------------------------------------------------- diff --git a/eo/test/t-eogeneration.cpp b/eo/test/t-eogeneration.cpp index 1b5219b72..f9f68a019 100644 --- a/eo/test/t-eogeneration.cpp +++ b/eo/test/t-eogeneration.cpp @@ -7,23 +7,14 @@ #include +#include "binary_value.h" + //----------------------------------------------------------------------------- typedef eoBin 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 inclusion; - + + // Evaluation + eoEvalFuncPtr eval( binary_value ); + // GA generation - eoGeneration generation(lottery, breeder, inclusion); + eoGeneration generation(lottery, breeder, inclusion, eval); // evolution unsigned g = 0; do { try { - generation(pop, binary_value); + generation(pop); } catch (exception& e) {