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