aGrUM  0.13.2
GeneralizedCNFWriter_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #ifndef DOXYGEN_SHOULD_SKIP_THIS
22 
23 // to ease parsing in IDE
25 
26 namespace gum {
27 
28  /* =========================================================================*/
29  /* === GUM_BN_WRITER === */
30  /* =========================================================================*/
31  // Default constructor.
32  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
33  INLINE GeneralizedCNFWriter< GUM_SCALAR,
34  IApproximationPolicy >::GeneralizedCNFWriter() {
35  GUM_CONSTRUCTOR(GeneralizedCNFWriter);
36  }
37 
38  // Default destructor.
39  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
40  INLINE GeneralizedCNFWriter< GUM_SCALAR,
41  IApproximationPolicy >::~GeneralizedCNFWriter() {
42  GUM_DESTRUCTOR(GeneralizedCNFWriter);
43  }
44 
45  //
46  // Writes a Bayesian Network in the output stream using the BN format.
47  //
48  // @param ouput The output stream.
49  // @param bn The Bayesian Network writen in output.
50  // @throws Raised if an I/O error occurs.
51  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
53  std::ostream& output, const IBayesNet< GUM_SCALAR >& bn) {
54  if (!output.good())
55  GUM_ERROR(IOError, "Stream states flags are not all unset.");
56 
57  std::stringstream strfile, strfile2;
58 
59  Size num = 0;
60  Size numparam = 0;
61 
62  for (auto node : bn.nodes())
63  numparam += bn.variable(node).domainSize();
64 
65  Idx clause = 0;
66  std::stringstream clausstr;
67  gum::HashTable< std::string, Idx > vartable; // key name::label val num;
69 
70  for (auto node : bn.nodes()) {
71  const auto& var = bn.variable(node);
72 
73  for (Idx i = 0; i < var.domainSize(); i++) {
74  std::stringstream str;
75  str << var.name() << "_" << var.label(i);
76  vartable.insert(str.str(), ++num);
77  strfile << num << "::" << str.str() << "\n";
78  }
79 
80  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
81 
82  Instantiation inst(cpt);
83 
84  for (inst.setFirst(); !inst.end(); ++inst) {
85  std::stringstream strinst;
86  strinst << inst.toString();
87  strinst << "_val=" << this->fromExact(cpt[inst]);
88 
89  protable.insert(inst.toString(), ++numparam);
90  strfile2 << numparam << "::" << strinst.str() << "\n";
91  }
92  }
93 
94  for (auto node : bn.nodes()) {
95  const auto& var = bn.variable(node);
96  std::stringstream str0, str1, str2, str3;
97 
98  for (Idx i = 0; i < var.domainSize(); i++) {
99  std::stringstream stri; //= bn.variable(iter).name()+"_"+
100  // bn.variable(iter).label( i ) ;
101  stri << var.name() << "_" << var.label(i);
102  str0 << vartable[stri.str()] << " ";
103 
104  for (Idx j = i + 1; j < var.domainSize(); j++) {
105  std::stringstream strj;
106  strj << var.name() << "_" << var.label(j);
107  str1 << "-" << vartable[stri.str()] << " -" << vartable[strj.str()]
108  << " 0\n";
109  clause++;
110  }
111  }
112 
113  str0 << "0\n";
114  clause++;
115  clausstr << str0.str() << str1.str();
116  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
117  Instantiation inst(cpt);
118 
119  for (inst.setFirst(); !inst.end(); ++inst) {
120  for (Idx i = 0; i < inst.nbrDim(); i++) {
121  std::stringstream str;
122  str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
123  str2 << "-" << vartable[str.str()] << " ";
124  str3 << "-" << protable[inst.toString()] << " " << vartable[str.str()]
125  << " 0\n";
126  clause++;
127  }
128 
129  str2 << protable[inst.toString()] << " 0\n";
130  clause++;
131  }
132 
133  clausstr << str2.str() << str3.str();
134  }
135 
136  output << "p cnf " << num + numparam << " " << clause << "\n"
137  << clausstr.str() << std::endl;
138  output.flush();
139  }
140 
141  // Writes a Bayesian Network in the referenced file using the BN format.
142  // If the file doesn't exists, it is created.
143  // If the file exists, it's content will be erased.
144  //
145  // @param filePath The path to the file used to write the Bayesian Network.
146  // @param bn The Bayesian Network writed in the file.
147  // @throws Raised if an I/O error occurs.
148  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
150  const std::string& filePath, const IBayesNet< GUM_SCALAR >& bn) {
151  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
152  std::ofstream outputvar((filePath + ".var").c_str(), std::ios_base::trunc);
153 
154  if (!output.good())
155  GUM_ERROR(IOError, "Stream states flags are not all unset.");
156 
157  std::stringstream strfile, strfile2;
158 
159  if (!outputvar.good())
160  GUM_ERROR(IOError, "Stream states flags are not all unset.");
161 
162  Idx num = 0;
163  Idx numparam = 0;
164 
165  for (auto node : bn.nodes())
166  numparam += bn.variable(node).domainSize();
167 
168  Idx clause = 0;
169  std::stringstream clausstr;
170  gum::HashTable< std::string, Idx > vartable; // key name::label val num;
172 
173  for (auto node : bn.nodes()) {
174  const auto& var = bn.variable(node);
175 
176  for (Idx i = 0; i < var.domainSize(); i++) {
177  std::stringstream str;
178  str << var.name() << "_" << var.label(i);
179  vartable.insert(str.str(), ++num);
180  strfile << num << "::" << str.str() << "\n";
181  }
182 
183  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
184 
185  Instantiation inst(cpt);
186 
187  for (inst.setFirst(); !inst.end(); ++inst) {
188  std::stringstream strinst;
189  strinst << inst.toString();
190  strinst << "_val=" << this->fromExact(cpt[inst]);
191 
192  protable.insert(inst.toString(), ++numparam);
193  strfile2 << numparam << "::" << strinst.str() << "\n";
194  }
195  }
196 
197  for (auto node : bn.nodes()) {
198  const auto& var = bn.variable(node);
199  std::stringstream str0, str1, str2, str3;
200 
201  for (Idx i = 0; i < var.domainSize(); i++) {
202  std::stringstream stri; //= bn.variable(iter).name()+"_"+
203  // bn.variable(iter).label( i ) ;
204  stri << var.name() << "_" << var.label(i);
205  str0 << vartable[stri.str()] << " ";
206 
207  for (Idx j = i + 1; j < var.domainSize(); j++) {
208  std::stringstream strj;
209  strj << var.name() << "_" << var.label(j);
210  str1 << "-" << vartable[stri.str()] << " -" << vartable[strj.str()]
211  << " 0\n";
212  clause++;
213  }
214  }
215 
216  str0 << "0\n";
217  clause++;
218  clausstr << str0.str() << str1.str();
219  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
220  Instantiation inst(cpt);
221 
222  for (inst.setFirst(); !inst.end(); ++inst) {
223  for (Idx i = 0; i < inst.nbrDim(); i++) {
224  std::stringstream str;
225  str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
226  str2 << "-" << vartable[str.str()] << " ";
227  str3 << "-" << protable[inst.toString()] << " " << vartable[str.str()]
228  << " 0\n";
229  clause++;
230  }
231 
232  str2 << protable[inst.toString()] << " 0\n";
233  clause++;
234  }
235 
236  clausstr << str2.str() << str3.str();
237  }
238 
239  output << "p cnf " << num + numparam << " " << clause << "\n"
240  << clausstr.str() << std::endl;
241  output.flush();
242  outputvar << strfile.str() << strfile2.str();
243  outputvar.flush();
244  outputvar.close();
245  output.close();
246 
247  if (outputvar.fail()) GUM_ERROR(IOError, "Writting in the ostream failed.");
248 
249  if (output.fail()) GUM_ERROR(IOError, "Writting in the ostream failed.");
250  }
251 
252  // Returns a bloc defining a variable's CPT in the BN format.
253  /*template<typename GUM_SCALAR, template<class> class IApproximationPolicy >
254  INLINE
255  std::string
256  CNFWriter<GUM_SCALAR>::__variableCPT( const Potential<GUM_SCALAR>& cpt ) {
257  std::stringstream str;
258  str << "";
259  return str.str();
260  }
261 
262  // Returns the header of the BN file.
263  template<typename GUM_SCALAR,> INLINE
264  std::string
265  CNFWriter<GUM_SCALAR>::__header( const IBayesNet<GUM_SCALAR>& ) {
266  std::stringstream str;
267  str << "";
268  return str.str();
269  }
270 
271  // Returns a bloc defining a variable in the BN format.
272  template<typename GUM_SCALAR> INLINE
273  std::string
274  CNFWriter<GUM_SCALAR>::__variableBloc( const DiscreteVariable& var ) {
275  std::stringstream str;
276  str << "" ;
277  return str.str();
278  }*/
279 
280  // Returns the modalities labels of the variables in varsSeq
281 
282 } /* namespace gum */
283 
284 #endif // DOXYGEN_SHOULD_SKIP_THIS
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the BN format.
Definition of classe for BN file output manipulation.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
unsigned long Idx
Type for indexes.
Definition: types.h:43
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66