-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmySAT.cpp
More file actions
157 lines (136 loc) · 4.34 KB
/
mySAT.cpp
File metadata and controls
157 lines (136 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;
#define UNDEF -1
#define TRUE 1
#define FALSE 0
uint numVars;
uint numClauses;
vector<vector<int> > clauses;
vector<int> model;
vector<int> modelStack;
uint indexOfNextLitToPropagate;
uint decisionLevel;
vector<int> heuristicV;
void readClauses( ){
// Skip comments
char c = cin.get();
while (c == 'c') {
while (c != '\n') c = cin.get();
c = cin.get();
}
// Read "cnf numVars numClauses"
string aux;
cin >> aux >> numVars >> numClauses;
clauses.resize(numClauses);
// Read clauses
for (uint i = 0; i < numClauses; ++i) {
int lit;
while (cin >> lit and lit != 0) clauses[i].push_back(lit);
}
}
int currentValueInModel(int lit){
if (lit >= 0) return model[lit];
else {
if (model[-lit] == UNDEF) return UNDEF;
else return 1 - model[-lit];
}
}
void setLiteralToTrue(int lit){
modelStack.push_back(lit);
if (lit > 0) model[lit] = TRUE;
else model[-lit] = FALSE;
}
bool propagateGivesConflict ( ) {
while ( indexOfNextLitToPropagate < modelStack.size() ) {
++indexOfNextLitToPropagate;
for (uint i = 0; i < numClauses; ++i) {
bool someLitTrue = false;
int numUndefs = 0;
int lastLitUndef = 0;
for (uint k = 0; not someLitTrue and k < clauses[i].size(); ++k){
int val = currentValueInModel(clauses[i][k]);
if (val == TRUE) someLitTrue = true;
else if (val == UNDEF){ ++numUndefs; lastLitUndef = clauses[i][k]; }
}
if (not someLitTrue and numUndefs == 0) return true; // conflict! all lits false
else if (not someLitTrue and numUndefs == 1) setLiteralToTrue(lastLitUndef);
}
}
return false;
}
void backtrack(){
uint i = modelStack.size() -1;
int lit = 0;
while (modelStack[i] != 0){ // 0 is the DL mark
lit = modelStack[i];
model[abs(lit)] = UNDEF;
modelStack.pop_back();
--i;
}
// at this point, lit is the last decision
modelStack.pop_back(); // remove the DL mark
--decisionLevel;
indexOfNextLitToPropagate = modelStack.size();
setLiteralToTrue(-lit); // reverse last decision
}
// Heuristic for finding the next decision literal:
int getNextDecisionLiteral(){
int max_heuristic_value = -1;
uint max_var;
for (uint var = 1; var <= numVars; ++var){
if (model[var] == UNDEF and heuristicV[var] > max_heuristic_value) {
max_heuristic_value = heuristicV[var];
max_var = var;
}
}
if(max_heuristic_value != -1) return max_var;
return 0;
/*for (uint i = 1; i <= numVars; ++i) // stupid heuristic:
if (model[i] == UNDEF) return i; // returns first UNDEF var, positively
return 0; // reurns 0 when all literals are defined*/
}
void checkmodel(){
for (uint i = 0; i < numClauses; ++i){
bool someTrue = false;
for (uint j = 0; not someTrue and j < clauses[i].size(); ++j)
someTrue = (currentValueInModel(clauses[i][j]) == TRUE);
if (not someTrue) {
cout << "Error in model, clause is not satisfied:";
for (uint j = 0; j < clauses[i].size(); ++j) cout << clauses[i][j] << " ";
cout << endl;
exit(1);
}
}
}
int main(){
readClauses(); // reads numVars, numClauses and clauses
model.resize(numVars+1,UNDEF);
heuristicV.resize(numVars+1,0);
indexOfNextLitToPropagate = 0;
decisionLevel = 0;
// Take care of initial unit clauses, if any
for (uint i = 0; i < numClauses; ++i)
if (clauses[i].size() == 1) {
int lit = clauses[i][0];
int val = currentValueInModel(lit);
if (val == FALSE) {cout << "UNSATISFIABLE" << endl; return 10;}
else if (val == UNDEF) setLiteralToTrue(lit);
}
// DPLL algorithm
while (true) {
while ( propagateGivesConflict() ) {
if ( decisionLevel == 0) { cout << "UNSATISFIABLE" << endl; return 10; }
backtrack();
}
int decisionLit = getNextDecisionLiteral();
if (decisionLit == 0) { checkmodel(); cout << "SATISFIABLE" << endl; return 20; }
// start new decision level:
modelStack.push_back(0); // push mark indicating new DL
++indexOfNextLitToPropagate;
++decisionLevel;
setLiteralToTrue(decisionLit); // now push decisionLit on top of the mark
}
}