aGrUM  0.16.0
GeneralizedCNFWriter_tpl.h
Go to the documentation of this file.
1 
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
24 
25 // to ease parsing in IDE
27 
28 namespace gum {
29 
30  /* =========================================================================*/
31  /* === GUM_BN_WRITER === */
32  /* =========================================================================*/
33  // Default constructor.
34  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
35  INLINE GeneralizedCNFWriter< GUM_SCALAR,
36  IApproximationPolicy >::GeneralizedCNFWriter() {
37  GUM_CONSTRUCTOR(GeneralizedCNFWriter);
38  }
39 
40  // Default destructor.
41  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
42  INLINE GeneralizedCNFWriter< GUM_SCALAR,
43  IApproximationPolicy >::~GeneralizedCNFWriter() {
44  GUM_DESTRUCTOR(GeneralizedCNFWriter);
45  }
46 
47  //
48  // Writes a Bayesian Network in the output stream using the BN format.
49  //
50  // @param ouput The output stream.
51  // @param bn The Bayesian Network writen in output.
52  // @throws Raised if an I/O error occurs.
53  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
55  std::ostream& output, const IBayesNet< GUM_SCALAR >& bn) {
56  if (!output.good())
57  GUM_ERROR(IOError, "Stream states flags are not all unset.");
58 
59  std::stringstream strfile, strfile2;
60 
61  Size num = 0;
62  Size numparam = 0;
63 
64  for (auto node : bn.nodes())
65  numparam += bn.variable(node).domainSize();
66 
67  Idx clause = 0;
68  std::stringstream clausstr;
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++) {
76  std::stringstream str;
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 
84  Instantiation inst(cpt);
85 
86  for (inst.setFirst(); !inst.end(); ++inst) {
87  std::stringstream strinst;
88  strinst << inst.toString();
89  strinst << "_val=" << this->fromExact(cpt[inst]);
90 
91  protable.insert(inst.toString(), ++numparam);
92  strfile2 << numparam << "::" << strinst.str() << "\n";
93  }
94  }
95 
96  for (auto node : bn.nodes()) {
97  const auto& var = bn.variable(node);
98  std::stringstream str0, str1, str2, str3;
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++) {
107  std::stringstream strj;
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);
119  Instantiation inst(cpt);
120 
121  for (inst.setFirst(); !inst.end(); ++inst) {
122  for (Idx i = 0; i < inst.nbrDim(); i++) {
123  std::stringstream str;
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, const IBayesNet< GUM_SCALAR >& bn) {
153  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
154  std::ofstream outputvar((filePath + ".var").c_str(), std::ios_base::trunc);
155 
156  if (!output.good())
157  GUM_ERROR(IOError, "Stream states flags are not all unset.");
158 
159  std::stringstream strfile, strfile2;
160 
161  if (!outputvar.good())
162  GUM_ERROR(IOError, "Stream states flags are not all unset.");
163 
164  Idx num = 0;
165  Idx numparam = 0;
166 
167  for (auto node : bn.nodes())
168  numparam += bn.variable(node).domainSize();
169 
170  Idx clause = 0;
171  std::stringstream clausstr;
172  gum::HashTable< std::string, Idx > vartable; // key name::label val num;
174 
175  for (auto node : bn.nodes()) {
176  const auto& var = bn.variable(node);
177 
178  for (Idx i = 0; i < var.domainSize(); i++) {
179  std::stringstream str;
180  str << var.name() << "_" << var.label(i);
181  vartable.insert(str.str(), ++num);
182  strfile << num << "::" << str.str() << "\n";
183  }
184 
185  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
186 
187  Instantiation inst(cpt);
188 
189  for (inst.setFirst(); !inst.end(); ++inst) {
190  std::stringstream strinst;
191  strinst << inst.toString();
192  strinst << "_val=" << this->fromExact(cpt[inst]);
193 
194  protable.insert(inst.toString(), ++numparam);
195  strfile2 << numparam << "::" << strinst.str() << "\n";
196  }
197  }
198 
199  for (auto node : bn.nodes()) {
200  const auto& var = bn.variable(node);
201  std::stringstream str0, str1, str2, str3;
202 
203  for (Idx i = 0; i < var.domainSize(); i++) {
204  std::stringstream stri; //= bn.variable(iter).name()+"_"+
205  // bn.variable(iter).label( i ) ;
206  stri << var.name() << "_" << var.label(i);
207  str0 << vartable[stri.str()] << " ";
208 
209  for (Idx j = i + 1; j < var.domainSize(); j++) {
210  std::stringstream strj;
211  strj << var.name() << "_" << var.label(j);
212  str1 << "-" << vartable[stri.str()] << " -" << vartable[strj.str()]
213  << " 0\n";
214  clause++;
215  }
216  }
217 
218  str0 << "0\n";
219  clause++;
220  clausstr << str0.str() << str1.str();
221  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
222  Instantiation inst(cpt);
223 
224  for (inst.setFirst(); !inst.end(); ++inst) {
225  for (Idx i = 0; i < inst.nbrDim(); i++) {
226  std::stringstream str;
227  str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
228  str2 << "-" << vartable[str.str()] << " ";
229  str3 << "-" << protable[inst.toString()] << " " << vartable[str.str()]
230  << " 0\n";
231  clause++;
232  }
233 
234  str2 << protable[inst.toString()] << " 0\n";
235  clause++;
236  }
237 
238  clausstr << str2.str() << str3.str();
239  }
240 
241  output << "p cnf " << num + numparam << " " << clause << "\n"
242  << clausstr.str() << std::endl;
243  output.flush();
244  outputvar << strfile.str() << strfile2.str();
245  outputvar.flush();
246  outputvar.close();
247  output.close();
248 
249  if (outputvar.fail()) GUM_ERROR(IOError, "Writting in the ostream failed.");
250 
251  if (output.fail()) GUM_ERROR(IOError, "Writting in the ostream failed.");
252  }
253 
254  // Returns a bloc defining a variable's CPT in the BN format.
255  /*template<typename GUM_SCALAR, template<class> class IApproximationPolicy >
256  INLINE
257  std::string
258  CNFWriter<GUM_SCALAR>::__variableCPT( const Potential<GUM_SCALAR>& cpt ) {
259  std::stringstream str;
260  str << "";
261  return str.str();
262  }
263 
264  // Returns the header of the BN file.
265  template<typename GUM_SCALAR,> INLINE
266  std::string
267  CNFWriter<GUM_SCALAR>::__header( const IBayesNet<GUM_SCALAR>& ) {
268  std::stringstream str;
269  str << "";
270  return str.str();
271  }
272 
273  // Returns a bloc defining a variable in the BN format.
274  template<typename GUM_SCALAR> INLINE
275  std::string
276  CNFWriter<GUM_SCALAR>::__variableBloc( const DiscreteVariable& var ) {
277  std::stringstream str;
278  str << "" ;
279  return str.str();
280  }*/
281 
282  // Returns the modalities labels of the variables in varsSeq
283 
284 } /* namespace gum */
285 
286 #endif // DOXYGEN_SHOULD_SKIP_THIS
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55