diff --git a/eo/src/do/make_checkpoint_assembled.h b/eo/src/do/make_checkpoint_assembled.h index 182e9220..92d855d4 100644 --- a/eo/src/do/make_checkpoint_assembled.h +++ b/eo/src/do/make_checkpoint_assembled.h @@ -93,19 +93,19 @@ eoCheckPoint& do_make_checkpoint_assembled(eoParser& _parser, eoState& _sta // --------------------------- // average vals - std::vector* > avgvals( nTerms ); + std::vector* > avgvals( nTerms ); for (unsigned i=0; i < nTerms; ++i){ std::string descr = "Avg. of " + fitness_descriptions[i]; - avgvals[i] = new eoAverageStat(i, descr); + avgvals[i] = new eoAssembledFitnessAverageStat(i, descr); _state.storeFunctor( avgvals[i] ); checkpoint->add( *avgvals[i] ); } // best vals - std::vector* > bestvals( nTerms ); + std::vector* > bestvals( nTerms ); for (unsigned j=0; j < nTerms; ++j){ std::string descr = fitness_descriptions[j] + " of best ind."; - bestvals[j] = new eoBestFitnessStat(j, descr); + bestvals[j] = new eoAssembledFitnessBestStat(j, descr); _state.storeFunctor( bestvals[j] ); checkpoint->add( *bestvals[j] ); } diff --git a/eo/src/utils/checkpointing b/eo/src/utils/checkpointing index 3210764c..e2b7ce42 100644 --- a/eo/src/utils/checkpointing +++ b/eo/src/utils/checkpointing @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include diff --git a/eo/src/utils/eoAssembledFitnessStat.h b/eo/src/utils/eoAssembledFitnessStat.h new file mode 100644 index 00000000..b468f564 --- /dev/null +++ b/eo/src/utils/eoAssembledFitnessStat.h @@ -0,0 +1,111 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoAssembledFitnessStat.h +// Marc Wintermantel & Oliver Koenig +// IMES-ST@ETHZ.CH +// April 2003 + +//----------------------------------------------------------------------------- +// eoStat.h +// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 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 + mkeijzer@dhi.dk + */ +//----------------------------------------------------------------------------- + +#ifndef _eoAssembledFitnessStat_h +#define _eoAssembledFitnessStat_h + +#include +#include + +/** + Average fitness values of a population, where the fitness is + of type eoScalarAssembledFitness. Specify in the constructor, + for which fitness term (index) the average should be evaluated. +*/ +#ifdef _MSC_VER +template +class eoAssembledFitnessAverageStat : public eoStat +#else +template +class eoAssembledFitnessAverageStat : public eoStat +#endif +{ +public : + + typedef typename EOT::Fitness Fitness; + + eoAssembledFitnessAverageStat(unsigned _whichTerm=0, std::string _description = "Average Fitness") + : eoStat(Fitness(), _description), whichFitnessTerm(_whichTerm) {} + + virtual void operator()(const eoPop& _pop){ + + if ( whichFitnessTerm >= _pop[0].fitness().size() ) + throw std::logic_error("Fitness term requested out of range"); + + double result =0.0; + for (typename eoPop::const_iterator it = _pop.begin(); it != _pop.end(); ++it) + result+= it->fitness()[whichFitnessTerm]; + + value().clear(); + value() = result / _pop.size(); + + } + +private: + // Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled + unsigned whichFitnessTerm; +}; + +/** + Fitness values of best individuum in a population, where the fitness is + of type eoScalarAssembledFitness. Specify in the constructor, + for which fitness term (index) the value should be evaluated. +*/ +#ifdef _MSC_VER +template +class eoAssembledFitnessBestStat : public eoStat +#else +template +class eoAssembledFitnessBestStat : public eoStat +#endif +{ +public : + typedef typename EOT::Fitness Fitness; + + eoAssembledFitnessBestStat(unsigned _whichTerm=0, std::string _description = "Best Fitness") + : eoStat(Fitness(), _description), whichFitnessTerm(_whichTerm) {} + + virtual void operator()(const eoPop& _pop){ + + if ( whichFitnessTerm >= _pop[0].fitness().size() ) + throw std::logic_error("Fitness term requested out of range"); + + value().clear(); + value() = _pop.best_element().fitness()[whichFitnessTerm]; + } + +private: + // Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled + unsigned whichFitnessTerm; +}; + +#endif