aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
FactorisedValuesCNFWriter_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/FactorisedValuesCNFWriter.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 FactorisedValuesCNFWriter< GUM_SCALAR, IApproximationPolicy >::
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 output 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  Idx num = 0;
62  Idx 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  for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
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 
83 
84  for (inst.setFirst(); !inst.end(); ++inst) {
86  strinst << inst.toString();
87  strinst << "_val=" << this->fromExact(cpt[inst]);
88 
89  if (!protable.exists(strinst.str())) {
91  strfile2 << numparam << "::" << strinst.str() << "\n";
92  }
93  }
94  }
95 
96  for (auto node: bn.nodes()) {
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);
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++) {
116  str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
117  str2 << "-" << vartable[str.str()] << " ";
118  }
119 
120  if (this->fromExact(cpt[inst])) {
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,
150  const IBayesNet< GUM_SCALAR >& bn) {
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 
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())
167 
168  Idx clause = 0;
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++) {
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 
186 
187  for (inst.setFirst(); !inst.end(); ++inst) {
188  if (this->fromExact(cpt[inst]) && this->fromExact(cpt[inst]) != 1.0) {
190  strinst << var.name();
191  strinst << "_val=" << this->fromExact(cpt[inst]);
192 
193  if (!protable.exists(strinst.str())) {
195  strfile2 << numparam << "::" << strinst.str() << "\n";
196  }
197  }
198  }
199  }
200 
201  for (auto node: bn.nodes()) {
203 
204  for (Idx i = 0; i < bn.variable(node).domainSize(); i++) {
205  std::stringstream stri; //= bn.variable(iter).name()+"_"+
206  // bn.variable(iter).label( i ) ;
207  stri << bn.variable(node).name() << "_" << bn.variable(node).label(i);
208  str0 << vartable[stri.str()] << " ";
209  }
210 
211  str0 << "0\n";
212  clause++;
213  clausstr << str0.str();
214  const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
216 
217  for (inst.setFirst(); !inst.end(); ++inst) {
218  if (this->fromExact(cpt[inst]) != 1.0) {
219  for (Idx i = 0; i < inst.nbrDim(); i++) {
221  str << inst.variable(i).name() << "_" << inst.val(inst.variable(i));
222  str2 << "-" << vartable[str.str()] << " ";
223  }
224 
225  if (this->fromExact(cpt[inst])) {
227  strinst << bn.variable(node).name();
228  strinst << "_val=" << this->fromExact(cpt[inst]);
229  str2 << protable[strinst.str()];
230  }
231 
232  str2 << " 0\n";
233  clause++;
234  }
235  }
236 
237  clausstr << str2.str();
238  }
239 
240  output << "p cnf " << num + numparam << " " << clause << "\n"
241  << clausstr.str() << std::endl;
242  output.flush();
243  outputvar << strfile.str() << strfile2.str();
244  outputvar.flush();
245  outputvar.close();
246  output.close();
247 
248  if (outputvar.fail()) GUM_ERROR(IOError, "Writting in the ostream failed.");
249 
250  if (output.fail()) GUM_ERROR(IOError, "Writting in the ostream failed.");
251  }
252 
253  // Returns a bloc defining a variable's CPT in the BN format.
254  /* template<typename GUM_SCALAR> INLINE
255  std::string
256  OCNFWriter<GUM_SCALAR>::variableCPT__( const Potential<GUM_SCALAR>& cpt )
257  {
258  std::stringstream str;
259  str << "";
260  return str.str();
261  }
262 
263  // Returns the header of the BN file.
264  template<typename GUM_SCALAR> INLINE
265  std::string
266  OCNFWriter<GUM_SCALAR>::header__( const IBayesNet<GUM_SCALAR>& ) {
267  std::stringstream str;
268  str << "";
269  return str.str();
270  }
271 
272  // Returns a bloc defining a variable in the BN format.
273  template<typename GUM_SCALAR> INLINE
274  std::string
275  OCNFWriter<GUM_SCALAR>::variableBloc__( const DiscreteVariable& var ) {
276  std::stringstream str;
277  str << "" ;
278  return str.str();
279  }*/
280 
281  // Returns the modalities labels of the variables in varsSeq
282 
283 } /* namespace gum */
284 
285 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669