Add an Nth element stat to compute median without sorting the pop

This commit is contained in:
Johann Dreo 2013-06-13 14:50:08 +02:00
commit 0c82be47df

View file

@ -475,6 +475,51 @@ public :
};
*/
//! A robust measure of the mass (generally used to compute the median). Do not alter the given pop.
template<class EOT>
class eoNthElementStat : public eoStat< EOT, typename EOT::Fitness >
{
protected:
int _nth;
double _ratio;
public:
using eoStat<EOT, typename EOT::Fitness>::value;
eoNthElementStat( int nth = 0, std::string description = "NthElement")
: eoStat<EOT,typename EOT::Fitness>( 0.0, description ), _nth(nth), _ratio(-1.0)
{}
eoNthElementStat( double ratio = 0.5, std::string description = "Median" )
: eoStat<EOT,typename EOT::Fitness>( 0.0, description ), _nth(-1), _ratio(ratio)
{}
virtual void operator()( const eoPop<EOT> & _pop )
{
if( _nth == -1 ) { // asked for a ratio
_nth = static_cast<int>( std::floor(_pop.size() * _ratio) );
} else {
assert( _ratio == -1 ); // asked for a position
}
if( _pop.size() == 0 ) {
//FIXME how to implement value() = 0 ?
eo::log << eo::warnings << "Called " << className() << " on an empty pop, value unchanged" << std::endl;
} else {
eoPop<EOT> pop = _pop; // copy, thus no sorting of the original pop
std::nth_element( pop.begin(), pop.begin()+_nth, pop.end() );
value() = pop[_nth].fitness();
}
}
virtual std::string className(void) const { return "eoNthElementStat"; }
};
/** @example t-eoIQRStat.cpp
*/
//! A robust measure of dispersion (also called midspread or middle fifty) that is the difference between the third and the first quartile.
template<class EOT>