21 #ifndef DOXYGEN_SHOULD_SKIP_THIS 32 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
33 INLINE GeneralizedCNFWriter< GUM_SCALAR,
34 IApproximationPolicy >::GeneralizedCNFWriter() {
35 GUM_CONSTRUCTOR(GeneralizedCNFWriter);
39 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
40 INLINE GeneralizedCNFWriter< GUM_SCALAR,
41 IApproximationPolicy >::~GeneralizedCNFWriter() {
42 GUM_DESTRUCTOR(GeneralizedCNFWriter);
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 const auto& var = bn.variable(node);
73 for (
Idx i = 0; i < var.domainSize(); i++) {
74 std::stringstream str;
75 str << var.name() <<
"_" << var.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 protable.
insert(inst.toString(), ++numparam);
90 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
94 for (
auto node : bn.nodes()) {
95 const auto& var = bn.variable(node);
96 std::stringstream str0, str1, str2, str3;
98 for (
Idx i = 0; i < var.domainSize(); i++) {
99 std::stringstream stri;
101 stri << var.name() <<
"_" << var.label(i);
102 str0 << vartable[stri.str()] <<
" ";
104 for (
Idx j = i + 1; j < var.domainSize(); j++) {
105 std::stringstream strj;
106 strj << var.name() <<
"_" << var.label(j);
107 str1 <<
"-" << vartable[stri.str()] <<
" -" << vartable[strj.str()]
115 clausstr << str0.str() << str1.str();
116 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
117 Instantiation inst(cpt);
119 for (inst.setFirst(); !inst.end(); ++inst) {
120 for (
Idx i = 0; i < inst.nbrDim(); i++) {
121 std::stringstream str;
122 str << inst.variable(i).name() <<
"_" << inst.val(inst.variable(i));
123 str2 <<
"-" << vartable[str.str()] <<
" ";
124 str3 <<
"-" << protable[inst.toString()] <<
" " << vartable[str.str()]
129 str2 << protable[inst.toString()] <<
" 0\n";
133 clausstr << str2.str() << str3.str();
136 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" 137 << clausstr.str() << std::endl;
148 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
150 const std::string& filePath,
const IBayesNet< GUM_SCALAR >& bn) {
151 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
152 std::ofstream outputvar((filePath +
".var").c_str(), std::ios_base::trunc);
155 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
157 std::stringstream strfile, strfile2;
159 if (!outputvar.good())
160 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
165 for (
auto node : bn.nodes())
166 numparam += bn.variable(node).domainSize();
169 std::stringstream clausstr;
173 for (
auto node : bn.nodes()) {
174 const auto& var = bn.variable(node);
176 for (
Idx i = 0; i < var.domainSize(); i++) {
177 std::stringstream str;
178 str << var.name() <<
"_" << var.label(i);
179 vartable.
insert(str.str(), ++num);
180 strfile << num <<
"::" << str.str() <<
"\n";
183 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
185 Instantiation inst(cpt);
187 for (inst.setFirst(); !inst.end(); ++inst) {
188 std::stringstream strinst;
189 strinst << inst.toString();
190 strinst <<
"_val=" << this->fromExact(cpt[inst]);
192 protable.
insert(inst.toString(), ++numparam);
193 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
197 for (
auto node : bn.nodes()) {
198 const auto& var = bn.variable(node);
199 std::stringstream str0, str1, str2, str3;
201 for (
Idx i = 0; i < var.domainSize(); i++) {
202 std::stringstream stri;
204 stri << var.name() <<
"_" << var.label(i);
205 str0 << vartable[stri.str()] <<
" ";
207 for (
Idx j = i + 1; j < var.domainSize(); j++) {
208 std::stringstream strj;
209 strj << var.name() <<
"_" << var.label(j);
210 str1 <<
"-" << vartable[stri.str()] <<
" -" << vartable[strj.str()]
218 clausstr << str0.str() << str1.str();
219 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
220 Instantiation inst(cpt);
222 for (inst.setFirst(); !inst.end(); ++inst) {
223 for (
Idx i = 0; i < inst.nbrDim(); i++) {
224 std::stringstream str;
225 str << inst.variable(i).name() <<
"_" << inst.val(inst.variable(i));
226 str2 <<
"-" << vartable[str.str()] <<
" ";
227 str3 <<
"-" << protable[inst.toString()] <<
" " << vartable[str.str()]
232 str2 << protable[inst.toString()] <<
" 0\n";
236 clausstr << str2.str() << str3.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 gum is the global namespace for all aGrUM entities
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.
Definition of classe for BN file output manipulation.
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)