aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
UAIBNWriter_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
/
BN
/
io
/
UAI
/
UAIBNWriter
.
h
>
25
26
namespace
gum
{
27
28
/*
29
* Default constructor.
30
*/
31
template
<
typename
GUM_SCALAR >
32
INLINE UAIBNWriter<
GUM_SCALAR
>::
UAIBNWriter
() {
33
GUM_CONSTRUCTOR
(
UAIBNWriter
);
34
}
35
36
/*
37
* Destructor.
38
*/
39
template
<
typename
GUM_SCALAR
>
40
INLINE
UAIBNWriter
<
GUM_SCALAR
>::~
UAIBNWriter
() {
41
GUM_DESTRUCTOR
(
UAIBNWriter
);
42
}
43
44
/*
45
* Writes a bayes net in the given ouput stream.
46
*
47
* @param output The output stream.
48
* @param bn The bayes net writen in the stream.
49
* @throws IOError Raised if an I/O error occurs.
50
*/
51
template
<
typename
GUM_SCALAR
>
52
INLINE
void
UAIBNWriter
<
GUM_SCALAR
>::
write
(
std
::
ostream
&
output
,
53
const
IBayesNet
<
GUM_SCALAR
>&
bn
) {
54
if
(!
output
.
good
()) {
GUM_ERROR
(
IOError
,
"Stream states flags are not all unset."
) }
55
56
output
<<
_preambule_
(
bn
) <<
std
::
endl
;
57
58
for
(
auto
node
:
bn
.
nodes
())
59
output
<<
_cptBloc_
(
bn
,
node
) <<
std
::
endl
;
60
61
output
<<
std
::
endl
;
62
63
output
.
flush
();
64
65
if
(
output
.
fail
()) {
GUM_ERROR
(
IOError
,
"Writing in the ostream failed."
) }
66
}
67
68
/*
69
* Writes a bayes net in the file referenced by filePath.
70
* If the file doesn't exists, it is created.
71
* If the file exists, it's content will be erased.
72
*
73
* @param filePath The path to the file used to write the bayes net.
74
* @param bn The bayes net writen in the file.
75
* @throw IOError Raised if an I/O error occurs.
76
*/
77
template
<
typename
GUM_SCALAR
>
78
INLINE
void
UAIBNWriter
<
GUM_SCALAR
>::
write
(
const
std
::
string
&
filePath
,
79
const
IBayesNet
<
GUM_SCALAR
>&
bn
) {
80
std
::
ofstream
output
(
filePath
.
c_str
(),
std
::
ios_base
::
trunc
);
81
82
write
(
output
,
bn
);
83
84
output
.
close
();
85
86
if
(
output
.
fail
()) {
GUM_ERROR
(
IOError
,
"Writing in the ostream failed."
) }
87
}
88
89
template
<
typename
GUM_SCALAR
>
90
INLINE
std
::
string
UAIBNWriter
<
GUM_SCALAR
>::
_preambule_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
) {
91
std
::
stringstream
str
;
92
93
str
<<
"BAYES"
<<
std
::
endl
;
94
95
str
<<
bn
.
size
() <<
std
::
endl
;
96
97
for
(
auto
node
:
bn
.
nodes
())
98
str
<<
bn
.
variable
(
node
).
domainSize
() <<
" "
;
99
str
<<
std
::
endl
;
100
101
str
<<
bn
.
size
() <<
std
::
endl
;
// number of potentials
102
103
for
(
auto
node
:
bn
.
nodes
()) {
104
const
auto
&
p
=
bn
.
cpt
(
node
);
105
str
<<
p
.
nbrDim
() <<
" "
;
106
// P(X|Y,Z) has to be written "Y Z X". So we need to keep the first var (X)
107
// in order to print it at last
108
NodeId
first
= 0;
109
bool
isFirst
=
true
;
110
for
(
auto
k
:
p
.
variablesSequence
()) {
111
if
(
isFirst
) {
112
isFirst
=
false
;
113
first
=
bn
.
idFromName
(
k
->
name
());
114
}
else
{
115
str
<<
bn
.
idFromName
(
k
->
name
()) <<
" "
;
116
}
117
}
118
str
<<
first
<<
" # "
<<
bn
.
variable
(
node
).
name
() <<
std
::
endl
;
119
}
120
str
<<
std
::
endl
;
121
122
return
str
.
str
();
123
}
124
template
<
typename
GUM_SCALAR
>
125
INLINE
std
::
string
UAIBNWriter
<
GUM_SCALAR
>::
_cptBloc_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
,
126
NodeId
node
) {
127
std
::
stringstream
str
;
128
129
const
auto
&
p
=
bn
.
cpt
(
node
);
130
str
<<
p
.
domainSize
();
131
Instantiation
I
(
p
);
132
for
(
I
.
setFirst
(); !
I
.
end
(); ++
I
) {
133
if
(
I
.
val
(0) == 0)
str
<<
std
::
endl
<<
" "
;
134
str
<<
p
[
I
] <<
" "
;
135
}
136
str
<<
std
::
endl
;
137
138
return
str
.
str
();
139
}
140
141
}
/* namespace gum */
142
143
#
endif
// DOXYGEN_SHOULD_SKIP_THIS
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643