aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
instanceBayesNet_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 InstanceBayesNet.
25
*
26
* @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27
*/
28
29
#
include
<
agrum
/
PRM
/
instanceBayesNet
.
h
>
// to ease IDE parser
30
31
namespace
gum
{
32
namespace
prm
{
33
34
template
<
typename
GUM_SCALAR >
35
void
36
InstanceBayesNet<
GUM_SCALAR
>::
init__
(
const
PRMInstance
<
GUM_SCALAR
>&
i
) {
37
for
(
const
auto
node
:
i
.
type
().
containerDag
().
nodes
()) {
38
try
{
39
// Adding the attribute
40
const
PRMAttribute
<
GUM_SCALAR
>&
attr
=
i
.
get
(
node
);
41
this
->
dag_
.
addNodeWithId
(
attr
.
id
());
42
varNodeMap__
.
insert
(&(
attr
.
type
().
variable
()), &
attr
);
43
}
catch
(
NotFound
&) {
44
// Not an attribute
45
}
46
}
47
48
for
(
const
auto
&
arc
:
i
.
type
().
containerDag
().
arcs
()) {
49
try
{
50
this
->
dag_
.
addArc
(
arc
.
tail
(),
arc
.
head
());
51
}
catch
(
InvalidNode
&) {
52
// Not added means not an attribute
53
}
54
}
55
}
56
57
template
<
typename
GUM_SCALAR
>
58
INLINE
InstanceBayesNet
<
GUM_SCALAR
>::
InstanceBayesNet
(
59
const
PRMInstance
<
GUM_SCALAR
>&
i
) :
60
IBayesNet
<
GUM_SCALAR
>(),
61
inst__
(&
i
) {
62
GUM_CONSTRUCTOR
(
InstanceBayesNet
);
63
init__
(
i
);
64
}
65
66
template
<
typename
GUM_SCALAR
>
67
INLINE
InstanceBayesNet
<
GUM_SCALAR
>::
InstanceBayesNet
(
68
const
InstanceBayesNet
&
from
) :
69
IBayesNet
<
GUM_SCALAR
>(
from
),
70
varNodeMap__
(
from
.
varNodeMap__
),
inst__
(
from
.
inst__
) {
71
GUM_CONS_CPY
(
InstanceBayesNet
);
72
}
73
74
template
<
typename
GUM_SCALAR
>
75
INLINE
InstanceBayesNet
<
GUM_SCALAR
>::~
InstanceBayesNet
() {
76
GUM_DESTRUCTOR
(
InstanceBayesNet
);
77
}
78
79
template
<
typename
GUM_SCALAR
>
80
INLINE
InstanceBayesNet
<
GUM_SCALAR
>&
81
InstanceBayesNet
<
GUM_SCALAR
>::
operator
=(
const
InstanceBayesNet
&
from
) {
82
if
(
this
!= &
from
) {
83
IBayesNet
<
GUM_SCALAR
>::
operator
=(
from
);
84
85
varNodeMap__
=
from
.
varNodeMap__
;
86
}
87
88
return
*
this
;
89
}
90
91
template
<
typename
GUM_SCALAR
>
92
INLINE
const
Potential
<
GUM_SCALAR
>&
93
InstanceBayesNet
<
GUM_SCALAR
>::
cpt
(
NodeId
varId
)
const
{
94
return
get__
(
varId
).
cpf
();
95
}
96
97
template
<
typename
GUM_SCALAR
>
98
INLINE
const
VariableNodeMap
&
99
InstanceBayesNet
<
GUM_SCALAR
>::
variableNodeMap
()
const
{
100
GUM_ERROR
(
NotFound
,
"no VariableNodeMap in an InstanceBayesNet"
);
101
}
102
103
template
<
typename
GUM_SCALAR
>
104
INLINE
const
DiscreteVariable
&
105
InstanceBayesNet
<
GUM_SCALAR
>::
variable
(
NodeId
id
)
const
{
106
return
get__
(
id
).
type
().
variable
();
107
}
108
109
template
<
typename
GUM_SCALAR
>
110
INLINE
NodeId
111
InstanceBayesNet
<
GUM_SCALAR
>::
nodeId
(
const
DiscreteVariable
&
var
)
const
{
112
return
varNodeMap__
[&
var
]->
id
();
113
}
114
115
template
<
typename
GUM_SCALAR
>
116
INLINE
NodeId
117
InstanceBayesNet
<
GUM_SCALAR
>::
idFromName
(
const
std
::
string
&
name
)
const
{
118
return
get__
(
name
).
id
();
119
}
120
121
template
<
typename
GUM_SCALAR
>
122
INLINE
const
DiscreteVariable
&
123
InstanceBayesNet
<
GUM_SCALAR
>::
variableFromName
(
124
const
std
::
string
&
name
)
const
{
125
return
get__
(
name
).
type
().
variable
();
126
}
127
128
template
<
typename
GUM_SCALAR
>
129
INLINE
const
PRMClassElement
<
GUM_SCALAR
>&
130
InstanceBayesNet
<
GUM_SCALAR
>::
get__
(
NodeId
id
)
const
{
131
return
inst__
->
get
(
id
);
132
}
133
134
template
<
typename
GUM_SCALAR
>
135
INLINE
const
PRMClassElement
<
GUM_SCALAR
>&
136
InstanceBayesNet
<
GUM_SCALAR
>::
get__
(
const
std
::
string
&
name
)
const
{
137
try
{
138
return
inst__
->
get
(
name
);
139
}
catch
(
NotFound
&) {
140
GUM_ERROR
(
NotFound
,
"no element found with that name"
);
141
}
142
}
143
144
template
<
typename
GUM_SCALAR
>
145
INLINE
const
NodeProperty
<
Size
>&
146
InstanceBayesNet
<
GUM_SCALAR
>::
modalities
()
const
{
147
if
(
modalities__
.
empty
()) {
148
for
(
const
auto
node
:
this
->
nodes
()) {
149
modalities__
.
insert
(
node
,
variable
(
node
).
domainSize
());
150
}
151
}
152
153
return
modalities__
;
154
}
155
156
template
<
typename
GUM_SCALAR
>
157
INLINE
std
::
string
InstanceBayesNet
<
GUM_SCALAR
>::
toDot
()
const
{
158
std
::
string
tab
=
" "
;
159
std
::
stringstream
output
;
160
output
<<
"digraph \""
;
161
output
<<
inst__
->
name
() <<
"\" {"
<<
std
::
endl
;
162
163
for
(
const
auto
node
:
this
->
nodes
()) {
164
if
(
this
->
children
(
node
).
size
() > 0) {
165
const
NodeSet
&
children
=
this
->
children
(
node
);
166
167
for
(
const
auto
chi
:
children
) {
168
output
<<
tab
<<
"\""
<<
variable
(
node
).
name
() <<
"\" -> "
;
169
output
<<
"\""
<<
variable
(
chi
).
name
() <<
"\";"
<<
std
::
endl
;
170
}
171
}
else
if
(
this
->
parents
(
node
).
size
() == 0) {
172
output
<<
tab
<<
"\""
<<
variable
(
node
).
name
() <<
"\";"
<<
std
::
endl
;
173
}
174
}
175
176
output
<<
"}"
<<
std
::
endl
;
177
return
output
.
str
();
178
}
179
180
}
/* namespace prm */
181
}
/* 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