45#ifndef DOXYGEN_SHOULD_SKIP_THIS
56 template < GUM_Numeric GUM_SCALAR,
template <
class >
class IApproximationPolicy >
62 template < GUM_Numeric GUM_SCALAR,
template <
class >
class IApproximationPolicy >
73 template < GUM_Numeric 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 const auto& var = bn.variable(node);
95 for (
Idx i = 0; i < var.domainSize(); i++) {
96 auto str = std::format(
"{}_{}", var.name(), var.label(i));
97 vartable.insert(str, ++num);
98 strfile << std::format(
"{}::{}\n", num, str);
105 for (inst.setFirst(); !inst.end(); ++inst) {
106 auto strinst = std::format(
"{}_val={}", inst.toString(), this->fromExact(cpt[inst]));
108 protable.insert(inst.toString(), ++numparam);
109 strfile2 << std::format(
"{}::{}\n", numparam, strinst);
113 for (
auto node: bn.nodes()) {
114 const auto& var = bn.variable(node);
115 std::stringstream str0, str1, str2, str3;
117 for (
Idx i = 0; i < var.domainSize(); i++) {
119 = std::format(
"{}_{}", var.name(), var.label(i));
121 str0 << std::format(
"{} ", vartable[stri]);
123 for (
Idx j = i + 1; j < var.domainSize(); j++) {
124 auto strj = std::format(
"{}_{}", var.name(), var.label(j));
125 str1 << std::format(
"-{} -{} 0\n", vartable[stri], vartable[strj]);
132 clausstr << str0.str() << str1.str();
136 for (inst.setFirst(); !inst.end(); ++inst) {
137 for (
Idx i = 0; i < inst.nbrDim(); i++) {
138 auto str = std::format(
"{}_{}", inst.variable(i).name(), inst.val(inst.variable(i)));
139 str2 << std::format(
"-{} ", vartable[str]);
140 str3 << std::format(
"-{} {} 0\n", protable[inst.toString()], vartable[str]);
144 str2 << std::format(
"{} 0\n", protable[inst.toString()]);
148 clausstr << str2.str() << str3.str();
151 output << std::format(
"p cnf {} {}\n", num + numparam, clause) << clausstr.str() <<
'\n';
162 template < GUM_Numeric GUM_SCALAR,
template <
class >
class IApproximationPolicy >
164 std::string_view filePath,
166 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
167 std::ofstream outputvar(std::string{filePath} +
".var", std::ios_base::trunc);
169 if (!output.good())
GUM_ERROR(IOError,
"Input/Output error : " << filePath <<
" not writable.")
171 std::stringstream strfile, strfile2;
173 if (!outputvar.good())
175 "Input/Output error : " << (std::
string(filePath) +
".var") <<
" not writable.")
180 for (auto node: bn.nodes())
181 numparam += bn.variable(node).domainSize();
184 std::stringstream clausstr;
188 for (auto node: bn.nodes()) {
189 const auto& var = bn.variable(node);
191 for (
Idx i = 0; i < var.domainSize(); i++) {
192 auto str = std::format(
"{}_{}", var.name(), var.label(i));
193 vartable.insert(str, ++num);
194 strfile << std::format(
"{}::{}\n", num, str);
201 for (inst.setFirst(); !inst.end(); ++inst) {
202 auto strinst = std::format(
"{}_val={}", inst.toString(), this->fromExact(cpt[inst]));
204 protable.insert(inst.toString(), ++numparam);
205 strfile2 << std::format(
"{}::{}\n", numparam, strinst);
209 for (
auto node: bn.nodes()) {
210 const auto& var = bn.variable(node);
211 std::stringstream str0, str1, str2, str3;
213 for (
Idx i = 0; i < var.domainSize(); i++) {
215 = std::format(
"{}_{}", var.name(), var.label(i));
217 str0 << std::format(
"{} ", vartable[stri]);
219 for (
Idx j = i + 1; j < var.domainSize(); j++) {
220 auto strj = std::format(
"{}_{}", var.name(), var.label(j));
221 str1 << std::format(
"-{} -{} 0\n", vartable[stri], vartable[strj]);
228 clausstr << str0.str() << str1.str();
232 for (inst.setFirst(); !inst.end(); ++inst) {
233 for (
Idx i = 0; i < inst.nbrDim(); i++) {
234 auto str = std::format(
"{}_{}", inst.variable(i).name(), inst.val(inst.variable(i)));
235 str2 << std::format(
"-{} ", vartable[str]);
236 str3 << std::format(
"-{} {} 0\n", protable[inst.toString()], vartable[str]);
240 str2 << std::format(
"{} 0\n", protable[inst.toString()]);
244 clausstr << str2.str() << str3.str();
247 output << std::format(
"p cnf {} {}\n", num + numparam, clause) << clausstr.str() <<
'\n';
249 outputvar << strfile.str() << strfile2.str();
254 if (outputvar.fail())
GUM_ERROR(IOError,
"Writing in the ostream failed.")
256 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() override
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