43#ifndef DOXYGEN_SHOULD_SKIP_THIS
54 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
60 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
71 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
75 if (!output.good())
GUM_ERROR(IOError,
"Input/Output error : stream not writable.")
77 std::stringstream strfile, strfile2;
82 for (auto node: bn.nodes())
83 numparam += bn.variable(node).domainSize();
86 std::stringstream clausstr;
90 for (auto node: bn.nodes()) {
91 const auto& var = bn.variable(node);
93 for (
Idx i = 0; i < var.domainSize(); i++) {
94 std::stringstream str;
95 str << var.name() <<
"_" << var.label(i);
96 vartable.insert(str.str(), ++num);
97 strfile << num <<
"::" << str.str() <<
"\n";
104 for (inst.setFirst(); !inst.end(); ++inst) {
105 std::stringstream strinst;
106 strinst << inst.toString();
107 strinst <<
"_val=" << this->fromExact(cpt[inst]);
109 protable.insert(inst.toString(), ++numparam);
110 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
114 for (
auto node: bn.nodes()) {
115 const auto& var = bn.variable(node);
116 std::stringstream str0, str1, str2, str3;
118 for (
Idx i = 0; i < var.domainSize(); i++) {
119 std::stringstream stri;
121 stri << var.name() <<
"_" << var.label(i);
122 str0 << vartable[stri.str()] <<
" ";
124 for (
Idx j = i + 1; j < var.domainSize(); j++) {
125 std::stringstream strj;
126 strj << var.name() <<
"_" << var.label(j);
127 str1 <<
"-" << vartable[stri.str()] <<
" -" << vartable[strj.str()] <<
" 0\n";
134 clausstr << str0.str() << str1.str();
138 for (inst.setFirst(); !inst.end(); ++inst) {
139 for (
Idx i = 0; i < inst.nbrDim(); i++) {
140 std::stringstream str;
141 str << inst.variable(i).name() <<
"_" << inst.val(inst.variable(i));
142 str2 <<
"-" << vartable[str.str()] <<
" ";
143 str3 <<
"-" << protable[inst.toString()] <<
" " << vartable[str.str()] <<
" 0\n";
147 str2 << protable[inst.toString()] <<
" 0\n";
151 clausstr << str2.str() << str3.str();
154 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" << clausstr.str() << std::endl;
165 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
167 const std::string& filePath,
169 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
170 std::ofstream outputvar((filePath +
".var").c_str(), std::ios_base::trunc);
172 if (!output.good())
GUM_ERROR(IOError,
"Input/Output error : " << filePath <<
" not writable.")
174 std::stringstream strfile, strfile2;
176 if (!outputvar.good())
177 GUM_ERROR(IOError,
"Input/Output error : " << (filePath +
".var") <<
" not writable.")
182 for (auto node: bn.nodes())
183 numparam += bn.variable(node).domainSize();
186 std::stringstream clausstr;
190 for (auto node: bn.nodes()) {
191 const auto& var = bn.variable(node);
193 for (
Idx i = 0; i < var.domainSize(); i++) {
194 std::stringstream str;
195 str << var.name() <<
"_" << var.label(i);
196 vartable.insert(str.str(), ++num);
197 strfile << num <<
"::" << str.str() <<
"\n";
204 for (inst.setFirst(); !inst.end(); ++inst) {
205 std::stringstream strinst;
206 strinst << inst.toString();
207 strinst <<
"_val=" << this->fromExact(cpt[inst]);
209 protable.insert(inst.toString(), ++numparam);
210 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
214 for (
auto node: bn.nodes()) {
215 const auto& var = bn.variable(node);
216 std::stringstream str0, str1, str2, str3;
218 for (
Idx i = 0; i < var.domainSize(); i++) {
219 std::stringstream stri;
221 stri << var.name() <<
"_" << var.label(i);
222 str0 << vartable[stri.str()] <<
" ";
224 for (
Idx j = i + 1; j < var.domainSize(); j++) {
225 std::stringstream strj;
226 strj << var.name() <<
"_" << var.label(j);
227 str1 <<
"-" << vartable[stri.str()] <<
" -" << vartable[strj.str()] <<
" 0\n";
234 clausstr << str0.str() << str1.str();
238 for (inst.setFirst(); !inst.end(); ++inst) {
239 for (
Idx i = 0; i < inst.nbrDim(); i++) {
240 std::stringstream str;
241 str << inst.variable(i).name() <<
"_" << inst.val(inst.variable(i));
242 str2 <<
"-" << vartable[str.str()] <<
" ";
243 str3 <<
"-" << protable[inst.toString()] <<
" " << vartable[str.str()] <<
" 0\n";
247 str2 << protable[inst.toString()] <<
" 0\n";
251 clausstr << str2.str() << str3.str();
254 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" << clausstr.str() << std::endl;
256 outputvar << strfile.str() << strfile2.str();
261 if (outputvar.fail())
GUM_ERROR(IOError,
"Writing in the ostream failed.")
263 if (output.fail())
GUM_ERROR(IOError,
"Writing in the ostream failed.")
Definition of classe for BN file output manipulation.
<agrum/BN/io/cnf/GeneralizedCNFWriter.h>
GeneralizedCNFWriter()
Default constructor.
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the BN format.
~GeneralizedCNFWriter()
Destructor.
The class for generic Hash Tables.
Class representing the minimal interface for Bayesian network with no numerical data.
Class for assigning/browsing values to tuples of discrete variables.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
gum is the global namespace for all aGrUM entities