From c6868cf494927c804b41e398962b140e0ab0ba14 Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Thu, 13 Jun 2013 16:25:32 +0200 Subject: [PATCH] bugfix: correct nth position in nth element stat --- eo/src/utils/eoStat.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/eo/src/utils/eoStat.h b/eo/src/utils/eoStat.h index 7099470cd..06cf2f451 100644 --- a/eo/src/utils/eoStat.h +++ b/eo/src/utils/eoStat.h @@ -488,19 +488,27 @@ public: eoNthElementStat( int nth = 0, std::string description = "NthElement") : eoStat( 0.0, description ), _nth(nth), _ratio(-1.0) - {} + { + assert( _nth >= 0 ); + } eoNthElementStat( double ratio = 0.5, std::string description = "Median" ) : eoStat( 0.0, description ), _nth(-1), _ratio(ratio) - {} + { + assert( _ratio >= 0 ); + } virtual void operator()( const eoPop & _pop ) { + unsigned int nth; if( _nth == -1 ) { // asked for a ratio - _nth = static_cast( std::floor(_pop.size() * _ratio) ); + nth = static_cast( std::floor(_pop.size() * _ratio) ); } else { assert( _ratio == -1 ); // asked for a position + nth = static_cast(_nth); } + assert( nth >= 0 ); + assert( nth < _pop.size() ); if( _pop.size() == 0 ) { //FIXME how to implement value() = 0 ? @@ -509,8 +517,8 @@ public: } else { eoPop pop = _pop; // copy, thus no sorting of the original pop - std::nth_element( pop.begin(), pop.begin()+_nth, pop.end() ); - value() = pop[_nth].fitness(); + std::nth_element( pop.begin(), pop.begin()+nth, pop.end() ); + value() = pop[nth].fitness(); } }