-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat.h
272 lines (245 loc) · 7.24 KB
/
sat.h
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#ifndef SAT_H_
#define SAT_H_
using namespace std;
#include <cstring>
#include <cstdlib>
#include <vector>
#include "parser.h"
#include "clause.h"
#include "ValliJGM.h"
// Ignore this enum, this is possibly for later use
/*enum heuristic {
// STATIC
MOM = 0,
JWS = 1,
SATZ = 2,
//DYNAMIC
DLIS = 3,
};*/
enum outcome {
// STATIC
UNSATISFIED = 0,
SATISFIED = 1
};
class SAT
{
// Assignment data structure
vector<vector<int> > _assignments;
// Assignment level is assignments[i]
// The variable assigned at level i is jth of assignments[i][j]
// Variable data structure
// Sorted linked list structure I have...
// Maintains sortedness and only one variable in the structure
Valli<Variable> _variables;
// Need this to track whether the variable has been assigned
int _decisionLevel;
public:
SAT ()
: _decisionLevel(0)
, _variables(Valli<Variable>())
, _unitAssigned(stack<int>())
{ }
virtual ~SAT () { }
outcome DPLL(vector<Clause *> &set_of_clauses) {
/*Valli<Variable>::iterator iter = _variables.begin();
Valli<Variable>::iterator end = _variables.end();
while (iter != end) {
cout << (*iter).to_short();
iter++;
}*/
// do BCP
//UnitPropogate(set_of_clauses);
if (isSatisfied(set_of_clauses)) {
cout << "SAT!" << endl;
return (SATISFIED); // you have simplified every clause to be "1"
} else if (isConflicting(set_of_clauses)) {
cout << "UNSAT!" << endl;
return (UNSATISFIED); // this is a conflict, this set of var assignment doesn't satisfy
// must recurse
} else if (hasUnit(set_of_clauses)) {
// get Unit and set appropriately
cout << "UNIT Found!" << endl;
findUnit(set_of_clauses);
return DPLL(set_of_clauses);
} else {
// Decision step using branch heuristic
// Heuristically choose an unassigned variable x and heuristically choose a value v
Valli<Variable>::iterator variable = decide(set_of_clauses);
// DPLL(set_of_clauses = simplified by setting x=v) == (SAT)
if ( DPLL(set_of_clauses) == (SATISFIED) ) {
cout << "SAT!" << endl;
return(SATISFIED);
} else {
// DPLL(set_of_clauses = simplified by setting x=-v)
--_decisionLevel;
int sign = (*variable).getSetting();
(*variable).setSetting( (sign != 1) );
cout << "Reversing setting ..." << endl;
cout << (*variable).to_string() << endl;
return( DPLL(set_of_clauses) );
}
}
}
Valli<Variable>::iterator decide(vector<Clause *> &set_of_clauses) {
++_decisionLevel;
/*//JW
int variable = 0;
int minScore = 1000000000;
Valli<Variable>::iterator it = _variables.begin();
while (it != _variables.end()) {
if ((*it).getSetting() == 2){
int posScore = (*it).scorePOS();
int negScore = (*it).scoreNEG();
if (posScore < minScore) {
minScore = posScore;
variable = (*it).getValue();
}
if (negScore < minScore) {
minScore = negScore;
variable = -(*it).getValue();
}
}
++it;
}*/
int variable = 0;
int minScore = 0;
Valli<Variable>::iterator it = _variables.begin();
bool sign = true;
while (it != _variables.end()) {
if ( ((*it).getSetting()) == 2 ) {
stringstream tt;
tt << (*it).getValue();
cout << "Found: " << tt.str() << endl;
int pos = (*it).scorePOS();
int neg = (*it).scoreNEG();
sign = pos > neg;
break;
}
++it;
}
(*it).setSetting(sign);
(*it).setDecisionLevel(_decisionLevel);
cout << (*it).to_string() << endl;
return it;
}
bool isSatisfied(vector<Clause *> &set_of_clauses) {
for (int i = 0, sz = set_of_clauses.size(); i < sz; i++) {
if (!clauseSatisfied( set_of_clauses[i] ))
return false;
}
return true;
}
bool clauseSatisfied(Clause *clause) {
for (int i = 0, cl_len = clause->numLiterals(); i < cl_len; i++) {
int lit = clause->literals(i);
int value = (lit > 0) ? lit : -lit;
bool litSign = (lit > 0) ? 0 : 1;
unsigned varSetting = (*_variables.find(Variable(value))).getSetting();
if ( (litSign ^ varSetting) == 1)
return true;
}
return false;
}
bool isConflicting(vector<Clause *> &set_of_clauses) {
for (int i = 0, sz = set_of_clauses.size(); i < sz; i++) {
if (clauseConflicting( set_of_clauses[i] ))
return true;
}
return false;
}
bool clauseConflicting(Clause *clause) {
for (int i = 0, cl_len = clause->numLiterals(); i < cl_len; i++) {
int lit = clause->literals(i);
int value = (lit > 0) ? lit : -lit;
bool litSign = (lit > 0) ? 0 : 1;
unsigned varSetting = (*_variables.find(Variable(value))).getSetting();
if ( (litSign ^ varSetting) != 0)
return false;
}
return true;
}
bool hasUnit(vector<Clause *> &set_of_clauses) {
for (int i = 0, sz = set_of_clauses.size(); i < sz; i++) {
if (clauseUnit(set_of_clauses[i]))
return true;
}
return false;
}
bool clauseUnit(Clause *clause) {
int unassigned = 0;
for (int i = 0, sz = clause->numLiterals(); i < sz; i++) {
int lit = clause->literals(i);
int value = (lit > 0) ? lit : -lit;
bool litSign = (lit > 0) ? 0 : 1;
unsigned varSetting = (*_variables.find(Variable(value))).getSetting();
unsigned setting = litSign ^ varSetting;
if (setting == 1)
// Satisfied
return false;
else if (setting != 0) {
// Not conflict
unassigned++;
}
}
return (unassigned == 1);
}
void findUnit(vector<Clause *> &set_of_clauses) {
++_decisionLevel;
for (int i = 0, sz = set_of_clauses.size(); i < sz; i++) {
if (clauseUnit(set_of_clauses[i])) {
findUnitLiteral(set_of_clauses[i]);
break;
}
}
}
void findUnitLiteral(Clause *clause) {
Valli<Variable>::iterator it = _variables.begin();
bool sign = true;
for (int i = 0, sz = clause->numLiterals(); i < sz; i++) {
int lit = clause->literals(i);
int value = (lit > 0) ? lit : -lit;
it = _variables.find(Variable(value));
unsigned varSetting = (*it).getSetting();
if (varSetting == 2) {
stringstream tt;
int value = (*it).getValue();
tt << value;
cout << "Unit Found: " << tt.str() << endl;
sign = (lit > 0);
break;
}
}
(*it).setSetting(sign);
(*it).setDecisionLevel(_decisionLevel);
cout << (*it).to_string() << endl;
}
void fillVariables(vector<Clause *> &set_of_clauses) {
for (int i = 0, sz = set_of_clauses.size(); i < sz; i++) {
for (int j = 0, len = set_of_clauses[i]->numLiterals(); j < len; j++) {
int literal = set_of_clauses[i]->literals(j);
//cout << literal << endl;
int variable = (literal > 0) ? literal : -literal;
if (literal > 0) {
Valli<Variable>::iterator it = _variables.insert( Variable(variable));
(*it).push_backPOS(set_of_clauses[i]);
//cout << (*it).to_string() << endl;
//cout << set_of_clauses[i]->to_string() << endl << endl;
} else {
Valli<Variable>::iterator it = _variables.insert( Variable(variable));
(*it).push_backNEG(set_of_clauses[i]);
//cout << (*it).to_string() << endl;
//cout << set_of_clauses[i]->to_string() << endl << endl;
}
}
}
}
//This is the first Variable
Valli<Variable>::iterator getVarStart() {
return _variables.begin();
}
//This is the dummy end node
Valli<Variable>::iterator getVarEnd() {
return _variables.end();
}
};
#endif