aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
PRMInference_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 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 Inline implementation of PRMInference.
25
*
26
* @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27
*
28
*/
29
#
include
<
agrum
/
PRM
/
inference
/
PRMInference
.
h
>
30
31
namespace
gum
{
32
namespace
prm
{
33
34
template
<
typename
GUM_SCALAR >
35
void
PRMInference<
GUM_SCALAR
>::
clearEvidence
() {
36
for
(
const
auto
&
elt
:
evidences__
) {
37
for
(
const
auto
&
elt2
: *
elt
.
second
)
38
delete
elt2
.
second
;
39
40
delete
elt
.
second
;
41
}
42
43
evidences__
.
clear
();
44
}
45
46
template
<
typename
GUM_SCALAR
>
47
PRMInference
<
GUM_SCALAR
>::
PRMInference
(
48
const
PRMInference
<
GUM_SCALAR
>&
source
) :
49
prm_
(
source
.
prm_
),
50
sys_
(
source
.
sys_
) {
51
GUM_CONS_CPY
(
PRMInference
);
52
53
for
(
const
auto
&
elt
:
source
.
evidences__
) {
54
evidences__
.
insert
(
elt
.
first
,
new
PRMInference
<
GUM_SCALAR
>::
EMap
());
55
56
for
(
const
auto
&
elt2
: *
elt
.
second
) {
57
Potential
<
GUM_SCALAR
>*
e
=
new
Potential
<
GUM_SCALAR
>();
58
e
->
add
(*(
elt2
.
second
->
variablesSequence
().
front
()));
59
Instantiation
i
(*
e
);
60
61
for
(
i
.
setFirst
(); !
i
.
end
();
i
.
inc
())
62
e
->
set
(
i
,
elt2
.
second
->
get
(
i
));
63
64
evidences__
[
elt
.
first
]->
insert
(
elt2
.
first
,
e
);
65
}
66
}
67
}
68
69
template
<
typename
GUM_SCALAR
>
70
PRMInference
<
GUM_SCALAR
>&
PRMInference
<
GUM_SCALAR
>::
operator
=(
71
const
PRMInference
<
GUM_SCALAR
>&
source
) {
72
clearEvidence
();
73
prm_
=
source
.
prm_
;
74
sys_
=
source
.
sys_
;
75
76
for
(
const
auto
&
elt
:
source
.
evidences__
) {
77
evidences__
.
insert
(
elt
.
first
,
new
PRMInference
<
GUM_SCALAR
>::
EMap
());
78
79
for
(
const
auto
&
elt2
: *
elt
.
second
) {
80
Potential
<
GUM_SCALAR
>*
e
=
new
Potential
<
GUM_SCALAR
>();
81
e
->
add
(*(
elt2
.
second
->
variablesSequence
().
front
()));
82
Instantiation
i
(*
e
);
83
84
for
(
i
.
setFirst
(); !
i
.
end
();
i
.
inc
()) {
85
e
->
set
(
i
,
elt2
.
second
->
get
(
i
));
86
}
87
88
evidences__
[
elt
.
first
]->
insert
(
elt2
.
first
,
e
);
89
}
90
}
91
92
return
*
this
;
93
}
94
95
template
<
typename
GUM_SCALAR
>
96
typename
PRMInference
<
GUM_SCALAR
>::
EMap
&
97
PRMInference
<
GUM_SCALAR
>::
EMap__
(
const
PRMInstance
<
GUM_SCALAR
>*
i
) {
98
if
(
evidences__
.
exists
(
i
)) {
99
return
*(
evidences__
[
i
]);
100
}
else
{
101
evidences__
.
insert
(
i
,
new
PRMInference
<
GUM_SCALAR
>::
EMap
());
102
return
*(
evidences__
[
i
]);
103
}
104
}
105
106
template
<
typename
GUM_SCALAR
>
107
void
108
PRMInference
<
GUM_SCALAR
>::
addEvidence
(
const
Chain
&
chain
,
109
const
Potential
<
GUM_SCALAR
>&
p
) {
110
if
(
chain
.
first
->
exists
(
chain
.
second
->
id
())) {
111
if
((
p
.
nbrDim
() != 1) || (!
p
.
contains
(
chain
.
second
->
type
().
variable
())))
112
GUM_ERROR
(
OperationNotAllowed
,
113
"illegal evidence for the given PRMAttribute."
);
114
115
Potential
<
GUM_SCALAR
>*
e
=
new
Potential
<
GUM_SCALAR
>();
116
e
->
add
(
chain
.
second
->
type
().
variable
());
117
Instantiation
i
(*
e
);
118
119
for
(
i
.
setFirst
(); !
i
.
end
();
i
.
inc
())
120
e
->
set
(
i
,
p
.
get
(
i
));
121
122
PRMInference
<
GUM_SCALAR
>::
EMap
&
emap
=
EMap__
(
chain
.
first
);
123
124
if
(
emap
.
exists
(
chain
.
second
->
id
())) {
125
delete
emap
[
chain
.
second
->
id
()];
126
emap
[
chain
.
second
->
id
()] =
e
;
127
}
else
{
128
emap
.
insert
(
chain
.
second
->
id
(),
e
);
129
}
130
131
evidenceAdded_
(
chain
);
132
}
else
{
133
GUM_ERROR
(
NotFound
,
134
"the given PRMAttribute does not belong to this "
135
"Instance<GUM_SCALAR>."
);
136
}
137
}
138
139
template
<
typename
GUM_SCALAR
>
140
INLINE
PRMInference
<
GUM_SCALAR
>::
PRMInference
(
141
const
PRM
<
GUM_SCALAR
>&
prm
,
142
const
PRMSystem
<
GUM_SCALAR
>&
system
) :
143
prm_
(&
prm
),
144
sys_
(&
system
) {
145
GUM_CONSTRUCTOR
(
PRMInference
);
146
}
147
148
template
<
typename
GUM_SCALAR
>
149
INLINE
PRMInference
<
GUM_SCALAR
>::~
PRMInference
() {
150
GUM_DESTRUCTOR
(
PRMInference
);
151
clearEvidence
();
152
}
153
154
template
<
typename
GUM_SCALAR
>
155
INLINE
typename
PRMInference
<
GUM_SCALAR
>::
EMap
&
156
PRMInference
<
GUM_SCALAR
>::
evidence
(
const
PRMInstance
<
GUM_SCALAR
>&
i
) {
157
try
{
158
return
*(
evidences__
[&
i
]);
159
}
catch
(
NotFound
&) {
160
GUM_ERROR
(
NotFound
,
"this instance has no evidence."
);
161
}
162
}
163
164
template
<
typename
GUM_SCALAR
>
165
INLINE
const
typename
PRMInference
<
GUM_SCALAR
>::
EMap
&
166
PRMInference
<
GUM_SCALAR
>::
evidence
(
167
const
PRMInstance
<
GUM_SCALAR
>&
i
)
const
{
168
try
{
169
return
*(
evidences__
[&
i
]);
170
}
catch
(
NotFound
&) {
171
GUM_ERROR
(
NotFound
,
"this instance has no evidence."
);
172
}
173
}
174
175
template
<
typename
GUM_SCALAR
>
176
INLINE
typename
PRMInference
<
GUM_SCALAR
>::
EMap
&
177
PRMInference
<
GUM_SCALAR
>::
evidence
(
const
PRMInstance
<
GUM_SCALAR
>*
i
) {
178
try
{
179
return
*(
evidences__
[
i
]);
180
}
catch
(
NotFound
&) {
181
GUM_ERROR
(
NotFound
,
"this instance has no evidence."
);
182
}
183
}
184
185
template
<
typename
GUM_SCALAR
>
186
INLINE
const
typename
PRMInference
<
GUM_SCALAR
>::
EMap
&
187
PRMInference
<
GUM_SCALAR
>::
evidence
(
188
const
PRMInstance
<
GUM_SCALAR
>*
i
)
const
{
189
try
{
190
return
*(
evidences__
[
i
]);
191
}
catch
(
NotFound
&) {
192
GUM_ERROR
(
NotFound
,
"this instance has no evidence."
);
193
}
194
}
195
196
template
<
typename
GUM_SCALAR
>
197
INLINE
bool
PRMInference
<
GUM_SCALAR
>::
hasEvidence
(
198
const
PRMInstance
<
GUM_SCALAR
>&
i
)
const
{
199
return
evidences__
.
exists
(&
i
);
200
}
201
202
template
<
typename
GUM_SCALAR
>
203
INLINE
bool
PRMInference
<
GUM_SCALAR
>::
hasEvidence
(
204
const
PRMInstance
<
GUM_SCALAR
>*
i
)
const
{
205
return
evidences__
.
exists
(
i
);
206
}
207
208
template
<
typename
GUM_SCALAR
>
209
INLINE
bool
PRMInference
<
GUM_SCALAR
>::
hasEvidence
(
const
Chain
&
chain
)
const
{
210
return
(
hasEvidence
(
chain
.
first
))
211
?
evidence
(
chain
.
first
).
exists
(
chain
.
second
->
id
())
212
:
false
;
213
}
214
215
template
<
typename
GUM_SCALAR
>
216
INLINE
bool
PRMInference
<
GUM_SCALAR
>::
hasEvidence
()
const
{
217
return
(
evidences__
.
size
() != (
Size
)0);
218
}
219
220
template
<
typename
GUM_SCALAR
>
221
INLINE
void
PRMInference
<
GUM_SCALAR
>::
removeEvidence
(
const
Chain
&
chain
) {
222
try
{
223
if
(
EMap__
(
chain
.
first
).
exists
(
chain
.
second
->
id
())) {
224
evidenceRemoved_
(
chain
);
225
delete
EMap__
(
chain
.
first
)[
chain
.
second
->
id
()];
226
EMap__
(
chain
.
first
).
erase
(
chain
.
second
->
id
());
227
}
228
}
catch
(
NotFound
&) {
229
// Ok, we are only removing
230
}
231
}
232
233
template
<
typename
GUM_SCALAR
>
234
INLINE
void
PRMInference
<
GUM_SCALAR
>::
posterior
(
235
const
typename
PRMInference
<
GUM_SCALAR
>::
Chain
&
chain
,
236
Potential
<
GUM_SCALAR
>&
m
) {
237
if
(
m
.
nbrDim
() > 0) {
238
GUM_ERROR
(
OperationNotAllowed
,
"the given Potential is not empty."
);
239
}
240
241
if
(
hasEvidence
(
chain
)) {
242
m
.
add
(
chain
.
second
->
type
().
variable
());
243
const
Potential
<
GUM_SCALAR
>&
e
244
= *(
evidence
(
chain
.
first
)[
chain
.
second
->
id
()]);
245
Instantiation
i
(
m
),
j
(
e
);
246
247
for
(
i
.
setFirst
(),
j
.
setFirst
(); !
i
.
end
();
i
.
inc
(),
j
.
inc
())
248
m
.
set
(
i
,
e
.
get
(
j
));
249
}
else
{
250
if
(
chain
.
second
!= &(
chain
.
first
->
get
(
chain
.
second
->
safeName
()))) {
251
typename
PRMInference
<
GUM_SCALAR
>::
Chain
good_chain
252
=
std
::
make_pair
(
chain
.
first
,
253
&(
chain
.
first
->
get
(
chain
.
second
->
safeName
())));
254
m
.
add
(
good_chain
.
second
->
type
().
variable
());
255
posterior_
(
good_chain
,
m
);
256
}
else
{
257
m
.
add
(
chain
.
second
->
type
().
variable
());
258
posterior_
(
chain
,
m
);
259
}
260
}
261
}
262
263
template
<
typename
GUM_SCALAR
>
264
INLINE
void
PRMInference
<
GUM_SCALAR
>::
joint
(
265
const
std
::
vector
<
typename
PRMInference
<
GUM_SCALAR
>::
Chain
>&
chains
,
266
Potential
<
GUM_SCALAR
>&
j
) {
267
if
(
j
.
nbrDim
() > 0) {
268
GUM_ERROR
(
OperationNotAllowed
,
"the given Potential is not empty."
);
269
}
270
271
for
(
auto
chain
=
chains
.
begin
();
chain
!=
chains
.
end
(); ++
chain
) {
272
j
.
add
(
chain
->
second
->
type
().
variable
());
273
}
274
275
joint_
(
chains
,
j
);
276
}
277
278
}
/* namespace prm */
279
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
gum::prm::ParamScopeData::ParamScopeData
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
Definition:
PRMClass_tpl.h:1101