Timetabler
cclause.cpp
1 #include "cclause.h"
2 
3 #include <iostream>
4 #include <vector>
5 #include "clauses.h"
6 #include "core/SolverTypes.h"
7 #include "utils.h"
8 
9 using namespace NSPACE;
10 
16 CClause::CClause(const std::vector<Lit> &lits) { this->lits = lits; }
17 
23 CClause::CClause(const Lit &lit) {
24  lits.clear();
25  lits.push_back(lit);
26 }
27 
34 CClause::CClause(const Var &var) {
35  lits.clear();
36  lits.push_back(mkLit(var, false));
37 }
38 
42 CClause::CClause() { lits.clear(); }
43 
53 std::vector<CClause> CClause::operator~() {
54  std::vector<CClause> result;
55  result.clear();
56  for (unsigned i = 0; i < lits.size(); i++) {
57  CClause unitClause(~(lits[i]));
58  result.push_back(unitClause);
59  }
60  return result;
61 }
62 
75 std::vector<CClause> CClause::operator&(const CClause &other) {
76  std::vector<CClause> result;
77  result.clear();
78  result.push_back(*this);
79  result.push_back(other);
80  return result;
81 }
82 
95 Clauses CClause::operator&(const Clauses &other) {
96  Clauses result(other.getClauses());
97  result.addClauses(*this);
98  return result;
99 }
100 
114  std::vector<Lit> thisLits = this->lits;
115  std::vector<Lit> otherLits = other.getLits();
116  // appending the literals
117  thisLits.insert(std::end(thisLits), std::begin(otherLits),
118  std::end(otherLits));
119  std::sort(thisLits.begin(), thisLits.end());
120  // removing duplicates
121  thisLits.erase(std::unique(thisLits.begin(), thisLits.end()), thisLits.end());
122  bool existBothPolarities = false;
123  int indexLitBothPolarities = -1;
124  for (unsigned i = 0; i < thisLits.size(); i++) {
125  for (unsigned j = i + 1; j < thisLits.size(); j++) {
126  // if any two literals are such that they are x and ~x
127  if (var(thisLits[i]) == var(thisLits[j])) {
128  existBothPolarities = true;
129  indexLitBothPolarities = i;
130  break;
131  }
132  }
133  if (existBothPolarities) {
134  break;
135  }
136  }
137  // replace the entire clause with x AND ~x, as it is true anyway
138  if (existBothPolarities) {
139  CClause result;
140  result.addLits(thisLits[indexLitBothPolarities]);
141  result.addLits(~thisLits[indexLitBothPolarities]);
142  return result;
143  }
144  CClause result(thisLits);
145  return result;
146 }
147 
161  Clauses thisLHS(*this);
162  return (thisLHS | other);
163 }
164 
178 std::vector<CClause> CClause::operator>>(const CClause &other) {
179  std::vector<CClause> lhs = ~(*this);
180  std::vector<CClause> result;
181  result.clear();
182  for (unsigned i = 0; i < lhs.size(); i++) {
183  CClause thisClause = lhs[i] | other;
184  result.push_back(thisClause);
185  }
186  return result;
187 }
188 
201  Clauses thisLHS(~(*this));
202  return (thisLHS | other);
203 }
204 
213 void CClause::createLitAndAdd(const Var &var1) {
214  lits.push_back(mkLit(var1, false));
215 }
216 
225 void CClause::createLitAndAdd(const Var &var1, const Var &var2) {
226  lits.push_back(mkLit(var1, false));
227  lits.push_back(mkLit(var2, false));
228 }
229 
239 void CClause::createLitAndAdd(const Var &var1, const Var &var2,
240  const Var &var3) {
241  lits.push_back(mkLit(var1, false));
242  lits.push_back(mkLit(var2, false));
243  lits.push_back(mkLit(var3, false));
244 }
245 
251 void CClause::addLits(const Lit &lit1) { lits.push_back(lit1); }
252 
259 void CClause::addLits(const Lit &lit1, const Lit &lit2) {
260  lits.push_back(lit1);
261  lits.push_back(lit2);
262 }
263 
271 void CClause::addLits(const Lit &lit1, const Lit &lit2, const Lit &lit3) {
272  lits.push_back(lit1);
273  lits.push_back(lit2);
274  lits.push_back(lit3);
275 }
276 
282 void CClause::addLits(const std::vector<Lit> &otherLits) {
283  lits.insert(std::end(lits), std::begin(otherLits), std::end(otherLits));
284 }
285 
291 std::vector<Lit> CClause::getLits() const { return lits; }
292 
297  for (Lit lit : lits) {
298  LOG_DEBUG(INFO) << (sign(lit) ? "-" : " ") << var(lit) << " ";
299  }
300  LOG_DEBUG(INFO) << "";
301 }
302 
306 void CClause::clear() { lits.clear(); }
void clear()
Clears the clause by removing all the literals.
Definition: cclause.cpp:306
Class for representing a clause.
Definition: cclause.h:21
void createLitAndAdd(const Var &)
Creates a Lit, a literal, given a variable, a Var, and adds it to the clause.
Definition: cclause.cpp:213
CClause()
Constructs the CClause object, as an empty clause.
Definition: cclause.cpp:42
std::vector< Lit > getLits() const
Gets the literals in the clause.
Definition: cclause.cpp:291
std::vector< CClause > getClauses() const
Gets the clauses in this object.
Definition: clauses.cpp:264
std::vector< CClause > operator~()
Defines the operator to negate the clause in this object.
Definition: cclause.cpp:53
std::vector< CClause > operator>>(const CClause &)
Defines the operator to perform an implication with another clause.
Definition: cclause.cpp:178
void addClauses(const CClause &)
Adds a CClause to the set of clauses.
Definition: clauses.cpp:238
void printClause()
Displays the clause.
Definition: cclause.cpp:296
void addLits(const Lit &)
Adds a Lit, a literal, to the clause.
Definition: cclause.cpp:251
Class for representing a set of clauses.
Definition: clauses.h:23
CClause operator|(const CClause &)
Defines the operator to perform a disjunction of two clauses.
Definition: cclause.cpp:113