From b83372b57b00b84206734659b92a314bffebcb1d Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Thu, 6 Sep 2012 16:05:00 +0200 Subject: [PATCH] Add a stat to keep the best individual found so far, even for non-monotonic algorithms --- eo/src/utils/eoStat.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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 {