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 namespace Minisat {
54 
55 using std::endl;
56 using std::string;
57 
65 public:
67 
68  Clause() { }
69 
70  Clause(const Clause& src) { src.m_ps.copyTo(m_ps); }
71 
72  virtual ~Clause() { }
73 
74  // this function allows to put signed Vars directly
75  // because Vars in Solver are starting with 0 (not signable with "-")
76  // values in input are staring with 1/-1 and are recalculated to 0-base in this function
78 
81  void add(Internal::Var signedVar) {
82  Internal::Lit lit;
83  if (signedVar >= 0) {
84  lit = Internal::mkLit(signedVar - 1, true);
85  } else {
86  lit = Internal::mkLit(-(signedVar + 1), false);
87  }
88  m_ps.push(lit);
89  }
90 
92 
95  void addMultiple(int Amount, ...);
96 
98  void setSign(Internal::Var x, bool sign) {
99  for (int i = 0; i < m_ps.size(); i++) {
100  if (Internal::var(m_ps[i]) == x) {
101  m_ps[i] = Internal::mkLit(x, sign);
102  break;
103  }
104  }
105  }
106 
110  for (int i = 0; i < m_ps.size(); i++) {
111  if (Internal::var(m_ps[i]) == x) {
112  return Internal::sign(m_ps[i]);
113  }
114  }
115  std::cout << "Variable not in Clause" << std::endl;
116  return false;
117  }
118 
120  //the vec-class doesn't allow to erase elements at a position
122  m_ps.copyTo(help);
123  m_ps.clear();
124  for (int i = 0; i < m_ps.size(); i++) {
125  if (Internal::var(m_ps[i]) != x) {
126  m_ps.push(help[i]);
127  }
128  }
129  }
130 
132  static char convertLitSign(Internal::Lit lit) {
133  if (sign(lit) == 0) {
134  return '-';
135  } else {
136  return ' ';
137  }
138  }
139 
140  void writeToConsole() {
141  for (int i = 0; i < m_ps.size() - 1; i++) {
142  std::cout << convertLitSign(m_ps[i]) << Internal::var(m_ps[i]) + 1 << " || ";
143  }
144  std::cout << convertLitSign(m_ps[m_ps.size() - 1])
145  << Internal::var(m_ps[m_ps.size() - 1]) + 1 << std::endl;
146  }
147 };
148 
149 using clause = Clause*;
150 
156 private:
158  std::vector<int> m_vModel;
159 
160  void reset() { m_vModel.clear(); }
161 
162 public:
164 
165  Model() {};
166 
167  virtual ~Model() { reset(); }
168 
170  bool getValue(int var) const {
171  OGDF_ASSERT(var > 0);
172  OGDF_ASSERT(var <= (int)m_vModel.size());
173  return m_vModel[var - 1] != 0;
174  }
175 
178  reset();
179 
180  m_vModel.reserve(S.model.size());
181  for (int i = 0; i < S.model.size(); i++) {
182  m_vModel.push_back(Internal::toInt(S.model[i]));
183  }
184  }
185 
186  void printModel() {
187  for (int i = 0; i < (int)m_vModel.size(); i++) {
188  std::cout << "Var " << i << " = " << m_vModel[i] << " ";
189  }
190  std::cout << std::endl;
191  }
192 
193  std::string intToString(const int i) {
194  std::string s;
195  switch (i) {
196  case 0:
197  s = "False";
198  break;
199  case 1:
200  s = "True";
201  break;
202  case 2:
203  s = "Undefined";
204  break;
205  default:
206  s = "";
207  };
208  return s;
209  };
210 };
211 
219 private:
220  std::stringstream m_messages;
221  std::vector<Clause*> m_Clauses;
222 
223  void free();
224 
225 public:
226  Formula() { }
227 
228  virtual ~Formula() { free(); }
229 
231  void newVar() { Solver::newVar(); }
232 
234  void newVars(unsigned int Count) {
235  //proofs if variables from clause are valid (still known to formula)
236  for (unsigned int i = 0; i < Count; i++) {
237  Solver::newVar();
238  }
239  }
240 
242 
245  Clause* newClause();
246 
248 
251  void finalizeClause(const clause cl);
252 
254  template<class Iteratable>
255  void addClause(const Iteratable& literals) {
256  auto cl = newClause();
257  for (auto literal : literals) {
258  cl->add(literal);
259  }
260  finalizeClause(cl);
261  }
262 
264 
268  bool finalizeNotExtensibleClause(const clause cl);
269 
271  Clause* getClause(const int pos);
272 
274  void removeClause(int i);
275 
277  int getProblemClauseCount() { return nClauses(); }
278 
280  int getClauseCount() { return (int)m_Clauses.size(); }
281 
283  int getVariableCount() { return nVars(); }
284 
286 
290  bool solve(Model& ReturnModel);
291 
293 
298  bool solve(Model& ReturnModel, double& timeLimit);
299 
301 
304  void clauseAddLiteral(unsigned int clausePos, Internal::Var signedVar) {
305  removeClause(clausePos);
306  m_Clauses[clausePos]->add(signedVar);
307  Solver::addClause(m_Clauses[clausePos]->m_ps);
308  }
309 
311  void reset();
312 
314  bool readDimacs(const char* filename);
315 
317  bool readDimacs(const string& filename);
318 
320  bool readDimacs(std::istream& in);
321 
323  bool writeDimacs(const char* filename);
324 
326  bool writeDimacs(const string& filename);
327 
329  bool writeDimacs(std::ostream& f);
330 };
331 
332 }
Minisat::Clause::m_ps
Internal::vec< Internal::Lit > m_ps
Definition: Minisat.h:66
SolverTypes.h
Minisat::Clause::writeToConsole
void writeToConsole()
Definition: Minisat.h:140
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:163
Minisat::Clause::add
void add(Internal::Var signedVar)
adds a literal to the clause
Definition: Minisat.h:81
Minisat::Model::setModel
void setModel(Internal::Solver &S)
sets the model to the model of minsat
Definition: Minisat.h:177
Minisat::Clause::~Clause
virtual ~Clause()
Definition: Minisat.h:72
Minisat::Formula::newVar
void newVar()
add a new variable to the formula
Definition: Minisat.h:231
Minisat::Formula
The Formula class.
Definition: Minisat.h:218
Minisat::Formula::newVars
void newVars(unsigned int Count)
add multiple new variables to the formula
Definition: Minisat.h:234
backward::Color::reset
@ reset
Definition: backward.hpp:1719
Solver.h
Minisat::Formula::Formula
Formula()
Definition: Minisat.h:226
Minisat::Internal::vec::copyTo
void copyTo(vec< T > &copy) const
Definition: Vec.h:90
Minisat::Clause::Clause
Clause(const Clause &src)
Definition: Minisat.h:70
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:98
Minisat::Model::m_vModel
std::vector< int > m_vModel
internal storage of a model by minisat
Definition: Minisat.h:158
Minisat::Formula::getProblemClauseCount
int getProblemClauseCount()
count of problem clauses
Definition: Minisat.h:277
Minisat::Formula::m_messages
std::stringstream m_messages
Definition: Minisat.h:220
Minisat::Internal::Solver
Definition: Solver.h:36
Minisat::Internal::Solver::SolverStatus
Definition: Solver.h:61
Minisat::Internal::vec::size
int size(void) const
Definition: Vec.h:63
Minisat::Internal::Lit
Definition: SolverTypes.h:45
Minisat::Formula::m_Clauses
std::vector< Clause * > m_Clauses
Definition: Minisat.h:221
Minisat::Internal::toInt
int toInt(Var v)
Definition: SolverTypes.h:64
Minisat::Model::getValue
bool getValue(int var) const
returns the value of the assignemt of a variable in the model
Definition: Minisat.h:170
Minisat::Model
Represents a simple class for model storage.
Definition: Minisat.h:155
Minisat::Internal::vec::push
void push(void)
Definition: Vec.h:73
Minisat::Clause::convertLitSign
static char convertLitSign(Internal::Lit lit)
converts the sign of a lit into char
Definition: Minisat.h:132
Minisat::Model::Model
Model()
Definition: Minisat.h:165
Minisat::Formula::getClauseCount
int getClauseCount()
count of learned clauses
Definition: Minisat.h:280
Minisat::Model::reset
void reset()
Definition: Minisat.h:160
Minisat::Formula::~Formula
virtual ~Formula()
Definition: Minisat.h:228
Minisat
Definition: Minisat.h:53
Minisat::Internal::sign
bool sign(Lit p)
Definition: SolverTypes.h:60
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:109
Minisat::Internal::vec
Definition: Vec.h:38
Minisat::Clause
Represents a simple class for clause storage.
Definition: Minisat.h:64
basic.h
Basic declarations, included by all source files.
Minisat::Internal::vec::clear
void clear(bool dealloc=false)
Definition: Vec.h:121
Minisat::Formula::addClause
void addClause(const Iteratable &literals)
Add a clause given by a list of literals.
Definition: Minisat.h:255
Minisat::Internal::mkLit
Lit mkLit(Var var, bool sign=false)
Definition: SolverTypes.h:57
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
Minisat::Internal::Solver::model
vec< lbool > model
Definition: Solver.h:126
Minisat::Model::intToString
std::string intToString(const int i)
Definition: Minisat.h:193
Minisat::Clause::Clause
Clause()
Definition: Minisat.h:68
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:304
Minisat::Formula::getVarFromLit
Internal::Var getVarFromLit(const Internal::Lit &lit)
Definition: Minisat.h:300
Minisat::Internal::var
int var(Lit p)
Definition: SolverTypes.h:61
Minisat::Model::printModel
void printModel()
Definition: Minisat.h:186
Minisat::Clause::removeLit
void removeLit(Internal::Var x)
Definition: Minisat.h:119
Minisat::Formula::getVariableCount
int getVariableCount()
returns varable count currently in solver
Definition: Minisat.h:283
Minisat::Internal::Var
int Var
Definition: SolverTypes.h:41
Minisat::Model::~Model
virtual ~Model()
Definition: Minisat.h:167