first stab for atom counting

This commit is contained in:
Tiago Gomes 2012-10-31 23:43:39 +00:00
parent e762d6be22
commit 6877be897e
6 changed files with 336 additions and 72 deletions

View File

@ -112,6 +112,8 @@ CTNode::copySubtree (const CTNode* root1)
const CTNode* n1 = stack.back().first; const CTNode* n1 = stack.back().first;
CTNode* n2 = stack.back().second; CTNode* n2 = stack.back().second;
stack.pop_back(); stack.pop_back();
// cout << "n2 childs: " << n2->childs();
// cout << "n1 childs: " << n1->childs();
n2->childs().reserve (n1->nrChilds()); n2->childs().reserve (n1->nrChilds());
stack.reserve (n1->nrChilds()); stack.reserve (n1->nrChilds());
for (CTChilds::const_iterator chIt = n1->childs().begin(); for (CTChilds::const_iterator chIt = n1->childs().begin();
@ -185,6 +187,28 @@ ConstraintTree::ConstraintTree (
ConstraintTree::ConstraintTree (vector<vector<string>> names)
{
assert (names.empty() == false);
assert (names.front().empty() == false);
unsigned nrLvs = names[0].size();
for (size_t i = 0; i < nrLvs; i++) {
logVars_.push_back (LogVar (i));
}
root_ = new CTNode (0, 0);
logVarSet_ = LogVarSet (logVars_);
for (size_t i = 0; i < names.size(); i++) {
Tuple t;
for (size_t j = 0; j < names[i].size(); j++) {
assert (names[i].size() == nrLvs);
t.push_back (LiftedUtils::getSymbol (names[i][j]));
}
addTuple (t);
}
}
ConstraintTree::ConstraintTree (const ConstraintTree& ct) ConstraintTree::ConstraintTree (const ConstraintTree& ct)
{ {
*this = ct; *this = ct;

View File

@ -109,6 +109,8 @@ class ConstraintTree
ConstraintTree (const LogVars&, const Tuples&); ConstraintTree (const LogVars&, const Tuples&);
ConstraintTree (vector<vector<string>> names);
ConstraintTree (const ConstraintTree&); ConstraintTree (const ConstraintTree&);
ConstraintTree (const CTChilds& rootChilds, const LogVars& logVars) ConstraintTree (const CTChilds& rootChilds, const LogVars& logVars)

View File

@ -115,7 +115,7 @@ CompilationFailedNode::weight (void) const
{ {
// we should not perform model counting // we should not perform model counting
// in compilation failed nodes // in compilation failed nodes
abort(); // abort();
return 0.0; return 0.0;
} }
@ -125,19 +125,11 @@ LiftedCircuit::LiftedCircuit (const LiftedWCNF* lwcnf)
: lwcnf_(lwcnf) : lwcnf_(lwcnf)
{ {
root_ = 0; root_ = 0;
Clauses ccc = lwcnf->clauses(); Clauses clauses = lwcnf->clauses();
//ccc.erase (ccc.begin() + 5, ccc.end()); compile (&root_, clauses);
//Clause c2 = ccc.front();
//c2.removeLiteralByIndex (1);
//ccc.push_back (c2);
//compile (&root_, lwcnf->clauses());
Clauses cccc = {ccc[6],ccc[4]};
cccc.front().removeLiteral (2);
compile (&root_, cccc);
exportToGraphViz("circuit.dot"); exportToGraphViz("circuit.dot");
smoothCircuit(); smoothCircuit();
exportToGraphViz("smooth.dot"); exportToGraphViz("circuit.smooth.dot");
cout << "WEIGHTED MODEL COUNT = " << getWeightedModelCount() << endl; cout << "WEIGHTED MODEL COUNT = " << getWeightedModelCount() << endl;
} }
@ -188,18 +180,7 @@ LiftedCircuit::compile (
} }
if (clauses.size() == 1 && clauses[0].isUnit()) { if (clauses.size() == 1 && clauses[0].isUnit()) {
static int count = 0; count ++;
*follow = new LeafNode (clauses[0]); *follow = new LeafNode (clauses[0]);
if (count == 1) {
// Clause c (new ConstraintTree({}));
// c.addLiteral (Literal (100,{}));
// *follow = new LeafNode (c);
}
if (count == 2) {
// Clause c (new ConstraintTree({}));
// c.addLiteral (Literal (101,{}));
// *follow = new LeafNode (c);
}
return; return;
} }
@ -219,7 +200,11 @@ LiftedCircuit::compile (
return; return;
} }
if (tryIndepPartialGrounding (follow, clauses)) { //if (tryIndepPartialGrounding (follow, clauses)) {
// return;
//}
if (tryAtomCounting (follow, clauses)) {
return; return;
} }
@ -229,7 +214,81 @@ LiftedCircuit::compile (
// assert (false); // assert (false);
*follow = new CompilationFailedNode (clauses); *follow = new CompilationFailedNode (clauses);
}
void
LiftedCircuit::propagate (
const Clause& c,
const Clause& unitClause,
Clauses& newClauses)
{
/*
Literals literals = c.literals();
for (size_t i = 0; i < literals.size(); i++) {
if (literals_[i].lid() == lid && literals[i].isPositive()) {
return true;
}
}
*/
}
bool
shatterCountedLogVars (Clauses& clauses, size_t idx1, size_t idx2)
{
Literals lits1 = clauses[idx1].literals();
Literals lits2 = clauses[idx2].literals();
for (size_t i = 0; i < lits1.size(); i++) {
for (size_t j = 0; j < lits2.size(); j++) {
if (lits1[i].lid() == lits2[j].lid()) {
LogVars lvs1 = lits1[i].logVars();
LogVars lvs2 = lits2[j].logVars();
for (size_t k = 0; k < lvs1.size(); k++) {
if (clauses[idx1].isCountedLogVar (lvs1[k])
&& clauses[idx2].isCountedLogVar (lvs2[k]) == false) {
clauses.push_back (clauses[idx2]);
clauses[idx2].addPositiveCountedLogVar (lvs2[k]);
clauses.back().addNegativeCountedLogVar (lvs2[k]);
return true;
}
if (clauses[idx2].isCountedLogVar (lvs2[k])
&& clauses[idx1].isCountedLogVar (lvs1[k]) == false) {
clauses.push_back (clauses[idx1]);
clauses[idx1].addPositiveCountedLogVar (lvs1[k]);
clauses.back().addNegativeCountedLogVar (lvs1[k]);
return true;
}
}
}
}
}
return false;
}
bool
shatterCountedLogVarsAux (Clauses& clauses)
{
for (size_t i = 0; i < clauses.size() - 1; i++) {
for (size_t j = i + 1; j < clauses.size(); j++) {
bool splitedSome = shatterCountedLogVars (clauses, i, j);
if (splitedSome) {
return true;
}
}
}
return false;
}
void
shatterCountedLogVars (Clauses& clauses)
{
while (shatterCountedLogVarsAux (clauses)) ;
} }
@ -239,22 +298,29 @@ LiftedCircuit::tryUnitPropagation (
CircuitNode** follow, CircuitNode** follow,
Clauses& clauses) Clauses& clauses)
{ {
cout << "ALL CLAUSES:" << endl;
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;
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) {
LiteralId lid = clauses[i].literals()[0].lid(); LiteralId lid = clauses[i].literals()[0].lid();
LogVarTypes types = clauses[i].logVarTypes (0);
if (clauses[i].literals()[0].isPositive()) { if (clauses[i].literals()[0].isPositive()) {
if (clauses[j].containsPositiveLiteral (lid) == false) { if (clauses[j].containsPositiveLiteral (lid, types) == false) {
Clause newClause = clauses[j]; Clause newClause = clauses[j];
newClause.removeNegativeLiterals (lid); cout << "removing negative literals on " << newClause << endl;
newClause.removeNegativeLiterals (lid, types);
newClauses.push_back (newClause); newClauses.push_back (newClause);
} }
} else if (clauses[i].literals()[0].isNegative()) { } else if (clauses[i].literals()[0].isNegative()) {
if (clauses[j].containsNegativeLiteral (lid) == false) { if (clauses[j].containsNegativeLiteral (lid, types) == false) {
Clause newClause = clauses[j]; Clause newClause = clauses[j];
newClause.removePositiveLiterals (lid); cout << "removing negative literals on " << newClause << endl;
newClause.removePositiveLiterals (lid, types);
newClauses.push_back (newClause); newClauses.push_back (newClause);
} }
} }
@ -264,6 +330,7 @@ LiftedCircuit::tryUnitPropagation (
explanation << " UP on" << clauses[i].literals()[0]; explanation << " UP on" << clauses[i].literals()[0];
AndNode* andNode = new AndNode (clauses, explanation.str()); AndNode* andNode = new AndNode (clauses, explanation.str());
Clauses leftClauses = {clauses[i]}; Clauses leftClauses = {clauses[i]};
cout << "new clauses: " << newClauses << endl;
compile (andNode->leftBranch(), leftClauses); compile (andNode->leftBranch(), leftClauses);
compile (andNode->rightBranch(), newClauses); compile (andNode->rightBranch(), newClauses);
(*follow) = andNode; (*follow) = andNode;
@ -454,6 +521,37 @@ LiftedCircuit::tryIndepPartialGroundingAux (
bool
LiftedCircuit::tryAtomCounting (
CircuitNode** follow,
Clauses& clauses)
{
for (size_t i = 0; i < clauses.size(); i++) {
Literals literals = clauses[i].literals();
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);
Clause c1 (clauses[i].constr().projectedCopy (literals[j].logVars()));
Clause c2 (clauses[i].constr().projectedCopy (literals[j].logVars()));
c1.addLiteral (literals[j]);
c2.addAndNegateLiteral (literals[j]);
c1.addPositiveCountedLogVar (literals[j].logVars().front());
c2.addNegativeCountedLogVar (literals[j].logVars().front());
clauses.push_back (c1);
clauses.push_back (c2);
shatterCountedLogVars (clauses);
compile (setOrNode->follow(), clauses);
*follow = setOrNode;
return true;
}
}
}
return false;
}
bool bool
LiftedCircuit::tryGrounding ( LiftedCircuit::tryGrounding (
CircuitNode**, CircuitNode**,
@ -664,8 +762,21 @@ LiftedCircuit::exportToGraphViz (CircuitNode* node, ofstream& os)
} }
case SET_OR_NODE: { case SET_OR_NODE: {
// TODO SetOrNode* casted = dynamic_cast<SetOrNode*>(node);
assert (false); printClauses (casted, os);
os << auxNode << " [label=\"(X)\"]" << endl;
os << escapeNode (node) << " -> " << auxNode;
os << " [label=\"" << node->explanation() << "\"]" ;
os << endl;
os << auxNode << " -> " ;
os << escapeNode (*casted->follow());
os << " [label=\" " << (*casted->follow())->weight() << "\"]" ;
os << endl;
exportToGraphViz (*casted->follow(), os);
break;
} }
case SET_AND_NODE: { case SET_AND_NODE: {

View File

@ -205,8 +205,11 @@ class LiftedCircuit
bool tryIndepPartialGrounding (CircuitNode** follow, Clauses& clauses); bool tryIndepPartialGrounding (CircuitNode** follow, Clauses& clauses);
bool tryIndepPartialGroundingAux (Clauses& clauses, ConstraintTree& ct, bool tryIndepPartialGroundingAux (Clauses& clauses, ConstraintTree& ct,
vector<unsigned>& indices); vector<unsigned>& indices);
bool tryAtomCounting (CircuitNode** follow, Clauses& clauses);
bool tryGrounding (CircuitNode** follow, Clauses& clauses); bool tryGrounding (CircuitNode** follow, Clauses& clauses);
void propagate (const Clause& c, const Clause& uc, Clauses& newClauses);
TinySet<LiteralId> smoothCircuit (CircuitNode* node); TinySet<LiteralId> smoothCircuit (CircuitNode* node);
void createSmoothNode (const TinySet<LiteralId>& lids, void createSmoothNode (const TinySet<LiteralId>& lids,

View File

@ -17,7 +17,10 @@ Literal::isGround (ConstraintTree constr, LogVarSet ipgLogVars) const
string string
Literal::toString (LogVarSet ipgLogVars) const Literal::toString (
LogVarSet ipgLogVars,
LogVarSet posCountedLvs,
LogVarSet negCountedLvs) const
{ {
stringstream ss; stringstream ss;
negated_ ? ss << "¬" : ss << "" ; negated_ ? ss << "¬" : ss << "" ;
@ -27,7 +30,11 @@ Literal::toString (LogVarSet ipgLogVars) const
ss << "(" ; ss << "(" ;
for (size_t i = 0; i < logVars_.size(); i++) { for (size_t i = 0; i < logVars_.size(); i++) {
if (i != 0) ss << ","; if (i != 0) ss << ",";
if (ipgLogVars.contains (logVars_[i])) { if (posCountedLvs.contains (logVars_[i])) {
ss << "+" << logVars_[i];
} else if (negCountedLvs.contains (logVars_[i])) {
ss << "-" << logVars_[i];
} else if (ipgLogVars.contains (logVars_[i])) {
LogVar X = logVars_[i]; LogVar X = logVars_[i];
const string labels[] = { const string labels[] = {
"a", "b", "c", "d", "e", "f", "a", "b", "c", "d", "e", "f",
@ -66,10 +73,14 @@ Clause::containsLiteral (LiteralId lid) const
bool bool
Clause::containsPositiveLiteral (LiteralId lid) const Clause::containsPositiveLiteral (
LiteralId lid,
const LogVarTypes& types) const
{ {
for (size_t i = 0; i < literals_.size(); i++) { for (size_t i = 0; i < literals_.size(); i++) {
if (literals_[i].lid() == lid && literals_[i].isPositive()) { if (literals_[i].lid() == lid
&& literals_[i].isPositive()
&& logVarTypes (i) == types) {
return true; return true;
} }
} }
@ -79,10 +90,14 @@ Clause::containsPositiveLiteral (LiteralId lid) const
bool bool
Clause::containsNegativeLiteral (LiteralId lid) const Clause::containsNegativeLiteral (
LiteralId lid,
const LogVarTypes& types) const
{ {
for (size_t i = 0; i < literals_.size(); i++) { for (size_t i = 0; i < literals_.size(); i++) {
if (literals_[i].lid() == lid && literals_[i].isNegative()) { if (literals_[i].lid() == lid
&& literals_[i].isNegative()
&& logVarTypes (i) == types) {
return true; return true;
} }
} }
@ -107,11 +122,15 @@ Clause::removeLiterals (LiteralId lid)
void void
Clause::removePositiveLiterals (LiteralId lid) Clause::removePositiveLiterals (
LiteralId lid,
const LogVarTypes& types)
{ {
size_t i = 0; size_t i = 0;
while (i != literals_.size()) { while (i != literals_.size()) {
if (literals_[i].lid() == lid && literals_[i].isPositive()) { if (literals_[i].lid() == lid
&& literals_[i].isPositive()
&& logVarTypes (i) == types) {
removeLiteral (i); removeLiteral (i);
} else { } else {
i ++; i ++;
@ -122,11 +141,15 @@ Clause::removePositiveLiterals (LiteralId lid)
void void
Clause::removeNegativeLiterals (LiteralId lid) Clause::removeNegativeLiterals (
LiteralId lid,
const LogVarTypes& types)
{ {
size_t i = 0; size_t i = 0;
while (i != literals_.size()) { while (i != literals_.size()) {
if (literals_[i].lid() == lid && literals_[i].isNegative()) { if (literals_[i].lid() == lid
&& literals_[i].isNegative()
&& logVarTypes (i) == types) {
removeLiteral (i); removeLiteral (i);
} else { } else {
i ++; i ++;
@ -171,6 +194,34 @@ Clause::lidSet (void) const
bool
Clause::isCountedLogVar (LogVar X) const
{
assert (constr_.logVarSet().contains (X));
return posCountedLvs_.contains (X)
|| negCountedLvs_.contains (X);
}
bool
Clause::isPositiveCountedLogVar (LogVar X) const
{
assert (constr_.logVarSet().contains (X));
return posCountedLvs_.contains (X);
}
bool
Clause::isNegativeCountedLogVar (LogVar X) const
{
assert (constr_.logVarSet().contains (X));
return negCountedLvs_.contains (X);
}
void void
Clause::removeLiteral (size_t idx) Clause::removeLiteral (size_t idx)
{ {
@ -182,6 +233,35 @@ Clause::removeLiteral (size_t idx)
LogVarTypes
Clause::logVarTypes (size_t litIdx) const
{
LogVarTypes types;
const LogVars lvs = literals_[litIdx].logVars();
for (size_t i = 0; i < lvs.size(); i++) {
if (posCountedLvs_.contains (lvs[i])) {
types.push_back (LogVarType::POS_LV);
} else if (negCountedLvs_.contains (lvs[i])) {
types.push_back (LogVarType::NEG_LV);
} else {
types.push_back (LogVarType::FULL_LV);
}
}
return types;
}
void
Clause::printClauses (const Clauses& clauses)
{
for (size_t i = 0; i < clauses.size(); i++) {
cout << clauses[i] << endl;
}
}
LogVarSet LogVarSet
Clause::getLogVarSetExcluding (size_t idx) const Clause::getLogVarSetExcluding (size_t idx) const
{ {
@ -200,7 +280,8 @@ std::ostream& operator<< (ostream &os, const Clause& clause)
{ {
for (unsigned i = 0; i < clause.literals_.size(); i++) { for (unsigned i = 0; i < clause.literals_.size(); i++) {
if (i != 0) os << " v " ; if (i != 0) os << " v " ;
os << clause.literals_[i].toString (clause.ipgLogVars_); os << clause.literals_[i].toString (clause.ipgLogVars_,
clause.posCountedLvs_, clause.negCountedLvs_);
} }
if (clause.constr_.empty() == false) { if (clause.constr_.empty() == false) {
ConstraintTree copy (clause.constr_); ConstraintTree copy (clause.constr_);
@ -215,10 +296,23 @@ std::ostream& operator<< (ostream &os, const Clause& clause)
LiftedWCNF::LiftedWCNF (const ParfactorList& pfList) LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
: pfList_(pfList), freeLiteralId_(0) : pfList_(pfList), freeLiteralId_(0)
{ {
addIndicatorClauses (pfList); //addIndicatorClauses (pfList);
addParameterClauses (pfList); //addParameterClauses (pfList);
vector<vector<string>> names = {{"p1","p1"},{"p2","p2"}};
Clause c1 (names);
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}));
c2.addAndNegateLiteral (Literal (1, {1,0}));
clauses_.push_back(c2);
cout << "FORMULA INDICATORS:" << endl; cout << "FORMULA INDICATORS:" << endl;
printFormulaIndicators(); // printFormulaIndicators();
cout << endl; cout << endl;
cout << "WEIGHTS:" << endl; cout << "WEIGHTS:" << endl;
printWeights(); printWeights();

View File

@ -10,6 +10,16 @@ typedef long LiteralId;
class ConstraintTree; class ConstraintTree;
enum LogVarType
{
FULL_LV,
POS_LV,
NEG_LV
};
typedef vector<LogVarType> LogVarTypes;
class Literal class Literal
{ {
public: public:
@ -39,7 +49,9 @@ class Literal
bool isGround (ConstraintTree constr, LogVarSet ipgLogVars) const; bool isGround (ConstraintTree constr, LogVarSet ipgLogVars) const;
string toString (LogVarSet ipgLogVars = LogVarSet()) const; string toString (LogVarSet ipgLogVars = LogVarSet(),
LogVarSet posCountedLvs = LogVarSet(),
LogVarSet negCountedLvs = LogVarSet()) const;
friend std::ostream& operator<< (ostream &os, const Literal& lit); friend std::ostream& operator<< (ostream &os, const Literal& lit);
@ -58,6 +70,8 @@ class Clause
public: public:
Clause (const ConstraintTree& ct) : constr_(ct) { } Clause (const ConstraintTree& ct) : constr_(ct) { }
Clause (vector<vector<string>> names) : constr_(ConstraintTree (names)) { }
void addLiteral (const Literal& l) { literals_.push_back (l); } void addLiteral (const Literal& l) { literals_.push_back (l); }
void addAndNegateLiteral (const Literal& l) void addAndNegateLiteral (const Literal& l)
@ -68,15 +82,15 @@ class Clause
bool containsLiteral (LiteralId lid) const; bool containsLiteral (LiteralId lid) const;
bool containsPositiveLiteral (LiteralId lid) const; bool containsPositiveLiteral (LiteralId lid, const LogVarTypes&) const;
bool containsNegativeLiteral (LiteralId lid) const; bool containsNegativeLiteral (LiteralId lid, const LogVarTypes&) const;
void removeLiterals (LiteralId lid); void removeLiterals (LiteralId lid);
void removePositiveLiterals (LiteralId lid); void removePositiveLiterals (LiteralId lid, const LogVarTypes&);
void removeNegativeLiterals (LiteralId lid); void removeNegativeLiterals (LiteralId lid, const LogVarTypes&);
const vector<Literal>& literals (void) const { return literals_; } const vector<Literal>& literals (void) const { return literals_; }
@ -92,20 +106,36 @@ class Clause
void addIpgLogVar (LogVar X) { ipgLogVars_.insert (X); } void addIpgLogVar (LogVar X) { ipgLogVars_.insert (X); }
void addPositiveCountedLogVar (LogVar X) { posCountedLvs_.insert (X); }
void addNegativeCountedLogVar (LogVar X) { negCountedLvs_.insert (X); }
LogVarSet ipgCandidates (void) const; LogVarSet ipgCandidates (void) const;
TinySet<LiteralId> lidSet (void) const; TinySet<LiteralId> lidSet (void) const;
bool isCountedLogVar (LogVar X) const;
bool isPositiveCountedLogVar (LogVar X) const;
bool isNegativeCountedLogVar (LogVar X) const;
friend std::ostream& operator<< (ostream &os, const Clause& clause); friend std::ostream& operator<< (ostream &os, const Clause& clause);
void removeLiteral (size_t idx); void removeLiteral (size_t idx);
LogVarTypes logVarTypes (size_t litIdx) const;
static void printClauses (const vector<Clause>& clauses);
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 negCountedLvs_;
ConstraintTree constr_; ConstraintTree constr_;
}; };