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
This commit is contained in:
parent
a172d34e5f
commit
45e3b86bcb
1 changed files with 14 additions and 76 deletions
|
|
@ -32,7 +32,6 @@
|
||||||
#include <eoFunctor.h>
|
#include <eoFunctor.h>
|
||||||
#include <utils/eoParam.h>
|
#include <utils/eoParam.h>
|
||||||
#include <eoPop.h>
|
#include <eoPop.h>
|
||||||
#include <eoScalarFitnessAssembled.h>
|
|
||||||
#include <eoParetoFitness.h>
|
#include <eoParetoFitness.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -87,10 +86,10 @@ public :
|
||||||
Average fitness of a population. Fitness can be:
|
Average fitness of a population. Fitness can be:
|
||||||
- double
|
- double
|
||||||
- eoMinimizingFitness or eoMaximizingFitness
|
- eoMinimizingFitness or eoMaximizingFitness
|
||||||
- eoScalarFitnessAssembled:
|
|
||||||
Specify in the constructor, for which fitness term (index) the average should be evaluated.
|
|
||||||
- eoParetoFitness:
|
- eoParetoFitness:
|
||||||
The average of each objective is evaluated.
|
The average of each objective is evaluated.
|
||||||
|
|
||||||
|
( For eoScalarFitnessAssembled user eoAssembledFitnessStat classes.)
|
||||||
*/
|
*/
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
template <class EOT> class eoAverageStat : public eoStat<EOT, EOT::Fitness>
|
template <class EOT> class eoAverageStat : public eoStat<EOT, EOT::Fitness>
|
||||||
|
|
@ -99,35 +98,22 @@ template <class EOT> class eoAverageStat : public eoStat<EOT, typename EOT::Fitn
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
public :
|
public :
|
||||||
typedef typename EOT::Fitness fitness_type;
|
typedef typename EOT::Fitness Fitness;
|
||||||
#ifdef _MSC_VER
|
|
||||||
eoAverageStat(std::string _description = "Average Fitness")
|
|
||||||
: eoStat<EOT, EOT::Fitness>(fitness_type(), _description), whichFitnessTerm() {}
|
|
||||||
eoAverageStat(unsigned _whichTerm, std::string _description = "Average Fitness")
|
|
||||||
: eoStat<EOT, EOT::Fitness>(fitness_type(), _description), whichFitnessTerm(_whichTerm) {}
|
|
||||||
#else
|
|
||||||
eoAverageStat(std::string _description = "Average Fitness")
|
|
||||||
: eoStat<EOT, typename EOT::Fitness>(fitness_type(), _description), whichFitnessTerm() {}
|
|
||||||
eoAverageStat(unsigned _whichTerm, std::string _description = "Average Fitness")
|
|
||||||
: eoStat<EOT, typename EOT::Fitness>(fitness_type(), _description), whichFitnessTerm(_whichTerm) {}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static fitness_type sumFitness(double _sum, const EOT& _eot)
|
eoAverageStat(std::string _description = "Average Fitness")
|
||||||
{
|
: eoStat<EOT, Fitness>(Fitness(), _description) {}
|
||||||
|
|
||||||
|
static Fitness sumFitness(double _sum, const EOT& _eot){
|
||||||
_sum += _eot.fitness();
|
_sum += _eot.fitness();
|
||||||
return _sum;
|
return _sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
eoAverageStat(double _value, std::string _desc) : eoStat<EOT, double>(_value, _desc) {}
|
eoAverageStat(double _value, std::string _desc) : eoStat<EOT, double>(_value, _desc) {}
|
||||||
|
|
||||||
virtual void operator()(const eoPop<EOT>& _pop)
|
virtual void operator()(const eoPop<EOT>& _pop){
|
||||||
{
|
doit(_pop, Fitness()); // specializations for scalar and std::vector
|
||||||
#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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
// Specialization for pareto fitness
|
// Specialization for pareto fitness
|
||||||
|
|
@ -148,32 +134,15 @@ private :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specialization for eoScalarFitnessAssembled
|
|
||||||
template <class ScalarType, class Compare, class FitnessTraits>
|
|
||||||
void doit(const eoPop<EOT>& _pop, eoScalarFitnessAssembled<ScalarType, Compare>)
|
|
||||||
{
|
|
||||||
|
|
||||||
if ( whichFitnessTerm >= _pop[0].fitness().size() )
|
|
||||||
throw std::logic_error("Fitness term requested out of range");
|
|
||||||
|
|
||||||
double result =0.0;
|
|
||||||
for (typename eoPop<EOT>::const_iterator it = _pop.begin(); it != _pop.end(); ++it)
|
|
||||||
result+= it->fitness()[whichFitnessTerm];
|
|
||||||
value().clear();
|
|
||||||
value() = result / _pop.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default behavior
|
// Default behavior
|
||||||
template <class T>
|
template <class T>
|
||||||
void doit(const eoPop<EOT>& _pop, T)
|
void doit(const eoPop<EOT>& _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();
|
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")
|
eoNthElementFitnessStat(unsigned _whichElement, std::string _description = "nth element fitness")
|
||||||
: eoSortedStat<EOT, Fitness>(Fitness(), _description), whichElement(_whichElement) {}
|
: eoSortedStat<EOT, Fitness>(Fitness(), _description), whichElement(_whichElement) {}
|
||||||
eoNthElementFitnessStat(unsigned _whichElement, unsigned _whichTerm, std::string _description = "nth element fitness")
|
|
||||||
: eoSortedStat<EOT, Fitness>(Fitness(), _description), whichElement(_whichElement), whichFitnessTerm(_whichTerm) {}
|
|
||||||
|
|
||||||
virtual void operator()(const std::vector<const EOT*>& _pop)
|
virtual void operator()(const std::vector<const EOT*>& _pop)
|
||||||
{
|
{
|
||||||
|
|
@ -271,16 +238,6 @@ private :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specialization for eoScalarFitnessAssembled
|
|
||||||
template <class ScalarType, class Compare>
|
|
||||||
void doit(const eoPop<EOT>& _pop, eoScalarFitnessAssembled<ScalarType, Compare>)
|
|
||||||
{
|
|
||||||
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
|
// for everything else
|
||||||
template <class T>
|
template <class T>
|
||||||
void doit(const std::vector<const EOT*>& _pop, T)
|
void doit(const std::vector<const EOT*>& _pop, T)
|
||||||
|
|
@ -289,7 +246,6 @@ private :
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned whichElement;
|
unsigned whichElement;
|
||||||
unsigned whichFitnessTerm;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Actually, you shouldn't need to sort the population to get the best fitness
|
/* 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:
|
Best fitness of a population. Fitness can be:
|
||||||
- double
|
- double
|
||||||
- eoMinimizingFitness or eoMaximizingFitness
|
- 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:
|
- eoParetoFitness:
|
||||||
|
|
||||||
|
( For eoScalarFitnessAssembled look at eoAssembledFitnessStat )
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
|
@ -339,15 +294,9 @@ public :
|
||||||
|
|
||||||
eoBestFitnessStat(std::string _description = "Best ")
|
eoBestFitnessStat(std::string _description = "Best ")
|
||||||
: eoStat<EOT, Fitness>(Fitness(), _description) {}
|
: eoStat<EOT, Fitness>(Fitness(), _description) {}
|
||||||
eoBestFitnessStat(unsigned _whichTerm, std::string _description = "Best ")
|
|
||||||
: eoStat<EOT, Fitness>(Fitness(), _description), whichFitnessTerm(_whichTerm) {}
|
|
||||||
|
|
||||||
void operator()(const eoPop<EOT>& _pop){
|
void operator()(const eoPop<EOT>& _pop){
|
||||||
#ifdef _MSC_VER
|
doit(_pop, Fitness() ); // specializations for scalar and std::vector
|
||||||
doit(_pop, EOT::Fitness() ); // specializations for scalar and std::vector
|
|
||||||
#else
|
|
||||||
doit(_pop, typename EOT::Fitness()); // specializations for scalar and std::vector
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
@ -382,16 +331,6 @@ private :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specialization for eoScalarFitnessAssembled
|
|
||||||
template <class ScalarType, class Compare, class FitnessTraits>
|
|
||||||
void doit(const eoPop<EOT>& _pop, eoScalarFitnessAssembled<ScalarType, Compare>){
|
|
||||||
|
|
||||||
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
|
// default
|
||||||
template<class T>
|
template<class T>
|
||||||
void doit(const eoPop<EOT>& _pop, T)
|
void doit(const eoPop<EOT>& _pop, T)
|
||||||
|
|
@ -399,7 +338,6 @@ private :
|
||||||
value() = _pop.best_element().fitness();
|
value() = _pop.best_element().fitness();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned whichFitnessTerm;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class EOT>
|
template <class EOT>
|
||||||
|
|
|
||||||
Reference in a new issue