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.
This commit is contained in:
evomarc 2000-12-04 06:58:43 +00:00
commit 0e62de2d14
5 changed files with 63 additions and 14 deletions

View file

@ -73,9 +73,9 @@ bool eoCheckPoint<EOT>::operator()(const eoPop<EOT>& _pop)
{
unsigned i;
vector<const EOT*> sorted_pop;
if (!sorted.empty())
{
vector<const EOT*> sorted_pop;
_pop.sort(sorted_pop);
for (i = 0; i < sorted.size(); ++i)
@ -93,7 +93,27 @@ bool eoCheckPoint<EOT>::operator()(const eoPop<EOT>& _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

View file

@ -45,6 +45,7 @@ class eoMonitor : public eoF<eoMonitor&>
{
public :
virtual void lastCall() {}
void add(const eoParam& _param) { vec.push_back(&_param); }

View file

@ -37,7 +37,10 @@
*/
template <class EOT>
class eoStatBase : public eoUF<const eoPop<EOT>&, void>
{};
{
public:
virtual void lastCall(const eoPop<EOT>&) {}
};
/**
The actual class that will be used as base for all statistics
@ -57,6 +60,8 @@ public :
template <class EOT>
class eoSortedStatBase : public eoUF<const vector<const EOT*>&, void>
{
public:
virtual void lastCall(const vector<const EOT*>&) {}
};
/**
@ -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 <class EOT>
@ -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 EOT>
class eoBestFitnessStat : public eoNthElementFitnessStat<EOT>

View file

@ -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();
}

View file

@ -35,7 +35,10 @@
Yet again an empty name
*/
class eoUpdater : public eoF<void>
{};
{
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;