From 324ea1a96c71b9bd51733a7403133b54bd01ea17 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Mon, 29 Oct 2012 20:49:21 +0000 Subject: [PATCH] make independent partial ground suck less --- packages/CLPBN/horus/LiftedCircuit.cpp | 69 ++++++++++++++++---------- packages/CLPBN/horus/LiftedCircuit.h | 8 +-- packages/CLPBN/horus/LiftedWCNF.cpp | 23 +++++++++ packages/CLPBN/horus/LiftedWCNF.h | 2 + 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/packages/CLPBN/horus/LiftedCircuit.cpp b/packages/CLPBN/horus/LiftedCircuit.cpp index e3e9f1443..0b1ebcdf1 100644 --- a/packages/CLPBN/horus/LiftedCircuit.cpp +++ b/packages/CLPBN/horus/LiftedCircuit.cpp @@ -329,33 +329,50 @@ LiftedCircuit::tryIndepPartialGrounding ( // assumes that all literals have logical variables // else, shannon decomp was possible - LogVarSet lvs = clauses[0].constr().logVarSet(); - lvs -= clauses[0].ipgLogVars(); - for (unsigned i = 0; i < lvs.size(); i++) { - LogVar X = lvs[i]; + vector lvIndices; + LogVarSet lvs = clauses[0].ipgCandidates(); + for (size_t i = 0; i < lvs.size(); i++) { + lvIndices.clear(); + lvIndices.push_back (i); ConstraintTree ct = clauses[0].constr(); - ct.project ({X}); - for (size_t j = 0; j < clauses.size(); j++) { - if (clauses[j].constr().logVars().size() == 1) { - if (ct.tupleSet() != clauses[j].constr().tupleSet()) { - return false; - } - } else { - return false; + ct.project ({lvs[i]}); + if (tryIndepPartialGroundingAux (clauses, ct, lvIndices)) { + Clauses newClauses = clauses; + for (size_t i = 0; i < clauses.size(); i++) { + LogVar lv = clauses[i].ipgCandidates()[lvIndices[i]]; + newClauses[i].addIpgLogVar (lv); } + SetAndNode* node = new SetAndNode (ct.size(), clauses); + *follow = node; + compile (node->follow(), newClauses); + return true; } } - - // FIXME this is so broken ... - Clauses newClauses = clauses; - for (size_t i = 0; i < clauses.size(); i++) { - newClauses[i].addIpgLogVar (clauses[i].constr().logVars()[0]); + return false; +} + + + +bool +LiftedCircuit::tryIndepPartialGroundingAux ( + Clauses& clauses, + ConstraintTree& ct, + vector& lvIndices) +{ + for (size_t j = 1; j < clauses.size(); j++) { + LogVarSet lvs2 = clauses[j].ipgCandidates(); + for (size_t k = 0; k < lvs2.size(); k++) { + ConstraintTree ct2 = clauses[j].constr(); + ct2.project ({lvs2[k]}); + if (ct.tupleSet() == ct2.tupleSet()) { + lvIndices.push_back (k); + break; + } + } + if (lvIndices.size() != j+1) { + return false; + } } - - // FIXME - SetAndNode* node = new SetAndNode (2, clauses); - *follow = node; - compile (node->follow(), newClauses); return true; } @@ -414,8 +431,8 @@ LiftedCircuit::smoothCircuit (CircuitNode* node) } SmoothNode* smoothNode = new SmoothNode (clauses); CircuitNode** prev = casted->leftBranch(); - string explanation = " smoothing" ; - AndNode* andNode = new AndNode ((*prev)->clauses(), smoothNode, *prev, explanation); + AndNode* andNode = new AndNode ((*prev)->clauses(), + smoothNode, *prev, " smoothing"); *prev = andNode; } if (missingRight.empty() == false) { @@ -427,8 +444,8 @@ LiftedCircuit::smoothCircuit (CircuitNode* node) } SmoothNode* smoothNode = new SmoothNode (clauses); CircuitNode** prev = casted->rightBranch(); - string explanation = " smoothing" ; - AndNode* andNode = new AndNode ((*prev)->clauses(), smoothNode, *prev, explanation); + AndNode* andNode = new AndNode ((*prev)->clauses(), smoothNode, + *prev, " smoothing"); *prev = andNode; } propagatingLids |= lids1; diff --git a/packages/CLPBN/horus/LiftedCircuit.h b/packages/CLPBN/horus/LiftedCircuit.h index 7006d2f82..0db93679c 100644 --- a/packages/CLPBN/horus/LiftedCircuit.h +++ b/packages/CLPBN/horus/LiftedCircuit.h @@ -109,15 +109,15 @@ class SetAndNode : public CircuitNode { public: SetAndNode (unsigned nrGroundings, const Clauses& clauses) - : CircuitNode (clauses, ""), nrGroundings_(nrGroundings), + : CircuitNode (clauses, "IPG"), nrGroundings_(nrGroundings), follow_(0) { } double weight (void) const; CircuitNode** follow (void) { return &follow_; } private: - unsigned nrGroundings_; - CircuitNode* follow_; + unsigned nrGroundings_; + CircuitNode* follow_; }; @@ -191,6 +191,8 @@ class LiftedCircuit bool tryIndependence (CircuitNode** follow, Clauses& clauses); bool tryShannonDecomp (CircuitNode** follow, Clauses& clauses); bool tryIndepPartialGrounding (CircuitNode** follow, Clauses& clauses); + bool tryIndepPartialGroundingAux (Clauses& clauses, ConstraintTree& ct, + vector& indices); bool tryGrounding (CircuitNode** follow, Clauses& clauses); TinySet smoothCircuit (CircuitNode* node); diff --git a/packages/CLPBN/horus/LiftedWCNF.cpp b/packages/CLPBN/horus/LiftedWCNF.cpp index 4ad651621..657016988 100644 --- a/packages/CLPBN/horus/LiftedWCNF.cpp +++ b/packages/CLPBN/horus/LiftedWCNF.cpp @@ -136,6 +136,29 @@ Clause::removeNegativeLiterals (LiteralId lid) +LogVarSet +Clause::ipgCandidates (void) const +{ + LogVarSet candidates; + LogVarSet allLvs = constr_.logVarSet(); + allLvs -= ipgLogVars_; + for (size_t i = 0; i < allLvs.size(); i++) { + bool valid = true; + for (size_t j = 0; j < literals_.size(); j++) { + if (Util::contains (literals_[j].logVars(), allLvs[i]) == false) { + valid = false; + break; + } + } + if (valid) { + candidates.insert (allLvs[i]); + } + } + return candidates; +} + + + TinySet Clause::lidSet (void) const { diff --git a/packages/CLPBN/horus/LiftedWCNF.h b/packages/CLPBN/horus/LiftedWCNF.h index 155ec48e8..564ec0fd4 100644 --- a/packages/CLPBN/horus/LiftedWCNF.h +++ b/packages/CLPBN/horus/LiftedWCNF.h @@ -90,6 +90,8 @@ class Clause void addIpgLogVar (LogVar X) { ipgLogVars_.insert (X); } + LogVarSet ipgCandidates (void) const; + TinySet lidSet (void) const; friend std::ostream& operator<< (ostream &os, const Clause& clause);