Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Minisat.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 // IWYU pragma: always_keep
35 
36 #include <ogdf/lib/minisat/core/Solver.h> // IWYU pragma: export
37 #include <ogdf/lib/minisat/core/SolverTypes.h> // IWYU pragma: export
38 
39 // IWYU pragma: begin_keep
40 #include <ogdf/basic/basic.h>
41 
42 #include <fstream>
43 #include <iostream>
44 #include <sstream>
45 #include <string>
46 #include <vector>
47 
48 #include <stdarg.h>
49 #include <stdio.h>
50 
51 // IWYU pragma: end_keep
52 
53 #pragma GCC visibility push(default)
54 
55 namespace Minisat {
56 
57 using std::endl;
58 using std::string;
59 
67 public:
69 
70  Clause() { }
71 
72  Clause(const Clause& src) { src.m_ps.copyTo(m_ps); }
73 
74  virtual ~Clause() { }
75 
76  // this function allows to put signed Vars directly
77  // because Vars in Solver are starting with 0 (not signable with "-")
78  // values in input are staring with 1/-1 and are recalculated to 0-base in this function
80 
83  void add(Internal::Var signedVar) {
84  Internal::Lit lit;
85  if (signedVar >= 0) {
86  lit = Internal::mkLit(signedVar - 1, true);
87  } else {
88  lit = Internal::mkLit(-(signedVar + 1), false);
89  }
90  m_ps.push(lit);
91  }
92 
94 
97  void addMultiple(int Amount, ...);
98 
100  void setSign(Internal::Var x, bool sign) {
101  for (int i = 0; i < m_ps.size(); i++) {
102  if (Internal::var(m_ps[i]) == x) {
103  m_ps[i] = Internal::mkLit(x, sign);
104  break;
105  }
106  }
107  }
108 
112  for (int i = 0; i < m_ps.size(); i++) {
113  if (Internal::var(m_ps[i]) == x) {
114  return Internal::sign(m_ps[i]);
115  }
116  }
117  std::cout << "Variable not in Clause" << std::endl;
118  return false;
119  }
120 
122  //the vec-class doesn't allow to erase elements at a position
124  m_ps.copyTo(help);
125  m_ps.clear();
126  for (int i = 0; i < m_ps.size(); i++) {
127  if (Internal::var(m_ps[i]) != x) {
128  m_ps.push(help[i]);
129  }
130  }
131  }
132 
134  static char convertLitSign(Internal::Lit lit) {
135  if (sign(lit) == 0) {
136  return '-';
137  } else {
138  return ' ';
139  }
140  }
141 
142  void writeToConsole() {
143  for (int i = 0; i < m_ps.size() - 1; i++) {
144  std::cout << convertLitSign(m_ps[i]) << Internal::var(m_ps[i]) + 1 << " || ";
145  }
146  std::cout << convertLitSign(m_ps[m_ps.size() - 1])
147  << Internal::var(m_ps[m_ps.size() - 1]) + 1 << std::endl;
148  }
149 };
150 
151 using clause = Clause*;
152 
158 private:
160  std::vector<int> m_vModel;
161 
162  void reset() { m_vModel.clear(); }
163 
164 public:
166 
167  Model() {};
168 
169  virtual ~Model() { reset(); }
170 
172  bool getValue(int var) const {
173  OGDF_ASSERT(var > 0);
174  OGDF_ASSERT(var <= (int)m_vModel.size());
175  return m_vModel[var - 1] != 0;
176  }
177 
180  reset();
181 
182  m_vModel.reserve(S.model.size());
183  for (int i = 0; i < S.model.size(); i++) {
184  m_vModel.push_back(Internal::toInt(S.model[i]));
185  }
186  }
187 
188  void printModel() {
189  for (int i = 0; i < (int)m_vModel.size(); i++) {
190  std::cout << "Var " << i << " = " << m_vModel[i] << " ";
191  }
192  std::cout << std::endl;
193  }
194 
195  std::string intToString(const int i) {
196  std::string s;
197  switch (i) {
198  case 0:
199  s = "False";
200  break;
201  case 1:
202  s = "True";
203  break;
204  case 2:
205  s = "Undefined";
206  break;
207  default:
208  s = "";
209  };
210  return s;
211  };
212 };
213 
221 private:
222  std::stringstream m_messages;
223  std::vector<Clause*> m_Clauses;
224 
225  void free();
226 
227 public:
228  Formula() { }
229 
230  virtual ~Formula() { free(); }
231 
233  void newVar() { Solver::newVar(); }
234 
236  void newVars(unsigned int Count) {
237  //proofs if variables from clause are valid (still known to formula)
238  for (unsigned int i = 0; i < Count; i++) {
239  Solver::newVar();
240  }
241  }
242 
244 
247  Clause* newClause();
248 
250 
253  void finalizeClause(const clause cl);
254 
256  template<class Iteratable>
257  void addClause(const Iteratable& literals) {
258  auto cl = newClause();
259  for (auto literal : literals) {
260  cl->add(literal);
261  }
262  finalizeClause(cl);
263  }
264 
266 
270  bool finalizeNotExtensibleClause(const clause cl);
271 
273  Clause* getClause(const int pos);
274 
276  void removeClause(int i);
277 
279  int getProblemClauseCount() { return nClauses(); }
280 
282  int getClauseCount() { return (int)m_Clauses.size(); }
283 
285  int getVariableCount() { return nVars(); }
286 
288 
292  bool solve(Model& ReturnModel);
293 
295 
300  bool solve(Model& ReturnModel, double& timeLimit);
301 
303 
306  void clauseAddLiteral(unsigned int clausePos, Internal::Var signedVar) {
307  removeClause(clausePos);
308  m_Clauses[clausePos]->add(signedVar);
309  Solver::addClause(m_Clauses[clausePos]->m_ps);
310  }
311 
313  void reset();
314 
316  bool readDimacs(const char* filename);
317 
319  bool readDimacs(const string& filename);
320 
322  bool readDimacs(std::istream& in);
323 
325  bool writeDimacs(const char* filename);
326 
328  bool writeDimacs(const string& filename);
329 
331  bool writeDimacs(std::ostream& f);
332 };
333 
334 }
335 
336 #pragma GCC visibility pop
Minisat::Clause::m_ps
Internal::vec< Internal::Lit > m_ps
Definition: Minisat.h:68
SolverTypes.h
Minisat::Clause::writeToConsole
void writeToConsole()
Definition: Minisat.h:142
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
Minisat::Model::solverStatus
Internal::Solver::SolverStatus solverStatus
Definition: Minisat.h:165
Minisat::Clause::add
void add(Internal::Var signedVar)
adds a literal to the clause
Definition: Minisat.h:83
Minisat::Model::setModel
void setModel(Internal::Solver &S)
sets the model to the model of minsat
Definition: Minisat.h:179
Minisat::Clause::~Clause
virtual ~Clause()
Definition: Minisat.h:74
Minisat::Formula::newVar
void newVar()
add a new variable to the formula
Definition: Minisat.h:233
Minisat::Formula
The Formula class.
Definition: Minisat.h:220
Minisat::Formula::newVars
void newVars(unsigned int Count)
add multiple new variables to the formula
Definition: Minisat.h:236
backward::Color::reset
@ reset
Definition: backward.hpp:1719
Solver.h
Minisat::Formula::Formula
Formula()
Definition: Minisat.h:228
Minisat::Internal::vec::copyTo
void copyTo(vec< T > &copy) const
Definition: Vec.h:91
Minisat::Clause::Clause
Clause(const Clause &src)
Definition: Minisat.h:72
Minisat::Clause::setSign
void setSign(Internal::Var x, bool sign)
sets the sign of a variable if its present within the clause
Definition: Minisat.h:100
Minisat::Model::m_vModel
std::vector< int > m_vModel
internal storage of a model by minisat
Definition: Minisat.h:160
Minisat::Formula::getProblemClauseCount
int getProblemClauseCount()
count of problem clauses
Definition: Minisat.h:279
Minisat::Formula::m_messages
std::stringstream m_messages
Definition: Minisat.h:222
Minisat::Internal::Solver
Definition: Solver.h:37
Minisat::Internal::Solver::SolverStatus
Definition: Solver.h:62
Minisat::Internal::vec::size
int size(void) const
Definition: Vec.h:64
Minisat::Internal::Lit
Definition: SolverTypes.h:46
Minisat::Formula::m_Clauses
std::vector< Clause * > m_Clauses
Definition: Minisat.h:223
Minisat::Internal::toInt
int toInt(Var v)
Definition: SolverTypes.h:65
Minisat::Model::getValue
bool getValue(int var) const
returns the value of the assignemt of a variable in the model
Definition: Minisat.h:172
Minisat::Model
Represents a simple class for model storage.
Definition: Minisat.h:157
Minisat::Internal::vec::push
void push(void)
Definition: Vec.h:74
Minisat::Clause::convertLitSign
static char convertLitSign(Internal::Lit lit)
converts the sign of a lit into char
Definition: Minisat.h:134
Minisat::Model::Model
Model()
Definition: Minisat.h:167
Minisat::Formula::getClauseCount
int getClauseCount()
count of learned clauses
Definition: Minisat.h:282
Minisat::Model::reset
void reset()
Definition: Minisat.h:162
Minisat::Formula::~Formula
virtual ~Formula()
Definition: Minisat.h:230
Minisat
Definition: Minisat.h:55
Minisat::Internal::sign
bool sign(Lit p)
Definition: SolverTypes.h:61
Minisat::Clause::getSign
bool getSign(Internal::Var x)
returns the sign of a variable if its present within the clause, if the variable is not representet f...
Definition: Minisat.h:111
Minisat::Internal::vec
Definition: Vec.h:39
Minisat::Clause
Represents a simple class for clause storage.
Definition: Minisat.h:66
basic.h
Basic declarations, included by all source files.
Minisat::Internal::vec::clear
void clear(bool dealloc=false)
Definition: Vec.h:122
Minisat::Formula::addClause
void addClause(const Iteratable &literals)
Add a clause given by a list of literals.
Definition: Minisat.h:257
Minisat::Internal::mkLit
Lit mkLit(Var var, bool sign=false)
Definition: SolverTypes.h:58
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition: config.h:117
Minisat::Internal::Solver::model
vec< lbool > model
Definition: Solver.h:127
Minisat::Model::intToString
std::string intToString(const int i)
Definition: Minisat.h:195
Minisat::Clause::Clause
Clause()
Definition: Minisat.h:70
Minisat::Formula::clauseAddLiteral
void clauseAddLiteral(unsigned int clausePos, Internal::Var signedVar)
adds a literal to a clause and moves clause to the end of list
Definition: Minisat.h:306
Minisat::Formula::getVarFromLit
Internal::Var getVarFromLit(const Internal::Lit &lit)
Definition: Minisat.h:302
Minisat::Internal::var
int var(Lit p)
Definition: SolverTypes.h:62
Minisat::Model::printModel
void printModel()
Definition: Minisat.h:188
Minisat::Clause::removeLit
void removeLit(Internal::Var x)
Definition: Minisat.h:121
Minisat::Formula::getVariableCount
int getVariableCount()
returns varable count currently in solver
Definition: Minisat.h:285
Minisat::Internal::Var
int Var
Definition: SolverTypes.h:42
Minisat::Model::~Model
virtual ~Model()
Definition: Minisat.h:169