21 #ifndef DOXYGEN_SHOULD_SKIP_THIS 32 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
35 GUM_CONSTRUCTOR(FactorisedValuesCNFWriter);
39 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
42 GUM_DESTRUCTOR(FactorisedValuesCNFWriter);
51 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
53 std::ostream& output,
const IBayesNet< GUM_SCALAR >& bn) {
55 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
57 std::stringstream strfile, strfile2;
62 for (
auto node : bn.nodes())
63 numparam += bn.variable(node).domainSize();
66 std::stringstream clausstr;
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";
78 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
80 Instantiation inst(cpt);
82 for (inst.setFirst(); !inst.end(); ++inst) {
83 std::stringstream strinst;
84 strinst << inst.toString();
85 strinst <<
"_val=" << this->fromExact(cpt[inst]);
87 if (!protable.
exists(strinst.str())) {
88 protable.
insert(inst.toString(), ++numparam);
89 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
94 for (
auto node : bn.nodes()) {
95 std::stringstream str0, str2;
97 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
98 std::stringstream stri;
100 stri << bn.variable(node).name() <<
"_" << bn.variable(node).label(i);
101 str0 << vartable[stri.str()] <<
" ";
106 clausstr << str0.str();
107 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
108 Instantiation inst(cpt);
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()] <<
" ";
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()];
130 clausstr << str2.str();
133 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" 134 << clausstr.str() << std::endl;
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);
152 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
154 std::stringstream strfile, strfile2;
156 if (!outputvar.good())
157 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
162 for (
auto node : bn.nodes())
163 numparam += bn.variable(node).domainSize();
166 std::stringstream clausstr;
170 for (
auto node : bn.nodes()) {
171 const auto& var = bn.variable(node);
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";
180 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
182 Instantiation inst(cpt);
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]);
190 if (!protable.
exists(strinst.str())) {
191 protable.
insert(strinst.str(), ++numparam);
192 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
198 for (
auto node : bn.nodes()) {
199 std::stringstream str0, str2;
201 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
202 std::stringstream stri;
204 stri << bn.variable(node).name() <<
"_" << bn.variable(node).label(i);
205 str0 << vartable[stri.str()] <<
" ";
210 clausstr << str0.str();
211 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
212 Instantiation inst(cpt);
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()] <<
" ";
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()];
234 clausstr << str2.str();
237 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" 238 << clausstr.str() << std::endl;
240 outputvar << strfile.str() << strfile2.str();
245 if (outputvar.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
247 if (output.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
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.
FactorisedValuesCNFWriter()
Default constructor.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
gum is the global namespace for all aGrUM entities
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.
#define GUM_ERROR(type, msg)