aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
simpleCPTDisturber_tpl.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
50
#include <
agrum/BN/generator/simpleCPTDisturber.h
>
51
52
namespace
gum
{
53
54
// Default constructor.
55
template
< GUM_Numeric GUM_SCALAR >
56
SimpleCPTDisturber< GUM_SCALAR >::SimpleCPTDisturber
() : ICPTDisturber< GUM_SCALAR >() {
57
GUM_CONSTRUCTOR(
SimpleCPTDisturber
);
58
}
59
60
// Destructor.
61
template
< GUM_Numeric GUM_SCALAR >
62
SimpleCPTDisturber< GUM_SCALAR >::~SimpleCPTDisturber
() {
63
GUM_DESTRUCTOR(
SimpleCPTDisturber
);
64
}
65
66
// Generates a CPT using GUM_SCALAR.
67
// @param varID The variable id of the CPT owner.
68
// @param cpt A reference on the CPT to fill.
69
70
template
< GUM_Numeric GUM_SCALAR >
71
void
SimpleCPTDisturber< GUM_SCALAR >::disturbReducCPT
(
NodeId
node,
72
BayesNet< GUM_SCALAR >& bayesNet,
73
Tensor< GUM_SCALAR >& src,
74
Tensor< GUM_SCALAR >& marg) {
75
Instantiation
i_src(src);
76
Instantiation
i_dest(bayesNet.cpt(node));
77
Instantiation
i_marg(marg);
78
79
for
(i_dest.
setFirst
(); !i_dest.
end
(); ++i_dest) {
80
GUM_SCALAR potval = 0;
81
82
i_src.
setVals
(i_dest);
83
for
(i_marg.
setFirst
(); !i_marg.
end
(); ++i_marg) {
84
i_src.
setVals
(i_marg);
85
potval += src.get(i_src) * marg.get(i_marg);
86
}
87
88
bayesNet.cpt(node).set(i_dest, potval);
89
}
90
91
bayesNet.cpt(node).normalizeAsCPT();
92
}
93
94
template
< GUM_Numeric GUM_SCALAR >
95
void
SimpleCPTDisturber< GUM_SCALAR >::disturbAugmCPT
(
NodeId
node,
96
BayesNet< GUM_SCALAR >& bayesNet,
97
Tensor< GUM_SCALAR >& src,
98
GUM_SCALAR variation) {
99
Instantiation
i(bayesNet.cpt(node));
100
101
for
(i.
setFirst
(); !i.
end
(); ++i)
102
bayesNet.cpt(node).set(i, src.get(i) + GUM_SCALAR(
randomProba
()) * variation);
103
104
bayesNet.cpt(node).normalizeAsCPT();
105
}
106
107
}
/* namespace gum */
gum::Instantiation
Class for assigning/browsing values to tuples of discrete variables.
Definition
instantiation.h:102
gum::Instantiation::end
bool end() const
Returns true if the Instantiation reached the end.
Definition
instantiation_inl.h:203
gum::Instantiation::setVals
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
Definition
instantiation_inl.h:286
gum::Instantiation::setFirst
void setFirst()
Assign the first values to the tuple of the Instantiation.
Definition
instantiation_inl.h:248
gum::SimpleCPTDisturber::disturbAugmCPT
void disturbAugmCPT(NodeId node, BayesNet< GUM_SCALAR > &bayesNet, Tensor< GUM_SCALAR > &src, GUM_SCALAR variation) override
Disturb a CPT using GUM_SCALAR when inserting a new parent new_parent.
Definition
simpleCPTDisturber_tpl.h:95
gum::SimpleCPTDisturber::~SimpleCPTDisturber
~SimpleCPTDisturber() override
Destructor.
Definition
simpleCPTDisturber_tpl.h:62
gum::SimpleCPTDisturber::SimpleCPTDisturber
SimpleCPTDisturber()
Default constructor.
Definition
simpleCPTDisturber_tpl.h:56
gum::SimpleCPTDisturber::disturbReducCPT
void disturbReducCPT(NodeId node, BayesNet< GUM_SCALAR > &bayesNet, Tensor< GUM_SCALAR > &src, Tensor< GUM_SCALAR > &marg) override
Disturb a CPT using GUM_SCALAR when removing parent varIdi.
Definition
simpleCPTDisturber_tpl.h:71
gum::NodeId
Size NodeId
Type for node ids.
Definition
graphElements.h:117
gum::randomProba
GUM_SHARED_PUBLIC double randomProba()
Returns a random double between 0 and 1 included (i.e.
Definition
utils_random_inl.h:73
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
simpleCPTDisturber.h
Source implementation of SimpleCPTDisturber.
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0