From 45e3b86bcb011b833182e1610bba02319d1f8e7b Mon Sep 17 00:00:00 2001 From: okoenig Date: Sat, 5 Apr 2003 13:40:57 +0000 Subject: [PATCH] Removed function template specializations for eoScalarFitnessAssembled from classes in this file, since they do not work under Visual Studio! Reimplemented these stat classes for this new fitness class in eoAssembledFitnessStat.h --- eo/src/utils/eoStat.h | 92 +++++++------------------------------------ 1 file changed, 15 insertions(+), 77 deletions(-) diff --git a/eo/src/utils/eoStat.h b/eo/src/utils/eoStat.h index 25da3e2b..cdf0f127 100644 --- a/eo/src/utils/eoStat.h +++ b/eo/src/utils/eoStat.h @@ -32,7 +32,6 @@ #include #include #include -#include #include /** @@ -87,10 +86,10 @@ public : Average fitness of a population. Fitness can be: - double - eoMinimizingFitness or eoMaximizingFitness - - eoScalarFitnessAssembled: - Specify in the constructor, for which fitness term (index) the average should be evaluated. - eoParetoFitness: The average of each objective is evaluated. + + ( For eoScalarFitnessAssembled user eoAssembledFitnessStat classes.) */ #ifdef _MSC_VER template class eoAverageStat : public eoStat @@ -99,35 +98,22 @@ template class eoAverageStat : public eoStat(fitness_type(), _description), whichFitnessTerm() {} - eoAverageStat(unsigned _whichTerm, std::string _description = "Average Fitness") - : eoStat(fitness_type(), _description), whichFitnessTerm(_whichTerm) {} -#else - eoAverageStat(std::string _description = "Average Fitness") - : eoStat(fitness_type(), _description), whichFitnessTerm() {} - eoAverageStat(unsigned _whichTerm, std::string _description = "Average Fitness") - : eoStat(fitness_type(), _description), whichFitnessTerm(_whichTerm) {} -#endif + typedef typename EOT::Fitness Fitness; - static fitness_type sumFitness(double _sum, const EOT& _eot) - { + eoAverageStat(std::string _description = "Average Fitness") + : eoStat(Fitness(), _description) {} + + static Fitness sumFitness(double _sum, const EOT& _eot){ _sum += _eot.fitness(); return _sum; } eoAverageStat(double _value, std::string _desc) : eoStat(_value, _desc) {} - virtual void operator()(const eoPop& _pop) - { -#ifdef _MSC_VER - doit(_pop, EOT::Fitness()); // specializations for scalar and std::vector -#else - doit(_pop, typename EOT::Fitness()); // specializations for scalar and std::vector -#endif + virtual void operator()(const eoPop& _pop){ + doit(_pop, Fitness()); // specializations for scalar and std::vector } + private : // Specialization for pareto fitness @@ -147,33 +133,16 @@ private : value()[o] /= _pop.size(); } } - - // Specialization for eoScalarFitnessAssembled - template - void doit(const eoPop& _pop, eoScalarFitnessAssembled) - { - - 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(); - } - + // Default behavior template void doit(const eoPop& _pop, T) { - fitness_type v = std::accumulate(_pop.begin(), _pop.end(), fitness_type(0.0), eoAverageStat::sumFitness); + Fitness v = std::accumulate(_pop.begin(), _pop.end(), Fitness(0.0), eoAverageStat::sumFitness); value() = v / _pop.size(); } - // Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled - unsigned whichFitnessTerm; }; /** @@ -223,8 +192,6 @@ public : eoNthElementFitnessStat(unsigned _whichElement, std::string _description = "nth element fitness") : eoSortedStat(Fitness(), _description), whichElement(_whichElement) {} - eoNthElementFitnessStat(unsigned _whichElement, unsigned _whichTerm, std::string _description = "nth element fitness") - : eoSortedStat(Fitness(), _description), whichElement(_whichElement), whichFitnessTerm(_whichTerm) {} virtual void operator()(const std::vector& _pop) { @@ -271,16 +238,6 @@ private : } } - // Specialization for eoScalarFitnessAssembled - template - void doit(const eoPop& _pop, eoScalarFitnessAssembled) - { - if ( whichFitnessTerm >= _pop[0].fitness().size() ) - throw std::logic_error("Fitness term requested out of range"); - value().clear(); - value() = _pop[whichElement]->fitness()[whichFitnessTerm]; - } - // for everything else template void doit(const std::vector& _pop, T) @@ -289,7 +246,6 @@ private : } unsigned whichElement; - unsigned whichFitnessTerm; }; /* Actually, you shouldn't need to sort the population to get the best fitness @@ -320,10 +276,9 @@ public : Best fitness of a population. Fitness can be: - double - eoMinimizingFitness or eoMaximizingFitness - - eoScalarFitnessAssembled: - Best individual is found according to its fitness, - specify in the constructor which fitness term of this individual should then be stored. - eoParetoFitness: + + ( For eoScalarFitnessAssembled look at eoAssembledFitnessStat ) */ #ifdef _MSC_VER @@ -339,15 +294,9 @@ public : eoBestFitnessStat(std::string _description = "Best ") : eoStat(Fitness(), _description) {} - eoBestFitnessStat(unsigned _whichTerm, std::string _description = "Best ") - : eoStat(Fitness(), _description), whichFitnessTerm(_whichTerm) {} void operator()(const eoPop& _pop){ -#ifdef _MSC_VER - doit(_pop, EOT::Fitness() ); // specializations for scalar and std::vector -#else - doit(_pop, typename EOT::Fitness()); // specializations for scalar and std::vector -#endif + doit(_pop, Fitness() ); // specializations for scalar and std::vector } private : @@ -382,16 +331,6 @@ private : } } - // Specialization for eoScalarFitnessAssembled - template - void doit(const eoPop& _pop, eoScalarFitnessAssembled){ - - if ( whichFitnessTerm >= _pop[0].fitness().size() ) - throw std::logic_error("Fitness term requested out of range"); - value().clear(); - value() = _pop.best_element().fitness()[whichFitnessTerm]; - } - // default template void doit(const eoPop& _pop, T) @@ -399,7 +338,6 @@ private : value() = _pop.best_element().fitness(); } - unsigned whichFitnessTerm; }; template