43#ifndef DOXYGEN_SHOULD_SKIP_THIS
54 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
61 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
73 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
77 if (!output.good())
GUM_ERROR(IOError,
"Input/Output error : stream not writable.")
79 std::stringstream strfile, strfile2;
84 for (auto node: bn.nodes())
85 numparam += bn.variable(node).domainSize();
88 std::stringstream clausstr;
92 for (auto node: bn.nodes()) {
93 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
94 std::stringstream str;
95 str << bn.variable(node).name() <<
"_" << bn.variable(node).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 if (!protable.exists(strinst.str())) {
110 protable.insert(inst.toString(), ++numparam);
111 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
116 for (
auto node: bn.nodes()) {
117 std::stringstream str0, str2;
119 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
120 std::stringstream stri;
122 stri << bn.variable(node).name() <<
"_" << bn.variable(node).label(i);
123 str0 << vartable[stri.str()] <<
" ";
128 clausstr << str0.str();
132 for (inst.setFirst(); !inst.end(); ++inst) {
133 if (this->fromExact(cpt[inst]) != 1.0) {
134 for (
Idx i = 0; i < inst.nbrDim(); i++) {
135 std::stringstream str;
136 str << inst.variable(i).name() <<
"_" << inst.val(inst.variable(i));
137 str2 <<
"-" << vartable[str.str()] <<
" ";
140 if (this->fromExact(cpt[inst])) {
141 std::stringstream strinst;
142 strinst << bn.variable(node).name();
143 strinst <<
"_val=" << this->fromExact(cpt[inst]);
144 str2 << protable[strinst.str()];
152 clausstr << str2.str();
155 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" << clausstr.str() << std::endl;
166 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
168 const std::string& filePath,
170 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
171 std::ofstream outputvar((filePath +
".var").c_str(), std::ios_base::trunc);
173 if (!output.good())
GUM_ERROR(IOError,
"Input/Output error : " << filePath <<
" not writable.")
175 std::stringstream strfile, strfile2;
177 if (!outputvar.good())
178 GUM_ERROR(IOError,
"Input/Output error : " << (filePath +
".var") <<
" not writable.")
183 for (auto node: bn.nodes())
184 numparam += bn.variable(node).domainSize();
187 std::stringstream clausstr;
191 for (auto node: bn.nodes()) {
192 const auto& var = bn.variable(node);
194 for (
Idx i = 0; i < var.domainSize(); i++) {
195 std::stringstream str;
196 str << var.name() <<
"_" << var.label(i);
197 vartable.insert(str.str(), ++num);
198 strfile << num <<
"::" << str.str() <<
"\n";
205 for (inst.setFirst(); !inst.end(); ++inst) {
206 if (this->fromExact(cpt[inst]) && this->fromExact(cpt[inst]) != 1.0) {
207 std::stringstream strinst;
208 strinst << var.name();
209 strinst <<
"_val=" << this->fromExact(cpt[inst]);
211 if (!protable.exists(strinst.str())) {
212 protable.insert(strinst.str(), ++numparam);
213 strfile2 << numparam <<
"::" << strinst.str() <<
"\n";
219 for (
auto node: bn.nodes()) {
220 std::stringstream str0, str2;
222 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
223 std::stringstream stri;
225 stri << bn.variable(node).name() <<
"_" << bn.variable(node).label(i);
226 str0 << vartable[stri.str()] <<
" ";
231 clausstr << str0.str();
235 for (inst.setFirst(); !inst.end(); ++inst) {
236 if (this->fromExact(cpt[inst]) != 1.0) {
237 for (
Idx i = 0; i < inst.nbrDim(); i++) {
238 std::stringstream str;
239 str << inst.variable(i).name() <<
"_" << inst.val(inst.variable(i));
240 str2 <<
"-" << vartable[str.str()] <<
" ";
243 if (this->fromExact(cpt[inst])) {
244 std::stringstream strinst;
245 strinst << bn.variable(node).name();
246 strinst <<
"_val=" << this->fromExact(cpt[inst]);
247 str2 << protable[strinst.str()];
255 clausstr << str2.str();
258 output <<
"p cnf " << num + numparam <<
" " << clause <<
"\n" << clausstr.str() << std::endl;
260 outputvar << strfile.str() << strfile2.str();
265 if (outputvar.fail())
GUM_ERROR(IOError,
"Writing in the ostream failed.")
267 if (output.fail())
GUM_ERROR(IOError,
"Writing in the ostream failed.")
Definition of classe for BN file output manipulation.
<agrum/BN/io/cnf/FactorisedValuesCNFWriter.h>
~FactorisedValuesCNFWriter()
Destructor.
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the BN format.
FactorisedValuesCNFWriter()
Default constructor.
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)
Size Idx
Type for indexes.
gum is the global namespace for all aGrUM entities