From 0e62de2d1499c8cca89dd9d6eb7d1bbc78022a69 Mon Sep 17 00:00:00 2001 From: evomarc Date: Mon, 4 Dec 2000 06:58:43 +0000 Subject: [PATCH] Added the lastCall construct: if the stopping condition becomes true in eoCheckPoint, a method called lastCall is called for everything contained in that checkpoint (stats, updaters and monitors). This can be extremely useful - for stateSavers (see below) - for monitoring things like rates of success of operators, where what you are interested in is the final result only. Added of course a virtual method lastCall that does nothing by default in classes eoBaseStat, eoBaseSortedStat, eoUpdater and eoMonitor Added a boolean to control the save of the state in method eoCountedStateSaver::lastCall so you can ask that the state is saved at final population, whatever happens. I also added the corresponding constructor to take this into account. --- eo/src/utils/eoCheckPoint.h | 24 ++++++++++++++++++++++-- eo/src/utils/eoMonitor.h | 1 + eo/src/utils/eoStat.h | 11 ++++++++--- eo/src/utils/eoUpdater.cpp | 22 ++++++++++++++++------ eo/src/utils/eoUpdater.h | 21 +++++++++++++++++---- 5 files changed, 64 insertions(+), 15 deletions(-) diff --git a/eo/src/utils/eoCheckPoint.h b/eo/src/utils/eoCheckPoint.h index 199188bd..7c8a1d11 100644 --- a/eo/src/utils/eoCheckPoint.h +++ b/eo/src/utils/eoCheckPoint.h @@ -73,9 +73,9 @@ bool eoCheckPoint::operator()(const eoPop& _pop) { unsigned i; + vector sorted_pop; if (!sorted.empty()) { - vector sorted_pop; _pop.sort(sorted_pop); for (i = 0; i < sorted.size(); ++i) @@ -93,7 +93,27 @@ bool eoCheckPoint::operator()(const eoPop& _pop) for (i = 0; i < monitors.size(); ++i) (*monitors[i])(); - return cont(_pop); + bool bContinue = cont(_pop); + + if (! bContinue) // we're going to stop: lastCall, gentlemen + { + if (!sorted.empty()) + { + for (i = 0; i < sorted.size(); ++i) + { + sorted[i]->lastCall(sorted_pop); + } + } + for (i = 0; i < stats.size(); ++i) + stats[i]->lastCall(_pop); + + for (i = 0; i < updaters.size(); ++i) + updaters[i]->lastCall(); + + for (i = 0; i < monitors.size(); ++i) + monitors[i]->lastCall(); + } + return bContinue; } #endif diff --git a/eo/src/utils/eoMonitor.h b/eo/src/utils/eoMonitor.h index 68235da3..ffddf27c 100644 --- a/eo/src/utils/eoMonitor.h +++ b/eo/src/utils/eoMonitor.h @@ -45,6 +45,7 @@ class eoMonitor : public eoF { public : + virtual void lastCall() {} void add(const eoParam& _param) { vec.push_back(&_param); } diff --git a/eo/src/utils/eoStat.h b/eo/src/utils/eoStat.h index 108f951f..7d06d833 100644 --- a/eo/src/utils/eoStat.h +++ b/eo/src/utils/eoStat.h @@ -37,7 +37,10 @@ */ template class eoStatBase : public eoUF&, void> -{}; +{ +public: + virtual void lastCall(const eoPop&) {} +}; /** The actual class that will be used as base for all statistics @@ -57,6 +60,8 @@ public : template class eoSortedStatBase : public eoUF&, void> { +public: + virtual void lastCall(const vector&) {} }; /** @@ -150,7 +155,7 @@ private : unsigned which; }; -/* Actually, you don'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 MS - 17/11/00 template @@ -171,7 +176,7 @@ public : */ /** - Best fitness in the population (this is NOT an eoSortedStat but an eoStat) + Best fitness in the population */ template class eoBestFitnessStat : public eoNthElementFitnessStat diff --git a/eo/src/utils/eoUpdater.cpp b/eo/src/utils/eoUpdater.cpp index 9bcdc1f8..87759cb4 100644 --- a/eo/src/utils/eoUpdater.cpp +++ b/eo/src/utils/eoUpdater.cpp @@ -24,13 +24,23 @@ void eoTimedStateSaver::operator()(void) } } +void eoCountedStateSaver::doItNow(void) +{ + ostrstream os; + os << prefix << counter << '.' << extension << ends; + state.save(os.str()); +} + void eoCountedStateSaver::operator()(void) { if (++counter % interval == 0) - { - ostrstream os; - os << prefix << counter << '.' << extension << ends; - state.save(os.str()); - } -}; + doItNow(); +} + +void eoCountedStateSaver::lastCall(void) +{ + if (saveOnLastCall) + doItNow(); +} + diff --git a/eo/src/utils/eoUpdater.h b/eo/src/utils/eoUpdater.h index 39319f9f..32b800db 100644 --- a/eo/src/utils/eoUpdater.h +++ b/eo/src/utils/eoUpdater.h @@ -35,7 +35,10 @@ Yet again an empty name */ class eoUpdater : public eoF -{}; +{ +public: + virtual void lastCall() {} +}; /** an eoUpdater that simply increments a counter @@ -85,16 +88,26 @@ private : class eoCountedStateSaver : public eoUpdater { public : - eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav") - : state(_state), interval(_interval), counter(0), - prefix(_prefix), extension(_extension) {} + eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix, bool _saveOnLastCall, std::string _extension = "sav") + : state(_state), interval(_interval), counter(0), + saveOnLastCall(_saveOnLastCall), + prefix(_prefix), extension(_extension) {} + eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav") + : state(_state), interval(_interval), counter(0), + saveOnLastCall(false), + prefix(_prefix), extension(_extension) {} + + virtual void lastCall(void); void operator()(void); private : + void doItNow(void); + const eoState& state; const unsigned interval; unsigned counter; + bool saveOnLastCall; const std::string prefix; const std::string extension;