aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
groundedInference_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 GroundedInference.
25
*
26
* @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27
*
28
*/
29
#
include
<
agrum
/
PRM
/
inference
/
groundedInference
.
h
>
30
31
namespace
gum
{
32
namespace
prm
{
33
34
template
<
typename
GUM_SCALAR
>
35
GroundedInference
<
GUM_SCALAR
>::~
GroundedInference
() {
36
GUM_DESTRUCTOR
(
GroundedInference
);
37
38
if
(
_inf_
!=
nullptr
)
delete
_inf_
;
39
40
if
(!
_obs_
.
empty
())
41
for
(
const
auto
pot
:
_obs_
)
42
// We used const ptrs only because of
43
// MarginalTargetedInference::addEvidence()
44
// requires it
45
delete
const_cast
<
Potential
<
GUM_SCALAR
>* >(
pot
);
46
}
47
48
template
<
typename
GUM_SCALAR
>
49
void
GroundedInference
<
GUM_SCALAR
>::
evidenceAdded_
(
50
const
typename
PRMInference
<
GUM_SCALAR
>::
Chain
&
chain
) {
51
Potential
<
GUM_SCALAR
>*
bn_obs
=
new
Potential
<
GUM_SCALAR
>();
52
// Retrieving the BN's variable
53
std
::
stringstream
var_name
;
54
var_name
<<
chain
.
first
->
name
() <<
"."
<<
chain
.
second
->
safeName
();
55
bn_obs
->
add
(
_inf_
->
BN
().
variableFromName
(
var_name
.
str
()));
56
// Retrievin the PRM<GUM_SCALAR>'s evidence and copying it in bn_obs
57
const
Potential
<
GUM_SCALAR
>*
prm_obs
=
this
->
evidence
(
chain
.
first
)[
chain
.
second
->
id
()];
58
Instantiation
i
(*
bn_obs
),
j
(*
prm_obs
);
59
60
for
(
i
.
setFirst
(),
j
.
setFirst
(); !
i
.
end
();
i
.
inc
(),
j
.
inc
()) {
61
bn_obs
->
set
(
i
,
prm_obs
->
get
(
j
));
62
}
63
64
_obs_
.
insert
(
bn_obs
);
65
}
66
67
template
<
typename
GUM_SCALAR
>
68
void
GroundedInference
<
GUM_SCALAR
>::
evidenceRemoved_
(
69
const
typename
PRMInference
<
GUM_SCALAR
>::
Chain
&
chain
) {
70
std
::
stringstream
var_name
;
71
var_name
<<
chain
.
first
->
name
() <<
"."
<<
chain
.
second
->
safeName
();
72
const
DiscreteVariable
&
var
=
_inf_
->
BN
().
variableFromName
(
var_name
.
str
());
73
74
for
(
auto
iter
=
_obs_
.
beginSafe
();
iter
!=
_obs_
.
endSafe
();
75
++
iter
) {
// safe iterator needed here
76
if
((**
iter
).
contains
(
var
)) {
77
_inf_
->
eraseEvidence
(
var_name
.
str
());
78
const
Potential
<
GUM_SCALAR
>*
e
= *
iter
;
79
_obs_
.
erase
(
iter
);
80
delete
e
;
81
break
;
82
}
83
}
84
}
85
86
template
<
typename
GUM_SCALAR
>
87
INLINE
88
GroundedInference
<
GUM_SCALAR
>::
GroundedInference
(
const
PRM
<
GUM_SCALAR
>&
prm
,
89
const
PRMSystem
<
GUM_SCALAR
>&
system
) :
90
PRMInference
<
GUM_SCALAR
>(
prm
,
system
),
91
_inf_
(0) {
92
GUM_CONSTRUCTOR
(
GroundedInference
);
93
}
94
95
template
<
typename
GUM_SCALAR
>
96
INLINE
GroundedInference
<
GUM_SCALAR
>::
GroundedInference
(
97
const
GroundedInference
<
GUM_SCALAR
>&
source
) :
98
PRMInference
<
GUM_SCALAR
>(
source
),
99
_inf_
(0) {
100
GUM_CONS_CPY
(
GroundedInference
);
101
GUM_ERROR
(
FatalError
,
"illegal to copy constructor"
)
102
}
103
104
template
<
typename
GUM_SCALAR
>
105
INLINE
GroundedInference
<
GUM_SCALAR
>&
106
GroundedInference
<
GUM_SCALAR
>::
operator
=(
const
GroundedInference
<
GUM_SCALAR
>&
source
) {
107
GUM_ERROR
(
FatalError
,
"illegal call to copy operator"
)
108
}
109
110
template
<
typename
GUM_SCALAR
>
111
INLINE
MarginalTargetedInference
<
GUM_SCALAR
>&
112
GroundedInference
<
GUM_SCALAR
>::
getBNInference
() {
113
if
(
_inf_
!= 0) {
114
return
*
_inf_
;
115
}
else
{
116
GUM_ERROR
(
NotFound
,
"the inference engine is not yet defined"
)
117
}
118
}
119
120
template
<
typename
GUM_SCALAR
>
121
INLINE
void
GroundedInference
<
GUM_SCALAR
>::
setBNInference
(
122
MarginalTargetedInference
<
GUM_SCALAR
>*
bn_inf
) {
123
if
(
_inf_
!= 0) {
delete
_inf_
; }
124
125
_inf_
=
bn_inf
;
126
}
127
128
template
<
typename
GUM_SCALAR
>
129
INLINE
void
GroundedInference
<
GUM_SCALAR
>::
posterior_
(
130
const
typename
PRMInference
<
GUM_SCALAR
>::
Chain
&
chain
,
131
Potential
<
GUM_SCALAR
>&
m
) {
132
if
(
_inf_
== 0) {
GUM_ERROR
(
OperationNotAllowed
,
"no inference engine defined"
) }
133
134
std
::
stringstream
sBuff
;
135
136
if
(!
_obs_
.
empty
()) {
137
for
(
auto
e
:
_obs_
) {
138
try
{
139
_inf_
->
addEvidence
(*
e
);
140
}
catch
(
InvalidArgument
&) {
_inf_
->
chgEvidence
(*
e
); }
141
}
142
}
143
144
sBuff
<<
chain
.
first
->
name
() <<
"."
<<
chain
.
second
->
safeName
();
145
m
=
_inf_
->
posterior
(
_inf_
->
BN
().
idFromName
(
sBuff
.
str
()));
146
}
147
148
template
<
typename
GUM_SCALAR
>
149
INLINE
void
GroundedInference
<
GUM_SCALAR
>::
joint_
(
150
const
std
::
vector
<
typename
PRMInference
<
GUM_SCALAR
>::
Chain
>&
queries
,
151
Potential
<
GUM_SCALAR
>&
j
) {
152
GUM_ERROR
(
FatalError
,
"not yet implemented"
)
153
}
154
155
template
<
typename
GUM_SCALAR
>
156
INLINE
std
::
string
GroundedInference
<
GUM_SCALAR
>::
name
()
const
{
157
return
"grounded inference"
;
158
}
159
160
}
/* namespace prm */
161
}
/* 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