23 #ifndef DOXYGEN_SHOULD_SKIP_THIS 34 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
37 GUM_CONSTRUCTOR(FactorisedValuesCNFWriter);
41 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
44 GUM_DESTRUCTOR(FactorisedValuesCNFWriter);
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 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";
80 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
82 Instantiation inst(cpt);
84 for (inst.setFirst(); !inst.end(); ++inst) {
85 std::stringstream strinst;
86 strinst << inst.toString();
87 strinst <<
"_val=" << this->fromExact(cpt[inst]);
89 if (!protable.
exists(strinst.str())) {
90 protable.
insert(inst.toString(), ++numparam);
91 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
96 for (
auto node : bn.nodes()) {
97 std::stringstream str0, str2;
99 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
100 std::stringstream stri;
102 stri << bn.variable(node).name() <<
"_" << bn.variable(node).label(i);
103 str0 << vartable[stri.str()] <<
" ";
108 clausstr << str0.str();
109 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
110 Instantiation inst(cpt);
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()] <<
" ";
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()];
132 clausstr << str2.str();
135 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" 136 << clausstr.str() << std::endl;
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);
154 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
156 std::stringstream strfile, strfile2;
158 if (!outputvar.good())
159 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
164 for (
auto node : bn.nodes())
165 numparam += bn.variable(node).domainSize();
168 std::stringstream clausstr;
172 for (
auto node : bn.nodes()) {
173 const auto& var = bn.variable(node);
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";
182 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
184 Instantiation inst(cpt);
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]);
192 if (!protable.
exists(strinst.str())) {
193 protable.
insert(strinst.str(), ++numparam);
194 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
200 for (
auto node : bn.nodes()) {
201 std::stringstream str0, str2;
203 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
204 std::stringstream stri;
206 stri << bn.variable(node).name() <<
"_" << bn.variable(node).label(i);
207 str0 << vartable[stri.str()] <<
" ";
212 clausstr << str0.str();
213 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
214 Instantiation inst(cpt);
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()] <<
" ";
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()];
236 clausstr << str2.str();
239 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" 240 << clausstr.str() << std::endl;
242 outputvar << strfile.str() << strfile2.str();
247 if (outputvar.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
249 if (output.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
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.
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)