23 #ifndef DOXYGEN_SHOULD_SKIP_THIS 33 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
36 GUM_CONSTRUCTOR(ContextualDependenciesCNFWriter);
40 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
43 GUM_DESTRUCTOR(ContextualDependenciesCNFWriter);
52 template <
typename GUM_SCALAR,
template <
class >
class IApproximationPolicy >
55 std::ostream& output,
const IBayesNet< GUM_SCALAR >& bn) {
58 for (
auto node : bn.topologicalOrder())
59 Order.add(bn.variable(node));
62 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
64 std::stringstream strfile, strfile2;
69 std::stringstream clausstr, clausstr2;
78 for (
auto node : bn.nodes()) {
79 std::stringstream str0;
80 const DiscreteVariable* var = &bn.variable(node);
82 for (
Idx i = 0; i < var->domainSize(); i++) {
83 std::stringstream stri;
84 stri << var->name() <<
"_" << var->label(i);
85 vartable.
insert(stri.str(), ++num);
86 strfile << num <<
"::" << stri.str() <<
"\n";
87 str0 << vartable[stri.str()] <<
" ";
93 clausstr2 << str0.str();
94 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
95 Instantiation inst(cpt);
104 for (inst.setFirst(); !inst.end(); ++inst) {
105 if (this->fromExact(cpt[inst]) != 1) {
106 std::stringstream strk;
107 strk << this->fromExact(cpt[inst]);
108 std::string valp = strk.str();
110 if (!(cptparamval[var])->exists(valp)) {
116 (*(cptparamval[var]))[valp]->insert(
119 if (this->fromExact(cpt[inst])) {
120 std::stringstream strinst;
121 strinst << var->name();
122 strinst <<
"_val=" << this->fromExact(cpt[inst]);
124 if (!protable.
exists(strinst.str())) {
125 protable.
insert(strinst.str(), ++num);
126 strfile << num <<
"::" << strinst.str() <<
"\n";
131 (*(cptparamval[var]))[valp]->front()->insert(
137 std::stringstream str2;
139 while (!cptparamval.empty()) {
141 const DiscreteVariable*,
145 itvar = cptparamval.
begin();
147 while (!(itvar.val())->empty()) {
150 iterator itpvall = (itvar.val())->begin();
152 for (
auto pv : *itpvall.val()) {
156 std::pair< gum::Set< Idx >*,
160 for (
auto seqv : *pv) {
161 if (seqv->nbrDim() > 1) {
162 for (
Idx iInst = 0; iInst < seqv->nbrDim(); iInst++) {
164 instpro.reorder(Order);
165 const gum::DiscreteVariable* var = &(seqv->variable(iInst));
169 if (!orderStruct.exists(instpro.toString())) {
173 const gum::DiscreteVariable*,
177 if (!orderStruct[instpro.toString()]->exists(var)) {
178 orderStruct[instpro.toString()]->insert(
183 seqv->variable(iInst)
189 orderStruct2 = orderStruct[instpro.toString()];
190 (*orderStruct2)[var]->first->
insert(linecount);
191 (*orderStruct2)[var]->second->insert(seqv->val(iInst));
201 for (
auto& elt : orderStruct) {
204 for (
auto& elt2 : *elt.second) {
205 if (elt2.second->second->size() == elt2.first->domainSize()) {
211 *((*pv)[(*(elt2.second->first->begin()))]),
false));
215 elimination = elimination + *(elt2.second->first);
221 (itpvall.val())->insert(newSeq);
223 for (
Idx itelem = pv->size(); itelem > 0; itelem--) {
224 if (elimination.
exists(itelem - 1)) {
225 delete ((*pv)[itelem - 1]);
226 pv->
erase((*pv)[itelem - 1]);
231 while (!orderStruct.empty()) {
232 while (!(orderStruct.begin().val())->empty()) {
233 delete orderStruct.begin().val()->begin().val()->first;
234 delete orderStruct.begin().val()->begin().val()->second;
235 (orderStruct.begin().val())
236 ->erase((orderStruct.begin().val())
240 delete orderStruct.begin().val();
242 orderStruct.beginSafe());
246 while (!(itpvall.val())->empty()) {
248 (itpvall.val())->begin();
250 while (!(*itpv)->empty()) {
254 for (
Idx i = 0; i < (*itseqv)->nbrDim(); i++) {
255 std::stringstream str;
256 str << (*itseqv)->variable(i).name() <<
"_" 257 << (*itseqv)->val((*itseqv)->variable(i));
258 str2 <<
"-" << vartable[str.str()] <<
" ";
263 if (itpvall.key() !=
"0" && itpvall.key() !=
"0.0") {
264 std::stringstream strinst;
265 strinst << itvar.
key()->name();
266 strinst <<
"_val=" << itpvall.key();
267 str2 << protable[strinst.str()];
273 (*itpv)->erase(itseqv);
277 (itpvall.val())->erase(itpv);
280 delete (itpvall.val());
282 ->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 >
306 const std::string& filePath,
const IBayesNet< GUM_SCALAR >& bn) {
307 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
308 std::ofstream outputvar((filePath +
".var").c_str(), std::ios_base::trunc);
311 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
313 std::stringstream strfile, strfile2;
315 if (!outputvar.good())
316 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
321 std::stringstream clausstr, clausstr2;
325 const gum::DiscreteVariable*,
332 for (
auto node : bn.topologicalOrder())
333 Order.add(bn.variable(node));
335 for (
auto node : bn.nodes()) {
336 std::stringstream str0;
337 const DiscreteVariable* var = &bn.variable(node);
339 for (
Idx i = 0; i < bn.variable(node).domainSize(); i++) {
340 std::stringstream stri;
341 stri << var->
name() <<
"_" << var->label(i);
342 vartable.
insert(stri.str(), ++num);
343 strfile << num <<
"::" << stri.str() <<
"\n";
344 str0 << vartable[stri.str()] <<
" ";
350 clausstr2 << str0.str();
351 const Potential< GUM_SCALAR >& cpt = bn.cpt(node);
352 Instantiation inst(cpt);
361 for (inst.setFirst(); !inst.end(); ++inst) {
362 if (this->fromExact(cpt[inst]) != 1) {
363 std::stringstream strk;
364 strk << this->fromExact(cpt[inst]);
365 std::string valp = strk.str();
367 if (!(cptparamval[var])->exists(valp)) {
378 (*(cptparamval[var]))[valp]->insert(
381 if (this->fromExact(cpt[inst])) {
382 std::stringstream strinst;
383 strinst << var->name();
384 strinst <<
"_val=" << this->fromExact(cpt[inst]);
386 if (!protable.
exists(strinst.str())) {
387 protable.
insert(strinst.str(), ++num);
388 strfile << num <<
"::" << strinst.str() <<
"\n";
393 (*(cptparamval[var]))[valp]->front()->insert(
399 std::stringstream str2;
401 while (!cptparamval.empty()) {
403 const DiscreteVariable*,
407 itvar = cptparamval.
begin();
409 while (!(itvar.val())->empty()) {
412 iterator itpvall = (itvar.val())->begin();
414 for (
auto pv : *itpvall.val()) {
418 std::pair< gum::Set< Idx >*,
425 for (
auto seqv : *pv) {
426 if (seqv->nbrDim() > 1) {
427 for (
Idx iInst = 0; iInst < seqv->nbrDim(); iInst++) {
429 const gum::DiscreteVariable* var = &(seqv->variable(iInst));
430 instpro->erase(*var);
431 instpro->reorder(Order);
433 if (!orderStruct.exists(instpro->toString())
434 && !newSeqpre.
exists(instpro->toString())) {
438 const gum::DiscreteVariable*,
442 if (orderStruct.exists(instpro->toString())
443 && !orderStruct[instpro->toString()]->exists(var)) {
444 orderStruct[instpro->toString()]->insert(
449 seqv->variable(iInst)
453 if (orderStruct.exists(instpro->toString())
454 && !newSeqpre.
exists(instpro->toString())) {
456 std::pair< gum::Set< Idx >*,
458 orderStruct[instpro->toString()];
459 (*orderStruct2)[var]->first->
insert(linecount);
460 (*orderStruct2)[var]->second->insert(seqv->val(iInst));
462 if ((*orderStruct2)[var]->second->size() == var->
domainSize()) {
463 newSeqpre.
insert(instpro->toString(), instpro);
465 for (
auto& elt : *orderStruct2) {
466 elimination = elimination + *(elt.second->first);
467 delete (elt.second->first);
468 delete (elt.second->second);
472 orderStruct2->
clear();
475 orderStruct.erase(instpro->toString());
478 }
else if (newSeqpre.
exists(instpro->toString())) {
479 elimination.
insert(linecount);
490 if (!newSeqpre.
empty()) {
493 for (
auto& elt : newSeqpre)
494 newSeq->
insert(elt.second);
496 (itpvall.val())->insert(newSeq);
498 for (
Idx itelem = pv->size(); itelem > 0; itelem--) {
499 if (elimination.
exists(itelem - 1)) {
500 delete ((*pv)[itelem - 1]);
501 pv->erase((*pv)[itelem - 1]);
506 while (!orderStruct.empty()) {
507 while (!(orderStruct.begin().val())->empty()) {
508 delete orderStruct.begin().val()->begin().val()->first;
509 delete orderStruct.begin().val()->begin().val()->second;
510 (orderStruct.begin().val())
511 ->erase(orderStruct.begin().val()->beginSafe());
516 delete orderStruct.begin().val();
518 orderStruct.beginSafe());
522 while (!(itpvall.val())->empty()) {
524 itpv = (itpvall.val())->begin();
526 while (!(*itpv)->empty()) {
530 for (
Idx i = 0; i < (*itseqv)->nbrDim(); i++) {
531 std::stringstream str;
532 str << (*itseqv)->variable(i).name() <<
"_" 533 << (*itseqv)->val((*itseqv)->variable(i));
534 str2 <<
"-" << vartable[str.str()] <<
" ";
539 if (itpvall.key() !=
"0" && itpvall.key() !=
"0.0") {
540 std::stringstream strinst;
541 strinst << itvar.
key()->name();
542 strinst <<
"_val=" << itpvall.key();
543 str2 << protable[strinst.str()];
549 (*itpv)->erase((*itpv)->beginSafe());
554 ->erase(itpvall.val()->beginSafe());
557 delete (itpvall.val());
559 ->erase(itvar.val()->beginSafe());
562 delete (itvar.val());
563 cptparamval.erase(cptparamval.beginSafe());
566 clausstr << str2.str();
568 output <<
"p cnf " << num <<
" " << clause <<
"\neclauses " << numvar <<
"\n" 569 << clausstr.str() << clausstr2.str() << std::endl;
571 outputvar << strfile.str();
577 if (outputvar.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
579 if (output.fail())
GUM_ERROR(IOError,
"Writting in the ostream failed.");
584 #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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
~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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.