21 #ifndef DOXYGEN_SHOULD_SKIP_THIS 31 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
34 GUM_CONSTRUCTOR(ContextualDependenciesCNFWriter);
38 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
41 GUM_DESTRUCTOR(ContextualDependenciesCNFWriter);
50 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
53 std::ostream& output,
const IBayesNet< GUM_SCALAR >& bn) {
56 for (
auto node : bn.topologicalOrder())
57 Order.add(bn.variable(node));
60 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
62 std::stringstream strfile, strfile2;
67 std::stringstream clausstr, clausstr2;
76 for (
auto node : bn.nodes()) {
77 std::stringstream str0;
78 const DiscreteVariable* var = &bn.variable(node);
80 for (
Idx i = 0; i < var->domainSize(); i++) {
81 std::stringstream stri;
82 stri << var->name() <<
"_" << var->label(i);
83 vartable.
insert(stri.str(), ++num);
84 strfile << num <<
"::" << stri.str() <<
"\n";
85 str0 << vartable[stri.str()] <<
" ";
91 clausstr2 << str0.str();
92 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
93 Instantiation inst(cpt);
102 for (inst.setFirst(); !inst.end(); ++inst) {
103 if (this->fromExact(cpt[inst]) != 1) {
104 std::stringstream strk;
105 strk << this->fromExact(cpt[inst]);
106 std::string valp = strk.str();
108 if (!(cptparamval[var])->exists(valp)) {
114 (*(cptparamval[var]))[valp]->insert(
117 if (this->fromExact(cpt[inst])) {
118 std::stringstream strinst;
119 strinst << var->name();
120 strinst <<
"_val=" << this->fromExact(cpt[inst]);
122 if (!protable.
exists(strinst.str())) {
123 protable.
insert(strinst.str(), ++num);
124 strfile << num <<
"::" << strinst.str() <<
"\n";
129 (*(cptparamval[var]))[valp]->front()->insert(
135 std::stringstream str2;
137 while (!cptparamval.empty()) {
139 const DiscreteVariable*,
143 itvar = cptparamval.
begin();
145 while (!(itvar.val())->empty()) {
148 iterator itpvall = (itvar.val())->begin();
150 for (
auto pv : *itpvall.val()) {
154 std::pair< gum::Set< Idx >*,
158 for (
auto seqv : *pv) {
159 if (seqv->nbrDim() > 1) {
160 for (
Idx iInst = 0; iInst < seqv->nbrDim(); iInst++) {
162 instpro.reorder(Order);
163 const gum::DiscreteVariable* var = &(seqv->variable(iInst));
167 if (!orderStruct.exists(instpro.toString())) {
171 const gum::DiscreteVariable*,
175 if (!orderStruct[instpro.toString()]->exists(var)) {
176 orderStruct[instpro.toString()]->insert(
181 seqv->variable(iInst)
187 orderStruct2 = orderStruct[instpro.toString()];
188 (*orderStruct2)[var]->first->
insert(linecount);
189 (*orderStruct2)[var]->second->insert(seqv->val(iInst));
199 for (
auto& elt : orderStruct) {
202 for (
auto& elt2 : *elt.second) {
203 if (elt2.second->second->size() == elt2.first->domainSize()) {
209 *((*pv)[(*(elt2.second->first->begin()))]),
false));
213 elimination = elimination + *(elt2.second->first);
219 (itpvall.val())->insert(newSeq);
221 for (
Idx itelem = pv->size(); itelem > 0; itelem--) {
222 if (elimination.
exists(itelem - 1)) {
223 delete ((*pv)[itelem - 1]);
224 pv->
erase((*pv)[itelem - 1]);
229 while (!orderStruct.empty()) {
230 while (!(orderStruct.begin().val())->empty()) {
231 delete orderStruct.begin().val()->begin().val()->first;
232 delete orderStruct.begin().val()->begin().val()->second;
233 (orderStruct.begin().val())
234 ->erase((orderStruct.begin().val())
238 delete orderStruct.begin().val();
240 orderStruct.beginSafe());
244 while (!(itpvall.val())->empty()) {
246 (itpvall.val())->begin();
248 while (!(*itpv)->empty()) {
252 for (
Idx i = 0; i < (*itseqv)->nbrDim(); i++) {
253 std::stringstream str;
254 str << (*itseqv)->variable(i).name() <<
"_" 255 << (*itseqv)->val((*itseqv)->variable(i));
256 str2 <<
"-" << vartable[str.str()] <<
" ";
261 if (itpvall.key() !=
"0" && itpvall.key() !=
"0.0") {
262 std::stringstream strinst;
263 strinst << itvar.
key()->name();
264 strinst <<
"_val=" << itpvall.key();
265 str2 << protable[strinst.str()];
271 (*itpv)->erase(itseqv);
275 (itpvall.val())->erase(itpv);
278 delete (itpvall.val());
280 ->erase(itvar.val()->beginSafe());
283 delete (itvar.val());
284 cptparamval.erase(cptparamval.beginSafe());
287 clausstr << str2.str();
289 output <<
"p cnf " << num <<
" " << clause <<
"\neclauses " << numvar <<
"\n" 290 << clausstr.str() << clausstr2.str() << std::endl;
301 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
304 const std::string& filePath,
const IBayesNet< GUM_SCALAR >& bn) {
305 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
306 std::ofstream outputvar((filePath +
".var").c_str(), std::ios_base::trunc);
309 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
311 std::stringstream strfile, strfile2;
313 if (!outputvar.good())
314 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
319 std::stringstream clausstr, clausstr2;
323 const gum::DiscreteVariable*,
330 for (
auto node : bn.topologicalOrder())
331 Order.add(bn.variable(node));
333 for (
auto node : bn.nodes()) {
334 std::stringstream str0;
335 const DiscreteVariable* var = &bn.variable(node);
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();
349 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
350 Instantiation inst(cpt);
359 for (inst.setFirst(); !inst.end(); ++inst) {
360 if (this->fromExact(cpt[inst]) != 1) {
361 std::stringstream strk;
362 strk << this->fromExact(cpt[inst]);
363 std::string valp = strk.str();
365 if (!(cptparamval[var])->exists(valp)) {
376 (*(cptparamval[var]))[valp]->insert(
379 if (this->fromExact(cpt[inst])) {
380 std::stringstream strinst;
381 strinst << var->name();
382 strinst <<
"_val=" << this->fromExact(cpt[inst]);
384 if (!protable.
exists(strinst.str())) {
385 protable.
insert(strinst.str(), ++num);
386 strfile << num <<
"::" << strinst.str() <<
"\n";
391 (*(cptparamval[var]))[valp]->front()->insert(
397 std::stringstream str2;
399 while (!cptparamval.empty()) {
401 const DiscreteVariable*,
405 itvar = cptparamval.
begin();
407 while (!(itvar.val())->empty()) {
410 iterator itpvall = (itvar.val())->begin();
412 for (
auto pv : *itpvall.val()) {
416 std::pair< gum::Set< Idx >*,
423 for (
auto seqv : *pv) {
424 if (seqv->nbrDim() > 1) {
425 for (
Idx iInst = 0; iInst < seqv->nbrDim(); iInst++) {
427 const gum::DiscreteVariable* var = &(seqv->variable(iInst));
428 instpro->erase(*var);
429 instpro->reorder(Order);
431 if (!orderStruct.exists(instpro->toString())
432 && !newSeqpre.
exists(instpro->toString())) {
436 const gum::DiscreteVariable*,
440 if (orderStruct.exists(instpro->toString())
441 && !orderStruct[instpro->toString()]->exists(var)) {
442 orderStruct[instpro->toString()]->insert(
447 seqv->variable(iInst)
451 if (orderStruct.exists(instpro->toString())
452 && !newSeqpre.
exists(instpro->toString())) {
454 std::pair< gum::Set< Idx >*,
456 orderStruct[instpro->toString()];
457 (*orderStruct2)[var]->first->
insert(linecount);
458 (*orderStruct2)[var]->second->insert(seqv->val(iInst));
460 if ((*orderStruct2)[var]->second->size() == var->
domainSize()) {
461 newSeqpre.
insert(instpro->toString(), instpro);
463 for (
auto& elt : *orderStruct2) {
464 elimination = elimination + *(elt.second->first);
465 delete (elt.second->first);
466 delete (elt.second->second);
470 orderStruct2->
clear();
473 orderStruct.erase(instpro->toString());
476 }
else if (newSeqpre.
exists(instpro->toString())) {
477 elimination.
insert(linecount);
488 if (!newSeqpre.
empty()) {
491 for (
auto& elt : newSeqpre)
492 newSeq->
insert(elt.second);
494 (itpvall.val())->insert(newSeq);
496 for (
Idx itelem = pv->size(); itelem > 0; itelem--) {
497 if (elimination.
exists(itelem - 1)) {
498 delete ((*pv)[itelem - 1]);
499 pv->erase((*pv)[itelem - 1]);
504 while (!orderStruct.empty()) {
505 while (!(orderStruct.begin().val())->empty()) {
506 delete orderStruct.begin().val()->begin().val()->first;
507 delete orderStruct.begin().val()->begin().val()->second;
508 (orderStruct.begin().val())
509 ->erase(orderStruct.begin().val()->beginSafe());
514 delete orderStruct.begin().val();
516 orderStruct.beginSafe());
520 while (!(itpvall.val())->empty()) {
522 itpv = (itpvall.val())->begin();
524 while (!(*itpv)->empty()) {
528 for (
Idx i = 0; i < (*itseqv)->nbrDim(); i++) {
529 std::stringstream str;
530 str << (*itseqv)->variable(i).name() <<
"_" 531 << (*itseqv)->val((*itseqv)->variable(i));
532 str2 <<
"-" << vartable[str.str()] <<
" ";
537 if (itpvall.key() !=
"0" && itpvall.key() !=
"0.0") {
538 std::stringstream strinst;
539 strinst << itvar.
key()->name();
540 strinst <<
"_val=" << itpvall.key();
541 str2 << protable[strinst.str()];
547 (*itpv)->erase((*itpv)->beginSafe());
552 ->erase(itpvall.val()->beginSafe());
555 delete (itpvall.val());
557 ->erase(itvar.val()->beginSafe());
560 delete (itvar.val());
561 cptparamval.erase(cptparamval.beginSafe());
564 clausstr << str2.str();
566 output <<
"p cnf " << num <<
" " << clause <<
"\neclauses " << numvar <<
"\n" 567 << clausstr.str() << clausstr2.str() << std::endl;
569 outputvar << strfile.str();
575 if (outputvar.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
577 if (output.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
582 #endif // DOXYGEN_SHOULD_SKIP_THIS Safe iterators for Sequence.
iterator begin() const
Returns an unsafe begin iterator.
The generic class for storing (ordered) sequences of objects.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
ContextualDependenciesCNFWriter()
Default constructor.
const Key & key(const Key &key) const
Returns a reference on a given key.
void erase(const Key &k)
Erases an element from the set.
Base class for discrete random variable.
gum is the global namespace for all aGrUM entities
~ContextualDependenciesCNFWriter() final
Destructor.
Representation of a setA Set is a structure that contains arbitrary elements.
SequenceIterator< Key > iterator
Types for STL compliance.
virtual Size domainSize() const =0
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Class for assigning/browsing values to tuples of discrete variables.
Size Idx
Type for indexes.
void erase(const DiscreteVariable &v) final
Removes a variable from the Instantiation.
void clear()
Removes all the elements, if any, from the set.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const std::string & name() const
returns the name of the variable
bool empty() const noexcept
Indicates whether the hash table is empty.
Definition of classe for BN file output manipulation.
void insert(const Key &k)
Inserts a new element into the set.
const Key & back() const
Returns the last element of the sequence.
#define GUM_ERROR(type, msg)
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the BN format.
void insert(const Key &k)
Insert an element at the end of the sequence.