2013-02-07 20:09:10 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
#include "LiftedWCNF.h"
|
2013-02-08 12:12:45 +00:00
|
|
|
#include "ParfactorList.h"
|
2012-10-22 23:01:13 +01:00
|
|
|
#include "ConstraintTree.h"
|
|
|
|
#include "Indexer.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
bool
|
2013-02-08 12:12:45 +00:00
|
|
|
Literal::isGround (
|
|
|
|
ConstraintTree constr,
|
|
|
|
const LogVarSet& ipgLogVars) const
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
2012-12-27 12:54:58 +00:00
|
|
|
if (logVars_.empty()) {
|
2012-10-22 23:01:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-27 00:13:11 +01:00
|
|
|
LogVarSet lvs (logVars_);
|
|
|
|
lvs -= ipgLogVars;
|
|
|
|
return constr.singletons().contains (lvs);
|
2012-10-22 23:01:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-06 23:35:14 +00:00
|
|
|
size_t
|
|
|
|
Literal::indexOfLogVar (LogVar X) const
|
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
return Util::indexOf (logVars_, X);
|
2012-11-06 23:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::stringstream ss;
|
2012-10-27 00:13:11 +01:00
|
|
|
negated_ ? ss << "¬" : ss << "" ;
|
2012-11-06 14:15:21 +00:00
|
|
|
ss << "λ" ;
|
2012-10-27 00:13:11 +01:00
|
|
|
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])) {
|
2012-10-27 00:13:11 +01:00
|
|
|
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",
|
2012-10-27 00:13:11 +01:00
|
|
|
"g", "h", "i", "j", "k", "m" };
|
|
|
|
(X >= 12) ? ss << "x_" << X : ss << labels[X];
|
|
|
|
} else {
|
|
|
|
ss << logVars_[i];
|
|
|
|
}
|
2012-10-22 23:01:13 +01:00
|
|
|
}
|
2012-10-27 00:13:11 +01:00
|
|
|
ss << ")" ;
|
2012-10-22 23:01:13 +01:00
|
|
|
}
|
2012-10-27 00:13:11 +01:00
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-10 18:48:54 +00:00
|
|
|
std::ostream&
|
2013-02-07 22:37:45 +00:00
|
|
|
operator<< (std::ostream& os, const Literal& lit)
|
2012-10-27 00:13:11 +01:00
|
|
|
{
|
|
|
|
os << lit.toString();
|
2012-10-22 23:01:13 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
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
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
|
|
|
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) {
|
2012-10-22 23:01:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
bool
|
2012-10-31 23:43:39 +00:00
|
|
|
Clause::containsNegativeLiteral (
|
|
|
|
LiteralId lid,
|
|
|
|
const LogVarTypes& types) const
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
|
|
|
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) {
|
2012-10-22 23:01:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Clause::removeLiterals (LiteralId lid)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
while (i != literals_.size()) {
|
|
|
|
if (literals_[i].lid() == lid) {
|
2012-10-27 00:13:11 +01:00
|
|
|
removeLiteral (i);
|
2012-10-22 23:01:13 +01:00
|
|
|
} else {
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
|
|
|
|
void
|
2012-10-31 23:43:39 +00:00
|
|
|
Clause::removePositiveLiterals (
|
|
|
|
LiteralId lid,
|
|
|
|
const LogVarTypes& types)
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
|
|
|
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) {
|
2012-10-27 00:13:11 +01:00
|
|
|
removeLiteral (i);
|
2012-10-22 23:01:13 +01:00
|
|
|
} else {
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-10-31 23:43:39 +00:00
|
|
|
Clause::removeNegativeLiterals (
|
|
|
|
LiteralId lid,
|
|
|
|
const LogVarTypes& types)
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
|
|
|
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) {
|
2012-10-27 00:13:11 +01:00
|
|
|
removeLiteral (i);
|
2012-10-22 23:01:13 +01:00
|
|
|
} 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));
|
2012-11-08 15:05:48 +00:00
|
|
|
return ipgLvs_.contains (X);
|
2012-11-06 15:56:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-31 23:58:07 +00:00
|
|
|
TinySet<LiteralId>
|
2013-02-28 19:45:37 +00:00
|
|
|
Clause::lidSet() const
|
2012-10-27 00:13:11 +01:00
|
|
|
{
|
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
|
2013-02-28 19:45:37 +00:00
|
|
|
Clause::ipgCandidates() const
|
2012-10-31 23:58:07 +00:00
|
|
|
{
|
|
|
|
LogVarSet candidates;
|
|
|
|
LogVarSet allLvs = constr_.logVarSet();
|
2012-11-08 15:05:48 +00:00
|
|
|
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++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
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-27 00:13:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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])) {
|
2013-02-13 18:54:15 +00:00
|
|
|
types.push_back (LogVarType::posLvt);
|
2012-10-31 23:43:39 +00:00
|
|
|
} else if (negCountedLvs_.contains (lvs[i])) {
|
2013-02-13 18:54:15 +00:00
|
|
|
types.push_back (LogVarType::negLvt);
|
2012-10-31 23:43:39 +00:00
|
|
|
} else {
|
2013-02-13 18:54:15 +00:00
|
|
|
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);
|
2012-11-08 15:05:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-07 21:21:42 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-08 19:32:19 +00:00
|
|
|
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-27 00:13:11 +01:00
|
|
|
{
|
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-10-27 00:13:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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&
|
2013-02-07 22:37:45 +00:00
|
|
|
operator<< (std::ostream& os, const Clause& clause)
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < clause.literals_.size(); i++) {
|
|
|
|
if (i != 0) os << " v " ;
|
2012-11-08 15:05:48 +00:00
|
|
|
os << clause.literals_[i].toString (clause.ipgLvs_,
|
2012-10-31 23:43:39 +00:00
|
|
|
clause.posCountedLvs_, clause.negCountedLvs_);
|
2012-10-22 23:01:13 +01:00
|
|
|
}
|
2012-10-27 00:13:11 +01:00
|
|
|
if (clause.constr_.empty() == false) {
|
2012-10-30 00:21:10 +00:00
|
|
|
ConstraintTree copy (clause.constr_);
|
2012-10-22 23:01:13 +01:00
|
|
|
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&
|
2013-02-07 22:37:45 +00:00
|
|
|
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]) {
|
2013-02-13 18:54:15 +00:00
|
|
|
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
|
2013-02-28 19:45:37 +00:00
|
|
|
LitLvTypes::setAllFullLogVars()
|
2013-02-08 12:12:45 +00:00
|
|
|
{
|
2013-02-13 18:54:15 +00:00
|
|
|
std::fill (lvTypes_.begin(), lvTypes_.end(), LogVarType::fullLvt);
|
2013-02-08 12:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
|
2012-11-07 15:28:33 +00:00
|
|
|
: freeLiteralId_(0), pfList_(pfList)
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
2012-11-09 18:42:21 +00:00
|
|
|
addIndicatorClauses (pfList);
|
|
|
|
addParameterClauses (pfList);
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2012-11-07 21:21:42 +00:00
|
|
|
/*
|
2012-11-14 21:05:55 +00:00
|
|
|
// INCLUSION-EXCLUSION TEST
|
2012-12-09 22:04:33 +00:00
|
|
|
clauses_.clear();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<std::vector<string>> names = {
|
2012-12-09 22:04:33 +00:00
|
|
|
{"a1","b1"},{"a2","b2"}
|
2012-11-14 21:05:55 +00:00
|
|
|
};
|
2012-12-09 22:04:33 +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);
|
|
|
|
*/
|
2012-12-09 22:04:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
// INDEPENDENT PARTIAL GROUND TEST
|
|
|
|
clauses_.clear();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<std::vector<string>> names = {
|
2012-12-09 22:04:33 +00:00
|
|
|
{"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
|
2012-12-09 22:04:33 +00:00
|
|
|
clauses_.clear();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<std::vector<string>> names = {
|
2012-12-09 22:04:33 +00:00
|
|
|
{"p1","p1"},{"p1","p2"},{"p1","p3"},
|
2012-11-07 21:21:42 +00:00
|
|
|
{"p2","p1"},{"p2","p2"},{"p2","p3"},
|
|
|
|
{"p3","p1"},{"p3","p2"},{"p3","p3"}
|
2012-11-06 14:15:21 +00:00
|
|
|
};
|
2012-12-09 22:04:33 +00:00
|
|
|
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);
|
2012-12-09 22:04:33 +00:00
|
|
|
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-11-07 21:21:42 +00:00
|
|
|
*/
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity > 1) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << "FORMULA INDICATORS:" << std::endl;
|
2012-11-26 14:46:45 +00:00
|
|
|
printFormulaIndicators();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << "WEIGHTED INDICATORS:" << std::endl;
|
2012-11-26 14:46:45 +00:00
|
|
|
printWeights();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << "CLAUSES:" << std::endl;
|
2012-11-26 14:46:45 +00:00
|
|
|
printClauses();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << std::endl;
|
2012-11-26 14:46:45 +00:00
|
|
|
}
|
2012-10-22 23:01:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LiftedWCNF::~LiftedWCNF()
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
2012-12-19 17:45:53 +00:00
|
|
|
Clause::deleteClauses (clauses_);
|
2012-10-22 23:01:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-09 18:42:21 +00:00
|
|
|
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-09 18:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2013-02-08 21:01:53 +00:00
|
|
|
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);
|
2013-02-08 21:01:53 +00:00
|
|
|
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>
|
2012-11-09 18:42:21 +00:00
|
|
|
LiftedWCNF::prvGroupLiterals (PrvGroup prvGroup)
|
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
assert (Util::contains (map_, prvGroup));
|
2012-11-09 18:42:21 +00:00
|
|
|
return map_[prvGroup];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-08 19:17:19 +00:00
|
|
|
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++) {
|
2012-12-08 19:17:19 +00:00
|
|
|
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());
|
2012-12-08 19:17:19 +00:00
|
|
|
Clause* c = new Clause (ct);
|
|
|
|
c->addLiteral (literals[j]);
|
|
|
|
return c;
|
2012-10-24 21:22:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-08 19:17:19 +00:00
|
|
|
return 0;
|
2012-11-07 15:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LiteralId
|
|
|
|
LiftedWCNF::getLiteralId (PrvGroup prvGroup, unsigned range)
|
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2012-10-22 23:01:13 +01: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++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Util::contains (map_, formulas[i].group()) == false) {
|
2012-12-09 22:13:58 +00:00
|
|
|
ConstraintTree tempConstr = (*it)->constr()->projectedCopy(
|
|
|
|
formulas[i].logVars());
|
2012-12-08 19:17:19 +00:00
|
|
|
Clause* clause = new Clause (tempConstr);
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<LiteralId> lids;
|
2012-10-22 23:01:13 +01:00
|
|
|
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()));
|
2012-10-22 23:01:13 +01:00
|
|
|
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());
|
2012-12-08 19:17:19 +00:00
|
|
|
Clause* clause2 = new Clause (tempConstr2);
|
|
|
|
clause2->addLiteralComplemented (Literal (clause->literals()[j]));
|
|
|
|
clause2->addLiteralComplemented (Literal (clause->literals()[k]));
|
2012-10-22 23:01:13 +01:00
|
|
|
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();
|
2012-10-22 23:01:13 +01:00
|
|
|
while (indexer.valid()) {
|
|
|
|
LiteralId paramVarLid = freeLiteralId_;
|
2012-10-27 00:13:11 +01:00
|
|
|
// λ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
|
2012-11-06 14:15:21 +00:00
|
|
|
double posWeight = (**it)[indexer];
|
2013-02-08 21:01:53 +00:00
|
|
|
addWeight (paramVarLid, posWeight, LogAware::one());
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2012-12-08 19:17:19 +00:00
|
|
|
Clause* clause1 = new Clause (*(*it)->constr());
|
2012-10-22 23:01:13 +01:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < groups.size(); i++) {
|
|
|
|
LiteralId lid = getLiteralId (groups[i], indexer[i]);
|
|
|
|
|
2012-12-08 19:17:19 +00:00
|
|
|
clause1->addLiteralComplemented (
|
2012-11-06 14:15:21 +00:00
|
|
|
Literal (lid, (*it)->argument(i).logVars()));
|
2012-10-22 23:01:13 +01:00
|
|
|
|
2012-10-27 00:13:11 +01:00
|
|
|
ConstraintTree ct = *(*it)->constr();
|
2012-12-08 19:17:19 +00:00
|
|
|
Clause* tempClause = new Clause (ct);
|
|
|
|
tempClause->addLiteralComplemented (Literal (
|
2012-11-06 14:15:21 +00:00
|
|
|
paramVarLid, (*it)->constr()->logVars()));
|
2012-12-08 19:17:19 +00:00
|
|
|
tempClause->addLiteral (Literal (lid, (*it)->argument(i).logVars()));
|
2012-12-20 23:19:10 +00:00
|
|
|
clauses_.push_back (tempClause);
|
2012-10-22 23:01:13 +01:00
|
|
|
}
|
2012-12-08 19:17:19 +00:00
|
|
|
clause1->addLiteral (Literal (paramVarLid, (*it)->constr()->logVars()));
|
2012-10-22 23:01:13 +01:00
|
|
|
clauses_.push_back (clause1);
|
|
|
|
freeLiteralId_ ++;
|
|
|
|
++ indexer;
|
|
|
|
}
|
|
|
|
++ it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-09 22:04:33 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
LiftedWCNF::printFormulaIndicators() const
|
2012-10-22 23:01:13 +01:00
|
|
|
{
|
2012-12-09 22:04:33 +00:00
|
|
|
if (map_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
std::set<PrvGroup> allGroups;
|
2012-10-22 23:01:13 +01:00
|
|
|
ParfactorList::const_iterator it = pfList_.begin();
|
|
|
|
while (it != pfList_.end()) {
|
|
|
|
const ProbFormulas& formulas = (*it)->arguments();
|
|
|
|
for (size_t i = 0; i < formulas.size(); i++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Util::contains (allGroups, formulas[i].group()) == false) {
|
2012-10-22 23:01:13 +01:00
|
|
|
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 =
|
2012-10-22 23:01:13 +01:00
|
|
|
(map_.find (formulas[i].group()))->second;
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << indicators << std::endl;
|
2012-10-22 23:01:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++ it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:22:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
LiftedWCNF::printWeights() const
|
2012-10-25 12:22:52 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::unordered_map<LiteralId, std::pair<double,double>>::const_iterator it
|
|
|
|
= weights_.begin();
|
2012-11-06 14:15:21 +00:00
|
|
|
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;
|
2012-11-06 14:15:21 +00:00
|
|
|
++ it;
|
|
|
|
}
|
2012-10-25 12:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
LiftedWCNF::printClauses() const
|
2012-10-25 12:22:52 +01:00
|
|
|
{
|
2012-12-08 19:17:19 +00:00
|
|
|
Clause::printClauses (clauses_);
|
2012-10-25 12:22:52 +01:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:53:13 +00:00
|
|
|
}
|
|
|
|
|