This repository has been archived on 2026-03-28. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
eodev/eo/src/utils/eoUpdater.h
evomarc 0e62de2d14 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.
2000-12-04 06:58:43 +00:00

117 lines
3.3 KiB
C++

// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoUpdater.h
// (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2000
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef _eoUpdater_h
#define _eoUpdater_h
#include <eoFunctor.h>
#include <utils/eoState.h>
/**
eoUpdater is a generic procudere for updating whatever you want.
Yet again an empty name
*/
class eoUpdater : public eoF<void>
{
public:
virtual void lastCall() {}
};
/**
an eoUpdater that simply increments a counter
*/
template <class T>
class eoIncrementor : public eoUpdater
{public :
eoIncrementor(T& _counter, T _stepsize = 1) : counter(_counter), stepsize(_stepsize) {}
virtual void operator()()
{
counter += stepsize;
}
private:
T& counter;
T stepsize;
};
#include <time.h>
/**
an eoUpdater that saves a state every given time interval
*/
class eoTimedStateSaver : public eoUpdater
{
public :
eoTimedStateSaver(time_t _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav") : state(_state),
interval(_interval), last_time(time(0)), first_time(time(0)),
prefix(_prefix), extension(_extension) {}
void operator()(void);
private :
const eoState& state;
const time_t interval;
time_t last_time;
const time_t first_time;
const std::string prefix;
const std::string extension;
};
/**
an eoUpdater that saves a state every given generations
*/
class eoCountedStateSaver : public eoUpdater
{
public :
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;
};
#endif