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 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
94 auto str = std::format(
"{}_{}", bn.variable(node).name(), bn.variable(node).label(i));
95 vartable.insert(str, ++num);
96 strfile << std::format(
"{}::{}\n", num, str);
103 for (inst.setFirst(); !inst.end(); ++inst) {
104 auto strinst = std::format(
"{}_val={}", inst.toString(), this->fromExact(cpt[inst]));
106 if (!protable.exists(strinst)) {
107 protable.insert(inst.toString(), ++numparam);
108 strfile2 << std::format(
"{}::{}\n", numparam, strinst);
113 for (
auto node: bn.nodes()) {
114 std::stringstream str0, str2;
116 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
117 auto stri = std::format(
"{}_{}",
118 bn.variable(node).name(),
119 bn.variable(node).label(i));
121 str0 << std::format(
"{} ", vartable[stri]);
126 clausstr << str0.str();
130 for (inst.setFirst(); !inst.end(); ++inst) {
131 if (this->fromExact(cpt[inst]) != 1.0) {
132 for (
Idx i = 0; i < inst.nbrDim(); i++) {
133 auto str = std::format(
"{}_{}", inst.variable(i).name(), inst.val(inst.variable(i)));
134 str2 << std::format(
"-{} ", vartable[str]);
137 if (this->fromExact(cpt[inst])) {
139 = std::format(
"{}_val={}", bn.variable(node).name(), this->fromExact(cpt[inst]));
140 str2 << protable[strinst];
148 clausstr << str2.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 if (this->fromExact(cpt[inst]) && this->fromExact(cpt[inst]) != 1.0) {
203 auto strinst = std::format(
"{}_val={}", var.name(), this->fromExact(cpt[inst]));
205 if (!protable.exists(strinst)) {
206 protable.insert(strinst, ++numparam);
207 strfile2 << std::format(
"{}::{}\n", numparam, strinst);
213 for (
auto node: bn.nodes()) {
214 std::stringstream str0, str2;
216 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
217 auto stri = std::format(
"{}_{}",
218 bn.variable(node).name(),
219 bn.variable(node).label(i));
221 str0 << std::format(
"{} ", vartable[stri]);
226 clausstr << str0.str();
230 for (inst.setFirst(); !inst.end(); ++inst) {
231 if (this->fromExact(cpt[inst]) != 1.0) {
232 for (
Idx i = 0; i < inst.nbrDim(); i++) {
233 auto str = std::format(
"{}_{}", inst.variable(i).name(), inst.val(inst.variable(i)));
234 str2 << std::format(
"-{} ", vartable[str]);
237 if (this->fromExact(cpt[inst])) {
239 = std::format(
"{}_val={}", bn.variable(node).name(), this->fromExact(cpt[inst]));
240 str2 << protable[strinst];
248 clausstr << str2.str();
251 output << std::format(
"p cnf {} {}\n", num + numparam, clause) << clausstr.str() <<
'\n';
253 outputvar << strfile.str() << strfile2.str();
258 if (outputvar.fail())
GUM_ERROR(IOError,
"Writing in the ostream failed.")
260 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() override
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