aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
O3prmBNWriter_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
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
23
24
#
include
<
agrum
/
PRM
/
o3prm
/
O3prmBNWriter
.
h
>
25
26
#
define
O3PRM_INDENT
" "
27
28
namespace
gum
{
29
/*
30
* Default constructor.
31
*/
32
template
<
typename
GUM_SCALAR >
33
INLINE O3prmBNWriter<
GUM_SCALAR
>::
O3prmBNWriter
() {
34
GUM_CONSTRUCTOR
(
O3prmBNWriter
);
35
}
36
37
/*
38
* Destructor.
39
*/
40
template
<
typename
GUM_SCALAR
>
41
INLINE
O3prmBNWriter
<
GUM_SCALAR
>::~
O3prmBNWriter
() {
42
GUM_DESTRUCTOR
(
O3prmBNWriter
);
43
}
44
45
/*
46
* Writes a bayes net in the given ouput stream.
47
*
48
* @param output The output stream.
49
* @param bn The bayes net writen in the stream.
50
* @throws IOError Raised if an I/O error occurs.
51
*/
52
template
<
typename
GUM_SCALAR
>
53
INLINE
void
O3prmBNWriter
<
GUM_SCALAR
>::
write
(
std
::
ostream
&
output
,
54
const
IBayesNet
<
GUM_SCALAR
>&
bn
) {
55
if
(!
output
.
good
()) {
GUM_ERROR
(
IOError
,
"Stream states flags are not all unset."
) }
56
std
::
string
bnName
=
bn
.
propertyWithDefault
(
"name"
,
""
);
57
if
(
bnName
==
""
)
bnName
=
"bayesnet"
;
58
59
output
<<
"class "
<<
bnName
<<
" {"
<<
std
::
endl
;
60
61
for
(
auto
node
:
bn
.
nodes
()) {
62
output
<<
_extractAttribute_
(
bn
,
node
) <<
std
::
endl
;
63
}
64
65
output
<<
"}"
<<
std
::
endl
;
66
67
output
<<
std
::
endl
;
68
69
output
.
flush
();
70
71
if
(
output
.
fail
()) {
GUM_ERROR
(
IOError
,
"Writing in the ostream failed."
) }
72
}
73
74
template
<
typename
GUM_SCALAR
>
75
INLINE
std
::
string
76
O3prmBNWriter
<
GUM_SCALAR
>::
_extractAttribute_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
,
77
NodeId
node
) {
78
std
::
stringstream
str
;
79
str
<< O3PRM_INDENT;
80
str
<<
_extractType_
(
bn
,
node
) <<
" "
;
81
str
<<
_extractName_
(
bn
,
node
) <<
" "
;
82
if
(
bn
.
parents
(
node
).
size
() > 0) {
str
<<
"dependson "
<<
_extractParents_
(
bn
,
node
) <<
" "
; }
83
str
<<
" {"
<<
_extractCPT_
(
bn
,
node
) <<
"};"
<<
std
::
endl
;
84
return
str
.
str
();
85
}
86
87
template
<
typename
GUM_SCALAR
>
88
INLINE
std
::
string
89
O3prmBNWriter
<
GUM_SCALAR
>::
_extractParents_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
,
NodeId
node
) {
90
std
::
stringstream
str
;
91
auto
var
= &(
bn
.
variable
(
node
));
92
for
(
auto
parent
:
bn
.
cpt
(
node
).
variablesSequence
()) {
93
if
(
var
!=
parent
) {
str
<<
parent
->
name
() <<
", "
; }
94
}
95
return
str
.
str
().
substr
(0,
str
.
str
().
size
() - 2);
96
}
97
98
template
<
typename
GUM_SCALAR
>
99
INLINE
std
::
string
O3prmBNWriter
<
GUM_SCALAR
>::
_extractCPT_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
,
100
NodeId
node
) {
101
std
::
stringstream
str
;
102
bool
first
=
true
;
103
Instantiation
inst
(
bn
.
cpt
(
node
));
104
105
str
<<
"["
;
106
if
(
inst
.
nbrDim
() == 1) {
107
// 1D potential
108
for
(
inst
.
setFirst
(); !
inst
.
end
();
inst
.
inc
()) {
109
if
(!
first
) {
110
str
<<
", "
;
111
}
else
{
112
first
=
false
;
113
}
114
str
<<
bn
.
cpt
(
node
)[
inst
];
115
}
116
}
else
{
117
// (>1)D potential (with parents)
118
Instantiation
jnst
;
119
for
(
auto
var
=
inst
.
variablesSequence
().
rbegin
();
var
!=
inst
.
variablesSequence
().
rend
();
120
--
var
) {
121
jnst
.
add
(**
var
);
122
}
123
inst
.
setFirst
();
124
auto
currentval
=
inst
.
val
(0) + 1;
125
for
(
jnst
.
setFirst
(); !
jnst
.
end
();
jnst
.
inc
()) {
126
inst
.
setVals
(
jnst
);
127
if
(!
first
) {
128
str
<<
", "
;
129
}
else
{
130
first
=
false
;
131
}
132
if
(
currentval
!=
inst
.
val
(0)) {
// begining line
133
str
<<
std
::
endl
<< O3PRM_INDENT << O3PRM_INDENT;
134
currentval
=
inst
.
val
(0);
135
}
136
str
<<
bn
.
cpt
(
node
)[
inst
];
137
}
138
str
<<
std
::
endl
<< O3PRM_INDENT;
139
}
140
141
str
<<
"]"
;
142
return
str
.
str
();
143
}
144
145
template
<
typename
GUM_SCALAR
>
146
INLINE
std
::
string
O3prmBNWriter
<
GUM_SCALAR
>::
_extractType_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
,
147
NodeId
node
) {
148
switch
(
bn
.
variable
(
node
).
varType
()) {
149
case
gum
::
VarType
::
Discretized
: {
150
auto
double_var
151
=
dynamic_cast
<
const
DiscretizedVariable
<
double
>* >(&(
bn
.
variable
(
node
)));
152
if
(
double_var
!=
nullptr
) {
153
return
_extractDiscretizedType_
<
DiscretizedVariable
<
double
> >(
double_var
);
154
}
else
{
155
auto
float_var
156
=
dynamic_cast
<
const
DiscretizedVariable
<
float
>* >(&(
bn
.
variable
(
node
)));
157
if
(
float_var
!=
nullptr
) {
158
return
_extractDiscretizedType_
<
DiscretizedVariable
<
float
> >(
float_var
);
159
}
160
}
161
GUM_ERROR
(
InvalidArgument
,
"DiscretizedVariable ticks are neither doubles or floats"
)
162
}
163
case
gum
::
VarType
::
Range
: {
164
return
_extractRangeType_
(
bn
,
node
);
165
}
166
default
: {
167
return
_extractLabelizedType_
(
bn
,
node
);
168
}
169
}
170
}
171
172
template
<
typename
GUM_SCALAR
>
173
INLINE
std
::
string
174
O3prmBNWriter
<
GUM_SCALAR
>::
_extractRangeType_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
,
175
NodeId
node
) {
176
const
auto
&
var
=
static_cast
<
const
RangeVariable
& >(
bn
.
variable
(
node
));
177
std
::
stringstream
str
;
178
str
<<
"int ("
<<
var
.
minVal
() <<
", "
<<
var
.
maxVal
() <<
")"
;
179
return
str
.
str
();
180
}
181
182
template
<
typename
GUM_SCALAR
>
183
INLINE
std
::
string
184
O3prmBNWriter
<
GUM_SCALAR
>::
_extractLabelizedType_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
,
185
NodeId
node
) {
186
std
::
stringstream
str
;
187
str
<<
"labels("
;
188
for
(
auto
l
:
bn
.
variable
(
node
).
labels
()) {
189
str
<<
l
<<
", "
;
190
}
191
return
str
.
str
().
substr
(0,
str
.
str
().
size
() - 2) +
")"
;
192
}
193
194
template
<
typename
GUM_SCALAR
>
195
template
<
typename
VARTYPE
>
196
INLINE
std
::
string
O3prmBNWriter
<
GUM_SCALAR
>::
_extractDiscretizedType_
(
const
VARTYPE
*
var
) {
197
std
::
stringstream
str
;
198
if
(
var
->
ticks
().
size
() >= 3) {
199
str
<<
"real("
<<
var
->
ticks
()[0];
200
for
(
size_t
i
= 1;
i
<
var
->
ticks
().
size
(); ++
i
) {
201
str
<<
", "
<<
var
->
ticks
()[
i
];
202
}
203
str
<<
")"
;
204
return
str
.
str
();
205
}
206
GUM_ERROR
(
InvalidArgument
,
"discretized variable does not have enough ticks"
)
207
}
208
209
template
<
typename
GUM_SCALAR
>
210
INLINE
std
::
string
O3prmBNWriter
<
GUM_SCALAR
>::
_extractName_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
,
211
NodeId
node
) {
212
if
(!
bn
.
variable
(
node
).
name
().
empty
()) {
213
return
bn
.
variable
(
node
).
name
();
214
}
else
{
215
std
::
stringstream
str
;
216
str
<<
node
;
217
return
str
.
str
();
218
}
219
}
220
221
/*
222
* Writes a bayes net in the file referenced by filePath.
223
* If the file doesn't exists, it is created.
224
* If the file exists, it's content will be erased.
225
*
226
* @param filePath The path to the file used to write the bayes net.
227
* @param bn The bayes net writen in the file.
228
* @throw IOError Raised if an I/O error occurs.
229
*/
230
template
<
typename
GUM_SCALAR
>
231
INLINE
void
O3prmBNWriter
<
GUM_SCALAR
>::
write
(
const
std
::
string
&
filePath
,
232
const
IBayesNet
<
GUM_SCALAR
>&
bn
) {
233
std
::
ofstream
output
(
filePath
.
c_str
(),
std
::
ios_base
::
trunc
);
234
235
write
(
output
,
bn
);
236
237
output
.
close
();
238
239
if
(
output
.
fail
()) {
GUM_ERROR
(
IOError
,
"Writing in the ostream failed."
) }
240
}
241
242
}
/* namespace gum */
243
244
#
endif
// DOXYGEN_SHOULD_SKIP_THIS
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643