aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
approximationScheme_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
55
56#include <agrum/agrum.h>
57// To help IDE parser
59
60namespace gum {
61
62 // Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)| If
63 // the criterion was disabled it will be enabled
64 INLINE void ApproximationScheme::setEpsilon(double eps) {
65 if (eps < 0.) { GUM_ERROR(OutOfBounds, "eps should be >=0") }
66
67 eps_ = eps;
68 enabled_eps_ = true;
69 }
70
71 // Get the value of epsilon
72 INLINE double ApproximationScheme::epsilon() const { return eps_; }
73
74 // Disable stopping criterion on epsilon
76
77 // Enable stopping criterion on epsilon
79
80 // @return true if stopping criterion on epsilon is enabled, false
81 // otherwise
83
84 // Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|)
85 INLINE void ApproximationScheme::setMinEpsilonRate(double rate) {
86 if (rate < 0) { GUM_ERROR(OutOfBounds, "rate should be >=0") }
87
88 min_rate_eps_ = rate;
90 }
91
92 // Get the value of the minimal epsilon rate
93 INLINE double ApproximationScheme::minEpsilonRate() const { return min_rate_eps_; }
94
95 // Disable stopping criterion on epsilon rate
97
98 // Enable stopping criterion on epsilon rate
100
101 // @return true if stopping criterion on epsilon rate is enabled, false
102 // otherwise
104
105 // stopping criterion on number of iterations
107 if (max < 1) { GUM_ERROR(OutOfBounds, "max should be >=1") }
108 max_iter_ = max;
109 enabled_max_iter_ = true;
110 }
111
112 // @return the criterion on number of iterations
113 INLINE Size ApproximationScheme::maxIter() const { return max_iter_; }
114
115 // Disable stopping criterion on max iterations
117
118 // Enable stopping criterion on max iterations
120
121 // @return true if stopping criterion on max iterations is enabled, false
122 // otherwise
124
125 // stopping criterion on timeout (in seconds)
126 // If the criterion was disabled it will be enabled
127 INLINE void ApproximationScheme::setMaxTime(double timeout) {
128 if (timeout <= 0.) { GUM_ERROR(OutOfBounds, "timeout should be >0.") }
129 max_time_ = timeout;
130 enabled_max_time_ = true;
131 }
132
133 // returns the timeout (in seconds)
134 INLINE double ApproximationScheme::maxTime() const { return max_time_; }
135
136 // get the current running time in second (double)
137 INLINE double ApproximationScheme::currentTime() const { return timer_.step(); }
138
139 // Disable stopping criterion on timeout
141
142 // Enable stopping criterion on timeout
144
145 // @return true if stopping criterion on timeout is enabled, false
146 // otherwise
149 // how many samples between 2 stopping isEnableds
151 if (p < 1) { GUM_ERROR(OutOfBounds, "p should be >=1") }
152
154 }
155
157
158 // verbosity
160
161 INLINE bool ApproximationScheme::verbosity() const { return verbosity_; }
162
163 // history
167 }
168
169 // @throw OperationNotAllowed if scheme not performed
172 GUM_ERROR(OperationNotAllowed, "state of the approximation scheme is undefined")
173 }
174
175 return current_step_;
176 }
178 // @throw OperationNotAllowed if scheme not performed or verbosity=false
179 INLINE const std::vector< double >& ApproximationScheme::history() const {
181 GUM_ERROR(OperationNotAllowed, "state of the approximation scheme is udefined")
182 }
184 if (!verbosity()) GUM_ERROR(OperationNotAllowed, "No history when verbosity=false")
185
186 return history_;
187 }
189 // initialise the scheme
192 current_step_ = 0;
194 history_.clear();
195 timer_.reset();
196 }
197
198 // @return true if we are at the beginning of a period (compute error is
199 // mandatory)
201 if (current_step_ < burn_in_) { return false; }
202
203 if (period_size_ == 1) { return true; }
204
205 return ((current_step_ - burn_in_) % period_size_ == 0);
206 }
207
208 // update the scheme w.r.t the new error and incr steps
209 INLINE void ApproximationScheme::updateApproximationScheme(unsigned int incr) {
210 current_step_ += incr;
212
214 if (burn_in_ > current_step_) {
215 return burn_in_ - current_step_;
216 } else {
217 return 0;
218 }
219 }
220
221 // stop approximation scheme by user request.
225 }
226 }
228 // update the scheme w.r.t the new error. Test the stopping criterions that
229 // are enabled
230
232 if (new_state == ApproximationSchemeSTATE::Continue) { return; }
233
234 if (new_state == ApproximationSchemeSTATE::Undefined) { return; }
236 current_state_ = new_state;
237 timer_.pause();
238
239 if (onStop.hasListener()) { GUM_EMIT1(onStop, messageApproximationScheme()); }
240 }
241
242} // namespace gum
This file contains general scheme for iteratively convergent algorithms.
Size remainingBurnIn() const
Returns the remaining burn in.
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
Size current_step_
The current step.
bool isEnabledEpsilon() const override
Returns true if stopping criterion on epsilon is enabled, false otherwise.
double eps_
Threshold for convergence.
void setMaxIter(Size max) override
Stopping criterion on number of iterations.
void disableMaxTime() override
Disable stopping criterion on timeout.
void setMaxTime(double timeout) override
Stopping criterion on timeout.
void enableMaxIter() override
Enable stopping criterion on max iterations.
double minEpsilonRate() const override
Returns the value of the minimal epsilon rate.
void setMinEpsilonRate(double rate) override
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
void disableEpsilon() override
Disable stopping criterion on epsilon.
bool enabled_max_time_
If true, the timeout is enabled.
Size max_iter_
The maximum iterations.
bool enabled_eps_
If true, the threshold convergence is enabled.
double min_rate_eps_
Threshold for the epsilon rate.
double epsilon() const override
Returns the value of epsilon.
void setPeriodSize(Size p) override
How many samples between two stopping is enable.
bool isEnabledMaxTime() const override
Returns true if stopping criterion on timeout is enabled, false otherwise.
ApproximationSchemeSTATE stateApproximationScheme() const override
Returns the approximation scheme state.
void enableEpsilon() override
Enable stopping criterion on epsilon.
double currentTime() const override
Returns the current running time in second.
bool isEnabledMinEpsilonRate() const override
Returns true if stopping criterion on epsilon rate is enabled, false otherwise.
bool startOfPeriod() const
Returns true if we are at the beginning of a period (compute error is mandatory).
bool enabled_max_iter_
If true, the maximum iterations stopping criterion is enabled.
double maxTime() const override
Returns the timeout (in seconds).
void disableMinEpsilonRate() override
Disable stopping criterion on epsilon rate.
Size periodSize() const override
Returns the period size.
void enableMaxTime() override
Enable stopping criterion on timeout.
ApproximationScheme(bool verbosity=false)
void initApproximationScheme()
Initialise the scheme.
Size nbrIterations() const override
Returns the number of iterations.
const std::vector< double > & history() const override
Returns the scheme history.
void stopScheme_(ApproximationSchemeSTATE new_state)
Stop the scheme given a new state.
Size maxIter() const override
Returns the criterion on number of iterations.
bool verbosity_
If true, verbosity is enabled.
void stopApproximationScheme()
Stop the approximation scheme.
void enableMinEpsilonRate() override
Enable stopping criterion on epsilon rate.
bool verbosity() const override
Returns true if verbosity is enabled.
bool isEnabledMaxIter() const override
Returns true if stopping criterion on max iterations is enabled, false otherwise.
void disableMaxIter() override
Disable stopping criterion on max iterations.
void setVerbosity(bool v) override
Set the verbosity on (true) or off (false).
bool enabled_min_rate_eps_
If true, the minimal threshold for epsilon rate is enabled.
void setEpsilon(double eps) override
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
ApproximationSchemeSTATE
The different state of an approximation scheme.
std::string messageApproximationScheme() const
Returns the approximation scheme message.
Exception : operation not allowed.
Exception : out of bound.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
#define GUM_EMIT1(signal, arg1)
Definition signaler.h:289