support model counting on atom couting nodes - still untested

This commit is contained in:
Tiago Gomes 2012-11-01 22:34:28 +00:00
parent d4912ea219
commit a1d0deb638
4 changed files with 91 additions and 56 deletions

View File

@ -26,13 +26,21 @@ AndNode::weight (void) const
double double
SetOrNode::weight (void) const SetOrNode::weight (void) const
{ {
// TODO double weightSum = LogAware::addIdenty();
return 0.0; for (unsigned i = 0; i < nrGroundings_ + 1; i++) {
nrGrsStack.push (make_pair (i, nrGroundings_ - i));
if (Globals::logDomain) {
double w = std::log (Util::nrCombinations (nrGroundings_, i));
weightSum = Util::logSum (weightSum, w + follow_->weight());
} else {
weightSum += Util::nrCombinations (nrGroundings_, i) * follow_->weight();
}
}
return weightSum;
} }
double double
SetAndNode::weight (void) const SetAndNode::weight (void) const
{ {
@ -67,14 +75,22 @@ LeafNode::weight (void) const
assert (clauses()[0].isUnit()); assert (clauses()[0].isUnit());
Clause c = clauses()[0]; Clause c = clauses()[0];
double weight = c.literals()[0].weight(); double weight = c.literals()[0].weight();
LogVarSet lvs = c.constr().logVarSet() - c.ipgLogVars(); LogVarSet lvs = c.constr().logVarSet();
lvs -= c.ipgLogVars();
lvs -= c.positiveCountedLogVars();
lvs -= c.negativeCountedLogVars();
unsigned nrGroundings = 1; unsigned nrGroundings = 1;
if (lvs.empty() == false) { if (lvs.empty() == false) {
ConstraintTree ct = c.constr(); ConstraintTree ct = c.constr();
ct.project (lvs); ct.project (lvs);
nrGroundings = ct.size(); nrGroundings = ct.size();
} }
assert (nrGroundings != 0); // TODO this only works for one counted log var
if (c.positiveCountedLogVars().empty() == false) {
nrGroundings *= SetOrNode::nrPositives();
} else if (c.negativeCountedLogVars().empty() == false) {
nrGroundings *= SetOrNode::nrNegatives();
}
return Globals::logDomain return Globals::logDomain
? weight * nrGroundings ? weight * nrGroundings
: std::pow (weight, nrGroundings); : std::pow (weight, nrGroundings);
@ -85,6 +101,7 @@ LeafNode::weight (void) const
double double
SmoothNode::weight (void) const SmoothNode::weight (void) const
{ {
// TODO and what happens if smoothing contains ipg or counted lvs ?
Clauses cs = clauses(); Clauses cs = clauses();
double totalWeight = LogAware::multIdenty(); double totalWeight = LogAware::multIdenty();
for (size_t i = 0; i < cs.size(); i++) { for (size_t i = 0; i < cs.size(); i++) {
@ -223,12 +240,11 @@ LiftedCircuit::tryUnitPropagation (
CircuitNode** follow, CircuitNode** follow,
Clauses& clauses) Clauses& clauses)
{ {
cout << "ALL CLAUSES:" << endl; // cout << "ALL CLAUSES:" << endl;
Clause::printClauses (clauses); // Clause::printClauses (clauses);
for (size_t i = 0; i < clauses.size(); i++) { for (size_t i = 0; i < clauses.size(); i++) {
if (clauses[i].isUnit()) { if (clauses[i].isUnit()) {
cout << clauses[i] << " is unit!" << endl; // cout << clauses[i] << " is unit!" << endl;
Clauses newClauses; Clauses newClauses;
for (size_t j = 0; j < clauses.size(); j++) { for (size_t j = 0; j < clauses.size(); j++) {
if (i != j) { if (i != j) {
@ -453,7 +469,9 @@ LiftedCircuit::tryAtomCounting (
for (size_t j = 0; j < literals.size(); j++) { for (size_t j = 0; j < literals.size(); j++) {
if (literals[j].logVars().size() == 1) { if (literals[j].logVars().size() == 1) {
// TODO check if not already in ipg and countedlvs // TODO check if not already in ipg and countedlvs
SetOrNode* setOrNode = new SetOrNode (clauses); unsigned nrGroundings = clauses[i].constr().projectedCopy (
literals[j].logVars()).size();
SetOrNode* setOrNode = new SetOrNode (nrGroundings, clauses);
Clause c1 (clauses[i].constr().projectedCopy (literals[j].logVars())); Clause c1 (clauses[i].constr().projectedCopy (literals[j].logVars()));
Clause c2 (clauses[i].constr().projectedCopy (literals[j].logVars())); Clause c2 (clauses[i].constr().projectedCopy (literals[j].logVars()));
c1.addLiteral (literals[j]); c1.addLiteral (literals[j]);

View File

@ -1,6 +1,8 @@
#ifndef HORUS_LIFTEDCIRCUIT_H #ifndef HORUS_LIFTEDCIRCUIT_H
#define HORUS_LIFTEDCIRCUIT_H #define HORUS_LIFTEDCIRCUIT_H
#include <stack>
#include "LiftedWCNF.h" #include "LiftedWCNF.h"
@ -45,11 +47,11 @@ class OrNode : public CircuitNode
OrNode (const Clauses& clauses, string explanation = "") OrNode (const Clauses& clauses, string explanation = "")
: CircuitNode (clauses, explanation), : CircuitNode (clauses, explanation),
leftBranch_(0), rightBranch_(0) { } leftBranch_(0), rightBranch_(0) { }
double weight (void) const;
CircuitNode** leftBranch (void) { return &leftBranch_; } CircuitNode** leftBranch (void) { return &leftBranch_; }
CircuitNode** rightBranch (void) { return &rightBranch_; } CircuitNode** rightBranch (void) { return &rightBranch_; }
double weight (void) const;
private: private:
CircuitNode* leftBranch_; CircuitNode* leftBranch_;
CircuitNode* rightBranch_; CircuitNode* rightBranch_;
@ -78,11 +80,12 @@ class AndNode : public CircuitNode
string explanation = "") string explanation = "")
: CircuitNode ({}, explanation), : CircuitNode ({}, explanation),
leftBranch_(leftBranch), rightBranch_(rightBranch) { } leftBranch_(leftBranch), rightBranch_(rightBranch) { }
double weight (void) const;
CircuitNode** leftBranch (void) { return &leftBranch_; } CircuitNode** leftBranch (void) { return &leftBranch_; }
CircuitNode** rightBranch (void) { return &rightBranch_; } CircuitNode** rightBranch (void) { return &rightBranch_; }
double weight (void) const;
private: private:
CircuitNode* leftBranch_; CircuitNode* leftBranch_;
CircuitNode* rightBranch_; CircuitNode* rightBranch_;
@ -93,14 +96,23 @@ class AndNode : public CircuitNode
class SetOrNode : public CircuitNode class SetOrNode : public CircuitNode
{ {
public: public:
SetOrNode (const Clauses& clauses, string explanation = "") SetOrNode (unsigned nrGroundings, const Clauses& clauses)
: CircuitNode (clauses, explanation), follow_(0) { } : CircuitNode (clauses, " AC"), follow_(0),
nrGroundings_(nrGroundings) { }
double weight (void) const;
CircuitNode** follow (void) { return &follow_; } CircuitNode** follow (void) { return &follow_; }
static unsigned nrPositives (void) { return nrGrsStack.top().first; }
static unsigned nrNegatives (void) { return nrGrsStack.top().second; }
double weight (void) const;
private: private:
CircuitNode* follow_; CircuitNode* follow_;
unsigned nrGroundings_;
static stack<pair<unsigned, unsigned>> nrGrsStack;
}; };
@ -109,15 +121,16 @@ class SetAndNode : public CircuitNode
{ {
public: public:
SetAndNode (unsigned nrGroundings, const Clauses& clauses) SetAndNode (unsigned nrGroundings, const Clauses& clauses)
: CircuitNode (clauses, " IPG"), nrGroundings_(nrGroundings), : CircuitNode (clauses, " IPG"), follow_(0),
follow_(0) { } nrGroundings_(nrGroundings) { }
CircuitNode** follow (void) { return &follow_; }
double weight (void) const; double weight (void) const;
CircuitNode** follow (void) { return &follow_; }
private: private:
unsigned nrGroundings_;
CircuitNode* follow_; CircuitNode* follow_;
unsigned nrGroundings_;
}; };
@ -129,11 +142,11 @@ class IncExcNode : public CircuitNode
: CircuitNode (clauses), plus1Branch_(0), : CircuitNode (clauses), plus1Branch_(0),
plus2Branch_(0), minusBranch_(0) { } plus2Branch_(0), minusBranch_(0) { }
double weight (void) const;
CircuitNode** plus1Branch (void) { return &plus1Branch_; } CircuitNode** plus1Branch (void) { return &plus1Branch_; }
CircuitNode** plus2Branch (void) { return &plus2Branch_; } CircuitNode** plus2Branch (void) { return &plus2Branch_; }
CircuitNode** minusBranch (void) { return &minusBranch_; } CircuitNode** minusBranch (void) { return &minusBranch_; }
double weight (void) const;
private: private:
CircuitNode* plus1Branch_; CircuitNode* plus1Branch_;

View File

@ -119,7 +119,7 @@ Clause::removeLiterals (LiteralId lid)
} }
} }
void void
Clause::removePositiveLiterals ( Clause::removePositiveLiterals (
@ -137,7 +137,7 @@ Clause::removePositiveLiterals (
} }
} }
} }
void void

View File

@ -25,36 +25,36 @@ class Literal
public: public:
Literal (LiteralId lid, double w = -1.0) : Literal (LiteralId lid, double w = -1.0) :
lid_(lid), weight_(w), negated_(false) { } lid_(lid), weight_(w), negated_(false) { }
Literal (LiteralId lid, const LogVars& lvs, double w = -1.0) : Literal (LiteralId lid, const LogVars& lvs, double w = -1.0) :
lid_(lid), logVars_(lvs), weight_(w), negated_(false) { } lid_(lid), logVars_(lvs), weight_(w), negated_(false) { }
Literal (const Literal& lit, bool negated) : Literal (const Literal& lit, bool negated) :
lid_(lit.lid_), logVars_(lit.logVars_), weight_(lit.weight_), negated_(negated) { } lid_(lit.lid_), logVars_(lit.logVars_), weight_(lit.weight_), negated_(negated) { }
LiteralId lid (void) const { return lid_; } LiteralId lid (void) const { return lid_; }
LogVars logVars (void) const { return logVars_; } LogVars logVars (void) const { return logVars_; }
LogVarSet logVarSet (void) const { return LogVarSet (logVars_); } LogVarSet logVarSet (void) const { return LogVarSet (logVars_); }
// FIXME this is not log aware :( // FIXME this is not log aware :(
double weight (void) const { return weight_ < 0.0 ? 1.0 : weight_; } double weight (void) const { return weight_ < 0.0 ? 1.0 : weight_; }
void negate (void) { negated_ = !negated_; } void negate (void) { negated_ = !negated_; }
bool isPositive (void) const { return negated_ == false; } bool isPositive (void) const { return negated_ == false; }
bool isNegative (void) const { return negated_; } bool isNegative (void) const { return negated_; }
bool isGround (ConstraintTree constr, LogVarSet ipgLogVars) const; bool isGround (ConstraintTree constr, LogVarSet ipgLogVars) const;
string toString (LogVarSet ipgLogVars = LogVarSet(), string toString (LogVarSet ipgLogVars = LogVarSet(),
LogVarSet posCountedLvs = LogVarSet(), LogVarSet posCountedLvs = LogVarSet(),
LogVarSet negCountedLvs = LogVarSet()) const; LogVarSet negCountedLvs = LogVarSet()) const;
friend std::ostream& operator<< (ostream &os, const Literal& lit); friend std::ostream& operator<< (ostream &os, const Literal& lit);
private: private:
LiteralId lid_; LiteralId lid_;
LogVars logVars_; LogVars logVars_;
@ -81,7 +81,7 @@ class Clause
literals_.push_back (l); literals_.push_back (l);
literals_.back().negate(); literals_.back().negate();
} }
const vector<Literal>& literals (void) const { return literals_; } const vector<Literal>& literals (void) const { return literals_; }
const ConstraintTree& constr (void) const { return constr_; } const ConstraintTree& constr (void) const { return constr_; }
@ -98,6 +98,10 @@ class Clause
void addNegativeCountedLogVar (LogVar X) { negCountedLvs_.insert (X); } void addNegativeCountedLogVar (LogVar X) { negCountedLvs_.insert (X); }
LogVarSet positiveCountedLogVars (void) const { return posCountedLvs_; }
LogVarSet negativeCountedLogVars (void) const { return negCountedLvs_; }
bool containsLiteral (LiteralId lid) const; bool containsLiteral (LiteralId lid) const;
bool containsPositiveLiteral (LiteralId lid, const LogVarTypes&) const; bool containsPositiveLiteral (LiteralId lid, const LogVarTypes&) const;
@ -115,26 +119,26 @@ class Clause
bool isPositiveCountedLogVar (LogVar X) const; bool isPositiveCountedLogVar (LogVar X) const;
bool isNegativeCountedLogVar (LogVar X) const; bool isNegativeCountedLogVar (LogVar X) const;
TinySet<LiteralId> lidSet (void) const; TinySet<LiteralId> lidSet (void) const;
LogVarSet ipgCandidates (void) const; LogVarSet ipgCandidates (void) const;
LogVarTypes logVarTypes (size_t litIdx) const; LogVarTypes logVarTypes (size_t litIdx) const;
void removeLiteral (size_t litIdx); void removeLiteral (size_t litIdx);
static void printClauses (const vector<Clause>& clauses); static void printClauses (const vector<Clause>& clauses);
friend std::ostream& operator<< (ostream &os, const Clause& clause); friend std::ostream& operator<< (ostream &os, const Clause& clause);
private: private:
LogVarSet getLogVarSetExcluding (size_t idx) const; LogVarSet getLogVarSetExcluding (size_t idx) const;
vector<Literal> literals_; vector<Literal> literals_;
LogVarSet ipgLogVars_; LogVarSet ipgLogVars_;
LogVarSet posCountedLvs_; LogVarSet posCountedLvs_;
LogVarSet negCountedLvs_; LogVarSet negCountedLvs_;
ConstraintTree constr_; ConstraintTree constr_;
}; };
@ -146,19 +150,19 @@ class LiftedWCNF
{ {
public: public:
LiftedWCNF (const ParfactorList& pfList); LiftedWCNF (const ParfactorList& pfList);
~LiftedWCNF (void); ~LiftedWCNF (void);
const Clauses& clauses (void) const { return clauses_; } const Clauses& clauses (void) const { return clauses_; }
Clause createClauseForLiteral (LiteralId lid) const; Clause createClauseForLiteral (LiteralId lid) const;
void printFormulaIndicators (void) const; void printFormulaIndicators (void) const;
void printWeights (void) const; void printWeights (void) const;
void printClauses (void) const; void printClauses (void) const;
private: private:
void addIndicatorClauses (const ParfactorList& pfList); void addIndicatorClauses (const ParfactorList& pfList);
@ -171,11 +175,11 @@ class LiftedWCNF
} }
Clauses clauses_; Clauses clauses_;
unordered_map<PrvGroup, vector<LiteralId>> map_; unordered_map<PrvGroup, vector<LiteralId>> map_;
const ParfactorList& pfList_; const ParfactorList& pfList_;
LiteralId freeLiteralId_; LiteralId freeLiteralId_;
}; };