diff --git a/eo/src/utils/eoStat.h b/eo/src/utils/eoStat.h index 32c3bf9f6..e7c5adca2 100644 --- a/eo/src/utils/eoStat.h +++ b/eo/src/utils/eoStat.h @@ -356,6 +356,38 @@ private : /** @example t-eoSSGA.cpp */ +/** Keep the best individual found so far + */ +template +class eoBestIndividualStat : public eoStat +{ +public: + using eoStat::value; + + eoBestIndividualStat(std::string _description = "BestIndiv ") + : eoStat( EOT(), _description ) + {} + + void operator()(const eoPop& pop) + { + EOT best = pop.best_element(); + // on the first call, value() is invalid + if( value().invalid() ) { + // thus we cannot compare it to something else + value() = best; + } else { + // keep the best individual found so far + if( best.fitness() > value().fitness() ) { + value() = best; + } + } + } + + virtual std::string className(void) const { return "eoBestIndividualStat"; } +}; + + + template class eoDistanceStat : public eoStat {