Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
55namespace Minisat {
56
57using std::endl;
58using std::string;
59
67public:
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
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
151using clause = Clause*;
152
158private:
160 std::vector<int> m_vModel;
161
162 void reset() { m_vModel.clear(); }
163
164public:
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
221private:
222 std::stringstream m_messages;
223 std::vector<Clause*> m_Clauses;
224
225 void free();
226
227public:
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
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
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
302 Internal::Var getVarFromLit(const Internal::Lit& lit) { return Internal::var(lit); }
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
Basic declarations, included by all source files.
Represents a simple class for clause storage.
Definition Minisat.h:66
static char convertLitSign(Internal::Lit lit)
converts the sign of a lit into char
Definition Minisat.h:134
Internal::vec< Internal::Lit > m_ps
Definition Minisat.h:68
void addMultiple(int Amount,...)
add multiple literals to the clause
void setSign(Internal::Var x, bool sign)
sets the sign of a variable if its present within the clause
Definition Minisat.h:100
void writeToConsole()
Definition Minisat.h:142
void removeLit(Internal::Var x)
Definition Minisat.h:121
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
void add(Internal::Var signedVar)
adds a literal to the clause
Definition Minisat.h:83
Clause(const Clause &src)
Definition Minisat.h:72
virtual ~Clause()
Definition Minisat.h:74
The Formula class.
Definition Minisat.h:220
bool readDimacs(const char *filename)
read a formula from a DIMACS file
void removeClause(int i)
removes a complete clause
std::vector< Clause * > m_Clauses
Definition Minisat.h:223
void addClause(const Iteratable &literals)
Add a clause given by a list of literals.
Definition Minisat.h:257
virtual ~Formula()
Definition Minisat.h:230
void newVar()
add a new variable to the formula
Definition Minisat.h:233
void newVars(unsigned int Count)
add multiple new variables to the formula
Definition Minisat.h:236
Clause * getClause(const int pos)
returns a clause at position pos in Formula
bool readDimacs(const string &filename)
read a formula from a DIMACS file
int getProblemClauseCount()
count of problem clauses
Definition Minisat.h:279
int getClauseCount()
count of learned clauses
Definition Minisat.h:282
Clause * newClause()
creates a new clause within the formula. If not all variables are known, missing ones are generated a...
bool solve(Model &ReturnModel, double &timeLimit)
tries to solve the formula with a time limit in milliseconds
bool writeDimacs(std::ostream &f)
write a formula in DIMACS format
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
bool solve(Model &ReturnModel)
tries to solve the formula
bool finalizeNotExtensibleClause(const clause cl)
adds a clause to the formula's solver if all variables are known
std::stringstream m_messages
Definition Minisat.h:222
bool writeDimacs(const char *filename)
write a formula to a DIMACS file
bool writeDimacs(const string &filename)
write a formula to a DIMACS file
bool readDimacs(std::istream &in)
read a formula in DIMACS format
int getVariableCount()
returns varable count currently in solver
Definition Minisat.h:285
Internal::Var getVarFromLit(const Internal::Lit &lit)
Definition Minisat.h:302
void finalizeClause(const clause cl)
adds a clause to the formula's solver. If not all variables are known, missing ones are generated auo...
void reset()
delete all clauses and variables
int size(void) const
Definition Vec.h:64
void clear(bool dealloc=false)
Definition Vec.h:122
void push(void)
Definition Vec.h:74
void copyTo(vec< T > &copy) const
Definition Vec.h:91
Represents a simple class for model storage.
Definition Minisat.h:157
bool getValue(int var) const
returns the value of the assignemt of a variable in the model
Definition Minisat.h:172
virtual ~Model()
Definition Minisat.h:169
Internal::Solver::SolverStatus solverStatus
Definition Minisat.h:165
void setModel(Internal::Solver &S)
sets the model to the model of minsat
Definition Minisat.h:179
std::string intToString(const int i)
Definition Minisat.h:195
std::vector< int > m_vModel
internal storage of a model by minisat
Definition Minisat.h:160
void printModel()
Definition Minisat.h:188
void reset()
Definition Minisat.h:162
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52