aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
FactorisedValuesCNFWriter_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44
45// to ease parsing in IDE
47
48namespace gum {
49
50 /* =========================================================================*/
51 /* === GUM_BN_WRITER === */
52 /* =========================================================================*/
53 // Default constructor.
54 template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
55 INLINE
57 GUM_CONSTRUCTOR(FactorisedValuesCNFWriter);
58 }
59
60 // Default destructor.
61 template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
62 INLINE
64 GUM_DESTRUCTOR(FactorisedValuesCNFWriter);
65 }
66
67 //
68 // Writes a Bayesian network in the output stream using the BN format.
69 //
70 // @param output The output stream.
71 // @param bn The Bayesian network writen in output.
72 // @throws Raised if an I/O error occurs.
73 template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
75 std::ostream& output,
76 const IBayesNet< GUM_SCALAR >& bn) {
77 if (!output.good()) GUM_ERROR(IOError, "Input/Output error : stream not writable.")
78
79 std::stringstream strfile, strfile2;
80
81 Idx num = 0;
82 Idx numparam = 0;
83
84 for (auto node: bn.nodes())
85 numparam += bn.variable(node).domainSize();
86
87 Idx clause = 0;
88 std::stringstream clausstr;
89 gum::HashTable< std::string, Idx > vartable; // key name::label val num;
90 gum::HashTable< std::string, Idx > protable;
91
92 for (auto node: bn.nodes()) {
93 for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
94 std::stringstream str;
95 str << bn.variable(node).name() << "_" << bn.variable(node).label(i);
96 vartable.insert(str.str(), ++num);
97 strfile << num << "::" << str.str() << "\n";
98 }
99
100 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
101
102 Instantiation inst(cpt);
103
104 for (inst.setFirst(); !inst.end(); ++inst) {
105 std::stringstream strinst;
106 strinst << inst.toString();
107 strinst << "_val=" << this->fromExact(cpt[inst]);
108
109 if (!protable.exists(strinst.str())) {
110 protable.insert(inst.toString(), ++numparam);
111 strfile2 << numparam << "::" << strinst.str() << "\n";
112 }
113 }
114 }
115
116 for (auto node: bn.nodes()) {
117 std::stringstream str0, str2;
118
119 for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
120 std::stringstream stri; //= bn.variable(iter).name()+"_"+
121 // bn.variable(iter).label( i ) ;
122 stri << bn.variable(node).name() << "_" << bn.variable(node).label(i);
123 str0 << vartable[stri.str()] << " ";
124 }
125
126 str0 << "0\n";
127 clause++;
128 clausstr << str0.str();
129 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
130 Instantiation inst(cpt);
131
132 for (inst.setFirst(); !inst.end(); ++inst) {
133 if (this->fromExact(cpt[inst]) != 1.0) {
134 for (Idx i = 0; i < inst.nbrDim(); i++) {
135 std::stringstream str;
136 str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
137 str2 << "-" << vartable[str.str()] << " ";
138 }
139
140 if (this->fromExact(cpt[inst])) {
141 std::stringstream strinst;
142 strinst << bn.variable(node).name();
143 strinst << "_val=" << this->fromExact(cpt[inst]);
144 str2 << protable[strinst.str()];
145 }
146
147 str2 << " 0\n";
148 clause++;
149 }
150 }
151
152 clausstr << str2.str();
153 }
154
155 output << "p cnf " << num + numparam << " " << clause << "\n" << clausstr.str() << std::endl;
156 output.flush();
157 }
158
159 // Writes a Bayesian network in the referenced file using the BN format.
160 // If the file doesn't exists, it is created.
161 // If the file exists, it's content will be erased.
162 //
163 // @param filePath The path to the file used to write the Bayesian network.
164 // @param bn The Bayesian network writed in the file.
165 // @throws Raised if an I/O error occurs.
166 template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
168 const std::string& filePath,
169 const IBayesNet< GUM_SCALAR >& bn) {
170 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
171 std::ofstream outputvar((filePath + ".var").c_str(), std::ios_base::trunc);
172
173 if (!output.good()) GUM_ERROR(IOError, "Input/Output error : " << filePath << " not writable.")
174
175 std::stringstream strfile, strfile2;
176
177 if (!outputvar.good())
178 GUM_ERROR(IOError, "Input/Output error : " << (filePath + ".var") << " not writable.")
179
180 Idx num = 0;
181 Idx numparam = 0;
182
183 for (auto node: bn.nodes())
184 numparam += bn.variable(node).domainSize();
185
186 Idx clause = 0;
187 std::stringstream clausstr;
188 gum::HashTable< std::string, Idx > vartable; // key name::label val num;
189 gum::HashTable< std::string, Idx > protable;
190
191 for (auto node: bn.nodes()) {
192 const auto& var = bn.variable(node);
193
194 for (Idx i = 0; i < var.domainSize(); i++) {
195 std::stringstream str;
196 str << var.name() << "_" << var.label(i);
197 vartable.insert(str.str(), ++num);
198 strfile << num << "::" << str.str() << "\n";
199 }
200
201 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
202
203 Instantiation inst(cpt);
204
205 for (inst.setFirst(); !inst.end(); ++inst) {
206 if (this->fromExact(cpt[inst]) && this->fromExact(cpt[inst]) != 1.0) {
207 std::stringstream strinst;
208 strinst << var.name();
209 strinst << "_val=" << this->fromExact(cpt[inst]);
210
211 if (!protable.exists(strinst.str())) {
212 protable.insert(strinst.str(), ++numparam);
213 strfile2 << numparam << "::" << strinst.str() << "\n";
214 }
215 }
216 }
217 }
218
219 for (auto node: bn.nodes()) {
220 std::stringstream str0, str2;
221
222 for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
223 std::stringstream stri; //= bn.variable(iter).name()+"_"+
224 // bn.variable(iter).label( i ) ;
225 stri << bn.variable(node).name() << "_" << bn.variable(node).label(i);
226 str0 << vartable[stri.str()] << " ";
227 }
228
229 str0 << "0\n";
230 clause++;
231 clausstr << str0.str();
232 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
233 Instantiation inst(cpt);
234
235 for (inst.setFirst(); !inst.end(); ++inst) {
236 if (this->fromExact(cpt[inst]) != 1.0) {
237 for (Idx i = 0; i < inst.nbrDim(); i++) {
238 std::stringstream str;
239 str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
240 str2 << "-" << vartable[str.str()] << " ";
241 }
242
243 if (this->fromExact(cpt[inst])) {
244 std::stringstream strinst;
245 strinst << bn.variable(node).name();
246 strinst << "_val=" << this->fromExact(cpt[inst]);
247 str2 << protable[strinst.str()];
248 }
249
250 str2 << " 0\n";
251 clause++;
252 }
253 }
254
255 clausstr << str2.str();
256 }
257
258 output << "p cnf " << num + numparam << " " << clause << "\n" << clausstr.str() << std::endl;
259 output.flush();
260 outputvar << strfile.str() << strfile2.str();
261 outputvar.flush();
262 outputvar.close();
263 output.close();
264
265 if (outputvar.fail()) GUM_ERROR(IOError, "Writing in the ostream failed.")
266
267 if (output.fail()) GUM_ERROR(IOError, "Writing in the ostream failed.")
268 }
269
270 // Returns a bloc defining a variable's CPT in the BN format.
271 /* template<typename GUM_SCALAR> INLINE
272 std::string
273 OCNFWriter<GUM_SCALAR>:: _variableCPT_( const Tensor<GUM_SCALAR>& cpt )
274 {
275 std::stringstream str;
276 str << "";
277 return str.str();
278 }
279
280 // Returns the header of the BN file.
281 template<typename GUM_SCALAR> INLINE
282 std::string
283 OCNFWriter<GUM_SCALAR>:: _header_( const IBayesNet<GUM_SCALAR>& ) {
284 std::stringstream str;
285 str << "";
286 return str.str();
287 }
288
289 // Returns a bloc defining a variable in the BN format.
290 template<typename GUM_SCALAR> INLINE
291 std::string
292 OCNFWriter<GUM_SCALAR>:: _variableBloc_( const DiscreteVariable& var ) {
293 std::stringstream str;
294 str << "" ;
295 return str.str();
296 }*/
297
298 // Returns the modalities labels of the variables in varsSeq
299
300} /* namespace gum */
301
302#endif // DOXYGEN_SHOULD_SKIP_THIS
Definition of classe for BN file output manipulation.
<agrum/BN/io/cnf/FactorisedValuesCNFWriter.h>
~FactorisedValuesCNFWriter()
Destructor.
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the BN format.
FactorisedValuesCNFWriter()
Default constructor.
The class for generic Hash Tables.
Definition hashTable.h:637
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
Class for assigning/browsing values to tuples of discrete variables.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46