aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
approximationScheme.cpp
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
53
54
#include <
agrum/base/core/approximations/approximationScheme.h
>
55
56
namespace
gum
{
57
58
ApproximationScheme::ApproximationScheme
(
bool
verbosity
) :
59
current_state_
(
ApproximationSchemeSTATE
::Undefined),
eps_
(5e-2),
enabled_eps_
(true),
60
min_rate_eps_
(1e-2),
enabled_min_rate_eps_
(true),
max_time_
(1.),
enabled_max_time_
(false),
61
max_iter_
(static_cast<
Size
>(10000)),
enabled_max_iter_
(true),
62
burn_in_
(static_cast<
Size
>(0)),
period_size_
(static_cast<
Size
>(1)),
63
verbosity_
(
verbosity
) {
64
GUM_CONSTRUCTOR(
ApproximationScheme
);
65
}
66
67
ApproximationScheme::~ApproximationScheme
() { GUM_DESTRUCTOR(
ApproximationScheme
); }
68
69
bool
ApproximationScheme::continueApproximationScheme
(
double
error) {
70
// For coherence, we fix the time used in the method
71
72
double
timer_step =
timer_
.
step
();
73
74
if
(
enabled_max_time_
) {
75
if
(timer_step >
max_time_
) {
76
stopScheme_
(
ApproximationSchemeSTATE::TimeLimit
);
77
return
false
;
78
}
79
}
80
81
if
(!
startOfPeriod
()) {
return
true
; }
82
83
if
(
current_state_
!=
ApproximationSchemeSTATE::Continue
) {
84
GUM_ERROR
(
85
OperationNotAllowed
,
86
"state of the approximation scheme is not correct : "
<<
messageApproximationScheme
());
87
}
88
89
if
(
verbosity
()) {
history_
.push_back(error); }
90
91
if
(
enabled_max_iter_
) {
92
if
(
current_step_
>=
max_iter_
) {
93
stopScheme_
(
ApproximationSchemeSTATE::Limit
);
94
return
false
;
95
}
96
}
97
98
last_epsilon_
=
current_epsilon_
;
99
current_epsilon_
= error;
// eps rate isEnabled needs it so affectation was
100
// moved from eps isEnabled below
101
102
if
(
enabled_eps_
) {
103
if
(
current_epsilon_
<=
eps_
) {
104
stopScheme_
(
ApproximationSchemeSTATE::Epsilon
);
105
return
false
;
106
}
107
}
108
109
if
(
last_epsilon_
>= 0.) {
110
if
(
current_epsilon_
> .0) {
111
// ! current_epsilon_ can be 0. AND epsilon
112
// isEnabled can be disabled !
113
current_rate_
= std::fabs((
current_epsilon_
-
last_epsilon_
) /
current_epsilon_
);
114
}
115
// limit with current eps ---> 0 is | 1 - ( last_eps / 0 ) | --->
116
// infinity the else means a return false if we isEnabled the rate below,
117
// as we would have returned false if epsilon isEnabled was enabled
118
else
{
119
current_rate_
=
min_rate_eps_
;
120
}
121
122
if
(
enabled_min_rate_eps_
) {
123
if
(
current_rate_
<=
min_rate_eps_
) {
124
stopScheme_
(
ApproximationSchemeSTATE::Rate
);
125
return
false
;
126
}
127
}
128
}
129
130
if
(
stateApproximationScheme
() ==
ApproximationSchemeSTATE::Continue
) {
131
if
(
onProgress
.
hasListener
()) {
132
GUM_EMIT3
(
onProgress
,
current_step_
,
current_epsilon_
, timer_step);
133
}
134
135
return
true
;
136
}
else
{
137
return
false
;
138
}
139
}
140
}
// namespace gum
141
142
#ifdef GUM_NO_INLINE
143
# include <
agrum/base/core/approximations/approximationScheme_inl.h
>
144
#endif
approximationScheme.h
This file contains general scheme for iteratively convergent algorithms.
approximationScheme_inl.h
Inline implementatioh of gum::ApproximationSettings.
gum::ApproximationScheme::current_step_
Size current_step_
The current step.
Definition
approximationScheme.h:387
gum::ApproximationScheme::current_epsilon_
double current_epsilon_
Current epsilon.
Definition
approximationScheme.h:378
gum::ApproximationScheme::last_epsilon_
double last_epsilon_
Last epsilon value.
Definition
approximationScheme.h:381
gum::ApproximationScheme::eps_
double eps_
Threshold for convergence.
Definition
approximationScheme.h:399
gum::ApproximationScheme::enabled_max_time_
bool enabled_max_time_
If true, the timeout is enabled.
Definition
approximationScheme.h:414
gum::ApproximationScheme::max_iter_
Size max_iter_
The maximum iterations.
Definition
approximationScheme.h:417
gum::ApproximationScheme::enabled_eps_
bool enabled_eps_
If true, the threshold convergence is enabled.
Definition
approximationScheme.h:402
gum::ApproximationScheme::current_state_
ApproximationSchemeSTATE current_state_
The current state.
Definition
approximationScheme.h:393
gum::ApproximationScheme::min_rate_eps_
double min_rate_eps_
Threshold for the epsilon rate.
Definition
approximationScheme.h:405
gum::ApproximationScheme::history_
std::vector< double > history_
The scheme history, used only if verbosity == true.
Definition
approximationScheme.h:396
gum::ApproximationScheme::~ApproximationScheme
~ApproximationScheme() override
Definition
approximationScheme.cpp:67
gum::ApproximationScheme::current_rate_
double current_rate_
Current rate.
Definition
approximationScheme.h:384
gum::ApproximationScheme::stateApproximationScheme
ApproximationSchemeSTATE stateApproximationScheme() const override
Returns the approximation scheme state.
Definition
approximationScheme_inl.h:165
gum::ApproximationScheme::timer_
Timer timer_
The timer.
Definition
approximationScheme.h:390
gum::ApproximationScheme::period_size_
Size period_size_
Checking criteria frequency.
Definition
approximationScheme.h:426
gum::ApproximationScheme::startOfPeriod
bool startOfPeriod() const
Returns true if we are at the beginning of a period (compute error is mandatory).
Definition
approximationScheme_inl.h:200
gum::ApproximationScheme::enabled_max_iter_
bool enabled_max_iter_
If true, the maximum iterations stopping criterion is enabled.
Definition
approximationScheme.h:420
gum::ApproximationScheme::max_time_
double max_time_
The timeout.
Definition
approximationScheme.h:411
gum::ApproximationScheme::ApproximationScheme
ApproximationScheme(bool verbosity=false)
Definition
approximationScheme.cpp:58
gum::ApproximationScheme::continueApproximationScheme
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
Definition
approximationScheme.cpp:69
gum::ApproximationScheme::stopScheme_
void stopScheme_(ApproximationSchemeSTATE new_state)
Stop the scheme given a new state.
Definition
approximationScheme_inl.h:231
gum::ApproximationScheme::verbosity_
bool verbosity_
If true, verbosity is enabled.
Definition
approximationScheme.h:429
gum::ApproximationScheme::verbosity
bool verbosity() const override
Returns true if verbosity is enabled.
Definition
approximationScheme_inl.h:161
gum::ApproximationScheme::burn_in_
Size burn_in_
Number of iterations before checking stopping criteria.
Definition
approximationScheme.h:423
gum::ApproximationScheme::enabled_min_rate_eps_
bool enabled_min_rate_eps_
If true, the minimal threshold for epsilon rate is enabled.
Definition
approximationScheme.h:408
gum::IApproximationSchemeConfiguration::onProgress
Signaler< Size, double, double > onProgress
Progression, error and time.
Definition
IApproximationSchemeConfiguration.h:81
gum::IApproximationSchemeConfiguration::ApproximationSchemeSTATE
ApproximationSchemeSTATE
The different state of an approximation scheme.
Definition
IApproximationSchemeConfiguration.h:87
gum::IApproximationSchemeConfiguration::ApproximationSchemeSTATE::TimeLimit
@ TimeLimit
Definition
IApproximationSchemeConfiguration.h:93
gum::IApproximationSchemeConfiguration::ApproximationSchemeSTATE::Limit
@ Limit
Definition
IApproximationSchemeConfiguration.h:92
gum::IApproximationSchemeConfiguration::ApproximationSchemeSTATE::Epsilon
@ Epsilon
Definition
IApproximationSchemeConfiguration.h:90
gum::IApproximationSchemeConfiguration::ApproximationSchemeSTATE::Continue
@ Continue
Definition
IApproximationSchemeConfiguration.h:89
gum::IApproximationSchemeConfiguration::ApproximationSchemeSTATE::Rate
@ Rate
Definition
IApproximationSchemeConfiguration.h:91
gum::IApproximationSchemeConfiguration::messageApproximationScheme
std::string messageApproximationScheme() const
Returns the approximation scheme message.
Definition
IApproximationSchemeConfiguration.cpp:64
OperationNotAllowed
Exception : operation not allowed.
gum::Timer::step
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition
timer_inl.h:72
gum::__sig__::BasicSignaler< Args... >::hasListener
bool hasListener() override
Definition
signaler_tpl.h:109
GUM_ERROR
#define GUM_ERROR(type, msg)
Definition
exceptions.h:76
gum::Size
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition
types.h:74
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
GUM_EMIT3
#define GUM_EMIT3(signal, arg1, arg2, arg3)
Definition
signaler.h:291
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0