2013-02-07 17:50:02 +00:00
|
|
|
#ifndef YAP_PACKAGES_CLPBN_HORUS_LIFTEDWCNF_H_
|
|
|
|
#define YAP_PACKAGES_CLPBN_HORUS_LIFTEDWCNF_H_
|
2012-10-22 23:01:13 +01:00
|
|
|
|
2013-02-07 20:09:10 +00:00
|
|
|
#include <vector>
|
2012-12-27 12:54:58 +00:00
|
|
|
#include <unordered_map>
|
2013-02-07 20:09:10 +00:00
|
|
|
#include <string>
|
|
|
|
#include <ostream>
|
2012-12-27 12:54:58 +00:00
|
|
|
|
2013-02-08 12:12:45 +00:00
|
|
|
#include "ConstraintTree.h"
|
|
|
|
#include "ProbFormula.h"
|
|
|
|
#include "LiftedUtils.h"
|
2012-10-22 23:01:13 +01:00
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-02-08 12:12:45 +00:00
|
|
|
class ParfactorList;
|
2012-10-22 23:01:13 +01:00
|
|
|
|
2013-03-09 15:39:39 +00:00
|
|
|
enum class LogVarType {
|
2013-02-13 18:54:15 +00:00
|
|
|
fullLvt,
|
|
|
|
posLvt,
|
|
|
|
negLvt
|
2013-02-13 14:26:47 +00:00
|
|
|
};
|
2012-10-31 23:43:39 +00:00
|
|
|
|
2013-04-16 21:07:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Workaround GCC bug #38064
|
|
|
|
inline bool operator< (LogVarType lvt1, LogVarType lvt2)
|
|
|
|
{
|
|
|
|
return (int)lvt1 < (int)lvt2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
typedef long LiteralId;
|
|
|
|
typedef std::vector<LogVarType> LogVarTypes;
|
2012-11-07 15:28:33 +00:00
|
|
|
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class Literal {
|
2012-10-22 23:01:13 +01:00
|
|
|
public:
|
2013-02-08 12:12:45 +00:00
|
|
|
Literal (LiteralId lid, const LogVars& lvs)
|
|
|
|
: lid_(lid), logVars_(lvs), negated_(false) { }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-08 12:12:45 +00:00
|
|
|
Literal (const Literal& lit, bool negated)
|
|
|
|
: lid_(lit.lid_), logVars_(lit.logVars_), negated_(negated) { }
|
2012-10-22 23:01:13 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LiteralId lid() const { return lid_; }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVars logVars() const { return logVars_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
size_t nrLogVars() const { return logVars_.size(); }
|
2012-10-22 23:01:13 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVarSet logVarSet() const { return LogVarSet (logVars_); }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void complement() { negated_ = !negated_; }
|
2012-10-22 23:01:13 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool isPositive() const { return negated_ == false; }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool isNegative() const { return negated_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-08 12:12:45 +00:00
|
|
|
bool isGround (ConstraintTree constr, const LogVarSet& ipgLogVars) const;
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-11-06 23:35:14 +00:00
|
|
|
size_t indexOfLogVar (LogVar X) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-08 12:12:45 +00:00
|
|
|
std::string toString (
|
|
|
|
LogVarSet ipgLogVars = LogVarSet(),
|
|
|
|
LogVarSet posCountedLvs = LogVarSet(),
|
|
|
|
LogVarSet negCountedLvs = LogVarSet()) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
private:
|
2013-02-07 22:37:45 +00:00
|
|
|
friend std::ostream& operator<< (std::ostream&, const Literal&);
|
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
LiteralId lid_;
|
|
|
|
LogVars logVars_;
|
|
|
|
bool negated_;
|
2012-10-22 23:01:13 +01:00
|
|
|
};
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
typedef std::vector<Literal> Literals;
|
2012-10-22 23:01:13 +01:00
|
|
|
|
|
|
|
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class Clause {
|
2012-10-22 23:01:13 +01:00
|
|
|
public:
|
2012-11-07 18:43:13 +00:00
|
|
|
Clause (const ConstraintTree& ct = ConstraintTree({})) : constr_(ct) { }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
Clause (std::vector<std::vector<std::string>> names) :
|
|
|
|
constr_(ConstraintTree (names)) { }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
void addLiteral (const Literal& l) { literals_.push_back (l); }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const Literals& literals() const { return literals_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
Literals& literals() { return literals_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
size_t nrLiterals() const { return literals_.size(); }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const ConstraintTree& constr() const { return constr_; }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
ConstraintTree constr() { return constr_; }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool isUnit() const { return literals_.size() == 1; }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVarSet ipgLogVars() const { return ipgLvs_; }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-11-08 15:05:48 +00:00
|
|
|
void addIpgLogVar (LogVar X) { ipgLvs_.insert (X); }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
void addPosCountedLogVar (LogVar X) { posCountedLvs_.insert (X); }
|
2012-10-31 23:43:39 +00:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
void addNegCountedLogVar (LogVar X) { negCountedLvs_.insert (X); }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVarSet posCountedLogVars() const { return posCountedLvs_; }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVarSet negCountedLogVars() const { return negCountedLvs_; }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
unsigned nrPosCountedLogVars() const { return posCountedLvs_.size(); }
|
2012-11-06 14:15:21 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
unsigned nrNegCountedLogVars() const { return negCountedLvs_.size(); }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
void addLiteralComplemented (const Literal& lit);
|
2012-11-06 14:15:21 +00:00
|
|
|
|
2012-10-31 23:58:07 +00:00
|
|
|
bool containsLiteral (LiteralId lid) const;
|
|
|
|
|
|
|
|
bool containsPositiveLiteral (LiteralId lid, const LogVarTypes&) const;
|
|
|
|
|
|
|
|
bool containsNegativeLiteral (LiteralId lid, const LogVarTypes&) const;
|
|
|
|
|
|
|
|
void removeLiterals (LiteralId lid);
|
|
|
|
|
|
|
|
void removePositiveLiterals (LiteralId lid, const LogVarTypes&);
|
|
|
|
|
|
|
|
void removeNegativeLiterals (LiteralId lid, const LogVarTypes&);
|
|
|
|
|
2012-10-31 23:43:39 +00:00
|
|
|
bool isCountedLogVar (LogVar X) const;
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-10-31 23:43:39 +00:00
|
|
|
bool isPositiveCountedLogVar (LogVar X) const;
|
|
|
|
|
2012-12-17 18:39:42 +00:00
|
|
|
bool isNegativeCountedLogVar (LogVar X) const;
|
|
|
|
|
2012-11-06 15:56:52 +00:00
|
|
|
bool isIpgLogVar (LogVar X) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
TinySet<LiteralId> lidSet() const;
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVarSet ipgCandidates() const;
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-10-31 23:43:39 +00:00
|
|
|
LogVarTypes logVarTypes (size_t litIdx) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-10-31 23:58:07 +00:00
|
|
|
void removeLiteral (size_t litIdx);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-11-07 21:21:42 +00:00
|
|
|
static bool independentClauses (Clause& c1, Clause& c2);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
static std::vector<Clause*> copyClauses (
|
|
|
|
const std::vector<Clause*>& clauses);
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
static void printClauses (const std::vector<Clause*>& clauses);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
static void deleteClauses (std::vector<Clause*>& clauses);
|
2012-12-19 17:45:53 +00:00
|
|
|
|
2012-10-30 00:21:10 +00:00
|
|
|
private:
|
2012-10-27 00:13:11 +01:00
|
|
|
LogVarSet getLogVarSetExcluding (size_t idx) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
friend std::ostream& operator<< (std::ostream&, const Clause&);
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
Literals literals_;
|
|
|
|
LogVarSet ipgLvs_;
|
|
|
|
LogVarSet posCountedLvs_;
|
|
|
|
LogVarSet negCountedLvs_;
|
|
|
|
ConstraintTree constr_;
|
|
|
|
|
2012-12-27 22:25:45 +00:00
|
|
|
DISALLOW_ASSIGN (Clause);
|
2012-10-22 23:01:13 +01:00
|
|
|
};
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
typedef std::vector<Clause*> Clauses;
|
2012-10-22 23:01:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class LitLvTypes {
|
2012-11-04 18:02:40 +00:00
|
|
|
public:
|
2012-11-07 15:28:33 +00:00
|
|
|
LitLvTypes (LiteralId lid, const LogVarTypes& lvTypes) :
|
2012-11-04 18:02:40 +00:00
|
|
|
lid_(lid), lvTypes_(lvTypes) { }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LiteralId lid() const { return lid_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const LogVarTypes& logVarTypes() const { return lvTypes_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void setAllFullLogVars();
|
2012-11-04 18:02:40 +00:00
|
|
|
|
|
|
|
private:
|
2013-02-07 22:37:45 +00:00
|
|
|
friend std::ostream& operator<< (std::ostream&, const LitLvTypes&);
|
|
|
|
|
2012-11-04 18:02:40 +00:00
|
|
|
LiteralId lid_;
|
|
|
|
LogVarTypes lvTypes_;
|
2013-02-07 22:37:45 +00:00
|
|
|
};
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
|
|
|
|
struct CmpLitLvTypes
|
|
|
|
{
|
2013-02-08 01:11:18 +00:00
|
|
|
bool operator() (
|
|
|
|
const LitLvTypes& types1,
|
|
|
|
const LitLvTypes& types2) const
|
2013-02-07 22:37:45 +00:00
|
|
|
{
|
|
|
|
if (types1.lid() < types2.lid()) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-16 21:14:05 +01:00
|
|
|
if (types1.lid() == types2.lid()){
|
|
|
|
return types1.logVarTypes() < types2.logVarTypes();
|
|
|
|
}
|
2013-02-07 22:37:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-04 18:02:40 +00:00
|
|
|
};
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
typedef TinySet<LitLvTypes, CmpLitLvTypes> LitLvTypesSet;
|
2012-11-04 18:02:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class LiftedWCNF {
|
2012-10-22 23:01:13 +01:00
|
|
|
public:
|
|
|
|
LiftedWCNF (const ParfactorList& pfList);
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
~LiftedWCNF();
|
2012-11-09 18:42:21 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const Clauses& clauses() const { return clauses_; }
|
2012-11-09 18:42:21 +00:00
|
|
|
|
|
|
|
void addWeight (LiteralId lid, double posW, double negW);
|
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
double posWeight (LiteralId lid) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
double negWeight (LiteralId lid) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<LiteralId> prvGroupLiterals (PrvGroup prvGroup);
|
2012-11-09 18:42:21 +00:00
|
|
|
|
2012-12-08 19:17:19 +00:00
|
|
|
Clause* createClause (LiteralId lid) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void printFormulaIndicators() const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void printWeights() const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void printClauses() const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
private:
|
2012-11-07 15:28:33 +00:00
|
|
|
LiteralId getLiteralId (PrvGroup prvGroup, unsigned range);
|
2012-11-06 14:15:21 +00:00
|
|
|
|
|
|
|
void addIndicatorClauses (const ParfactorList& pfList);
|
|
|
|
|
|
|
|
void addParameterClauses (const ParfactorList& pfList);
|
2012-10-22 23:01:13 +01:00
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
Clauses clauses_;
|
|
|
|
LiteralId freeLiteralId_;
|
|
|
|
const ParfactorList& pfList_;
|
|
|
|
std::unordered_map<PrvGroup, std::vector<LiteralId>> map_;
|
|
|
|
std::unordered_map<LiteralId, std::pair<double,double>> weights_;
|
2012-12-27 22:25:45 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN (LiftedWCNF);
|
2012-10-22 23:01:13 +01:00
|
|
|
};
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-02-08 00:20:01 +00:00
|
|
|
#endif // YAP_PACKAGES_CLPBN_HORUS_LIFTEDWCNF_H_
|
2012-10-24 21:22:49 +01:00
|
|
|
|