aGrUM  0.16.0
FactorisedValuesCNFWriter_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 >
37  GUM_CONSTRUCTOR(FactorisedValuesCNFWriter);
38  }
39 
40  // Default destructor.
41  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
44  GUM_DESTRUCTOR(FactorisedValuesCNFWriter);
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  Idx num = 0;
62  Idx 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  for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
74  std::stringstream str;
75  str << bn.variable(node).name() << "_" << bn.variable(node).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  if (!protable.exists(strinst.str())) {
90  protable.insert(inst.toString(), ++numparam);
91  strfile2 << numparam << "::" << strinst.str() << "\n";
92  }
93  }
94  }
95 
96  for (auto node : bn.nodes()) {
97  std::stringstream str0, str2;
98 
99  for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
100  std::stringstream stri; //= bn.variable(iter).name()+"_"+
101  // bn.variable(iter).label( i ) ;
102  stri << bn.variable(node).name() << "_" << bn.variable(node).label(i);
103  str0 << vartable[stri.str()] << " ";
104  }
105 
106  str0 << "0\n";
107  clause++;
108  clausstr << str0.str();
109  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
110  Instantiation inst(cpt);
111 
112  for (inst.setFirst(); !inst.end(); ++inst) {
113  if (this->fromExact(cpt[inst]) != 1.0) {
114  for (Idx i = 0; i < inst.nbrDim(); i++) {
115  std::stringstream str;
116  str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
117  str2 << "-" << vartable[str.str()] << " ";
118  }
119 
120  if (this->fromExact(cpt[inst])) {
121  std::stringstream strinst;
122  strinst << bn.variable(node).name();
123  strinst << "_val=" << this->fromExact(cpt[inst]);
124  str2 << protable[strinst.str()];
125  }
126 
127  str2 << " 0\n";
128  clause++;
129  }
130  }
131 
132  clausstr << str2.str();
133  }
134 
135  output << "p cnf " << num + numparam << " " << clause << "\n"
136  << clausstr.str() << std::endl;
137  output.flush();
138  }
139 
140  // Writes a Bayesian Network in the referenced file using the BN format.
141  // If the file doesn't exists, it is created.
142  // If the file exists, it's content will be erased.
143  //
144  // @param filePath The path to the file used to write the Bayesian Network.
145  // @param bn The Bayesian Network writed in the file.
146  // @throws Raised if an I/O error occurs.
147  template < typename GUM_SCALAR, template < class > class IApproximationPolicy >
149  const std::string& filePath, const IBayesNet< GUM_SCALAR >& bn) {
150  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
151  std::ofstream outputvar((filePath + ".var").c_str(), std::ios_base::trunc);
152 
153  if (!output.good())
154  GUM_ERROR(IOError, "Stream states flags are not all unset.");
155 
156  std::stringstream strfile, strfile2;
157 
158  if (!outputvar.good())
159  GUM_ERROR(IOError, "Stream states flags are not all unset.");
160 
161  Idx num = 0;
162  Idx numparam = 0;
163 
164  for (auto node : bn.nodes())
165  numparam += bn.variable(node).domainSize();
166 
167  Idx clause = 0;
168  std::stringstream clausstr;
169  gum::HashTable< std::string, Idx > vartable; // key name::label val num;
171 
172  for (auto node : bn.nodes()) {
173  const auto& var = bn.variable(node);
174 
175  for (Idx i = 0; i < var.domainSize(); i++) {
176  std::stringstream str;
177  str << var.name() << "_" << var.label(i);
178  vartable.insert(str.str(), ++num);
179  strfile << num << "::" << str.str() << "\n";
180  }
181 
182  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
183 
184  Instantiation inst(cpt);
185 
186  for (inst.setFirst(); !inst.end(); ++inst) {
187  if (this->fromExact(cpt[inst]) && this->fromExact(cpt[inst]) != 1.0) {
188  std::stringstream strinst;
189  strinst << var.name();
190  strinst << "_val=" << this->fromExact(cpt[inst]);
191 
192  if (!protable.exists(strinst.str())) {
193  protable.insert(strinst.str(), ++numparam);
194  strfile2 << numparam << "::" << strinst.str() << "\n";
195  }
196  }
197  }
198  }
199 
200  for (auto node : bn.nodes()) {
201  std::stringstream str0, str2;
202 
203  for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
204  std::stringstream stri; //= bn.variable(iter).name()+"_"+
205  // bn.variable(iter).label( i ) ;
206  stri << bn.variable(node).name() << "_" << bn.variable(node).label(i);
207  str0 << vartable[stri.str()] << " ";
208  }
209 
210  str0 << "0\n";
211  clause++;
212  clausstr << str0.str();
213  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
214  Instantiation inst(cpt);
215 
216  for (inst.setFirst(); !inst.end(); ++inst) {
217  if (this->fromExact(cpt[inst]) != 1.0) {
218  for (Idx i = 0; i < inst.nbrDim(); i++) {
219  std::stringstream str;
220  str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
221  str2 << "-" << vartable[str.str()] << " ";
222  }
223 
224  if (this->fromExact(cpt[inst])) {
225  std::stringstream strinst;
226  strinst << bn.variable(node).name();
227  strinst << "_val=" << this->fromExact(cpt[inst]);
228  str2 << protable[strinst.str()];
229  }
230 
231  str2 << " 0\n";
232  clause++;
233  }
234  }
235 
236  clausstr << str2.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> INLINE
254  std::string
255  OCNFWriter<GUM_SCALAR>::__variableCPT( const Potential<GUM_SCALAR>& cpt )
256  {
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  OCNFWriter<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  OCNFWriter<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
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the BN format.
FactorisedValuesCNFWriter()
Default constructor.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
~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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55