aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
classBayesNet_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 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
(
const
PRMClass
<
GUM_SCALAR
>&
c
) :
61
IBayesNet
<
GUM_SCALAR
>(),
_class_
(&
c
) {
62
GUM_CONSTRUCTOR
(
ClassBayesNet
);
63
_init_
(
c
);
64
}
65
66
template
<
typename
GUM_SCALAR
>
67
INLINE
ClassBayesNet
<
GUM_SCALAR
>::
ClassBayesNet
(
const
ClassBayesNet
<
GUM_SCALAR
>&
from
) :
68
IBayesNet
<
GUM_SCALAR
>(
from
),
_class_
(
from
.
_class_
) {
69
GUM_CONS_CPY
(
ClassBayesNet
);
70
}
71
72
template
<
typename
GUM_SCALAR
>
73
INLINE
ClassBayesNet
<
GUM_SCALAR
>::~
ClassBayesNet
() {
74
GUM_DESTRUCTOR
(
ClassBayesNet
);
75
}
76
77
template
<
typename
GUM_SCALAR
>
78
INLINE
ClassBayesNet
<
GUM_SCALAR
>&
79
ClassBayesNet
<
GUM_SCALAR
>::
operator
=(
const
ClassBayesNet
<
GUM_SCALAR
>&
from
) {
80
if
(
this
!= &
from
) {
81
IBayesNet
<
GUM_SCALAR
>::
operator
=(
from
);
82
83
_class_
=
from
.
_class_
;
84
}
85
86
return
*
this
;
87
}
88
89
template
<
typename
GUM_SCALAR
>
90
INLINE
const
Potential
<
GUM_SCALAR
>&
ClassBayesNet
<
GUM_SCALAR
>::
cpt
(
NodeId
varId
)
const
{
91
return
_get_
(
varId
).
cpf
();
92
}
93
94
template
<
typename
GUM_SCALAR
>
95
INLINE
const
VariableNodeMap
&
ClassBayesNet
<
GUM_SCALAR
>::
variableNodeMap
()
const
{
96
GUM_ERROR
(
FatalError
,
"Sorry no VarMap in a ClassBayesNet."
)
97
}
98
99
template
<
typename
GUM_SCALAR
>
100
INLINE
const
DiscreteVariable
&
ClassBayesNet
<
GUM_SCALAR
>::
variable
(
NodeId
id
)
const
{
101
return
_get_
(
id
).
type
().
variable
();
102
}
103
104
template
<
typename
GUM_SCALAR
>
105
INLINE
NodeId
ClassBayesNet
<
GUM_SCALAR
>::
nodeId
(
const
DiscreteVariable
&
var
)
const
{
106
return
_varNodeMap_
[&
var
]->
id
();
107
}
108
109
template
<
typename
GUM_SCALAR
>
110
INLINE
NodeId
ClassBayesNet
<
GUM_SCALAR
>::
idFromName
(
const
std
::
string
&
name
)
const
{
111
return
_get_
(
name
).
id
();
112
}
113
114
template
<
typename
GUM_SCALAR
>
115
INLINE
const
DiscreteVariable
&
116
ClassBayesNet
<
GUM_SCALAR
>::
variableFromName
(
const
std
::
string
&
name
)
const
{
117
return
_get_
(
name
).
type
().
variable
();
118
}
119
120
template
<
typename
GUM_SCALAR
>
121
INLINE
const
PRMClassElement
<
GUM_SCALAR
>&
122
ClassBayesNet
<
GUM_SCALAR
>::
_get_
(
NodeId
id
)
const
{
123
if
(
this
->
dag_
.
exists
(
id
)) {
124
return
_class_
->
get
(
id
);
125
}
else
{
126
GUM_ERROR
(
NotFound
,
"no element found with that id."
)
127
}
128
}
129
130
template
<
typename
GUM_SCALAR
>
131
INLINE
const
PRMClassElement
<
GUM_SCALAR
>&
132
ClassBayesNet
<
GUM_SCALAR
>::
_get_
(
const
std
::
string
&
name
)
const
{
133
try
{
134
return
_class_
->
get
(
name
);
135
}
catch
(
NotFound
&) {
GUM_ERROR
(
NotFound
,
"no element found with that id."
) }
136
}
137
138
template
<
typename
GUM_SCALAR
>
139
INLINE
const
NodeProperty
<
Size
>&
ClassBayesNet
<
GUM_SCALAR
>::
modalities
()
const
{
140
if
(
_modalities_
.
empty
()) {
141
for
(
const
auto
node
:
this
->
nodes
()) {
142
_modalities_
.
insert
(
node
, (
Size
)
variable
(
node
).
domainSize
());
143
}
144
}
145
146
return
_modalities_
;
147
}
148
149
template
<
typename
GUM_SCALAR
>
150
INLINE
std
::
string
ClassBayesNet
<
GUM_SCALAR
>::
toDot
()
const
{
151
std
::
string
tab
=
" "
;
152
std
::
stringstream
output
;
153
output
<<
"digraph \""
;
154
output
<<
_class_
->
name
() <<
"\" {"
<<
std
::
endl
;
155
156
for
(
const
auto
node
:
this
->
nodes
()) {
157
if
(
this
->
children
(
node
).
size
() > 0)
158
for
(
const
auto
chi
:
this
->
children
(
node
)) {
159
output
<<
tab
<<
"\""
<<
variable
(
node
).
name
() <<
"\" -> "
;
160
output
<<
"\""
<<
variable
(
chi
).
name
() <<
"\";"
<<
std
::
endl
;
161
}
162
else
if
(
this
->
parents
(
node
).
size
() == 0) {
163
output
<<
tab
<<
"\""
<<
variable
(
node
).
name
() <<
"\";"
<<
std
::
endl
;
164
}
165
}
166
167
output
<<
"}"
<<
std
::
endl
;
168
return
output
.
str
();
169
}
170
171
}
/* namespace prm */
172
}
/* 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