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