fix weighted model counting in atom counting nodes

This commit is contained in:
Tiago Gomes 2012-11-06 14:15:21 +00:00
parent 4518a3db5d
commit ab334f82b2
4 changed files with 162 additions and 110 deletions

View File

@ -32,12 +32,20 @@ SetOrNode::weight (void) const
{
double weightSum = LogAware::addIdenty();
for (unsigned i = 0; i < nrGroundings_ + 1; i++) {
nrGrsStack.push (make_pair (i, nrGroundings_ - i));
nrGrsStack.push (make_pair (nrGroundings_ - i, 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();
cout << endl;
cout << "nr groundings = " << nrGroundings_ << endl;
cout << "nr positives = " << nrPositives() << endl;
cout << "nr negatives = " << nrNegatives() << endl;
cout << "i = " << i << endl;
cout << "nr combos = " << Util::nrCombinations (nrGroundings_, i) << endl;
double w = follow_->weight();
cout << "weight = " << w << endl;
weightSum += Util::nrCombinations (nrGroundings_, i) * w;
}
}
return weightSum;
@ -78,7 +86,9 @@ LeafNode::weight (void) const
assert (clauses().size() == 1);
assert (clauses()[0].isUnit());
Clause c = clauses()[0];
double weight = c.literals()[0].weight();
double weight = c.literals()[0].isPositive()
? lwcnf_.posWeight (c.literals().front().lid())
: lwcnf_.negWeight (c.literals().front().lid());
LogVarSet lvs = c.constr().logVarSet();
lvs -= c.ipgLogVars();
lvs -= c.positiveCountedLogVars();
@ -90,11 +100,20 @@ LeafNode::weight (void) const
nrGroundings = ct.size();
}
// TODO this only works for one counted log var
cout << "calc weight for " << clauses().front() << endl;
if (c.positiveCountedLogVars().empty() == false) {
nrGroundings *= SetOrNode::nrPositives();
} else if (c.negativeCountedLogVars().empty() == false) {
nrGroundings *= SetOrNode::nrNegatives();
cout << " -> nr pos = " << SetOrNode::nrPositives() << endl;
nrGroundings *= std::pow (SetOrNode::nrPositives(),
c.nrPositiveCountedLogVars());
}
if (c.negativeCountedLogVars().empty() == false) {
cout << " -> nr neg = " << SetOrNode::nrNegatives() << endl;
nrGroundings *= std::pow (SetOrNode::nrNegatives(),
c.nrNegativeCountedLogVars());
}
cout << " -> nr groundings = " << nrGroundings << endl;
cout << " -> lit weight = " << weight << endl;
cout << " -> ret weight = " << std::pow (weight, nrGroundings) << endl;
return Globals::logDomain
? weight * nrGroundings
: std::pow (weight, nrGroundings);
@ -109,14 +128,38 @@ SmoothNode::weight (void) const
Clauses cs = clauses();
double totalWeight = LogAware::multIdenty();
for (size_t i = 0; i < cs.size(); i++) {
double posWeight = cs[i].literals()[0].weight();
double negWeight = cs[i].literals()[1].weight();
unsigned nrGroundings = cs[i].constr().size();
double posWeight = lwcnf_.posWeight (cs[i].literals()[0].lid());
double negWeight = lwcnf_.negWeight (cs[i].literals()[0].lid());
LogVarSet lvs = cs[i].constr().logVarSet();
lvs -= cs[i].ipgLogVars();
lvs -= cs[i].positiveCountedLogVars();
lvs -= cs[i].negativeCountedLogVars();
unsigned nrGroundings = 1;
if (lvs.empty() == false) {
ConstraintTree ct = cs[i].constr();
ct.project (lvs);
nrGroundings = ct.size();
}
cout << "calc smooth weight for " << cs[i] << endl;
if (cs[i].positiveCountedLogVars().empty() == false) {
cout << " -> nr pos = " << SetOrNode::nrPositives() << endl;
nrGroundings *= std::pow (SetOrNode::nrPositives(),
cs[i].nrPositiveCountedLogVars());
}
if (cs[i].negativeCountedLogVars().empty() == false) {
cout << " -> nr neg = " << SetOrNode::nrNegatives() << endl;
nrGroundings *= std::pow (SetOrNode::nrNegatives(),
cs[i].nrNegativeCountedLogVars());
}
cout << " -> pos+neg = " << posWeight + negWeight << endl;
cout << " -> nrgroun = " << nrGroundings << endl;
if (Globals::logDomain) {
// TODO i think i have to do log on nrGrounginds here!
totalWeight += (Util::logSum (posWeight, negWeight) * nrGroundings);
} else {
totalWeight *= std::pow (posWeight + negWeight, nrGroundings);
}
cout << " -> smooth weight = " << totalWeight << endl;
}
return totalWeight;
}
@ -151,6 +194,8 @@ LiftedCircuit::LiftedCircuit (const LiftedWCNF* lwcnf)
exportToGraphViz("circuit.dot");
smoothCircuit();
exportToGraphViz("circuit.smooth.dot");
cout << "--------------------------------------------------" << endl;
cout << "--------------------------------------------------" << endl;
cout << "WEIGHTED MODEL COUNT = " << getWeightedModelCount() << endl;
}
@ -201,7 +246,7 @@ LiftedCircuit::compile (
}
if (clauses.size() == 1 && clauses[0].isUnit()) {
*follow = new LeafNode (clauses[0]);
*follow = new LeafNode (clauses[0], *lwcnf_);
return;
}
@ -587,33 +632,6 @@ LiftedCircuit::shatterCountedLogVarsAux (
LogVarTypes
unionTypes (const LogVarTypes& types1, const LogVarTypes& types2)
{
if (types1.empty()) {
return types2;
}
if (types2.empty()) {
return types1;
}
assert (types1.size() == types2.size());
LogVarTypes res;
for (size_t i = 0; i < types1.size(); i++) {
if (types1[i] == LogVarType::POS_LV
&& types2[i] == LogVarType::POS_LV) {
res.push_back (LogVarType::POS_LV);
} else if (types1[i] == LogVarType::NEG_LV
&& types2[i] == LogVarType::NEG_LV) {
res.push_back (LogVarType::NEG_LV);
} else {
res.push_back (LogVarType::FULL_LV);
}
}
return res;
}
vector<LogVarTypes>
getAllPossibleTypes (unsigned nrLogVars)
{
@ -779,7 +797,7 @@ LiftedCircuit::createSmoothNode (
c.addAndNegateLiteral (c.literals()[0]);
clauses.push_back (c);
}
SmoothNode* smoothNode = new SmoothNode (clauses);
SmoothNode* smoothNode = new SmoothNode (clauses, *lwcnf_);
*prev = new AndNode ((*prev)->clauses(), smoothNode,
*prev, " Smoothing");
}

View File

@ -159,9 +159,13 @@ class IncExcNode : public CircuitNode
class LeafNode : public CircuitNode
{
public:
LeafNode (const Clause& clause) : CircuitNode ({clause}) { }
LeafNode (const Clause& clause, const LiftedWCNF& lwcnf)
: CircuitNode (Clauses() = {clause}), lwcnf_(lwcnf) { }
double weight (void) const;
private:
const LiftedWCNF& lwcnf_;
};
@ -169,9 +173,13 @@ class LeafNode : public CircuitNode
class SmoothNode : public CircuitNode
{
public:
SmoothNode (const Clauses& clauses) : CircuitNode (clauses) { }
SmoothNode (const Clauses& clauses, const LiftedWCNF& lwcnf)
: CircuitNode (clauses), lwcnf_(lwcnf) { }
double weight (void) const;
private:
const LiftedWCNF& lwcnf_;
};

View File

@ -3,6 +3,7 @@
#include "Indexer.h"
bool
Literal::isGround (ConstraintTree constr, LogVarSet ipgLogVars) const
{
@ -24,7 +25,12 @@ Literal::toString (
{
stringstream ss;
negated_ ? ss << "¬" : ss << "" ;
weight_ < 0.0 ? ss << "λ" : ss << "Θ" ;
// if (negated_ == false) {
// posWeight_ < 0.0 ? ss << "λ" : ss << "Θ" ;
// } else {
// negWeight_ < 0.0 ? ss << "λ" : ss << "Θ" ;
// }
ss << "λ" ;
ss << lid_ ;
if (logVars_.empty() == false) {
ss << "(" ;
@ -300,18 +306,44 @@ LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
//addIndicatorClauses (pfList);
//addParameterClauses (pfList);
vector<vector<string>> names = {{"p1","p1"},{"p2","p2"}};
vector<vector<string>> names = {
/*
{"p1","p1"},
{"p1","p2"},
{"p2","p1"},
{"p2","p2"},
{"p1","p3"},
{"p2","p3"},
{"p3","p3"},
{"p3","p2"},
{"p3","p1"}
*/
{"p1","p1"},
{"p1","p2"},
{"p1","p3"},
{"p2","p1"},
{"p2","p2"},
{"p2","p3"},
{"p3","p1"},
{"p3","p2"},
{"p3","p3"}
};
Clause c1 (names);
c1.addLiteral (Literal (0, LogVars()={0}, 3.0));
c1.addAndNegateLiteral (Literal (1, {0,1}, 1.0));
c1.addLiteral (Literal (0, LogVars() = {0}));
c1.addAndNegateLiteral (Literal (1, {0,1}));
clauses_.push_back(c1);
Clause c2 (names);
c2.addLiteral (Literal (0, LogVars()={0}, 2.0));
c2.addAndNegateLiteral (Literal (1, {1,0}, 5.0));
c2.addLiteral (Literal (0, LogVars()={0}));
c2.addAndNegateLiteral (Literal (1, {1,0}));
clauses_.push_back(c2);
addWeight (0, 3.0, 4.0);
addWeight (1, 2.0, 5.0);
freeLiteralId_ = 2;
cout << "FORMULA INDICATORS:" << endl;
// printFormulaIndicators();
cout << endl;
@ -320,6 +352,7 @@ LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
cout << endl;
cout << "CLAUSES:" << endl;
printClauses();
// abort();
cout << endl;
}
@ -349,7 +382,7 @@ LiftedWCNF::createClauseForLiteral (LiteralId lid) const
}
// FIXME
Clause c (ConstraintTree({}));
c.addLiteral (Literal (lid,{}));
c.addLiteral (Literal (lid,LogVars() = {}));
return c;
//assert (false);
//return Clause (0);
@ -410,22 +443,25 @@ LiftedWCNF::addParameterClauses (const ParfactorList& pfList)
// ¬λu1 ... ¬λun v θxi|u1,...,un -> clause1
// ¬θxi|u1,...,un v λu1 -> tempClause
// ¬θxi|u1,...,un v λu2 -> tempClause
double weight = (**it)[indexer];
double posWeight = (**it)[indexer];
addWeight (paramVarLid, posWeight, 1.0);
Clause clause1 (*(*it)->constr());
for (unsigned i = 0; i < groups.size(); i++) {
LiteralId lid = getLiteralId (groups[i], indexer[i]);
clause1.addAndNegateLiteral (Literal (lid, (*it)->argument(i).logVars()));
clause1.addAndNegateLiteral (
Literal (lid, (*it)->argument(i).logVars()));
ConstraintTree ct = *(*it)->constr();
Clause tempClause (ct);
tempClause.addAndNegateLiteral (Literal (paramVarLid, (*it)->constr()->logVars(), 1.0));
tempClause.addAndNegateLiteral (Literal (
paramVarLid, (*it)->constr()->logVars()));
tempClause.addLiteral (Literal (lid, (*it)->argument(i).logVars()));
clauses_.push_back (tempClause);
}
clause1.addLiteral (Literal (paramVarLid, (*it)->constr()->logVars(),weight));
clause1.addLiteral (Literal (paramVarLid, (*it)->constr()->logVars()));
clauses_.push_back (clause1);
freeLiteralId_ ++;
++ indexer;
@ -464,42 +500,13 @@ LiftedWCNF::printFormulaIndicators (void) const
void
LiftedWCNF::printWeights (void) const
{
for (LiteralId i = 0; i < freeLiteralId_; i++) {
bool found = false;
for (size_t j = 0; j < clauses_.size(); j++) {
Literals literals = clauses_[j].literals();
for (size_t k = 0; k < literals.size(); k++) {
if (literals[k].lid() == i && literals[k].isPositive()) {
cout << "weight(" << literals[k] << ") = " ;
cout << literals[k].weight();
unordered_map<LiteralId, std::pair<double,double>>::const_iterator it;
it = weights_.begin();
while (it != weights_.end()) {
cout << "λ" << it->first << " weights: " ;
cout << it->second.first << " " << it->second.second;
cout << endl;
found = true;
break;
}
}
if (found == true) {
break;
}
}
found = false;
for (size_t j = 0; j < clauses_.size(); j++) {
Literals literals = clauses_[j].literals();
for (size_t k = 0; k < literals.size(); k++) {
if (literals[k].lid() == i && literals[k].isNegative()) {
cout << "weight(" << literals[k] << ") = " ;
cout << literals[k].weight();
cout << endl;
found = true;
break;
}
}
if (found == true) {
break;
}
}
++ it;
}
}

View File

@ -23,14 +23,11 @@ typedef vector<LogVarType> LogVarTypes;
class Literal
{
public:
Literal (LiteralId lid, double w = -1.0) :
lid_(lid), weight_(w), negated_(false) { }
Literal (LiteralId lid, const LogVars& lvs, double w = -1.0) :
lid_(lid), logVars_(lvs), weight_(w), negated_(false) { }
Literal (LiteralId lid, const LogVars& lvs) :
lid_(lid), logVars_(lvs), negated_(false) { }
Literal (const Literal& lit, bool negated) :
lid_(lit.lid_), logVars_(lit.logVars_), weight_(lit.weight_), negated_(negated) { }
lid_(lit.lid_), logVars_(lit.logVars_), negated_(negated) { }
LiteralId lid (void) const { return lid_; }
@ -38,9 +35,6 @@ class Literal
LogVarSet logVarSet (void) const { return LogVarSet (logVars_); }
// FIXME this is not log aware :(
double weight (void) const { return weight_ < 0.0 ? 1.0 : weight_; }
void negate (void) { negated_ = !negated_; }
bool isPositive (void) const { return negated_ == false; }
@ -58,7 +52,6 @@ class Literal
private:
LiteralId lid_;
LogVars logVars_;
double weight_;
bool negated_;
};
@ -102,6 +95,10 @@ class Clause
LogVarSet negativeCountedLogVars (void) const { return negCountedLvs_; }
unsigned nrPositiveCountedLogVars (void) const { return posCountedLvs_.size(); }
unsigned nrNegativeCountedLogVars (void) const { return negCountedLvs_.size(); }
bool containsLiteral (LiteralId lid) const;
bool containsPositiveLiteral (LiteralId lid, const LogVarTypes&) const;
@ -187,6 +184,20 @@ class LiftedWCNF
const Clauses& clauses (void) const { return clauses_; }
double posWeight (LiteralId lid) const
{
unordered_map<LiteralId, std::pair<double,double>>::const_iterator it;
it = weights_.find (lid);
return it != weights_.end() ? it->second.first : 1.0;
}
double negWeight (LiteralId lid) const
{
unordered_map<LiteralId, std::pair<double,double>>::const_iterator it;
it = weights_.find (lid);
return it != weights_.end() ? it->second.second : 1.0;
}
Clause createClauseForLiteral (LiteralId lid) const;
void printFormulaIndicators (void) const;
@ -196,9 +207,6 @@ class LiftedWCNF
void printClauses (void) const;
private:
void addIndicatorClauses (const ParfactorList& pfList);
void addParameterClauses (const ParfactorList& pfList);
LiteralId getLiteralId (PrvGroup prvGroup, unsigned range)
{
@ -206,10 +214,21 @@ class LiftedWCNF
return map_[prvGroup][range];
}
void addWeight (LiteralId lid, double posW, double negW)
{
weights_[lid] = make_pair (posW, negW);
}
void addIndicatorClauses (const ParfactorList& pfList);
void addParameterClauses (const ParfactorList& pfList);
Clauses clauses_;
unordered_map<PrvGroup, vector<LiteralId>> map_;
unordered_map<LiteralId, std::pair<double,double>> weights_;
const ParfactorList& pfList_;
LiteralId freeLiteralId_;