2012-10-22 23:01:13 +01:00
|
|
|
#ifndef HORUS_LIFTEDWCNF_H
|
|
|
|
#define HORUS_LIFTEDWCNF_H
|
|
|
|
|
2012-12-27 12:54:58 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
#include "ParfactorList.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class ConstraintTree;
|
|
|
|
|
2012-10-31 23:43:39 +00:00
|
|
|
enum LogVarType
|
|
|
|
{
|
|
|
|
FULL_LV,
|
|
|
|
POS_LV,
|
|
|
|
NEG_LV
|
|
|
|
};
|
|
|
|
|
2012-12-27 12:54:58 +00:00
|
|
|
typedef long LiteralId;
|
|
|
|
typedef vector<LogVarType> LogVarTypes;
|
2012-11-07 15:28:33 +00:00
|
|
|
|
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
class Literal
|
|
|
|
{
|
|
|
|
public:
|
2012-11-06 14:15:21 +00:00
|
|
|
Literal (LiteralId lid, const LogVars& lvs) :
|
|
|
|
lid_(lid), logVars_(lvs), negated_(false) { }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
Literal (const Literal& lit, bool negated) :
|
2012-11-06 14:15:21 +00:00
|
|
|
lid_(lit.lid_), logVars_(lit.logVars_), negated_(negated) { }
|
2012-10-22 23:01:13 +01:00
|
|
|
|
|
|
|
LiteralId lid (void) const { return lid_; }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-10-24 21:22:49 +01:00
|
|
|
LogVars logVars (void) const { return logVars_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
|
|
|
size_t nrLogVars (void) const { return logVars_.size(); }
|
2012-10-22 23:01:13 +01:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
LogVarSet logVarSet (void) const { return LogVarSet (logVars_); }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
void complement (void) { negated_ = !negated_; }
|
2012-10-22 23:01:13 +01:00
|
|
|
|
|
|
|
bool isPositive (void) const { return negated_ == false; }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
bool isNegative (void) const { return negated_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-10-27 00:13:11 +01:00
|
|
|
bool isGround (ConstraintTree constr, 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
|
|
|
|
2012-10-31 23:43:39 +00:00
|
|
|
string toString (LogVarSet ipgLogVars = LogVarSet(),
|
|
|
|
LogVarSet posCountedLvs = LogVarSet(),
|
|
|
|
LogVarSet negCountedLvs = LogVarSet()) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
friend std::ostream& operator<< (std::ostream &os, const Literal& lit);
|
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 lid_;
|
|
|
|
LogVars logVars_;
|
|
|
|
bool negated_;
|
2012-10-22 23:01:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef vector<Literal> Literals;
|
|
|
|
|
|
|
|
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
class Clause
|
|
|
|
{
|
|
|
|
public:
|
2012-11-07 18:43:13 +00:00
|
|
|
Clause (const ConstraintTree& ct = ConstraintTree({})) : constr_(ct) { }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-10-31 23:43:39 +00:00
|
|
|
Clause (vector<vector<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
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
const Literals& literals (void) const { return literals_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-12-08 19:17:19 +00:00
|
|
|
Literals& literals (void) { return literals_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-12-08 19:17:19 +00:00
|
|
|
size_t nrLiterals (void) const { return literals_.size(); }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-10-27 00:13:11 +01:00
|
|
|
const ConstraintTree& constr (void) const { return constr_; }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-10-27 00:13:11 +01:00
|
|
|
ConstraintTree constr (void) { return constr_; }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
bool isUnit (void) const { return literals_.size() == 1; }
|
2012-10-31 23:58:07 +00:00
|
|
|
|
2012-11-08 15:05:48 +00:00
|
|
|
LogVarSet ipgLogVars (void) 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
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
LogVarSet posCountedLogVars (void) const { return posCountedLvs_; }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
LogVarSet negCountedLogVars (void) const { return negCountedLvs_; }
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
unsigned nrPosCountedLogVars (void) const { return posCountedLvs_.size(); }
|
2012-11-06 14:15:21 +00:00
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
unsigned nrNegCountedLogVars (void) 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
|
|
|
|
2012-10-31 23:58:07 +00:00
|
|
|
TinySet<LiteralId> lidSet (void) const;
|
|
|
|
|
|
|
|
LogVarSet ipgCandidates (void) const;
|
|
|
|
|
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
|
|
|
|
|
|
|
static vector<Clause*> copyClauses (const vector<Clause*>& clauses);
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-12-08 19:17:19 +00:00
|
|
|
static void printClauses (const vector<Clause*>& clauses);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-12-19 17:45:53 +00:00
|
|
|
static void deleteClauses (vector<Clause*>& clauses);
|
|
|
|
|
2012-10-31 23:58:07 +00:00
|
|
|
friend std::ostream& operator<< (ostream &os, const Clause& clause);
|
2012-11-01 22:34:28 +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
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
Literals literals_;
|
2012-11-08 15:05:48 +00:00
|
|
|
LogVarSet ipgLvs_;
|
2012-10-31 23:43:39 +00:00
|
|
|
LogVarSet posCountedLvs_;
|
2012-11-01 22:34:28 +00:00
|
|
|
LogVarSet negCountedLvs_;
|
2012-10-27 00:13:11 +01:00
|
|
|
ConstraintTree constr_;
|
2012-12-27 22:25:45 +00:00
|
|
|
|
|
|
|
DISALLOW_ASSIGN (Clause);
|
2012-10-22 23:01:13 +01:00
|
|
|
};
|
|
|
|
|
2012-12-08 19:17:19 +00:00
|
|
|
typedef vector<Clause*> Clauses;
|
2012-10-22 23:01:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
class LitLvTypes
|
2012-11-04 18:02:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-11-07 15:28:33 +00:00
|
|
|
struct CompareLitLvTypes
|
2012-11-04 18:02:40 +00:00
|
|
|
{
|
|
|
|
bool operator() (
|
2012-11-07 15:28:33 +00:00
|
|
|
const LitLvTypes& types1,
|
|
|
|
const LitLvTypes& types2) const
|
2012-11-04 18:02:40 +00:00
|
|
|
{
|
|
|
|
if (types1.lid_ < types2.lid_) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-10 19:37:41 +00:00
|
|
|
if (types1.lid_ == types2.lid_) {
|
|
|
|
return types1.lvTypes_ < types2.lvTypes_;
|
|
|
|
}
|
|
|
|
return false;
|
2012-11-04 18:02:40 +00:00
|
|
|
}
|
|
|
|
};
|
2012-12-17 18:39:42 +00:00
|
|
|
|
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
|
|
|
|
2012-11-04 18:02:40 +00:00
|
|
|
LiteralId lid (void) const { return lid_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-11-04 18:02:40 +00:00
|
|
|
const LogVarTypes& logVarTypes (void) const { return lvTypes_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-11-06 15:56:52 +00:00
|
|
|
void setAllFullLogVars (void) {
|
2012-11-07 15:28:33 +00:00
|
|
|
std::fill (lvTypes_.begin(), lvTypes_.end(), LogVarType::FULL_LV); }
|
2012-11-04 18:02:40 +00:00
|
|
|
|
2012-12-10 18:48:54 +00:00
|
|
|
friend std::ostream& operator<< (std::ostream &os, const LitLvTypes& lit);
|
|
|
|
|
2012-11-04 18:02:40 +00:00
|
|
|
private:
|
|
|
|
LiteralId lid_;
|
|
|
|
LogVarTypes lvTypes_;
|
|
|
|
};
|
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
typedef TinySet<LitLvTypes,LitLvTypes::CompareLitLvTypes> LitLvTypesSet;
|
2012-11-04 18:02:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
class LiftedWCNF
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LiftedWCNF (const ParfactorList& pfList);
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-10-22 23:01:13 +01:00
|
|
|
~LiftedWCNF (void);
|
2012-11-09 18:42:21 +00:00
|
|
|
|
2012-11-06 14:15:21 +00:00
|
|
|
const Clauses& clauses (void) 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
|
|
|
|
2012-11-09 18:42:21 +00:00
|
|
|
vector<LiteralId> prvGroupLiterals (PrvGroup prvGroup);
|
|
|
|
|
2012-12-08 19:17:19 +00:00
|
|
|
Clause* createClause (LiteralId lid) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-11-06 14:15:21 +00:00
|
|
|
void printFormulaIndicators (void) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-11-06 14:15:21 +00:00
|
|
|
void printWeights (void) const;
|
2012-11-01 22:34:28 +00:00
|
|
|
|
2012-11-06 14:15:21 +00:00
|
|
|
void printClauses (void) 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
|
|
|
|
2012-11-07 15:28:33 +00:00
|
|
|
Clauses clauses_;
|
|
|
|
LiteralId freeLiteralId_;
|
2012-12-17 18:39:42 +00:00
|
|
|
const ParfactorList& pfList_;
|
2012-11-07 15:28:33 +00:00
|
|
|
unordered_map<PrvGroup, vector<LiteralId>> map_;
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
#endif // HORUS_LIFTEDWCNF_H
|
2012-10-24 21:22:49 +01:00
|
|
|
|