aGrUM  0.13.2
FactorisedValuesCNFWriter_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 >
35  GUM_CONSTRUCTOR(FactorisedValuesCNFWriter);
36  }
37 
38  // Default destructor.
39  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
42  GUM_DESTRUCTOR(FactorisedValuesCNFWriter);
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  Idx num = 0;
60  Idx 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  for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
72  std::stringstream str;
73  str << bn.variable(node).name() << "_" << bn.variable(node).label(i);
74  vartable.insert(str.str(), ++num);
75  strfile << num << "::" << str.str() << "\n";
76  }
77 
78  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
79 
80  Instantiation inst(cpt);
81 
82  for (inst.setFirst(); !inst.end(); ++inst) {
83  std::stringstream strinst;
84  strinst << inst.toString();
85  strinst << "_val=" << this->fromExact(cpt[inst]);
86 
87  if (!protable.exists(strinst.str())) {
88  protable.insert(inst.toString(), ++numparam);
89  strfile2 << numparam << "::" << strinst.str() << "\n";
90  }
91  }
92  }
93 
94  for (auto node : bn.nodes()) {
95  std::stringstream str0, str2;
96 
97  for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
98  std::stringstream stri; //= bn.variable(iter).name()+"_"+
99  // bn.variable(iter).label( i ) ;
100  stri << bn.variable(node).name() << "_" << bn.variable(node).label(i);
101  str0 << vartable[stri.str()] << " ";
102  }
103 
104  str0 << "0\n";
105  clause++;
106  clausstr << str0.str();
107  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
108  Instantiation inst(cpt);
109 
110  for (inst.setFirst(); !inst.end(); ++inst) {
111  if (this->fromExact(cpt[inst]) != 1.0) {
112  for (Idx i = 0; i < inst.nbrDim(); i++) {
113  std::stringstream str;
114  str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
115  str2 << "-" << vartable[str.str()] << " ";
116  }
117 
118  if (this->fromExact(cpt[inst])) {
119  std::stringstream strinst;
120  strinst << bn.variable(node).name();
121  strinst << "_val=" << this->fromExact(cpt[inst]);
122  str2 << protable[strinst.str()];
123  }
124 
125  str2 << " 0\n";
126  clause++;
127  }
128  }
129 
130  clausstr << str2.str();
131  }
132 
133  output << "p cnf " << num + numparam << " " << clause << "\n"
134  << clausstr.str() << std::endl;
135  output.flush();
136  }
137 
138  // Writes a Bayesian Network in the referenced file using the BN format.
139  // If the file doesn't exists, it is created.
140  // If the file exists, it's content will be erased.
141  //
142  // @param filePath The path to the file used to write the Bayesian Network.
143  // @param bn The Bayesian Network writed in the file.
144  // @throws Raised if an I/O error occurs.
145  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
147  const std::string& filePath, const IBayesNet< GUM_SCALAR >& bn) {
148  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
149  std::ofstream outputvar((filePath + ".var").c_str(), std::ios_base::trunc);
150 
151  if (!output.good())
152  GUM_ERROR(IOError, "Stream states flags are not all unset.");
153 
154  std::stringstream strfile, strfile2;
155 
156  if (!outputvar.good())
157  GUM_ERROR(IOError, "Stream states flags are not all unset.");
158 
159  Idx num = 0;
160  Idx numparam = 0;
161 
162  for (auto node : bn.nodes())
163  numparam += bn.variable(node).domainSize();
164 
165  Idx clause = 0;
166  std::stringstream clausstr;
167  gum::HashTable< std::string, Idx > vartable; // key name::label val num;
169 
170  for (auto node : bn.nodes()) {
171  const auto& var = bn.variable(node);
172 
173  for (Idx i = 0; i < var.domainSize(); i++) {
174  std::stringstream str;
175  str << var.name() << "_" << var.label(i);
176  vartable.insert(str.str(), ++num);
177  strfile << num << "::" << str.str() << "\n";
178  }
179 
180  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
181 
182  Instantiation inst(cpt);
183 
184  for (inst.setFirst(); !inst.end(); ++inst) {
185  if (this->fromExact(cpt[inst]) && this->fromExact(cpt[inst]) != 1.0) {
186  std::stringstream strinst;
187  strinst << var.name();
188  strinst << "_val=" << this->fromExact(cpt[inst]);
189 
190  if (!protable.exists(strinst.str())) {
191  protable.insert(strinst.str(), ++numparam);
192  strfile2 << numparam << "::" << strinst.str() << "\n";
193  }
194  }
195  }
196  }
197 
198  for (auto node : bn.nodes()) {
199  std::stringstream str0, str2;
200 
201  for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
202  std::stringstream stri; //= bn.variable(iter).name()+"_"+
203  // bn.variable(iter).label( i ) ;
204  stri << bn.variable(node).name() << "_" << bn.variable(node).label(i);
205  str0 << vartable[stri.str()] << " ";
206  }
207 
208  str0 << "0\n";
209  clause++;
210  clausstr << str0.str();
211  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
212  Instantiation inst(cpt);
213 
214  for (inst.setFirst(); !inst.end(); ++inst) {
215  if (this->fromExact(cpt[inst]) != 1.0) {
216  for (Idx i = 0; i < inst.nbrDim(); i++) {
217  std::stringstream str;
218  str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
219  str2 << "-" << vartable[str.str()] << " ";
220  }
221 
222  if (this->fromExact(cpt[inst])) {
223  std::stringstream strinst;
224  strinst << bn.variable(node).name();
225  strinst << "_val=" << this->fromExact(cpt[inst]);
226  str2 << protable[strinst.str()];
227  }
228 
229  str2 << " 0\n";
230  clause++;
231  }
232  }
233 
234  clausstr << str2.str();
235  }
236 
237  output << "p cnf " << num + numparam << " " << clause << "\n"
238  << clausstr.str() << std::endl;
239  output.flush();
240  outputvar << strfile.str() << strfile2.str();
241  outputvar.flush();
242  outputvar.close();
243  output.close();
244 
245  if (outputvar.fail()) GUM_ERROR(IOError, "Writting in the ostream failed.");
246 
247  if (output.fail()) GUM_ERROR(IOError, "Writting in the ostream failed.");
248  }
249 
250  // Returns a bloc defining a variable's CPT in the BN format.
251  /* template<typename GUM_SCALAR> INLINE
252  std::string
253  OCNFWriter<GUM_SCALAR>::__variableCPT( const Potential<GUM_SCALAR>& cpt )
254  {
255  std::stringstream str;
256  str << "";
257  return str.str();
258  }
259 
260  // Returns the header of the BN file.
261  template<typename GUM_SCALAR> INLINE
262  std::string
263  OCNFWriter<GUM_SCALAR>::__header( const IBayesNet<GUM_SCALAR>& ) {
264  std::stringstream str;
265  str << "";
266  return str.str();
267  }
268 
269  // Returns a bloc defining a variable in the BN format.
270  template<typename GUM_SCALAR> INLINE
271  std::string
272  OCNFWriter<GUM_SCALAR>::__variableBloc( const DiscreteVariable& var ) {
273  std::stringstream str;
274  str << "" ;
275  return str.str();
276  }*/
277 
278  // Returns the modalities labels of the variables in varsSeq
279 
280 } /* namespace gum */
281 
282 #endif // DOXYGEN_SHOULD_SKIP_THIS
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the BN format.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
FactorisedValuesCNFWriter()
Default constructor.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Definition of classe for BN file output manipulation.
~FactorisedValuesCNFWriter() final
Destructor.
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