aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
GeneralizedCNFWriter_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 >
56 GUM_CONSTRUCTOR(GeneralizedCNFWriter);
57 }
58
59 // Default destructor.
60 template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
62 GUM_DESTRUCTOR(GeneralizedCNFWriter);
63 }
64
65 //
66 // Writes a Bayesian network in the output stream using the BN format.
67 //
68 // @param ouput The output stream.
69 // @param bn The Bayesian network writen in output.
70 // @throws Raised if an I/O error occurs.
71 template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
73 std::ostream& output,
74 const IBayesNet< GUM_SCALAR >& bn) {
75 if (!output.good()) GUM_ERROR(IOError, "Input/Output error : stream not writable.")
76
77 std::stringstream strfile, strfile2;
78
79 Size num = 0;
80 Size numparam = 0;
81
82 for (auto node: bn.nodes())
83 numparam += bn.variable(node).domainSize();
84
85 Idx clause = 0;
86 std::stringstream clausstr;
87 gum::HashTable< std::string, Idx > vartable; // key name::label val num;
88 gum::HashTable< std::string, Idx > protable;
89
90 for (auto node: bn.nodes()) {
91 const auto& var = bn.variable(node);
92
93 for (Idx i = 0; i < var.domainSize(); i++) {
94 std::stringstream str;
95 str << var.name() << "_" << var.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 protable.insert(inst.toString(), ++numparam);
110 strfile2 << numparam << "::" << strinst.str() << "\n";
111 }
112 }
113
114 for (auto node: bn.nodes()) {
115 const auto& var = bn.variable(node);
116 std::stringstream str0, str1, str2, str3;
117
118 for (Idx i = 0; i < var.domainSize(); i++) {
119 std::stringstream stri; //= bn.variable(iter).name()+"_"+
120 // bn.variable(iter).label( i ) ;
121 stri << var.name() << "_" << var.label(i);
122 str0 << vartable[stri.str()] << " ";
123
124 for (Idx j = i + 1; j < var.domainSize(); j++) {
125 std::stringstream strj;
126 strj << var.name() << "_" << var.label(j);
127 str1 << "-" << vartable[stri.str()] << " -" << vartable[strj.str()] << " 0\n";
128 clause++;
129 }
130 }
131
132 str0 << "0\n";
133 clause++;
134 clausstr << str0.str() << str1.str();
135 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
136 Instantiation inst(cpt);
137
138 for (inst.setFirst(); !inst.end(); ++inst) {
139 for (Idx i = 0; i < inst.nbrDim(); i++) {
140 std::stringstream str;
141 str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
142 str2 << "-" << vartable[str.str()] << " ";
143 str3 << "-" << protable[inst.toString()] << " " << vartable[str.str()] << " 0\n";
144 clause++;
145 }
146
147 str2 << protable[inst.toString()] << " 0\n";
148 clause++;
149 }
150
151 clausstr << str2.str() << str3.str();
152 }
153
154 output << "p cnf " << num + numparam << " " << clause << "\n" << clausstr.str() << std::endl;
155 output.flush();
156 }
157
158 // Writes a Bayesian network in the referenced file using the BN format.
159 // If the file doesn't exists, it is created.
160 // If the file exists, it's content will be erased.
161 //
162 // @param filePath The path to the file used to write the Bayesian network.
163 // @param bn The Bayesian network writed in the file.
164 // @throws Raised if an I/O error occurs.
165 template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
167 const std::string& filePath,
168 const IBayesNet< GUM_SCALAR >& bn) {
169 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
170 std::ofstream outputvar((filePath + ".var").c_str(), std::ios_base::trunc);
171
172 if (!output.good()) GUM_ERROR(IOError, "Input/Output error : " << filePath << " not writable.")
173
174 std::stringstream strfile, strfile2;
175
176 if (!outputvar.good())
177 GUM_ERROR(IOError, "Input/Output error : " << (filePath + ".var") << " not writable.")
178
179 Idx num = 0;
180 Idx numparam = 0;
181
182 for (auto node: bn.nodes())
183 numparam += bn.variable(node).domainSize();
184
185 Idx clause = 0;
186 std::stringstream clausstr;
187 gum::HashTable< std::string, Idx > vartable; // key name::label val num;
188 gum::HashTable< std::string, Idx > protable;
189
190 for (auto node: bn.nodes()) {
191 const auto& var = bn.variable(node);
192
193 for (Idx i = 0; i < var.domainSize(); i++) {
194 std::stringstream str;
195 str << var.name() << "_" << var.label(i);
196 vartable.insert(str.str(), ++num);
197 strfile << num << "::" << str.str() << "\n";
198 }
199
200 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
201
202 Instantiation inst(cpt);
203
204 for (inst.setFirst(); !inst.end(); ++inst) {
205 std::stringstream strinst;
206 strinst << inst.toString();
207 strinst << "_val=" << this->fromExact(cpt[inst]);
208
209 protable.insert(inst.toString(), ++numparam);
210 strfile2 << numparam << "::" << strinst.str() << "\n";
211 }
212 }
213
214 for (auto node: bn.nodes()) {
215 const auto& var = bn.variable(node);
216 std::stringstream str0, str1, str2, str3;
217
218 for (Idx i = 0; i < var.domainSize(); i++) {
219 std::stringstream stri; //= bn.variable(iter).name()+"_"+
220 // bn.variable(iter).label( i ) ;
221 stri << var.name() << "_" << var.label(i);
222 str0 << vartable[stri.str()] << " ";
223
224 for (Idx j = i + 1; j < var.domainSize(); j++) {
225 std::stringstream strj;
226 strj << var.name() << "_" << var.label(j);
227 str1 << "-" << vartable[stri.str()] << " -" << vartable[strj.str()] << " 0\n";
228 clause++;
229 }
230 }
231
232 str0 << "0\n";
233 clause++;
234 clausstr << str0.str() << str1.str();
235 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
236 Instantiation inst(cpt);
237
238 for (inst.setFirst(); !inst.end(); ++inst) {
239 for (Idx i = 0; i < inst.nbrDim(); i++) {
240 std::stringstream str;
241 str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
242 str2 << "-" << vartable[str.str()] << " ";
243 str3 << "-" << protable[inst.toString()] << " " << vartable[str.str()] << " 0\n";
244 clause++;
245 }
246
247 str2 << protable[inst.toString()] << " 0\n";
248 clause++;
249 }
250
251 clausstr << str2.str() << str3.str();
252 }
253
254 output << "p cnf " << num + numparam << " " << clause << "\n" << clausstr.str() << std::endl;
255 output.flush();
256 outputvar << strfile.str() << strfile2.str();
257 outputvar.flush();
258 outputvar.close();
259 output.close();
260
261 if (outputvar.fail()) GUM_ERROR(IOError, "Writing in the ostream failed.")
262
263 if (output.fail()) GUM_ERROR(IOError, "Writing in the ostream failed.")
264 }
265
266 // Returns a bloc defining a variable's CPT in the BN format.
267 /*template<typename GUM_SCALAR, template<class> class IApproximationPolicy >
268 INLINE
269 std::string
270 CNFWriter<GUM_SCALAR>:: _variableCPT_( const Tensor<GUM_SCALAR>& cpt ) {
271 std::stringstream str;
272 str << "";
273 return str.str();
274 }
275
276 // Returns the header of the BN file.
277 template<typename GUM_SCALAR,> INLINE
278 std::string
279 CNFWriter<GUM_SCALAR>:: _header_( const IBayesNet<GUM_SCALAR>& ) {
280 std::stringstream str;
281 str << "";
282 return str.str();
283 }
284
285 // Returns a bloc defining a variable in the BN format.
286 template<typename GUM_SCALAR> INLINE
287 std::string
288 CNFWriter<GUM_SCALAR>:: _variableBloc_( const DiscreteVariable& var ) {
289 std::stringstream str;
290 str << "" ;
291 return str.str();
292 }*/
293
294 // Returns the modalities labels of the variables in varsSeq
295
296} /* namespace gum */
297
298#endif // DOXYGEN_SHOULD_SKIP_THIS
Definition of classe for BN file output manipulation.
<agrum/BN/io/cnf/GeneralizedCNFWriter.h>
GeneralizedCNFWriter()
Default constructor.
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the BN format.
~GeneralizedCNFWriter()
Destructor.
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
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46