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
SetOrNode::weight (void) const
{
// TODO
return 0.0;
double weightSum = LogAware::addIdenty();
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
SetAndNode::weight (void) const
{
@ -67,14 +75,22 @@ LeafNode::weight (void) const
assert (clauses()[0].isUnit());
Clause c = clauses()[0];
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;
if (lvs.empty() == false) {
ConstraintTree ct = c.constr();
ct.project (lvs);
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
? weight * nrGroundings
: std::pow (weight, nrGroundings);
@ -85,6 +101,7 @@ LeafNode::weight (void) const
double
SmoothNode::weight (void) const
{
// TODO and what happens if smoothing contains ipg or counted lvs ?
Clauses cs = clauses();
double totalWeight = LogAware::multIdenty();
for (size_t i = 0; i < cs.size(); i++) {
@ -223,12 +240,11 @@ LiftedCircuit::tryUnitPropagation (
CircuitNode** follow,
Clauses& clauses)
{
cout << "ALL CLAUSES:" << endl;
Clause::printClauses (clauses);
// cout << "ALL CLAUSES:" << endl;
// Clause::printClauses (clauses);
for (size_t i = 0; i < clauses.size(); i++) {
if (clauses[i].isUnit()) {
cout << clauses[i] << " is unit!" << endl;
// cout << clauses[i] << " is unit!" << endl;
Clauses newClauses;
for (size_t j = 0; j < clauses.size(); j++) {
if (i != j) {
@ -453,7 +469,9 @@ LiftedCircuit::tryAtomCounting (
for (size_t j = 0; j < literals.size(); j++) {
if (literals[j].logVars().size() == 1) {
// 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 c2 (clauses[i].constr().projectedCopy (literals[j].logVars()));
c1.addLiteral (literals[j]);

View File

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

View File

@ -98,6 +98,10 @@ class Clause
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 containsPositiveLiteral (LiteralId lid, const LogVarTypes&) const;