This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/horus/LiftedWCNF.cpp

679 lines
14 KiB
C++
Raw Normal View History

2013-02-07 20:09:10 +00:00
#include <cassert>
#include <iostream>
#include <sstream>
#include "LiftedWCNF.h"
2013-02-08 12:12:45 +00:00
#include "ParfactorList.h"
#include "ConstraintTree.h"
#include "Indexer.h"
namespace Horus {
2013-02-07 23:53:13 +00:00
bool
2013-02-08 12:12:45 +00:00
Literal::isGround (
ConstraintTree constr,
const LogVarSet& ipgLogVars) const
{
2012-12-27 12:54:58 +00:00
if (logVars_.empty()) {
return true;
}
LogVarSet lvs (logVars_);
lvs -= ipgLogVars;
return constr.singletons().contains (lvs);
}
size_t
Literal::indexOfLogVar (LogVar X) const
{
return Util::indexOf (logVars_, X);
}
2013-02-07 13:37:15 +00:00
std::string
2012-10-31 23:43:39 +00:00
Literal::toString (
LogVarSet ipgLogVars,
LogVarSet posCountedLvs,
LogVarSet negCountedLvs) const
{
2013-02-07 13:37:15 +00:00
std::stringstream ss;
negated_ ? ss << "¬" : ss << "" ;
ss << "λ" ;
ss << lid_ ;
if (logVars_.empty() == false) {
ss << "(" ;
for (size_t i = 0; i < logVars_.size(); i++) {
if (i != 0) ss << ",";
2012-10-31 23:43:39 +00:00
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];
2013-02-07 13:37:15 +00:00
const std::string labels[] = {
2012-10-31 23:43:39 +00:00
"a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "m" };
(X >= 12) ? ss << "x_" << X : ss << labels[X];
} else {
ss << logVars_[i];
}
}
ss << ")" ;
}
return ss.str();
}
2012-12-10 18:48:54 +00:00
std::ostream&
operator<< (std::ostream& os, const Literal& lit)
{
os << lit.toString();
return os;
}
2012-11-07 15:28:33 +00:00
void
Clause::addLiteralComplemented (const Literal& lit)
{
assert (constr_.logVarSet().contains (lit.logVars()));
literals_.push_back (lit);
literals_.back().complement();
}
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
2012-10-31 23:43:39 +00:00
Clause::containsPositiveLiteral (
LiteralId lid,
const LogVarTypes& types) const
{
for (size_t i = 0; i < literals_.size(); i++) {
2012-10-31 23:43:39 +00:00
if (literals_[i].lid() == lid
&& literals_[i].isPositive()
&& logVarTypes (i) == types) {
return true;
}
}
return false;
}
2012-12-17 18:39:42 +00:00
bool
2012-10-31 23:43:39 +00:00
Clause::containsNegativeLiteral (
LiteralId lid,
const LogVarTypes& types) const
{
for (size_t i = 0; i < literals_.size(); i++) {
2012-10-31 23:43:39 +00:00
if (literals_[i].lid() == lid
&& literals_[i].isNegative()
&& logVarTypes (i) == types) {
return true;
}
}
return false;
}
void
Clause::removeLiterals (LiteralId lid)
{
size_t i = 0;
while (i != literals_.size()) {
if (literals_[i].lid() == lid) {
removeLiteral (i);
} else {
i ++;
}
}
}
void
2012-10-31 23:43:39 +00:00
Clause::removePositiveLiterals (
LiteralId lid,
const LogVarTypes& types)
{
size_t i = 0;
while (i != literals_.size()) {
2012-10-31 23:43:39 +00:00
if (literals_[i].lid() == lid
&& literals_[i].isPositive()
&& logVarTypes (i) == types) {
removeLiteral (i);
} else {
i ++;
}
}
}
void
2012-10-31 23:43:39 +00:00
Clause::removeNegativeLiterals (
LiteralId lid,
const LogVarTypes& types)
{
size_t i = 0;
while (i != literals_.size()) {
2012-10-31 23:43:39 +00:00
if (literals_[i].lid() == lid
&& literals_[i].isNegative()
&& logVarTypes (i) == types) {
removeLiteral (i);
} else {
i ++;
}
}
}
2012-10-31 23:43:39 +00:00
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);
}
2012-12-20 23:19:10 +00:00
2012-10-31 23:43:39 +00:00
bool
Clause::isNegativeCountedLogVar (LogVar X) const
{
assert (constr_.logVarSet().contains (X));
return negCountedLvs_.contains (X);
}
2012-11-06 15:56:52 +00:00
bool
Clause::isIpgLogVar (LogVar X) const
{
assert (constr_.logVarSet().contains (X));
return ipgLvs_.contains (X);
2012-11-06 15:56:52 +00:00
}
2012-10-31 23:58:07 +00:00
TinySet<LiteralId>
Clause::lidSet() const
{
2012-10-31 23:58:07 +00:00
TinySet<LiteralId> lidSet;
for (size_t i = 0; i < literals_.size(); i++) {
lidSet.insert (literals_[i].lid());
}
return lidSet;
}
LogVarSet
Clause::ipgCandidates() const
2012-10-31 23:58:07 +00:00
{
LogVarSet candidates;
LogVarSet allLvs = constr_.logVarSet();
allLvs -= ipgLvs_;
2012-11-06 16:24:59 +00:00
allLvs -= posCountedLvs_;
2012-12-20 23:19:10 +00:00
allLvs -= negCountedLvs_;
2012-10-31 23:58:07 +00:00
for (size_t i = 0; i < allLvs.size(); i++) {
bool valid = true;
for (size_t j = 0; j < literals_.size(); j++) {
if (Util::contains (literals_[j].logVars(), allLvs[i]) == false) {
2012-10-31 23:58:07 +00:00
valid = false;
break;
}
}
if (valid) {
candidates.insert (allLvs[i]);
}
}
return candidates;
}
2012-10-31 23:43:39 +00:00
LogVarTypes
Clause::logVarTypes (size_t litIdx) const
{
LogVarTypes types;
2012-12-10 18:48:54 +00:00
const LogVars& lvs = literals_[litIdx].logVars();
2012-10-31 23:43:39 +00:00
for (size_t i = 0; i < lvs.size(); i++) {
if (posCountedLvs_.contains (lvs[i])) {
types.push_back (LogVarType::posLvt);
2012-10-31 23:43:39 +00:00
} else if (negCountedLvs_.contains (lvs[i])) {
types.push_back (LogVarType::negLvt);
2012-10-31 23:43:39 +00:00
} else {
types.push_back (LogVarType::fullLvt);
2012-10-31 23:43:39 +00:00
}
}
return types;
}
void
2012-10-31 23:58:07 +00:00
Clause::removeLiteral (size_t litIdx)
2012-10-31 23:43:39 +00:00
{
2012-11-08 14:02:18 +00:00
LogVarSet lvsToRemove = literals_[litIdx].logVarSet()
- getLogVarSetExcluding (litIdx);
ipgLvs_ -= lvsToRemove;
2012-11-08 14:02:18 +00:00
posCountedLvs_ -= lvsToRemove;
negCountedLvs_ -= lvsToRemove;
constr_.remove (lvsToRemove);
2012-10-31 23:58:07 +00:00
literals_.erase (literals_.begin() + litIdx);
2012-10-31 23:43:39 +00:00
}
bool
Clause::independentClauses (Clause& c1, Clause& c2)
{
const Literals& lits1 = c1.literals();
const Literals& lits2 = c2.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()
&& c1.logVarTypes (i) == c2.logVarTypes (j)) {
return false;
}
}
}
return true;
}
Clauses
Clause::copyClauses (const Clauses& clauses)
{
Clauses copy;
copy.reserve (clauses.size());
for (size_t i = 0; i < clauses.size(); i++) {
copy.push_back (new Clause (*clauses[i]));
}
return copy;
}
2012-10-31 23:58:07 +00:00
void
Clause::printClauses (const Clauses& clauses)
{
2012-10-31 23:58:07 +00:00
for (size_t i = 0; i < clauses.size(); i++) {
2013-02-07 13:37:15 +00:00
std::cout << *clauses[i] << std::endl;
}
}
2012-12-19 17:45:53 +00:00
void
Clause::deleteClauses (Clauses& clauses)
{
for (size_t i = 0; i < clauses.size(); i++) {
delete clauses[i];
}
}
2012-12-10 18:48:54 +00:00
std::ostream&
operator<< (std::ostream& os, const Clause& clause)
{
for (unsigned i = 0; i < clause.literals_.size(); i++) {
if (i != 0) os << " v " ;
os << clause.literals_[i].toString (clause.ipgLvs_,
2012-10-31 23:43:39 +00:00
clause.posCountedLvs_, clause.negCountedLvs_);
}
if (clause.constr_.empty() == false) {
2012-10-30 00:21:10 +00:00
ConstraintTree copy (clause.constr_);
copy.moveToTop (copy.logVarSet().elements());
os << " | " << copy.tupleSet();
}
return os;
}
2012-10-31 23:58:07 +00:00
LogVarSet
Clause::getLogVarSetExcluding (size_t idx) const
{
LogVarSet lvs;
for (size_t i = 0; i < literals_.size(); i++) {
if (i != idx) {
lvs |= literals_[i].logVars();
}
}
return lvs;
}
2012-12-10 18:48:54 +00:00
std::ostream&
operator<< (std::ostream& os, const LitLvTypes& lit)
2012-12-10 18:48:54 +00:00
{
os << lit.lid_ << "<" ;
for (size_t i = 0; i < lit.lvTypes_.size(); i++) {
switch (lit.lvTypes_[i]) {
case LogVarType::fullLvt: os << "F" ; break;
case LogVarType::posLvt: os << "P" ; break;
case LogVarType::negLvt: os << "N" ; break;
2012-12-10 18:48:54 +00:00
}
}
os << ">" ;
return os;
}
2013-02-08 12:12:45 +00:00
void
LitLvTypes::setAllFullLogVars()
2013-02-08 12:12:45 +00:00
{
std::fill (lvTypes_.begin(), lvTypes_.end(), LogVarType::fullLvt);
2013-02-08 12:12:45 +00:00
}
LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
2012-11-07 15:28:33 +00:00
: freeLiteralId_(0), pfList_(pfList)
{
addIndicatorClauses (pfList);
addParameterClauses (pfList);
2012-12-20 23:19:10 +00:00
/*
2012-11-14 21:05:55 +00:00
// INCLUSION-EXCLUSION TEST
clauses_.clear();
2013-02-07 13:37:15 +00:00
std::vector<std::vector<string>> names = {
{"a1","b1"},{"a2","b2"}
2012-11-14 21:05:55 +00:00
};
Clause* c1 = new Clause (names);
c1->addLiteral (Literal (0, LogVars() = {0}));
c1->addLiteral (Literal (1, LogVars() = {1}));
2012-11-14 21:05:55 +00:00
clauses_.push_back(c1);
*/
/*
// INDEPENDENT PARTIAL GROUND TEST
clauses_.clear();
2013-02-07 13:37:15 +00:00
std::vector<std::vector<string>> names = {
{"a1","b1"},{"a2","b2"}
};
Clause* c1 = new Clause (names);
c1->addLiteral (Literal (0, LogVars() = {0,1}));
c1->addLiteral (Literal (1, LogVars() = {0,1}));
clauses_.push_back(c1);
Clause* c2 = new Clause (names);
c2->addLiteral (Literal (2, LogVars() = {0}));
c2->addLiteral (Literal (1, LogVars() = {0,1}));
clauses_.push_back(c2);
*/
2012-11-14 21:05:55 +00:00
/*
// ATOM-COUNTING TEST
clauses_.clear();
2013-02-07 13:37:15 +00:00
std::vector<std::vector<string>> names = {
{"p1","p1"},{"p1","p2"},{"p1","p3"},
{"p2","p1"},{"p2","p2"},{"p2","p3"},
{"p3","p1"},{"p3","p2"},{"p3","p3"}
};
Clause* c1 = new Clause (names);
c1->addLiteral (Literal (0, LogVars() = {0}));
c1->addLiteralComplemented (Literal (1, {0,1}));
2012-10-31 23:43:39 +00:00
clauses_.push_back(c1);
Clause* c2 = new Clause (names);
c2->addLiteral (Literal (0, LogVars()={0}));
c2->addLiteralComplemented (Literal (1, {1,0}));
2012-10-31 23:43:39 +00:00
clauses_.push_back(c2);
*/
2012-12-17 18:39:42 +00:00
if (Globals::verbosity > 1) {
2013-02-07 13:37:15 +00:00
std::cout << "FORMULA INDICATORS:" << std::endl;
printFormulaIndicators();
2013-02-07 13:37:15 +00:00
std::cout << std::endl;
std::cout << "WEIGHTED INDICATORS:" << std::endl;
printWeights();
2013-02-07 13:37:15 +00:00
std::cout << std::endl;
std::cout << "CLAUSES:" << std::endl;
printClauses();
2013-02-07 13:37:15 +00:00
std::cout << std::endl;
}
}
LiftedWCNF::~LiftedWCNF()
{
2012-12-19 17:45:53 +00:00
Clause::deleteClauses (clauses_);
}
void
LiftedWCNF::addWeight (LiteralId lid, double posW, double negW)
{
2013-02-07 13:37:15 +00:00
weights_[lid] = std::make_pair (posW, negW);
}
2012-11-07 15:28:33 +00:00
double
LiftedWCNF::posWeight (LiteralId lid) const
{
2013-02-07 13:37:15 +00:00
std::unordered_map<LiteralId, std::pair<double,double>>::const_iterator it
= weights_.find (lid);
return it != weights_.end() ? it->second.first : LogAware::one();
2012-11-07 15:28:33 +00:00
}
double
LiftedWCNF::negWeight (LiteralId lid) const
{
2013-02-07 13:37:15 +00:00
std::unordered_map<LiteralId, std::pair<double,double>>::const_iterator it
= weights_.find (lid);
return it != weights_.end() ? it->second.second : LogAware::one();
2012-11-07 15:28:33 +00:00
}
2013-02-07 13:37:15 +00:00
std::vector<LiteralId>
LiftedWCNF::prvGroupLiterals (PrvGroup prvGroup)
{
assert (Util::contains (map_, prvGroup));
return map_[prvGroup];
}
Clause*
2012-11-07 15:28:33 +00:00
LiftedWCNF::createClause (LiteralId lid) const
2012-10-24 21:22:49 +01:00
{
for (size_t i = 0; i < clauses_.size(); i++) {
const Literals& literals = clauses_[i]->literals();
2012-10-24 21:22:49 +01:00
for (size_t j = 0; j < literals.size(); j++) {
if (literals[j].lid() == lid) {
2012-12-09 22:13:58 +00:00
ConstraintTree ct = clauses_[i]->constr().projectedCopy (
literals[j].logVars());
Clause* c = new Clause (ct);
c->addLiteral (literals[j]);
return c;
2012-10-24 21:22:49 +01:00
}
}
}
return 0;
2012-11-07 15:28:33 +00:00
}
LiteralId
LiftedWCNF::getLiteralId (PrvGroup prvGroup, unsigned range)
{
assert (Util::contains (map_, prvGroup));
2012-11-07 15:28:33 +00:00
return map_[prvGroup][range];
}
2012-12-10 18:48:54 +00:00
2012-11-07 15:28:33 +00:00
void
LiftedWCNF::addIndicatorClauses (const ParfactorList& pfList)
{
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 (map_, formulas[i].group()) == false) {
2012-12-09 22:13:58 +00:00
ConstraintTree tempConstr = (*it)->constr()->projectedCopy(
formulas[i].logVars());
Clause* clause = new Clause (tempConstr);
2013-02-07 13:37:15 +00:00
std::vector<LiteralId> lids;
for (size_t j = 0; j < formulas[i].range(); j++) {
2013-03-09 17:14:00 +00:00
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++) {
2012-12-09 22:13:58 +00:00
ConstraintTree tempConstr2 = (*it)->constr()->projectedCopy (
formulas[i].logVars());
Clause* clause2 = new Clause (tempConstr2);
clause2->addLiteralComplemented (Literal (clause->literals()[j]));
clause2->addLiteralComplemented (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());
2013-02-07 13:37:15 +00:00
std::vector<PrvGroup> groups = (*it)->getAllGroups();
while (indexer.valid()) {
LiteralId paramVarLid = freeLiteralId_;
// λu1 ∧ ... ∧ λun ∧ λxi <=> θxi|u1,...,un
//
// ¬λu1 ... ¬λun v θxi|u1,...,un -> clause1
// ¬θxi|u1,...,un v λu1 -> tempClause
// ¬θxi|u1,...,un v λu2 -> tempClause
double posWeight = (**it)[indexer];
addWeight (paramVarLid, posWeight, LogAware::one());
2012-12-20 23:19:10 +00:00
Clause* clause1 = new Clause (*(*it)->constr());
for (unsigned i = 0; i < groups.size(); i++) {
LiteralId lid = getLiteralId (groups[i], indexer[i]);
clause1->addLiteralComplemented (
Literal (lid, (*it)->argument(i).logVars()));
ConstraintTree ct = *(*it)->constr();
Clause* tempClause = new Clause (ct);
tempClause->addLiteralComplemented (Literal (
paramVarLid, (*it)->constr()->logVars()));
tempClause->addLiteral (Literal (lid, (*it)->argument(i).logVars()));
2012-12-20 23:19:10 +00:00
clauses_.push_back (tempClause);
}
clause1->addLiteral (Literal (paramVarLid, (*it)->constr()->logVars()));
clauses_.push_back (clause1);
freeLiteralId_ ++;
++ indexer;
}
++ it;
}
}
void
LiftedWCNF::printFormulaIndicators() const
{
if (map_.empty()) {
return;
}
2013-02-07 13:37:15 +00:00
std::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());
2013-02-07 13:37:15 +00:00
std::cout << formulas[i] << " | " ;
2012-12-09 22:13:58 +00:00
ConstraintTree tempCt = (*it)->constr()->projectedCopy (
formulas[i].logVars());
2013-02-07 13:37:15 +00:00
std::cout << tempCt.tupleSet();
std::cout << " indicators => " ;
std::vector<LiteralId> indicators =
(map_.find (formulas[i].group()))->second;
2013-02-07 13:37:15 +00:00
std::cout << indicators << std::endl;
}
}
++ it;
}
}
void
LiftedWCNF::printWeights() const
{
2013-02-07 13:37:15 +00:00
std::unordered_map<LiteralId, std::pair<double,double>>::const_iterator it
= weights_.begin();
while (it != weights_.end()) {
2013-02-07 13:37:15 +00:00
std::cout << "λ" << it->first << " weights: " ;
std::cout << it->second.first << " " << it->second.second;
std::cout << std::endl;
++ it;
}
}
void
LiftedWCNF::printClauses() const
{
Clause::printClauses (clauses_);
}
2013-02-07 23:53:13 +00:00
}