43#ifndef DOXYGEN_SHOULD_SKIP_THIS
53 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
56 IApproximationPolicy >::ContextualDependenciesCNFWriter() {
61 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
64 IApproximationPolicy >::~ContextualDependenciesCNFWriter() {
74 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
80 for (
auto node: bn.topologicalOrder())
81 Order.
add(bn.variable(node));
83 if (!output.good())
GUM_ERROR(IOError,
"Input/Output error : stream not writable.")
85 std::stringstream strfile;
86 std::stringstream strfile2;
91 std::stringstream clausstr;
92 std::stringstream clausstr2;
100 for (auto node: bn.nodes()) {
101 std::stringstream str0;
104 for (
Idx i = 0; i < var->domainSize(); i++) {
105 std::stringstream stri;
106 stri << var->name() <<
"_" << var->label(i);
107 vartable.insert(stri.str(), ++num);
108 strfile << num <<
"::" << stri.str() <<
"\n";
109 str0 << vartable[stri.str()] <<
" ";
115 clausstr2 << str0.str();
122 new gum::HashTable< std::string,
123 gum::Sequence< gum::Sequence< gum::Instantiation* >* >* >());
125 for (inst.setFirst(); !inst.end(); ++inst) {
126 if (this->fromExact(cpt[inst]) != 1) {
127 std::stringstream strk;
128 strk << this->fromExact(cpt[inst]);
129 std::string valp = strk.str();
131 if (!(cptparamval[var])->exists(valp)) {
133 ->insert(valp,
new gum::Sequence< gum::Sequence< gum::Instantiation* >* >());
135 (*(cptparamval[var]))[valp]->insert(
new gum::Sequence< gum::Instantiation* >);
137 if (this->fromExact(cpt[inst])) {
138 std::stringstream strinst;
139 strinst << var->name();
140 strinst <<
"_val=" << this->fromExact(cpt[inst]);
142 if (!protable.exists(strinst.str())) {
143 protable.insert(strinst.str(), ++num);
144 strfile << num <<
"::" << strinst.str() <<
"\n";
149 (*(cptparamval[var]))[valp]->front()->insert(
new gum::Instantiation(inst));
154 std::stringstream str2;
156 while (!cptparamval.empty()) {
157 auto itvar = cptparamval.begin();
159 while (!(itvar.val())->empty()) {
160 auto itpvall = (itvar.val())->begin();
162 for (
auto pv: *itpvall.val()) {
164 gum::HashTable< std::string,
165 gum::HashTable<
const gum::DiscreteVariable*,
166 std::pair< gum::Set< Idx >*,
167 gum::Set< Idx >* >* >* >
170 for (
const auto* seqv: *pv) {
171 if (seqv->nbrDim() > 1) {
172 for (
Idx iInst = 0; iInst < seqv->nbrDim(); iInst++) {
173 gum::Instantiation instpro(*seqv,
false);
174 instpro.reorder(Order);
175 const gum::DiscreteVariable* var = &(seqv->variable(iInst));
179 if (!orderStruct.exists(instpro.toString())) {
182 new gum::HashTable<
const gum::DiscreteVariable*,
183 std::pair< gum::Set< Idx >*, gum::Set< Idx >* >* >());
186 if (!orderStruct[instpro.toString()]->exists(var)) {
187 orderStruct[instpro.toString()]->insert(
189 new std::pair< gum::Set< Idx >*, gum::Set< Idx >* >(
192 seqv->variable(iInst).domainSize())));
195 gum::HashTable<
const gum::DiscreteVariable*,
196 std::pair< gum::Set< Idx >*, gum::Set< Idx >* >* >* orderStruct2
197 = orderStruct[instpro.toString()];
198 (*orderStruct2)[var]->first->insert(linecount);
199 (*orderStruct2)[var]->second->insert(seqv->val(iInst));
206 gum::Set< gum::Idx > elimination;
207 gum::Sequence< gum::Instantiation* >* newSeq =
nullptr;
209 for (
const auto& [first, second]: orderStruct) {
212 for (
const auto& [first2, second2]: *second) {
213 if (second2->second->size() == first2->domainSize()) {
214 if (!newSeq) newSeq =
new gum::Sequence< gum::Instantiation* >();
219 new gum::Instantiation(*((*pv)[(*(second2->first->begin()))]),
false));
220 newSeq->
back()->erase(*first2);
223 elimination = elimination + *(second2->first);
229 (itpvall.val())->insert(newSeq);
231 for (
Idx itelem = pv->size(); itelem > 0; itelem--) {
232 if (elimination.
exists(itelem - 1)) {
233 delete ((*pv)[itelem - 1]);
234 pv->erase((*pv)[itelem - 1]);
239 while (!orderStruct.empty()) {
240 while (!(orderStruct.begin().val())->empty()) {
241 delete orderStruct.begin().val()->begin().val()->first;
242 delete orderStruct.begin().val()->begin().val()->second;
243 (orderStruct.begin().val())
244 ->erase((orderStruct.begin().val())->beginSafe());
247 delete orderStruct.begin().val();
248 orderStruct.erase(orderStruct.beginSafe());
252 while (!(itpvall.val())->empty()) {
253 auto itpv = (itpvall.val())->begin();
255 while (!(*itpv)->empty()) {
256 auto itseqv = (*itpv)->begin();
258 for (
Idx i = 0; i < (*itseqv)->nbrDim(); i++) {
259 std::stringstream str;
260 str << (*itseqv)->variable(i).name() <<
"_" << (*itseqv)->val((*itseqv)->variable(i));
261 str2 <<
"-" << vartable[str.str()] <<
" ";
264 if (itpvall.key() !=
"0" && itpvall.key() !=
"0.0") {
265 std::stringstream strinst;
266 strinst << itvar.key()->name();
267 strinst <<
"_val=" << itpvall.key();
268 str2 << protable[strinst.str()];
274 (*itpv)->erase(itseqv);
278 (itpvall.val())->erase(itpv);
281 delete (itpvall.val());
282 (itvar.val())->erase(itvar.val()->beginSafe());
285 delete (itvar.val());
286 cptparamval.erase(cptparamval.beginSafe());
289 clausstr << str2.str();
291 output <<
"p cnf " << num <<
" " << clause <<
"\neclauses " << numvar <<
"\n"
292 << clausstr.str() << clausstr2.str() << std::endl;
303 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
305 const std::string& filePath,
307 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
308 std::ofstream outputvar((filePath +
".var").c_str(), std::ios_base::trunc);
310 if (!output.good())
GUM_ERROR(IOError,
"Input/Output error : " << filePath <<
" not writable.")
312 std::stringstream strfile, strfile2;
314 if (!outputvar.good())
315 GUM_ERROR(IOError,
"Input/Output error : " << (filePath +
".var") <<
" not writable.")
320 std::stringstream clausstr, clausstr2;
330 for (auto node: bn.topologicalOrder())
331 Order.add(bn.variable(node));
333 for (auto node: bn.nodes()) {
334 std::stringstream str0;
337 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
338 std::stringstream stri;
339 stri << var->name() <<
"_" << var->label(i);
340 vartable.insert(stri.str(), ++num);
341 strfile << num <<
"::" << stri.str() <<
"\n";
342 str0 << vartable[stri.str()] <<
" ";
348 clausstr2 << str0.str();
355 new gum::HashTable< std::string,
356 gum::Sequence< gum::Sequence< gum::Instantiation* >* >* >());
358 for (inst.setFirst(); !inst.end(); ++inst) {
359 if (this->fromExact(cpt[inst]) != 1) {
360 std::stringstream strk;
361 strk << this->fromExact(cpt[inst]);
362 std::string valp = strk.str();
364 if (!(cptparamval[var])->exists(valp)) {
368 new gum::Sequence< gum::Sequence< gum::Instantiation* >* >());
374 (*(cptparamval[var]))[valp]->insert(
new gum::Sequence< gum::Instantiation* >);
376 if (this->fromExact(cpt[inst])) {
377 std::stringstream strinst;
378 strinst << var->name();
379 strinst <<
"_val=" << this->fromExact(cpt[inst]);
381 if (!protable.exists(strinst.str())) {
382 protable.insert(strinst.str(), ++num);
383 strfile << num <<
"::" << strinst.str() <<
"\n";
388 (*(cptparamval[var]))[valp]->front()->insert(
new gum::Instantiation(inst));
393 std::stringstream str2;
395 while (!cptparamval.empty()) {
396 auto itvar = cptparamval.begin();
398 while (!(itvar.val())->empty()) {
399 auto itpvall = (itvar.val())->begin();
401 for (
auto pv: *itpvall.val()) {
403 gum::HashTable< std::string,
404 gum::HashTable<
const gum::DiscreteVariable*,
405 std::pair< gum::Set< Idx >*,
406 gum::Set< Idx >* >* >* >
409 gum::Set< gum::Idx > elimination;
410 gum::HashTable< std::string, gum::Instantiation* > newSeqpre;
412 for (
const auto* seqv: *pv) {
413 if (seqv->nbrDim() > 1) {
414 for (
Idx iInst = 0; iInst < seqv->nbrDim(); iInst++) {
415 auto instpro =
new gum::Instantiation(*seqv,
false);
416 const gum::DiscreteVariable* var = &(seqv->variable(iInst));
417 instpro->erase(*var);
418 instpro->reorder(Order);
420 if (!orderStruct.exists(instpro->toString())
421 && !newSeqpre.
exists(instpro->toString())) {
424 new gum::HashTable<
const gum::DiscreteVariable*,
425 std::pair< gum::Set< Idx >*, gum::Set< Idx >* >* >());
428 if (orderStruct.exists(instpro->toString())
429 && !orderStruct[instpro->toString()]->exists(var)) {
430 orderStruct[instpro->toString()]->insert(
432 new std::pair< gum::Set< Idx >*, gum::Set< Idx >* >(
435 seqv->variable(iInst).domainSize())));
438 if (orderStruct.exists(instpro->toString())
439 && !newSeqpre.
exists(instpro->toString())) {
440 gum::HashTable<
const gum::DiscreteVariable*,
441 std::pair< gum::Set< Idx >*, gum::Set< Idx >* >* >* orderStruct2
442 = orderStruct[instpro->toString()];
443 (*orderStruct2)[var]->first->insert(linecount);
444 (*orderStruct2)[var]->second->insert(seqv->val(iInst));
446 if ((*orderStruct2)[var]->second->size() == var->
domainSize()) {
447 newSeqpre.
insert(instpro->toString(), instpro);
449 for (
const auto& elt: *orderStruct2) {
450 elimination = elimination + *(elt.second->first);
451 delete (elt.second->first);
452 delete (elt.second->second);
456 orderStruct2->clear();
459 orderStruct.erase(instpro->toString());
460 }
else delete instpro;
461 }
else if (newSeqpre.
exists(instpro->toString())) {
462 elimination.
insert(linecount);
471 gum::Sequence< gum::Instantiation* >* newSeq =
nullptr;
473 if (!newSeqpre.
empty()) {
474 newSeq =
new gum::Sequence< gum::Instantiation* >();
476 for (
auto& elt: newSeqpre)
477 newSeq->
insert(elt.second);
479 (itpvall.val())->insert(newSeq);
481 for (
Idx itelem = pv->size(); itelem > 0; itelem--) {
482 if (elimination.
exists(itelem - 1)) {
483 delete ((*pv)[itelem - 1]);
484 pv->erase((*pv)[itelem - 1]);
489 while (!orderStruct.empty()) {
490 while (!(orderStruct.begin().val())->empty()) {
491 delete orderStruct.begin().val()->begin().val()->first;
492 delete orderStruct.begin().val()->begin().val()->second;
493 (orderStruct.begin().val())->erase(orderStruct.begin().val()->beginSafe());
498 delete orderStruct.begin().val();
499 orderStruct.erase(orderStruct.beginSafe());
503 while (!(itpvall.val())->empty()) {
504 gum::Sequence< gum::Sequence< gum::Instantiation* >* >::iterator_safe itpv
505 = (itpvall.val())->begin();
507 while (!(*itpv)->empty()) {
510 for (
Idx i = 0; i < (*itseqv)->nbrDim(); i++) {
511 std::stringstream str;
512 str << (*itseqv)->variable(i).name() <<
"_" << (*itseqv)->val((*itseqv)->variable(i));
513 str2 <<
"-" << vartable[str.str()] <<
" ";
518 if (itpvall.key() !=
"0" && itpvall.key() !=
"0.0") {
519 std::stringstream strinst;
520 strinst << itvar.key()->name();
521 strinst <<
"_val=" << itpvall.key();
522 str2 << protable[strinst.str()];
528 (*itpv)->erase((*itpv)->beginSafe());
532 (itpvall.val())->erase(itpvall.val()->beginSafe());
535 delete (itpvall.val());
536 (itvar.val())->erase(itvar.val()->beginSafe());
539 delete (itvar.val());
540 cptparamval.erase(cptparamval.beginSafe());
543 clausstr << str2.str();
545 output <<
"p cnf " << num <<
" " << clause <<
"\neclauses " << numvar <<
"\n"
546 << clausstr.str() << clausstr2.str() << std::endl;
548 outputvar << strfile.str();
554 if (outputvar.fail())
GUM_ERROR(IOError,
"Writing in the ostream failed.")
556 if (output.fail())
GUM_ERROR(IOError,
"Writing in the ostream failed.")
Definition of classe for BN file output manipulation.
<agrum/BN/io/cnf/ContextualDependenciesCNFWriter.h>
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the BN format.
Base class for discrete random variable.
virtual Size domainSize() const =0
The class for generic Hash Tables.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
bool empty() const noexcept
Indicates whether the hash table is empty.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Class representing the minimal interface for Bayesian network with no numerical data.
Class for assigning/browsing values to tuples of discrete variables.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
const Key & back() const
Returns the last element of the sequence.
void insert(const Key &k)
Insert an element at the end of the sequence.
The generic class for storing (ordered) sequences of objects.
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
void insert(const Key &k)
Inserts a new element into the set.
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