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