aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
approximationScheme.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief This file contains general scheme for iteratively convergent
25  * algorithms.
26  *
27  * ApproximationSettings provides as well 2 signals :
28  * - onProgress(int pourcent,double error)
29  * - onStop(std::string message)
30  * @see gum::ApproximationListener for dedicated listener.
31  *
32  * @author Pierre-Henri WUILLEMIN(@LIP6)
33  */
34 #ifndef GUM_APPROXIMATION_SCHEME_H
35 #define GUM_APPROXIMATION_SCHEME_H
36 
37 #include <agrum/agrum.h>
38 #include <agrum/tools/core/math/math_utils.h>
39 #include <agrum/tools/core/approximations/IApproximationSchemeConfiguration.h>
40 #include <agrum/tools/core/signal/signaler.h>
41 #include <agrum/tools/core/timer.h>
42 
43 namespace gum {
44 
45  /**
46  * @class ApproximationScheme
47  * @brief Approximation Scheme.
48  * @ingroup approximationscheme_group
49  *
50  * @warning Doxygen does not like spanning command on multiple line, so we
51  * could not configure it with the correct include directive. Use the
52  * following code snippet to include this file.
53  * @code
54  * #include <agrum/tools/core/approximations/approximationScheme.h>
55  * @endcode
56  *
57  * The approximation scheme is assumed to be used like this:
58  * @code
59  * initApproximationScheme();
60  *
61  * // this loop can stop with ApproximationSchemeSTATE::Epsilon,
62  * // ApproximationSchemeSTATE::Rate and ApproximationSchemeSTATE::Limit
63  * do {
64  * // compute new values and a GUM_SCALAR error representing the progress
65  * // in this step.
66  * updateApproximationScheme();
67  * // compute state of the approximation
68  * if (startOfPeriod()) {
69  * // w.r.t to the state of approximation compute epsilon
70  * }
71  * } while ( continueApproximationScheme( epsilon ));
72  * // end of loop
73  *
74  * if ( verbosity() ) {
75  * switch ( stateApproximationScheme() ) {
76  * case ApproximationSchemeSTATE::Continue: // should not be possible
77  * break;
78  * case ApproximationSchemeSTATE::Epsilon: {
79  * GUM_TRACE( "stop with
80  * epsilon="<<epsilon() );
81  * break;
82  * }
83  * case ApproximationSchemeSTATE::Rate: {
84  * GUM_TRACE( "stop with
85  * rate="<<minEpsilonRate() );
86  * break;
87  * }
88  * case ApproximationSchemeSTATE::Limit: {
89  * GUM_TRACE( "stop with max
90  * iteration="<<maxIter() );
91  * break;
92  * }
93  * case ApproximationSchemeSTATE::TimeLimit: {
94  * GUM_TRACE( "stop with
95  * timemout="<<currentTime() );
96  * break;
97  * }
98  * }
99  * }
100  * // equivalent to
101  * if (verbosity()) {
102  * GUM_TRACE(messageApproximationScheme());
103  * }
104  * @endcode
105  */
107  public:
108  // ========================================================================
109  /// @name Constructors and Destructors
110  // ========================================================================
111  /// @{
112 
113  ApproximationScheme(bool verbosity = false);
114 
115  virtual ~ApproximationScheme();
116 
117  /// @}
118  // ========================================================================
119  /// @name Getters and setters
120  // ========================================================================
121  /// @{
122 
123  /**
124  * @brief Given that we approximate f(t), stopping criterion on
125  * |f(t+1)-f(t)|.
126  *
127  * If the criterion was disabled it will be enabled.
128  *
129  * @param eps The new epsilon value.
130  * @throw OutOfLowerBound Raised if eps < 0.
131  */
132  void setEpsilon(double eps);
133 
134  /**
135  * @brief Returns the value of epsilon.
136  * @return Returns the value of epsilon.
137  */
138  double epsilon() const;
139 
140  /**
141  * @brief Disable stopping criterion on epsilon.
142  */
143  void disableEpsilon();
144 
145  /**
146  * @brief Enable stopping criterion on epsilon.
147  */
148  void enableEpsilon();
149 
150  /**
151  * @brief Returns true if stopping criterion on epsilon is enabled, false
152  * otherwise.
153  * @return Returns true if stopping criterion on epsilon is enabled, false
154  * otherwise.
155  */
156  bool isEnabledEpsilon() const;
157 
158  /**
159  * @brief Given that we approximate f(t), stopping criterion on
160  * d/dt(|f(t+1)-f(t)|).
161  *
162  * If the criterion was disabled it will be enabled
163  *
164  * @param rate The minimal epsilon rate.
165  * @throw OutOfLowerBound if rate<0
166  */
167  void setMinEpsilonRate(double rate);
168 
169  /**
170  * @brief Returns the value of the minimal epsilon rate.
171  * @return Returns the value of the minimal epsilon rate.
172  */
173  double minEpsilonRate() const;
174 
175  /**
176  * @brief Disable stopping criterion on epsilon rate.
177  */
178  void disableMinEpsilonRate();
179 
180  /**
181  * @brief Enable stopping criterion on epsilon rate.
182  */
183  void enableMinEpsilonRate();
184 
185  /**
186  * @brief Returns true if stopping criterion on epsilon rate is enabled,
187  * false otherwise.
188  * @return Returns true if stopping criterion on epsilon rate is enabled,
189  * false otherwise.
190  */
191  bool isEnabledMinEpsilonRate() const;
192 
193  /**
194  * @brief Stopping criterion on number of iterations.
195  *
196  * If the criterion was disabled it will be enabled.
197  *
198  * @param max The maximum number of iterations.
199  * @throw OutOfLowerBound Raised if max <= 1.
200  */
201  void setMaxIter(Size max);
202 
203  /**
204  * @brief Returns the criterion on number of iterations.
205  * @return Returns the criterion on number of iterations.
206  */
207  Size maxIter() const;
208 
209  /**
210  * @brief Disable stopping criterion on max iterations.
211  */
212  void disableMaxIter();
213 
214  /**
215  * @brief Enable stopping criterion on max iterations.
216  */
217  void enableMaxIter();
218 
219  /**
220  * @brief Returns true if stopping criterion on max iterations is enabled,
221  * false otherwise.
222  * @return Returns true if stopping criterion on max iterations is enabled,
223  * false otherwise.
224  */
225  bool isEnabledMaxIter() const;
226 
227  /**
228  * @brief Stopping criterion on timeout.
229  *
230  * If the criterion was disabled it will be enabled.
231  *
232  * @param timeout The timeout value in seconds.
233  * @throw OutOfLowerBound Raised if timeout <= 0.0.
234  */
235  void setMaxTime(double timeout);
236 
237  /**
238  * @brief Returns the timeout (in seconds).
239  * @return Returns the timeout (in seconds).
240  */
241  double maxTime() const;
242 
243  /**
244  * @brief Returns the current running time in second.
245  * @return Returns the current running time in second.
246  */
247  double currentTime() const;
248 
249  /**
250  * @brief Disable stopping criterion on timeout.
251  * @return Disable stopping criterion on timeout.
252  */
253  void disableMaxTime();
254 
255  /**
256  * @brief Enable stopping criterion on timeout.
257  */
258  void enableMaxTime();
259 
260  /**
261  * @brief Returns true if stopping criterion on timeout is enabled, false
262  * otherwise.
263  * @return Returns true if stopping criterion on timeout is enabled, false
264  * otherwise.
265  */
266  bool isEnabledMaxTime() const;
267 
268  /**
269  * @brief How many samples between two stopping is enable.
270  * @param p The new period value.
271  * @throw OutOfLowerBound Raised if p < 1.
272  */
273  void setPeriodSize(Size p);
274 
275  /**
276  * @brief Returns the period size.
277  * @return Returns the period size.
278  */
279  Size periodSize() const;
280 
281  /**
282  * @brief Set the verbosity on (true) or off (false).
283  * @param v If true, then verbosity is turned on.
284  */
285  void setVerbosity(bool v);
286 
287  /**
288  * @brief Returns true if verbosity is enabled.
289  * @return Returns true if verbosity is enabled.
290  */
291  bool verbosity() const;
292 
293  /**
294  * @brief Returns the approximation scheme state.
295  * @return Returns the approximation scheme state.
296  */
298 
299  /**
300  * @brief Returns the number of iterations.
301  * @return Returns the number of iterations.
302  * @throw OperationNotAllowed Raised if the scheme did not perform.
303  */
304  Size nbrIterations() const;
305 
306  /**
307  * @brief Returns the scheme history.
308  * @return Returns the scheme history.
309  * @throw OperationNotAllowed Raised if the scheme did not performed or
310  * if verbosity is set to false.
311  */
312  const std::vector< double >& history() const;
313 
314  /**
315  * @brief Initialise the scheme.
316  */
318 
319  /**
320  * @brief Returns true if we are at the beginning of a period (compute
321  * error is mandatory).
322  * @return Returns true if we are at the beginning of a period (compute
323  * error is mandatory).
324  */
325  bool startOfPeriod();
326 
327  /**
328  * @brief Update the scheme w.r.t the new error and increment steps.
329  * @param incr The new increment steps.
330  */
331  void updateApproximationScheme(unsigned int incr = 1);
332 
333  /**
334  * @brief Returns the remaining burn in.
335  * @return Returns the remaining burn in.
336  */
338 
339  /**
340  * @brief Stop the approximation scheme.
341  */
343 
344  /**
345  * @brief Update the scheme w.r.t the new error.
346  *
347  * Test the stopping criterion that are enabled.
348  *
349  * @param error The new error value.
350  * @return false if state become != ApproximationSchemeSTATE::Continue
351  * @throw OperationNotAllowed Raised if state !=
352  * ApproximationSchemeSTATE::Continue.
353  */
354  bool continueApproximationScheme(double error);
355 
356 
357  /// @}
358 
359  private:
360  /**
361  * @brief Stop the scheme given a new state.
362  * @param new_state The scheme new state.
363  */
364  void stopScheme_(ApproximationSchemeSTATE new_state);
365 
366  protected:
367  /// Current epsilon.
369 
370  /// Last epsilon value.
372 
373  /// Current rate.
375 
376  /// The current step.
378 
379  /// The timer.
381 
382  /// The current state.
384 
385  /// The scheme history, used only if verbosity == true.
386  std::vector< double > history_;
387 
388  /// Threshold for convergence.
389  double eps_;
390 
391  /// If true, the threshold convergence is enabled.
393 
394  /// Threshold for the epsilon rate.
396 
397  /// If true, the minimal threshold for epsilon rate is enabled.
399 
400  /// The timeout.
401  double max_time_;
402 
403  /// If true, the timeout is enabled.
405 
406  /// The maximum iterations.
408 
409  /// If true, the maximum iterations stopping criterion is enabled.
411 
412  /// Number of iterations before checking stopping criteria.
414 
415  /// Checking criteria frequency.
417 
418  /// If true, verbosity is enabled.
420  };
421 } // namespace gum
422 
423 #ifndef GUM_NO_INLINE
424 # include <agrum/tools/core/approximations/approximationScheme_inl.h>
425 #endif
426 
427 #endif // GUM_APPROXIMATION_SCHEME_H
double max_time_
The timeout.
double minEpsilonRate() const
Returns the value of the minimal epsilon rate.
Size burn_in_
Number of iterations before checking stopping criteria.
void disableMinEpsilonRate()
Disable stopping criterion on epsilon rate.
double maxTime() const
Returns the timeout (in seconds).
Approximation Scheme.
ApproximationSchemeSTATE current_state_
The current state.
void enableMinEpsilonRate()
Enable stopping criterion on epsilon rate.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
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.
bool startOfPeriod()
Returns true if we are at the beginning of a period (compute error is mandatory). ...
void initApproximationScheme()
Initialise the scheme.
bool enabled_max_iter_
If true, the maximum iterations stopping criterion is enabled.
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).
ApproximationScheme(bool verbosity=false)
Size periodSize() const
Returns the period size.
void setMaxTime(double timeout)
Stopping criterion on timeout.
void disableEpsilon()
Disable stopping criterion on epsilon.
double last_epsilon_
Last epsilon value.
Size nbrIterations() const
Returns the number of iterations.
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
double eps_
Threshold for convergence.
double min_rate_eps_
Threshold for the epsilon rate.
bool enabled_max_time_
If true, the timeout is enabled.
bool isEnabledMinEpsilonRate() const
Returns true if stopping criterion on epsilon rate is enabled, false otherwise.
void disableMaxTime()
Disable stopping criterion on timeout.
double current_rate_
Current rate.
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 max_iter_
The maximum iterations.
double current_epsilon_
Current epsilon.
bool enabled_eps_
If true, the threshold convergence is enabled.
const std::vector< double > & history() const
Returns the scheme history.
bool verbosity_
If true, verbosity is enabled.
Size period_size_
Checking criteria frequency.
ApproximationSchemeSTATE stateApproximationScheme() const
Returns the approximation scheme state.
void setMaxIter(Size max)
Stopping criterion on number of iterations.
std::vector< double > history_
The scheme history, used only if verbosity == true.
bool isEnabledMaxIter() const
Returns true if stopping criterion on max iterations is enabled, false otherwise. ...
bool verbosity() const
Returns true if verbosity is enabled.
double epsilon() const
Returns the value of epsilon.
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.
bool enabled_min_rate_eps_
If true, the minimal threshold for epsilon rate is enabled.
void disableMaxIter()
Disable stopping criterion on max iterations.
Size current_step_
The current step.
void enableMaxTime()
Enable stopping criterion on timeout.
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.