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