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.h

187 lines
4.6 KiB
C
Raw Normal View History

#ifndef HORUS_LIFTEDWCNF_H
#define HORUS_LIFTEDWCNF_H
#include "ParfactorList.h"
using namespace std;
typedef long LiteralId;
class ConstraintTree;
2012-10-31 23:43:39 +00:00
enum LogVarType
{
FULL_LV,
POS_LV,
NEG_LV
};
typedef vector<LogVarType> LogVarTypes;
class Literal
{
public:
Literal (LiteralId lid, double w = -1.0) :
lid_(lid), weight_(w), negated_(false) { }
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_; }
2012-10-24 21:22:49 +01:00
LogVars logVars (void) const { return logVars_; }
2012-10-30 00:21:10 +00:00
LogVarSet logVarSet (void) const { return LogVarSet (logVars_); }
2012-10-30 00:21:10 +00:00
// FIXME this is not log aware :(
double weight (void) const { return weight_ < 0.0 ? 1.0 : weight_; }
void negate (void) { negated_ = !negated_; }
bool isPositive (void) const { return negated_ == false; }
bool isNegative (void) const { return negated_; }
bool isGround (ConstraintTree constr, LogVarSet ipgLogVars) const;
2012-10-31 23:43:39 +00:00
string toString (LogVarSet ipgLogVars = LogVarSet(),
LogVarSet posCountedLvs = LogVarSet(),
LogVarSet negCountedLvs = LogVarSet()) 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 (const ConstraintTree& ct) : constr_(ct) { }
2012-10-31 23:43:39 +00:00
Clause (vector<vector<string>> names) : constr_(ConstraintTree (names)) { }
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;
2012-10-31 23:43:39 +00:00
bool containsPositiveLiteral (LiteralId lid, const LogVarTypes&) const;
2012-10-31 23:43:39 +00:00
bool containsNegativeLiteral (LiteralId lid, const LogVarTypes&) const;
void removeLiterals (LiteralId lid);
2012-10-31 23:43:39 +00:00
void removePositiveLiterals (LiteralId lid, const LogVarTypes&);
2012-10-31 23:43:39 +00:00
void removeNegativeLiterals (LiteralId lid, const LogVarTypes&);
const vector<Literal>& literals (void) const { return literals_; }
void removeLiteralByIndex (size_t idx) { literals_.erase (literals_.begin() + idx); }
const ConstraintTree& constr (void) const { return constr_; }
ConstraintTree constr (void) { return constr_; }
bool isUnit (void) const { return literals_.size() == 1; }
LogVarSet ipgLogVars (void) const { return ipgLogVars_; }
void addIpgLogVar (LogVar X) { ipgLogVars_.insert (X); }
2012-10-31 23:43:39 +00:00
void addPositiveCountedLogVar (LogVar X) { posCountedLvs_.insert (X); }
void addNegativeCountedLogVar (LogVar X) { negCountedLvs_.insert (X); }
LogVarSet ipgCandidates (void) const;
TinySet<LiteralId> lidSet (void) const;
2012-10-31 23:43:39 +00:00
bool isCountedLogVar (LogVar X) const;
bool isPositiveCountedLogVar (LogVar X) const;
bool isNegativeCountedLogVar (LogVar X) const;
friend std::ostream& operator<< (ostream &os, const Clause& clause);
void removeLiteral (size_t idx);
2012-10-30 00:21:10 +00:00
2012-10-31 23:43:39 +00:00
LogVarTypes logVarTypes (size_t litIdx) const;
static void printClauses (const vector<Clause>& clauses);
2012-10-30 00:21:10 +00:00
private:
LogVarSet getLogVarSetExcluding (size_t idx) const;
vector<Literal> literals_;
LogVarSet ipgLogVars_;
2012-10-31 23:43:39 +00:00
LogVarSet posCountedLvs_;
LogVarSet negCountedLvs_;
ConstraintTree constr_;
};
typedef vector<Clause> Clauses;
class LiftedWCNF
{
public:
LiftedWCNF (const ParfactorList& pfList);
~LiftedWCNF (void);
const Clauses& clauses (void) const { return clauses_; }
2012-10-24 21:22:49 +01:00
Clause createClauseForLiteral (LiteralId lid) const;
void printFormulaIndicators (void) const;
void printWeights (void) const;
void printClauses (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_;
2012-10-24 21:22:49 +01:00
unordered_map<PrvGroup, vector<LiteralId>> map_;
const ParfactorList& pfList_;
LiteralId freeLiteralId_;
};
#endif // HORUS_LIFTEDWCNF_H
2012-10-24 21:22:49 +01:00