aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
FactorisedValuesCNFWriter_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
// to ease parsing in IDE
25
#
include
<
agrum
/
BN
/
io
/
cnf
/
FactorisedValuesCNFWriter
.
h
>
26
27
namespace
gum
{
28
29
/* =========================================================================*/
30
/* === GUM_BN_WRITER === */
31
/* =========================================================================*/
32
// Default constructor.
33
template
<
typename
GUM_SCALAR,
template
<
class
>
class
IApproximationPolicy >
34
INLINE
35
FactorisedValuesCNFWriter<
GUM_SCALAR
,
IApproximationPolicy
>::
FactorisedValuesCNFWriter
() {
36
GUM_CONSTRUCTOR
(
FactorisedValuesCNFWriter
);
37
}
38
39
// Default destructor.
40
template
<
typename
GUM_SCALAR
,
template
<
class
>
class
IApproximationPolicy
>
41
INLINE
42
FactorisedValuesCNFWriter
<
GUM_SCALAR
,
IApproximationPolicy
>::~
FactorisedValuesCNFWriter
() {
43
GUM_DESTRUCTOR
(
FactorisedValuesCNFWriter
);
44
}
45
46
//
47
// Writes a Bayesian network in the output stream using the BN format.
48
//
49
// @param output The output stream.
50
// @param bn The Bayesian network writen in output.
51
// @throws Raised if an I/O error occurs.
52
template
<
typename
GUM_SCALAR
,
template
<
class
>
class
IApproximationPolicy
>
53
INLINE
void
FactorisedValuesCNFWriter
<
GUM_SCALAR
,
IApproximationPolicy
>::
write
(
54
std
::
ostream
&
output
,
55
const
IBayesNet
<
GUM_SCALAR
>&
bn
) {
56
if
(!
output
.
good
())
GUM_ERROR
(
IOError
,
"Stream states flags are not all unset."
)
57
58
std
::
stringstream
strfile
,
strfile2
;
59
60
Idx
num
= 0;
61
Idx
numparam
= 0;
62
63
for
(
auto
node
:
bn
.
nodes
())
64
numparam
+=
bn
.
variable
(
node
).
domainSize
();
65
66
Idx
clause
= 0;
67
std
::
stringstream
clausstr
;
68
gum
::
HashTable
<
std
::
string
,
Idx
>
vartable
;
// key name::label val num;
69
gum
::
HashTable
<
std
::
string
,
Idx
>
protable
;
70
71
for
(
auto
node
:
bn
.
nodes
()) {
72
for
(
Idx
i
= 0;
i
<
bn
.
variable
(
node
).
domainSize
();
i
++) {
73
std
::
stringstream
str
;
74
str
<<
bn
.
variable
(
node
).
name
() <<
"_"
<<
bn
.
variable
(
node
).
label
(
i
);
75
vartable
.
insert
(
str
.
str
(), ++
num
);
76
strfile
<<
num
<<
"::"
<<
str
.
str
() <<
"\n"
;
77
}
78
79
const
Potential
<
GUM_SCALAR
>&
cpt
=
bn
.
cpt
(
node
);
80
81
Instantiation
inst
(
cpt
);
82
83
for
(
inst
.
setFirst
(); !
inst
.
end
(); ++
inst
) {
84
std
::
stringstream
strinst
;
85
strinst
<<
inst
.
toString
();
86
strinst
<<
"_val="
<<
this
->
fromExact
(
cpt
[
inst
]);
87
88
if
(!
protable
.
exists
(
strinst
.
str
())) {
89
protable
.
insert
(
inst
.
toString
(), ++
numparam
);
90
strfile2
<<
numparam
<<
"::"
<<
strinst
.
str
() <<
"\n"
;
91
}
92
}
93
}
94
95
for
(
auto
node
:
bn
.
nodes
()) {
96
std
::
stringstream
str0
,
str2
;
97
98
for
(
Idx
i
= 0;
i
<
bn
.
variable
(
node
).
domainSize
();
i
++) {
99
std
::
stringstream
stri
;
//= bn.variable(iter).name()+"_"+
100
// bn.variable(iter).label( i ) ;
101
stri
<<
bn
.
variable
(
node
).
name
() <<
"_"
<<
bn
.
variable
(
node
).
label
(
i
);
102
str0
<<
vartable
[
stri
.
str
()] <<
" "
;
103
}
104
105
str0
<<
"0\n"
;
106
clause
++;
107
clausstr
<<
str0
.
str
();
108
const
Potential
<
GUM_SCALAR
>&
cpt
=
bn
.
cpt
(
node
);
109
Instantiation
inst
(
cpt
);
110
111
for
(
inst
.
setFirst
(); !
inst
.
end
(); ++
inst
) {
112
if
(
this
->
fromExact
(
cpt
[
inst
]) != 1.0) {
113
for
(
Idx
i
= 0;
i
<
inst
.
nbrDim
();
i
++) {
114
std
::
stringstream
str
;
115
str
<<
inst
.
variable
(
i
).
name
() <<
"_"
<<
inst
.
val
(
inst
.
variable
(
i
));
116
str2
<<
"-"
<<
vartable
[
str
.
str
()] <<
" "
;
117
}
118
119
if
(
this
->
fromExact
(
cpt
[
inst
])) {
120
std
::
stringstream
strinst
;
121
strinst
<<
bn
.
variable
(
node
).
name
();
122
strinst
<<
"_val="
<<
this
->
fromExact
(
cpt
[
inst
]);
123
str2
<<
protable
[
strinst
.
str
()];
124
}
125
126
str2
<<
" 0\n"
;
127
clause
++;
128
}
129
}
130
131
clausstr
<<
str2
.
str
();
132
}
133
134
output
<<
"p cnf "
<<
num
+
numparam
<<
" "
<<
clause
<<
"\n"
<<
clausstr
.
str
() <<
std
::
endl
;
135
output
.
flush
();
136
}
137
138
// Writes a Bayesian network in the referenced file using the BN format.
139
// If the file doesn't exists, it is created.
140
// If the file exists, it's content will be erased.
141
//
142
// @param filePath The path to the file used to write the Bayesian network.
143
// @param bn The Bayesian network writed in the file.
144
// @throws Raised if an I/O error occurs.
145
template
<
typename
GUM_SCALAR
,
template
<
class
>
class
IApproximationPolicy
>
146
INLINE
void
FactorisedValuesCNFWriter
<
GUM_SCALAR
,
IApproximationPolicy
>::
write
(
147
const
std
::
string
&
filePath
,
148
const
IBayesNet
<
GUM_SCALAR
>&
bn
) {
149
std
::
ofstream
output
(
filePath
.
c_str
(),
std
::
ios_base
::
trunc
);
150
std
::
ofstream
outputvar
((
filePath
+
".var"
).
c_str
(),
std
::
ios_base
::
trunc
);
151
152
if
(!
output
.
good
())
GUM_ERROR
(
IOError
,
"Stream states flags are not all unset."
)
153
154
std
::
stringstream
strfile
,
strfile2
;
155
156
if
(!
outputvar
.
good
())
GUM_ERROR
(
IOError
,
"Stream states flags are not all unset."
)
157
158
Idx
num
= 0;
159
Idx
numparam
= 0;
160
161
for
(
auto
node
:
bn
.
nodes
())
162
numparam
+=
bn
.
variable
(
node
).
domainSize
();
163
164
Idx
clause
= 0;
165
std
::
stringstream
clausstr
;
166
gum
::
HashTable
<
std
::
string
,
Idx
>
vartable
;
// key name::label val num;
167
gum
::
HashTable
<
std
::
string
,
Idx
>
protable
;
168
169
for
(
auto
node
:
bn
.
nodes
()) {
170
const
auto
&
var
=
bn
.
variable
(
node
);
171
172
for
(
Idx
i
= 0;
i
<
var
.
domainSize
();
i
++) {
173
std
::
stringstream
str
;
174
str
<<
var
.
name
() <<
"_"
<<
var
.
label
(
i
);
175
vartable
.
insert
(
str
.
str
(), ++
num
);
176
strfile
<<
num
<<
"::"
<<
str
.
str
() <<
"\n"
;
177
}
178
179
const
Potential
<
GUM_SCALAR
>&
cpt
=
bn
.
cpt
(
node
);
180
181
Instantiation
inst
(
cpt
);
182
183
for
(
inst
.
setFirst
(); !
inst
.
end
(); ++
inst
) {
184
if
(
this
->
fromExact
(
cpt
[
inst
]) &&
this
->
fromExact
(
cpt
[
inst
]) != 1.0) {
185
std
::
stringstream
strinst
;
186
strinst
<<
var
.
name
();
187
strinst
<<
"_val="
<<
this
->
fromExact
(
cpt
[
inst
]);
188
189
if
(!
protable
.
exists
(
strinst
.
str
())) {
190
protable
.
insert
(
strinst
.
str
(), ++
numparam
);
191
strfile2
<<
numparam
<<
"::"
<<
strinst
.
str
() <<
"\n"
;
192
}
193
}
194
}
195
}
196
197
for
(
auto
node
:
bn
.
nodes
()) {
198
std
::
stringstream
str0
,
str2
;
199
200
for
(
Idx
i
= 0;
i
<
bn
.
variable
(
node
).
domainSize
();
i
++) {
201
std
::
stringstream
stri
;
//= bn.variable(iter).name()+"_"+
202
// bn.variable(iter).label( i ) ;
203
stri
<<
bn
.
variable
(
node
).
name
() <<
"_"
<<
bn
.
variable
(
node
).
label
(
i
);
204
str0
<<
vartable
[
stri
.
str
()] <<
" "
;
205
}
206
207
str0
<<
"0\n"
;
208
clause
++;
209
clausstr
<<
str0
.
str
();
210
const
Potential
<
GUM_SCALAR
>&
cpt
=
bn
.
cpt
(
node
);
211
Instantiation
inst
(
cpt
);
212
213
for
(
inst
.
setFirst
(); !
inst
.
end
(); ++
inst
) {
214
if
(
this
->
fromExact
(
cpt
[
inst
]) != 1.0) {
215
for
(
Idx
i
= 0;
i
<
inst
.
nbrDim
();
i
++) {
216
std
::
stringstream
str
;
217
str
<<
inst
.
variable
(
i
).
name
() <<
"_"
<<
inst
.
val
(
inst
.
variable
(
i
));
218
str2
<<
"-"
<<
vartable
[
str
.
str
()] <<
" "
;
219
}
220
221
if
(
this
->
fromExact
(
cpt
[
inst
])) {
222
std
::
stringstream
strinst
;
223
strinst
<<
bn
.
variable
(
node
).
name
();
224
strinst
<<
"_val="
<<
this
->
fromExact
(
cpt
[
inst
]);
225
str2
<<
protable
[
strinst
.
str
()];
226
}
227
228
str2
<<
" 0\n"
;
229
clause
++;
230
}
231
}
232
233
clausstr
<<
str2
.
str
();
234
}
235
236
output
<<
"p cnf "
<<
num
+
numparam
<<
" "
<<
clause
<<
"\n"
<<
clausstr
.
str
() <<
std
::
endl
;
237
output
.
flush
();
238
outputvar
<<
strfile
.
str
() <<
strfile2
.
str
();
239
outputvar
.
flush
();
240
outputvar
.
close
();
241
output
.
close
();
242
243
if
(
outputvar
.
fail
())
GUM_ERROR
(
IOError
,
"Writting in the ostream failed."
)
244
245
if
(
output
.
fail
())
GUM_ERROR
(
IOError
,
"Writting in the ostream failed."
)
246
}
247
248
// Returns a bloc defining a variable's CPT in the BN format.
249
/* template<typename GUM_SCALAR> INLINE
250
std::string
251
OCNFWriter<GUM_SCALAR>:: _variableCPT_( const Potential<GUM_SCALAR>& cpt )
252
{
253
std::stringstream str;
254
str << "";
255
return str.str();
256
}
257
258
// Returns the header of the BN file.
259
template<typename GUM_SCALAR> INLINE
260
std::string
261
OCNFWriter<GUM_SCALAR>:: _header_( const IBayesNet<GUM_SCALAR>& ) {
262
std::stringstream str;
263
str << "";
264
return str.str();
265
}
266
267
// Returns a bloc defining a variable in the BN format.
268
template<typename GUM_SCALAR> INLINE
269
std::string
270
OCNFWriter<GUM_SCALAR>:: _variableBloc_( const DiscreteVariable& var ) {
271
std::stringstream str;
272
str << "" ;
273
return str.str();
274
}*/
275
276
// Returns the modalities labels of the variables in varsSeq
277
278
}
/* namespace gum */
279
280
#
endif
// DOXYGEN_SHOULD_SKIP_THIS
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643