aGrUM  0.16.0
approximationScheme_inl.h
Go to the documentation of this file.
1 
35 #include <agrum/agrum.h>
36 // To help IDE parser
38 
39 namespace gum {
40 
41  // Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)| If
42  // the criterion was disabled it will be enabled
43  INLINE void ApproximationScheme::setEpsilon(double eps) {
44  if (eps < 0.) { GUM_ERROR(OutOfLowerBound, "eps should be >=0"); }
45 
46  _eps = eps;
47  _enabled_eps = true;
48  }
49 
50  // Get the value of epsilon
51  INLINE double ApproximationScheme::epsilon() const { return _eps; }
52 
53  // Disable stopping criterion on epsilon
55 
56  // Enable stopping criterion on epsilon
58 
59  // @return true if stopping criterion on epsilon is enabled, false
60  // otherwise
62  return _enabled_eps;
63  }
64 
65  // Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|)
66  INLINE void ApproximationScheme::setMinEpsilonRate(double rate) {
67  if (rate < 0) { GUM_ERROR(OutOfLowerBound, "rate should be >=0"); }
68 
69  _min_rate_eps = rate;
70  _enabled_min_rate_eps = true;
71  }
72 
73  // Get the value of the minimal epsilon rate
74  INLINE double ApproximationScheme::minEpsilonRate() const {
75  return _min_rate_eps;
76  }
77 
78  // Disable stopping criterion on epsilon rate
80  _enabled_min_rate_eps = false;
81  }
82 
83  // Enable stopping criterion on epsilon rate
85  _enabled_min_rate_eps = true;
86  }
87 
88  // @return true if stopping criterion on epsilon rate is enabled, false
89  // otherwise
91  return _enabled_min_rate_eps;
92  }
93 
94  // stopping criterion on number of iterations
96  if (max < 1) { GUM_ERROR(OutOfLowerBound, "max should be >=1"); }
97  _max_iter = max;
98  _enabled_max_iter = true;
99  }
100 
101  // @return the criterion on number of iterations
102  INLINE Size ApproximationScheme::maxIter() const { return _max_iter; }
103 
104  // Disable stopping criterion on max iterations
106 
107  // Enable stopping criterion on max iterations
109 
110  // @return true if stopping criterion on max iterations is enabled, false
111  // otherwise
113  return _enabled_max_iter;
114  }
115 
116  // stopping criterion on timeout (in seconds)
117  // If the criterion was disabled it will be enabled
118  INLINE void ApproximationScheme::setMaxTime(double timeout) {
119  if (timeout <= 0.) { GUM_ERROR(OutOfLowerBound, "timeout should be >0."); }
120  _max_time = timeout;
121  _enabled_max_time = true;
122  }
123 
124  // returns the timeout (in seconds)
125  INLINE double ApproximationScheme::maxTime() const { return _max_time; }
126 
127  // get the current running time in second (double)
128  INLINE double ApproximationScheme::currentTime() const { return _timer.step(); }
129 
130  // Disable stopping criterion on timeout
132 
133  // Enable stopping criterion on timeout
135 
136  // @return true if stopping criterion on timeout is enabled, false
137  // otherwise
139  return _enabled_max_time;
140  }
141 
142  // how many samples between 2 stopping isEnableds
144  if (p < 1) { GUM_ERROR(OutOfLowerBound, "p should be >=1"); }
145 
146  _period_size = p;
147  }
148 
150 
151  // verbosity
152  INLINE void ApproximationScheme::setVerbosity(bool v) { _verbosity = v; }
153 
154  INLINE bool ApproximationScheme::verbosity() const { return _verbosity; }
155 
156  // history
159  return _current_state;
160  }
161 
162  // @throw OperationNotAllowed if scheme not performed
166  "state of the approximation scheme is undefined");
167  }
168 
169  return _current_step;
170  }
171 
172  // @throw OperationNotAllowed if scheme not performed or verbosity=false
173  INLINE const std::vector< double >& ApproximationScheme::history() const {
176  "state of the approximation scheme is udefined");
177  }
178 
179  if (verbosity() == false) {
180  GUM_ERROR(OperationNotAllowed, "No history when verbosity=false");
181  }
182 
183  return _history;
184  }
185 
186  // initialise the scheme
189  _current_step = 0;
191  _history.clear();
192  _timer.reset();
193  }
194 
195  // @return true if we are at the beginning of a period (compute error is
196  // mandatory)
198  if (_current_step < _burn_in) { return false; }
199 
200  if (_period_size == 1) { return true; }
201 
202  return ((_current_step - _burn_in) % _period_size == 0);
203  }
204 
205  // update the scheme w.r.t the new error and incr steps
206  INLINE void ApproximationScheme::updateApproximationScheme(unsigned int incr) {
207  _current_step += incr;
208  }
209 
211  if (_burn_in > _current_step) {
212  return _burn_in - _current_step;
213  } else {
214  return 0;
215  }
216  }
217 
218  // stop approximation scheme by user request.
222  }
223  }
224 
225  // update the scheme w.r.t the new error. Test the stopping criterions that
226  // are enabled
228  // For coherence, we fix the time used in the method
229 
230  double timer_step = _timer.step();
231 
232  if (_enabled_max_time) {
233  if (timer_step > _max_time) {
235  return false;
236  }
237  }
238 
239  if (!startOfPeriod()) { return true; }
240 
243  "state of the approximation scheme is not correct : "
245  }
246 
247  if (verbosity()) { _history.push_back(error); }
248 
249  if (_enabled_max_iter) {
250  if (_current_step > _max_iter) {
252  return false;
253  }
254  }
255 
257  _current_epsilon = error; // eps rate isEnabled needs it so affectation was
258  // moved from eps isEnabled below
259 
260  if (_enabled_eps) {
261  if (_current_epsilon <= _eps) {
263  return false;
264  }
265  }
266 
267  if (_last_epsilon >= 0.) {
268  if (_current_epsilon > .0) {
269  // ! _current_epsilon can be 0. AND epsilon
270  // isEnabled can be disabled !
271  _current_rate =
273  }
274  // limit with current eps ---> 0 is | 1 - ( last_eps / 0 ) | --->
275  // infinity the else means a return false if we isEnabled the rate below,
276  // as we would have returned false if epsilon isEnabled was enabled
277  else {
279  }
280 
281  if (_enabled_min_rate_eps) {
282  if (_current_rate <= _min_rate_eps) {
284  return false;
285  }
286  }
287  }
288 
290  if (onProgress.hasListener()) {
292  }
293 
294  return true;
295  } else {
296  return false;
297  }
298  }
299 
300  INLINE void
302  if (new_state == ApproximationSchemeSTATE::Continue) { return; }
303 
304  if (new_state == ApproximationSchemeSTATE::Undefined) { return; }
305 
306  _current_state = new_state;
307  _timer.pause();
308 
309  if (onStop.hasListener()) { GUM_EMIT1(onStop, messageApproximationScheme()); }
310  }
311 
312 } // namespace gum
double minEpsilonRate() const
Returns the value of the minimal epsilon rate.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:42
Signaler3< Size, double, double > onProgress
Progression, error and time.
void disableMinEpsilonRate()
Disable stopping criterion on epsilon rate.
bool _enabled_max_iter
If true, the maximum iterations stopping criterion is enabled.
double maxTime() const
Returns the timeout (in seconds).
bool _enabled_eps
If true, the threshold convergence is enabled.
#define GUM_EMIT1(signal, arg1)
Definition: signaler1.h:42
void enableMinEpsilonRate()
Enable stopping criterion on epsilon rate.
void _stopScheme(ApproximationSchemeSTATE new_state)
Stop the scheme given a new state.
void setPeriodSize(Size p)
How many samples between two stopping is enable.
Size remainingBurnIn()
Returns the remaining burn in.
double _current_epsilon
Current epsilon.
bool _enabled_min_rate_eps
If true, the minimal threshold for epsilon rate is enabled.
bool startOfPeriod()
Returns true if we are at the beginning of a period (compute error is mandatory). ...
void initApproximationScheme()
Initialise the scheme.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void setMinEpsilonRate(double rate)
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
void setVerbosity(bool v)
Set the verbosity on (true) or off (false).
Size periodSize() const
Returns the period size.
void setMaxTime(double timeout)
Stopping criterion on timeout.
Size _burn_in
Number of iterations before checking stopping criteria.
void disableEpsilon()
Disable stopping criterion on epsilon.
Size nbrIterations() const
Returns the number of iterations.
double _eps
Threshold for convergence.
void reset()
Reset the timer.
Definition: timer_inl.h:32
double _current_rate
Current rate.
double pause()
Pause the timer and return the delta (.
Definition: timer_inl.h:53
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
bool isEnabledMinEpsilonRate() const
Returns true if stopping criterion on epsilon rate is enabled, false otherwise.
Signaler1< std::string > onStop
Criteria messageApproximationScheme.
bool _enabled_max_time
If true, the timeout is enabled.
void disableMaxTime()
Disable stopping criterion on timeout.
void enableMaxIter()
Enable stopping criterion on max iterations.
void stopApproximationScheme()
Stop the approximation scheme.
Size maxIter() const
Returns the criterion on number of iterations.
Size _current_step
The current step.
std::vector< double > _history
The scheme history, used only if verbosity == true.
Size _period_size
Checking criteria frequency.
double _min_rate_eps
Threshold for the epsilon rate.
const std::vector< double > & history() const
Returns the scheme history.
ApproximationSchemeSTATE stateApproximationScheme() const
Returns the approximation scheme state.
void setMaxIter(Size max)
Stopping criterion on number of iterations.
bool isEnabledMaxIter() const
Returns true if stopping criterion on max iterations is enabled, false otherwise. ...
bool _verbosity
If true, verbosity is enabled.
bool verbosity() const
Returns true if verbosity is enabled.
std::string messageApproximationScheme() const
Returns the approximation scheme message.
double epsilon() const
Returns the value of epsilon.
double _last_epsilon
Last epsilon value.
bool isEnabledEpsilon() const
Returns true if stopping criterion on epsilon is enabled, false otherwise.
bool isEnabledMaxTime() const
Returns true if stopping criterion on timeout is enabled, false otherwise.
void setEpsilon(double eps)
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
double currentTime() const
Returns the current running time in second.
void disableMaxIter()
Disable stopping criterion on max iterations.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
ApproximationSchemeSTATE
The different state of an approximation scheme.
Size _max_iter
The maximum iterations.
#define GUM_EMIT3(signal, arg1, arg2, arg3)
Definition: signaler3.h:42
ApproximationSchemeSTATE _current_state
The current state.
double _max_time
The timeout.
void enableMaxTime()
Enable stopping criterion on timeout.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
void enableEpsilon()
Enable stopping criterion on epsilon.