aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
groundedInference_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 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
58
=
this
->
evidence
(
chain
.
first
)[
chain
.
second
->
id
()];
59
Instantiation
i
(*
bn_obs
),
j
(*
prm_obs
);
60
61
for
(
i
.
setFirst
(),
j
.
setFirst
(); !
i
.
end
();
i
.
inc
(),
j
.
inc
()) {
62
bn_obs
->
set
(
i
,
prm_obs
->
get
(
j
));
63
}
64
65
obs__
.
insert
(
bn_obs
);
66
}
67
68
template
<
typename
GUM_SCALAR
>
69
void
GroundedInference
<
GUM_SCALAR
>::
evidenceRemoved_
(
70
const
typename
PRMInference
<
GUM_SCALAR
>::
Chain
&
chain
) {
71
std
::
stringstream
var_name
;
72
var_name
<<
chain
.
first
->
name
() <<
"."
<<
chain
.
second
->
safeName
();
73
const
DiscreteVariable
&
var
=
inf__
->
BN
().
variableFromName
(
var_name
.
str
());
74
75
for
(
auto
iter
=
obs__
.
beginSafe
();
iter
!=
obs__
.
endSafe
();
76
++
iter
) {
// safe iterator needed here
77
if
((**
iter
).
contains
(
var
)) {
78
inf__
->
eraseEvidence
(
var_name
.
str
());
79
const
Potential
<
GUM_SCALAR
>*
e
= *
iter
;
80
obs__
.
erase
(
iter
);
81
delete
e
;
82
break
;
83
}
84
}
85
}
86
87
template
<
typename
GUM_SCALAR
>
88
INLINE
GroundedInference
<
GUM_SCALAR
>::
GroundedInference
(
89
const
PRM
<
GUM_SCALAR
>&
prm
,
90
const
PRMSystem
<
GUM_SCALAR
>&
system
) :
91
PRMInference
<
GUM_SCALAR
>(
prm
,
system
),
92
inf__
(0) {
93
GUM_CONSTRUCTOR
(
GroundedInference
);
94
}
95
96
template
<
typename
GUM_SCALAR
>
97
INLINE
GroundedInference
<
GUM_SCALAR
>::
GroundedInference
(
98
const
GroundedInference
<
GUM_SCALAR
>&
source
) :
99
PRMInference
<
GUM_SCALAR
>(
source
),
100
inf__
(0) {
101
GUM_CONS_CPY
(
GroundedInference
);
102
GUM_ERROR
(
FatalError
,
"illegal to copy constructor"
);
103
}
104
105
template
<
typename
GUM_SCALAR
>
106
INLINE
GroundedInference
<
GUM_SCALAR
>&
107
GroundedInference
<
GUM_SCALAR
>::
operator
=(
108
const
GroundedInference
<
GUM_SCALAR
>&
source
) {
109
GUM_ERROR
(
FatalError
,
"illegal call to copy operator"
);
110
}
111
112
template
<
typename
GUM_SCALAR
>
113
INLINE
MarginalTargetedInference
<
GUM_SCALAR
>&
114
GroundedInference
<
GUM_SCALAR
>::
getBNInference
() {
115
if
(
inf__
!= 0) {
116
return
*
inf__
;
117
}
else
{
118
GUM_ERROR
(
NotFound
,
"the inference engine is not yet defined"
);
119
}
120
}
121
122
template
<
typename
GUM_SCALAR
>
123
INLINE
void
GroundedInference
<
GUM_SCALAR
>::
setBNInference
(
124
MarginalTargetedInference
<
GUM_SCALAR
>*
bn_inf
) {
125
if
(
inf__
!= 0) {
delete
inf__
; }
126
127
inf__
=
bn_inf
;
128
}
129
130
template
<
typename
GUM_SCALAR
>
131
INLINE
void
GroundedInference
<
GUM_SCALAR
>::
posterior_
(
132
const
typename
PRMInference
<
GUM_SCALAR
>::
Chain
&
chain
,
133
Potential
<
GUM_SCALAR
>&
m
) {
134
if
(
inf__
== 0) {
135
GUM_ERROR
(
OperationNotAllowed
,
"no inference engine defined"
);
136
}
137
138
std
::
stringstream
sBuff
;
139
140
if
(!
obs__
.
empty
()) {
141
for
(
auto
e
:
obs__
) {
142
try
{
143
inf__
->
addEvidence
(*
e
);
144
}
catch
(
InvalidArgument
&) {
inf__
->
chgEvidence
(*
e
); }
145
}
146
}
147
148
sBuff
<<
chain
.
first
->
name
() <<
"."
<<
chain
.
second
->
safeName
();
149
m
=
inf__
->
posterior
(
inf__
->
BN
().
idFromName
(
sBuff
.
str
()));
150
}
151
152
template
<
typename
GUM_SCALAR
>
153
INLINE
void
GroundedInference
<
GUM_SCALAR
>::
joint_
(
154
const
std
::
vector
<
typename
PRMInference
<
GUM_SCALAR
>::
Chain
>&
queries
,
155
Potential
<
GUM_SCALAR
>&
j
) {
156
GUM_ERROR
(
FatalError
,
"not yet implemented"
);
157
}
158
159
template
<
typename
GUM_SCALAR
>
160
INLINE
std
::
string
GroundedInference
<
GUM_SCALAR
>::
name
()
const
{
161
return
"grounded inference"
;
162
}
163
164
}
/* namespace prm */
165
}
/* 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