inital code for lifted knowledge compilation
This commit is contained in:
parent
b24922fd38
commit
a8926fe38b
266
packages/CLPBN/horus/LiftedCircuit.cpp
Normal file
266
packages/CLPBN/horus/LiftedCircuit.cpp
Normal file
@ -0,0 +1,266 @@
|
||||
#include <fstream>
|
||||
|
||||
#include "LiftedCircuit.h"
|
||||
|
||||
|
||||
LiftedCircuit::LiftedCircuit (const LiftedWCNF* lwcnf)
|
||||
: lwcnf_(lwcnf)
|
||||
{
|
||||
root_ = 0;
|
||||
Clauses ccc = lwcnf->clauses();
|
||||
//ccc.erase (ccc.begin() + 3, ccc.end());
|
||||
//Clause c2 = ccc.front();
|
||||
//c2.removeLiteralByIndex (1);
|
||||
//ccc.push_back (c2);
|
||||
|
||||
|
||||
//compile (&root_, lwcnf->clauses());
|
||||
compile (&root_, ccc);
|
||||
cout << "done compiling..." << endl;
|
||||
printToDot();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
LiftedCircuit::printToDot (void)
|
||||
{
|
||||
const char* fileName = "circuit.dot" ;
|
||||
ofstream out (fileName);
|
||||
if (!out.is_open()) {
|
||||
cerr << "error: cannot open file to write at " ;
|
||||
cerr << "BayesBallGraph::exportToDotFile()" << endl;
|
||||
abort();
|
||||
}
|
||||
out << "digraph {" << endl;
|
||||
out << "ranksep=1" << endl;
|
||||
printToDot (root_, out);
|
||||
out << "}" << endl;
|
||||
out.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
LiftedCircuit::compile (
|
||||
CircuitNode** follow,
|
||||
const Clauses& clauses)
|
||||
{
|
||||
if (clauses.empty()) {
|
||||
*follow = new TrueNode ();
|
||||
return;
|
||||
}
|
||||
|
||||
if (clauses.size() == 1 && clauses[0].isUnit()) {
|
||||
*follow = new LeafNode (clauses[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tryUnitPropagation (follow, clauses)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (tryIndependence (follow, clauses)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (tryShannonDecomposition (follow, clauses)) {
|
||||
return;
|
||||
}
|
||||
|
||||
*follow = new FailNode (clauses);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
LiftedCircuit::tryUnitPropagation (
|
||||
CircuitNode** follow,
|
||||
const Clauses& clauses)
|
||||
{
|
||||
for (size_t i = 0; i < clauses.size(); i++) {
|
||||
if (clauses[i].isUnit()) {
|
||||
Clauses newClauses;
|
||||
for (size_t j = 0; j < clauses.size(); j++) {
|
||||
if (i != j) {
|
||||
LiteralId lid = clauses[i].literals()[0].lid();
|
||||
if (clauses[i].literals()[0].isPositive()) {
|
||||
if (clauses[j].containsPositiveLiteral (lid) == false) {
|
||||
Clause newClause = clauses[j];
|
||||
newClause.removeNegativeLiterals (lid);
|
||||
newClauses.push_back (newClause);
|
||||
}
|
||||
}
|
||||
if (clauses[i].literals()[0].isNegative()) {
|
||||
if (clauses[j].containsNegativeLiteral (lid) == false) {
|
||||
Clause newClause = clauses[j];
|
||||
newClause.removePositiveLiterals (lid);
|
||||
newClauses.push_back (newClause);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stringstream explanation;
|
||||
explanation << " UP of " << clauses[i];
|
||||
AndNode* andNode = new AndNode (clauses, explanation.str());
|
||||
compile (andNode->leftFollow(), {clauses[i]});
|
||||
compile (andNode->rightFollow(), newClauses);
|
||||
(*follow) = andNode;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
LiftedCircuit::tryIndependence (
|
||||
CircuitNode** follow,
|
||||
const Clauses& clauses)
|
||||
{
|
||||
if (clauses.size() == 1) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < clauses.size(); i++) {
|
||||
bool indep = true;
|
||||
TinySet<LiteralId> lids1 = clauses[i].lidSet();
|
||||
for (size_t j = 0; j < clauses.size(); j++) {
|
||||
TinySet<LiteralId> lids2 = clauses[j].lidSet();
|
||||
if (((lids1 & lids2).empty() == false) && i != j) {
|
||||
indep = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (indep == true) {
|
||||
Clauses newClauses = clauses;
|
||||
newClauses.erase (newClauses.begin() + i);
|
||||
stringstream explanation;
|
||||
explanation << " independence" ;
|
||||
AndNode* andNode = new AndNode (clauses, explanation.str());
|
||||
compile (andNode->leftFollow(), {clauses[i]});
|
||||
compile (andNode->rightFollow(), newClauses);
|
||||
(*follow) = andNode;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
LiftedCircuit::tryShannonDecomposition (
|
||||
CircuitNode** follow,
|
||||
const Clauses& clauses)
|
||||
{
|
||||
for (size_t i = 0; i < clauses.size(); i++) {
|
||||
const Literals& literals = clauses[i].literals();
|
||||
for (size_t j = 0; j < literals.size(); j++) {
|
||||
if (literals[j].isGround (clauses[i].constr())) {
|
||||
Literal posLit (literals[j], false);
|
||||
Literal negLit (literals[j], true);
|
||||
ConstraintTree* ct1 = new ConstraintTree (*clauses[i].constr());
|
||||
ConstraintTree* ct2 = new ConstraintTree (*clauses[i].constr());
|
||||
Clause c1 (ct1);
|
||||
Clause c2 (ct2);
|
||||
c1.addLiteral (posLit);
|
||||
c2.addLiteral (negLit);
|
||||
Clauses leftClauses = { c1 };
|
||||
Clauses rightClauses = { c2 };
|
||||
leftClauses.insert (leftClauses.end(), clauses.begin(), clauses.end());
|
||||
rightClauses.insert (rightClauses.end(), clauses.begin(), clauses.end());
|
||||
stringstream explanation;
|
||||
explanation << " SD on " << literals[j];
|
||||
OrNode* orNode = new OrNode (clauses, explanation.str());
|
||||
(*follow) = orNode;
|
||||
compile (orNode->leftFollow(), leftClauses);
|
||||
compile (orNode->rightFollow(), rightClauses);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
string
|
||||
LiftedCircuit::escapeNode (const CircuitNode* node) const
|
||||
{
|
||||
stringstream ss;
|
||||
ss << "\"" << node << "\"" ;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
LiftedCircuit::printToDot (CircuitNode* node, ofstream& os)
|
||||
{
|
||||
assert (node != 0);
|
||||
static unsigned nrAndNodes = 0;
|
||||
static unsigned nrOrNodes = 0;
|
||||
|
||||
if (dynamic_cast<AndNode*>(node) != 0) {
|
||||
nrAndNodes ++;
|
||||
AndNode* casted = dynamic_cast<AndNode*>(node);
|
||||
os << escapeNode (node) << " [shape=box,label=\"" ;
|
||||
const Clauses& clauses = node->clauses();
|
||||
for (size_t i = 0; i < clauses.size(); i++) {
|
||||
os << clauses[i] << "\\n" ;
|
||||
}
|
||||
os << "\"]" ;
|
||||
os << endl;
|
||||
os << "and" << nrAndNodes << " [label=\"∧\"]" << endl;
|
||||
os << '"' << node << '"' << " -> " << "and" << nrAndNodes;
|
||||
os << " [label=\"" << node->explanation() << "\"]" << endl;
|
||||
os << "and" << nrAndNodes << " -> " ;
|
||||
os << escapeNode (*casted->leftFollow()) << endl;
|
||||
os << "and" << nrAndNodes << " -> " ;
|
||||
os << escapeNode (*casted->rightFollow()) << endl;
|
||||
printToDot (*casted->leftFollow(), os);
|
||||
printToDot (*casted->rightFollow(), os);
|
||||
} else if (dynamic_cast<OrNode*>(node) != 0) {
|
||||
nrOrNodes ++;
|
||||
OrNode* casted = dynamic_cast<OrNode*>(node);
|
||||
os << escapeNode (node) << " [shape=box,label=\"" ;
|
||||
const Clauses& clauses = node->clauses();
|
||||
for (size_t i = 0; i < clauses.size(); i++) {
|
||||
os << clauses[i] << "\\n" ;
|
||||
}
|
||||
os << "\"]" ;
|
||||
os << endl;
|
||||
os << "or" << nrOrNodes << " [label=\"∨\"]" << endl;
|
||||
os << '"' << node << '"' << " -> " << "or" << nrOrNodes;
|
||||
os << " [label=\"" << node->explanation() << "\"]" << endl;
|
||||
os << "or" << nrOrNodes << " -> " ;
|
||||
os << escapeNode (*casted->leftFollow()) << endl;
|
||||
os << "or" << nrOrNodes << " -> " ;
|
||||
os << escapeNode (*casted->rightFollow()) << endl;
|
||||
printToDot (*casted->leftFollow(), os);
|
||||
printToDot (*casted->rightFollow(), os);
|
||||
} else if (dynamic_cast<LeafNode*>(node) != 0) {
|
||||
os << escapeNode (node) << " [shape=box,label=\"" ;
|
||||
os << node->clauses()[0] << "\\n" ;
|
||||
os << "\"]" ;
|
||||
os << endl;
|
||||
} else if (dynamic_cast<TrueNode*>(node) != 0) {
|
||||
os << escapeNode (node);
|
||||
os << " [shape=box,label=\"⊤\"]" ;
|
||||
os << endl;
|
||||
} else if (dynamic_cast<FailNode*>(node) != 0) {
|
||||
os << escapeNode (node);
|
||||
os << " [shape=box,style=filled,fillcolor=red,label=\"" ;
|
||||
const Clauses& clauses = node->clauses();
|
||||
for (size_t i = 0; i < clauses.size(); i++) {
|
||||
os << clauses[i] << "\\n" ;
|
||||
}
|
||||
os << "\"]" ;
|
||||
os << endl;
|
||||
} else {
|
||||
cout << "something really failled" << endl;
|
||||
}
|
||||
}
|
||||
|
137
packages/CLPBN/horus/LiftedCircuit.h
Normal file
137
packages/CLPBN/horus/LiftedCircuit.h
Normal file
@ -0,0 +1,137 @@
|
||||
#ifndef HORUS_LIFTEDCIRCUIT_H
|
||||
#define HORUS_LIFTEDCIRCUIT_H
|
||||
|
||||
#include "LiftedWCNF.h"
|
||||
|
||||
|
||||
class CircuitNode
|
||||
{
|
||||
public:
|
||||
CircuitNode (const Clauses& clauses, string explanation = "")
|
||||
: clauses_(clauses), explanation_(explanation) { }
|
||||
|
||||
const Clauses& clauses (void) { return clauses_; }
|
||||
|
||||
virtual double weight (void) const { return 0; }
|
||||
|
||||
string explanation (void) const { return explanation_; }
|
||||
|
||||
private:
|
||||
Clauses clauses_;
|
||||
string explanation_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class AndNode : public CircuitNode
|
||||
{
|
||||
public:
|
||||
AndNode (const Clauses& clauses, string explanation = "")
|
||||
: CircuitNode (clauses, explanation),
|
||||
leftFollow_(0), rightFollow_(0) { }
|
||||
|
||||
CircuitNode** leftFollow (void) { return &leftFollow_; }
|
||||
CircuitNode** rightFollow (void) { return &rightFollow_; }
|
||||
private:
|
||||
CircuitNode* leftFollow_;
|
||||
CircuitNode* rightFollow_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class OrNode : public CircuitNode
|
||||
{
|
||||
public:
|
||||
OrNode (const Clauses& clauses, string explanation = "")
|
||||
: CircuitNode (clauses, explanation),
|
||||
leftFollow_(0), rightFollow_(0) { }
|
||||
|
||||
CircuitNode** leftFollow (void) { return &leftFollow_; }
|
||||
CircuitNode** rightFollow (void) { return &rightFollow_; }
|
||||
private:
|
||||
CircuitNode* leftFollow_;
|
||||
CircuitNode* rightFollow_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class SetAndNode : public CircuitNode
|
||||
{
|
||||
public:
|
||||
private:
|
||||
CircuitNode* follow_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class SetOrNode : public CircuitNode
|
||||
{
|
||||
public:
|
||||
private:
|
||||
CircuitNode* follow_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class IncExclNode : public CircuitNode
|
||||
{
|
||||
public:
|
||||
private:
|
||||
CircuitNode* xFollow_;
|
||||
CircuitNode* yFollow_;
|
||||
CircuitNode* zFollow_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class LeafNode : public CircuitNode
|
||||
{
|
||||
public:
|
||||
LeafNode (const Clause& clause) : CircuitNode ({clause}) { }
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
|
||||
class TrueNode : public CircuitNode
|
||||
{
|
||||
public:
|
||||
TrueNode () : CircuitNode ({}) { }
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
class FailNode : public CircuitNode
|
||||
{
|
||||
public:
|
||||
FailNode (const Clauses& clauses) : CircuitNode (clauses) { }
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
|
||||
class LiftedCircuit
|
||||
{
|
||||
public:
|
||||
LiftedCircuit (const LiftedWCNF* lwcnf);
|
||||
|
||||
void printToDot (void);
|
||||
|
||||
private:
|
||||
|
||||
void compile (CircuitNode** follow, const Clauses& clauses);
|
||||
|
||||
bool tryUnitPropagation (CircuitNode** follow, const Clauses& clauses);
|
||||
bool tryIndependence (CircuitNode** follow, const Clauses& clauses);
|
||||
bool tryShannonDecomposition (CircuitNode** follow, const Clauses& clauses);
|
||||
|
||||
string escapeNode (const CircuitNode*) const;
|
||||
void printToDot (CircuitNode* node, ofstream&);
|
||||
|
||||
CircuitNode* root_;
|
||||
const LiftedWCNF* lwcnf_;
|
||||
};
|
||||
|
||||
#endif // HORUS_LIFTEDCIRCUIT_H
|
270
packages/CLPBN/horus/LiftedWCNF.cpp
Normal file
270
packages/CLPBN/horus/LiftedWCNF.cpp
Normal file
@ -0,0 +1,270 @@
|
||||
#include "LiftedWCNF.h"
|
||||
#include "ConstraintTree.h"
|
||||
#include "Indexer.h"
|
||||
|
||||
|
||||
bool
|
||||
Literal::isGround (ConstraintTree* constr) const
|
||||
{
|
||||
if (logVars_.size() == 0) {
|
||||
return true;
|
||||
}
|
||||
LogVarSet singletons = constr->singletons();
|
||||
return singletons.contains (logVars_);
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::ostream& operator<< (ostream &os, const Literal& lit)
|
||||
{
|
||||
lit.negated_ ? os << "¬" : os << "" ;
|
||||
lit.weight_ < 0.0 ? os << "λ" : os << "Θ" ;
|
||||
os << lit.lid_ ;
|
||||
if (lit.logVars_.empty() == false) {
|
||||
os << "(" ;
|
||||
for (size_t i = 0; i < lit.logVars_.size(); i++) {
|
||||
if (i != 0) os << ",";
|
||||
os << lit.logVars_[i];
|
||||
}
|
||||
os << ")" ;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
Clause::containsLiteral (LiteralId lid) const
|
||||
{
|
||||
for (size_t i = 0; i < literals_.size(); i++) {
|
||||
if (literals_[i].lid() == lid) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
Clause::containsPositiveLiteral (LiteralId lid) const
|
||||
{
|
||||
for (size_t i = 0; i < literals_.size(); i++) {
|
||||
if (literals_[i].lid() == lid && literals_[i].isPositive()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
Clause::containsNegativeLiteral (LiteralId lid) const
|
||||
{
|
||||
for (size_t i = 0; i < literals_.size(); i++) {
|
||||
if (literals_[i].lid() == lid && literals_[i].isNegative()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
Clause::removeLiterals (LiteralId lid)
|
||||
{
|
||||
size_t i = 0;
|
||||
while (i != literals_.size()) {
|
||||
if (literals_[i].lid() == lid) {
|
||||
literals_.erase (literals_.begin() + i);
|
||||
} else {
|
||||
i ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
Clause::removePositiveLiterals (LiteralId lid)
|
||||
{
|
||||
size_t i = 0;
|
||||
while (i != literals_.size()) {
|
||||
if (literals_[i].lid() == lid && literals_[i].isPositive()) {
|
||||
literals_.erase (literals_.begin() + i);
|
||||
} else {
|
||||
i ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
Clause::removeNegativeLiterals (LiteralId lid)
|
||||
{
|
||||
size_t i = 0;
|
||||
while (i != literals_.size()) {
|
||||
if (literals_[i].lid() == lid && literals_[i].isNegative()) {
|
||||
literals_.erase (literals_.begin() + i);
|
||||
} else {
|
||||
i ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
TinySet<LiteralId>
|
||||
Clause::lidSet (void) const
|
||||
{
|
||||
TinySet<LiteralId> lidSet;
|
||||
for (size_t i = 0; i < literals_.size(); i++) {
|
||||
lidSet.insert (literals_[i].lid());
|
||||
}
|
||||
return lidSet;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::ostream& operator<< (ostream &os, const Clause& clause)
|
||||
{
|
||||
for (unsigned i = 0; i < clause.literals_.size(); i++) {
|
||||
if (i != 0) os << " v " ;
|
||||
os << clause.literals_[i];
|
||||
}
|
||||
if (clause.ct_->empty() == false) {
|
||||
ConstraintTree copy (*clause.ct_);
|
||||
copy.moveToTop (copy.logVarSet().elements());
|
||||
os << " | " << copy.tupleSet();
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
|
||||
LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
|
||||
: pfList_(pfList), freeLiteralId_(0)
|
||||
{
|
||||
addIndicatorClauses (pfList);
|
||||
addParameterClauses (pfList);
|
||||
printFormulasToIndicators();
|
||||
printClauses();
|
||||
}
|
||||
|
||||
|
||||
|
||||
LiftedWCNF::~LiftedWCNF (void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
LiftedWCNF::addIndicatorClauses (const ParfactorList& pfList)
|
||||
{
|
||||
ParfactorList::const_iterator it = pfList.begin();
|
||||
set<PrvGroup> allGroups;
|
||||
while (it != pfList.end()) {
|
||||
const ProbFormulas& formulas = (*it)->arguments();
|
||||
for (size_t i = 0; i < formulas.size(); i++) {
|
||||
if (Util::contains (allGroups, formulas[i].group()) == false) {
|
||||
allGroups.insert (formulas[i].group());
|
||||
ConstraintTree* tempConstr = new ConstraintTree (*(*it)->constr());
|
||||
tempConstr->project (formulas[i].logVars());
|
||||
Clause clause (tempConstr);
|
||||
vector<LiteralId> lids;
|
||||
for (size_t j = 0; j < formulas[i].range(); j++) {
|
||||
clause.addLiteral (Literal (freeLiteralId_, formulas[i].logVars()));
|
||||
lids.push_back (freeLiteralId_);
|
||||
freeLiteralId_ ++;
|
||||
}
|
||||
clauses_.push_back (clause);
|
||||
for (size_t j = 0; j < formulas[i].range() - 1; j++) {
|
||||
for (size_t k = j + 1; k < formulas[i].range(); k++) {
|
||||
ConstraintTree* tempConstr2 = new ConstraintTree (*(*it)->constr());
|
||||
tempConstr2->project (formulas[i].logVars());
|
||||
Clause clause2 (tempConstr2);
|
||||
clause2.addAndNegateLiteral (Literal (clause.literals()[j]));
|
||||
clause2.addAndNegateLiteral (Literal (clause.literals()[k]));
|
||||
clauses_.push_back (clause2);
|
||||
}
|
||||
}
|
||||
map_[formulas[i].group()] = lids;
|
||||
}
|
||||
}
|
||||
++ it;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
LiftedWCNF::addParameterClauses (const ParfactorList& pfList)
|
||||
{
|
||||
ParfactorList::const_iterator it = pfList.begin();
|
||||
while (it != pfList.end()) {
|
||||
Indexer indexer ((*it)->ranges());
|
||||
vector<PrvGroup> groups = (*it)->getAllGroups();
|
||||
while (indexer.valid()) {
|
||||
LiteralId paramVarLid = freeLiteralId_;
|
||||
|
||||
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()));
|
||||
|
||||
Clause tempClause ((*it)->constr());
|
||||
tempClause.addAndNegateLiteral (Literal (paramVarLid, LogVars(), 1.0));
|
||||
tempClause.addLiteral (Literal (lid, (*it)->argument(i).logVars()));
|
||||
clauses_.push_back (tempClause);
|
||||
}
|
||||
clause1.addLiteral (Literal (paramVarLid, LogVars(), 1.0));
|
||||
clauses_.push_back (clause1);
|
||||
freeLiteralId_ ++;
|
||||
++ indexer;
|
||||
}
|
||||
++ it;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
LiftedWCNF::printClauses (void) const
|
||||
{
|
||||
for (unsigned i = 0; i < clauses_.size(); i++) {
|
||||
cout << clauses_[i] << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
LiftedWCNF::printFormulasToIndicators (void) const
|
||||
{
|
||||
set<PrvGroup> allGroups;
|
||||
ParfactorList::const_iterator it = pfList_.begin();
|
||||
while (it != pfList_.end()) {
|
||||
const ProbFormulas& formulas = (*it)->arguments();
|
||||
for (size_t i = 0; i < formulas.size(); i++) {
|
||||
if (Util::contains (allGroups, formulas[i].group()) == false) {
|
||||
allGroups.insert (formulas[i].group());
|
||||
cout << formulas[i] << " | " ;
|
||||
ConstraintTree tempCt (*(*it)->constr());
|
||||
tempCt.project (formulas[i].logVars());
|
||||
cout << tempCt.tupleSet();
|
||||
cout << " indicators => " ;
|
||||
vector<LiteralId> indicators =
|
||||
(map_.find (formulas[i].group()))->second;
|
||||
cout << indicators << endl;
|
||||
}
|
||||
}
|
||||
++ it;
|
||||
}
|
||||
}
|
||||
|
129
packages/CLPBN/horus/LiftedWCNF.h
Normal file
129
packages/CLPBN/horus/LiftedWCNF.h
Normal file
@ -0,0 +1,129 @@
|
||||
#ifndef HORUS_LIFTEDWCNF_H
|
||||
#define HORUS_LIFTEDWCNF_H
|
||||
|
||||
#include "ParfactorList.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef long LiteralId;
|
||||
|
||||
class ConstraintTree;
|
||||
|
||||
|
||||
class Literal
|
||||
{
|
||||
public:
|
||||
Literal (LiteralId lid, const LogVars& lvs, double w = -1.0) :
|
||||
lid_(lid), logVars_(lvs), weight_(w), negated_(false) { }
|
||||
|
||||
Literal (const Literal& lit, bool negated) :
|
||||
lid_(lit.lid_), logVars_(lit.logVars_), weight_(lit.weight_), negated_(negated) { }
|
||||
|
||||
LiteralId lid (void) const { return lid_; }
|
||||
|
||||
double weight (void) const { return weight_; }
|
||||
|
||||
void negate (void) { negated_ = !negated_; }
|
||||
|
||||
bool isPositive (void) const { return negated_ == false; }
|
||||
|
||||
bool isNegative (void) const { return negated_; }
|
||||
|
||||
bool isGround (ConstraintTree* constr) const;
|
||||
|
||||
friend std::ostream& operator<< (ostream &os, const Literal& lit);
|
||||
|
||||
private:
|
||||
LiteralId lid_;
|
||||
LogVars logVars_;
|
||||
double weight_;
|
||||
bool negated_;
|
||||
};
|
||||
|
||||
typedef vector<Literal> Literals;
|
||||
|
||||
|
||||
class Clause
|
||||
{
|
||||
public:
|
||||
Clause (ConstraintTree* ct) : ct_(ct) { }
|
||||
|
||||
void addLiteral (const Literal& l) { literals_.push_back (l); }
|
||||
|
||||
void addAndNegateLiteral (const Literal& l)
|
||||
{
|
||||
literals_.push_back (l);
|
||||
literals_.back().negate();
|
||||
}
|
||||
|
||||
bool containsLiteral (LiteralId lid) const;
|
||||
|
||||
bool containsPositiveLiteral (LiteralId lid) const;
|
||||
|
||||
bool containsNegativeLiteral (LiteralId lid) const;
|
||||
|
||||
void removeLiterals (LiteralId lid);
|
||||
|
||||
void removePositiveLiterals (LiteralId lid);
|
||||
|
||||
void removeNegativeLiterals (LiteralId lid);
|
||||
|
||||
const vector<Literal>& literals (void) const { return literals_; }
|
||||
|
||||
void removeLiteralByIndex (size_t idx) { literals_.erase (literals_.begin() + idx); }
|
||||
|
||||
ConstraintTree* constr (void) const { return ct_; }
|
||||
|
||||
ConstraintTree* constr (void) { return ct_; }
|
||||
|
||||
bool isUnit (void) const { return literals_.size() == 1; }
|
||||
|
||||
TinySet<LiteralId> lidSet (void) const;
|
||||
|
||||
friend std::ostream& operator<< (ostream &os, const Clause& clause);
|
||||
|
||||
private:
|
||||
vector<Literal> literals_;
|
||||
ConstraintTree* ct_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
typedef vector<Clause> Clauses;
|
||||
|
||||
|
||||
|
||||
class LiftedWCNF
|
||||
{
|
||||
public:
|
||||
LiftedWCNF (const ParfactorList& pfList);
|
||||
|
||||
~LiftedWCNF (void);
|
||||
|
||||
const Clauses& clauses (void) const { return clauses_; }
|
||||
|
||||
void printClauses (void) const;
|
||||
|
||||
void printFormulasToIndicators (void) const;
|
||||
|
||||
private:
|
||||
void addIndicatorClauses (const ParfactorList& pfList);
|
||||
|
||||
void addParameterClauses (const ParfactorList& pfList);
|
||||
|
||||
LiteralId getLiteralId (PrvGroup prvGroup, unsigned range)
|
||||
{
|
||||
assert (Util::contains (map_, prvGroup));
|
||||
return map_[prvGroup][range];
|
||||
}
|
||||
|
||||
Clauses clauses_;
|
||||
|
||||
unordered_map<PrvGroup,vector<LiteralId>> map_;
|
||||
|
||||
const ParfactorList& pfList_;
|
||||
|
||||
LiteralId freeLiteralId_;
|
||||
};
|
||||
|
||||
#endif // HORUS_LIFTEDWCNF_H
|
@ -23,10 +23,10 @@ CC=@CC@
|
||||
CXX=@CXX@
|
||||
|
||||
# normal
|
||||
CXXFLAGS= -std=c++0x @SHLIB_CXXFLAGS@ $(YAP_EXTRAS) $(DEFS) -D_YAP_NOT_INSTALLED_=1 -I$(srcdir) -I../../.. -I$(srcdir)/../../../include @CPPFLAGS@ -DNDEBUG
|
||||
#CXXFLAGS= -std=c++0x @SHLIB_CXXFLAGS@ $(YAP_EXTRAS) $(DEFS) -D_YAP_NOT_INSTALLED_=1 -I$(srcdir) -I../../.. -I$(srcdir)/../../../include @CPPFLAGS@ -DNDEBUG
|
||||
|
||||
# debug
|
||||
#CXXFLAGS= -std=c++0x @SHLIB_CXXFLAGS@ $(YAP_EXTRAS) $(DEFS) -D_YAP_NOT_INSTALLED_=1 -I$(srcdir) -I../../.. -I$(srcdir)/../../../include @CPPFLAGS@ -g -O0 -Wextra
|
||||
CXXFLAGS= -std=c++0x @SHLIB_CXXFLAGS@ $(YAP_EXTRAS) $(DEFS) -D_YAP_NOT_INSTALLED_=1 -I$(srcdir) -I../../.. -I$(srcdir)/../../../include @CPPFLAGS@ -g -O0 -Wextra
|
||||
|
||||
|
||||
#
|
||||
@ -57,8 +57,10 @@ HEADERS = \
|
||||
$(srcdir)/Horus.h \
|
||||
$(srcdir)/Indexer.h \
|
||||
$(srcdir)/LiftedBp.h \
|
||||
$(srcdir)/LiftedCircuit.h \
|
||||
$(srcdir)/LiftedUtils.h \
|
||||
$(srcdir)/LiftedVe.h \
|
||||
$(srcdir)/LiftedWCNF.h \
|
||||
$(srcdir)/Parfactor.h \
|
||||
$(srcdir)/ParfactorList.h \
|
||||
$(srcdir)/ProbFormula.h \
|
||||
@ -82,8 +84,10 @@ CPP_SOURCES = \
|
||||
$(srcdir)/HorusCli.cpp \
|
||||
$(srcdir)/HorusYap.cpp \
|
||||
$(srcdir)/LiftedBp.cpp \
|
||||
$(srcdir)/LiftedCircuit.h \
|
||||
$(srcdir)/LiftedUtils.cpp \
|
||||
$(srcdir)/LiftedVe.cpp \
|
||||
$(srcdir)/LiftedWCNF.cpp \
|
||||
$(srcdir)/Parfactor.cpp \
|
||||
$(srcdir)/ParfactorList.cpp \
|
||||
$(srcdir)/ProbFormula.cpp \
|
||||
@ -91,7 +95,7 @@ CPP_SOURCES = \
|
||||
$(srcdir)/Util.cpp \
|
||||
$(srcdir)/Var.cpp \
|
||||
$(srcdir)/VarElim.cpp \
|
||||
$(srcdir)/WeightedBp.cpp \
|
||||
$(srcdir)/WeightedBp.cpp
|
||||
|
||||
OBJS = \
|
||||
BayesBall.o \
|
||||
@ -105,8 +109,10 @@ OBJS = \
|
||||
Histogram.o \
|
||||
HorusYap.o \
|
||||
LiftedBp.o \
|
||||
LiftedCircuit.o \
|
||||
LiftedUtils.o \
|
||||
LiftedVe.o \
|
||||
LiftedWCNF.o \
|
||||
ProbFormula.o \
|
||||
Parfactor.o \
|
||||
ParfactorList.o \
|
||||
|
Reference in New Issue
Block a user