23 #ifndef DOXYGEN_SHOULD_SKIP_THIS 34 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
35 INLINE GeneralizedCNFWriter< GUM_SCALAR,
36 IApproximationPolicy >::GeneralizedCNFWriter() {
37 GUM_CONSTRUCTOR(GeneralizedCNFWriter);
41 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
42 INLINE GeneralizedCNFWriter< GUM_SCALAR,
43 IApproximationPolicy >::~GeneralizedCNFWriter() {
44 GUM_DESTRUCTOR(GeneralizedCNFWriter);
53 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
55 std::ostream& output,
const IBayesNet< GUM_SCALAR >& bn) {
57 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
59 std::stringstream strfile, strfile2;
64 for (
auto node : bn.nodes())
65 numparam += bn.variable(node).domainSize();
68 std::stringstream clausstr;
72 for (
auto node : bn.nodes()) {
73 const auto& var = bn.variable(node);
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";
82 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
84 Instantiation inst(cpt);
86 for (inst.setFirst(); !inst.end(); ++inst) {
87 std::stringstream strinst;
88 strinst << inst.toString();
89 strinst <<
"_val=" << this->fromExact(cpt[inst]);
91 protable.
insert(inst.toString(), ++numparam);
92 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
96 for (
auto node : bn.nodes()) {
97 const auto& var = bn.variable(node);
98 std::stringstream str0, str1, str2, str3;
100 for (
Idx i = 0; i < var.domainSize(); i++) {
101 std::stringstream stri;
103 stri << var.name() <<
"_" << var.label(i);
104 str0 << vartable[stri.str()] <<
" ";
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()]
117 clausstr << str0.str() << str1.str();
118 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
119 Instantiation inst(cpt);
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()]
131 str2 << protable[inst.toString()] <<
" 0\n";
135 clausstr << str2.str() << str3.str();
138 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" 139 << clausstr.str() << std::endl;
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);
157 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
159 std::stringstream strfile, strfile2;
161 if (!outputvar.good())
162 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
167 for (
auto node : bn.nodes())
168 numparam += bn.variable(node).domainSize();
171 std::stringstream clausstr;
175 for (
auto node : bn.nodes()) {
176 const auto& var = bn.variable(node);
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";
185 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
187 Instantiation inst(cpt);
189 for (inst.setFirst(); !inst.end(); ++inst) {
190 std::stringstream strinst;
191 strinst << inst.toString();
192 strinst <<
"_val=" << this->fromExact(cpt[inst]);
194 protable.
insert(inst.toString(), ++numparam);
195 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
199 for (
auto node : bn.nodes()) {
200 const auto& var = bn.variable(node);
201 std::stringstream str0, str1, str2, str3;
203 for (
Idx i = 0; i < var.domainSize(); i++) {
204 std::stringstream stri;
206 stri << var.name() <<
"_" << var.label(i);
207 str0 << vartable[stri.str()] <<
" ";
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()]
220 clausstr << str0.str() << str1.str();
221 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
222 Instantiation inst(cpt);
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()]
234 str2 << protable[inst.toString()] <<
" 0\n";
238 clausstr << str2.str() << str3.str();
241 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" 242 << clausstr.str() << std::endl;
244 outputvar << strfile.str() << strfile2.str();
249 if (outputvar.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
251 if (output.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
286 #endif // DOXYGEN_SHOULD_SKIP_THIS Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
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)