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