-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsat.cpp
378 lines (354 loc) · 9.17 KB
/
sat.cpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <optional>
#include <vector>
extern "C" {
#include <unistd.h>
}
using namespace std;
bool opt_quiet = false;
FILE * opt_cert_file = NULL;
typedef unsigned uint;
typedef unsigned char uchar;
uint N; // number of variables
uint M; // number of initial clauses
vector<vector<int>> F; // problem
enum {
MODEL_DEFINED = 1,
MODEL_PHASE = 2,
};
vector<uchar> model;
vector<int> trail; // 0 for decision mark
uint decision_level;
enum {
CLAUSE_LEARNT = 1,
};
struct clause {
uint num_lit;
int flags;
int lits[]; // lits[0] and lits[1] are watched literals
};
vector<vector<clause *>> pos_list, neg_list; // watch lists
vector<uint> level;
vector<clause *> reason; // nullptr for decision
vector<bool> seen; // only used in `analyze`
vector<int> learnt; // only used in `analyze`
bool defined(uint var) {
return (model[var] & MODEL_DEFINED) != 0;
}
bool phase(uint var) {
return (model[var] & MODEL_PHASE) != 0;
}
int ev(uint var) {
return ! defined(var) ? 0 : phase(var) ? (int) var : -(int) var;
}
void push(int lit, clause * c) {
uint var = abs(lit);
model[var] = lit > 0 ? MODEL_DEFINED | MODEL_PHASE : MODEL_DEFINED;
level[var] = decision_level;
reason[var] = c;
trail.push_back(lit);
}
void pop() {
int lit = trail.back();
uint var = abs(lit);
model[var] &= ~MODEL_DEFINED;
trail.pop_back();
}
clause * make_clause(const vector<int> & lits, int flags) {
clause * c = reinterpret_cast<clause *>(malloc(sizeof(clause) + sizeof(int) * lits.size()));
c->num_lit = lits.size();
for (uint i = 0; i < lits.size(); ++i)
c->lits[i] = lits[i];
c->flags = flags;
return c;
}
auto & watch_list(int lit) {
return lit > 0 ? pos_list[lit] : neg_list[-lit];
}
void watch_clause(clause * c) {
for (auto i : { 0, 1 }) {
watch_list(c->lits[i]).push_back(c);
}
}
void unwatch_clause(clause * c) {
for (auto i : { 0, 1 }) {
auto & wlist = watch_list(c->lits[i]);
for (auto & wc : wlist) {
if (wc == c) {
wc = wlist.back();
wlist.pop_back();
break;
}
}
}
}
void backjump(uint level) {
while (decision_level != level) {
for (uint i = trail.size() - 1; trail[i] != 0; --i)
pop();
trail.pop_back(); // remove the mark
--decision_level;
}
}
void analyze(clause * conflict) {
learnt.push_back(0); // reserve learnt[0] for UIP
uint count = 0;
for (uint i = 0; i < conflict->num_lit; ++i) {
int lit = conflict->lits[i];
uint v = abs(lit);
seen[v] = true;
if (level[v] < decision_level) {
learnt.push_back(lit);
} else {
++count;
}
}
int uip;
for (uint i = trail.size() - 1; true; --i) {
int lit = trail[i];
uint v = abs(lit);
if (! seen[v])
continue;
seen[v] = false;
--count;
if (count == 0) {
uip = lit;
break;
}
auto c = reason[v];
for (uint i = 1; i < c->num_lit; ++i) {
int lit = c->lits[i];
uint v = abs(lit);
if (seen[v])
continue;
seen[v] = true;
if (level[v] < decision_level) {
learnt.push_back(lit);
} else {
++count;
}
}
}
learnt[0] = -uip;
uint num_lit = learnt.size();
for (uint i = 1; i < num_lit; ++i)
seen[abs(learnt[i])] = false;
uint max_lv = 0;
for (uint i = 1; i < num_lit; ++i) {
uint lv = level[abs(learnt[i])];
if (lv > max_lv) {
max_lv = lv;
swap(learnt[1], learnt[i]);
}
}
backjump(max_lv);
if (num_lit == 1) {
push(-uip, nullptr);
learnt.clear();
return;
}
auto c = make_clause(learnt, CLAUSE_LEARNT);
push(-uip, c);
learnt.clear();
watch_clause(c);
}
optional<clause *> find_conflict() {
for (uint prop = trail.size() - 1; prop < trail.size(); ++prop) {
int lit = trail[prop];
auto & wlist = watch_list(-lit);
for (uint i = 0; i < wlist.size(); ++i) {
auto c = wlist[i];
if (c->lits[0] == -lit)
swap(c->lits[0], c->lits[1]);
int lit = c->lits[0];
if (ev(abs(lit)) == lit) // satisfied
continue;
for (uint k = 2; k < c->num_lit; ++k) {
int lit = c->lits[k];
if (ev(abs(lit)) != -lit) { // update watch list
watch_list(lit).push_back(c);
swap(c->lits[1], c->lits[k]);
wlist[i] = wlist.back();
wlist.pop_back();
--i;
goto next;
}
}
if (defined(abs(lit)))
return c; // conflict found
push(lit, c);
next:;
}
}
return nullopt; // no conflict found
}
int choose() {
for (uint v = 1; v <= N; ++v) {
if (! defined(v))
return (int) v;
}
return 0;
}
int decide() {
int lit;
if ((lit = choose()) == 0)
return false; // sat
trail.push_back(0); // push mark
++decision_level;
push(lit, nullptr);
return true;
}
bool solve() {
model.resize(N + 1);
trail.reserve(2 * N);
decision_level = 0;
pos_list.resize(N + 1);
neg_list.resize(N + 1);
level.resize(N + 1);
reason.resize(N + 1);
seen.resize(N + 1);
learnt.reserve(N);
vector<int> unit;
vector<int> new_lits;
for (auto & lits : F) {
size_t size = lits.size();
if (size == 0)
return false; // unsat
if (size == 1) {
unit.push_back(lits[0]);
continue;
}
for (uint i = 0; i < lits.size(); ++i) {
bool last = true;
for (uint j = i + 1; j < lits.size(); ++j) {
if (lits[i] == -lits[j]) // tautology found
goto next;
if (lits[i] == lits[j]) {
last = false;
break;
}
}
if (last)
new_lits.push_back(lits[i]);
}
watch_clause(make_clause(new_lits, 0));
next:
new_lits.clear();
}
while (! unit.empty()) {
int lit = unit.back();
unit.pop_back();
push(lit, nullptr);
if (find_conflict())
return false;
}
if (trail.empty()) {
if (! decide())
return true;
}
while (1) {
while (auto conflict = find_conflict()) {
if (decision_level == 0)
return false;
analyze(*conflict);
}
if (! decide())
return true;
}
}
void check_model() {
for (auto & lits : F) {
bool found = false;
for (int lit : lits) {
if (ev(abs(lit)) == lit) {
found = true;
break;
}
}
if (! found) {
fputs("model broken!\n", stderr);
exit(2);
}
}
}
void usage() {
fputs("Usage: sat [options] [input-file] [output-file]\n", stderr);
fputs("\n", stderr);
fputs("Options:\n", stderr);
fputs("\n", stderr);
fputs(" -q Do not print results to stdout\n", stderr);
fputs(" -C <DRUP_FILE> Output certificates for unsatisfiable formulas\n", stderr);
fputs(" -h Show this message\n", stderr);
fputs("\n", stderr);
exit(1);
}
int main(int argc, char * argv[]) {
int c;
while ((c = getopt(argc, argv, "qC:")) != -1) {
switch (c) {
case 'q':
opt_quiet = true;
break;
case 'C':
opt_cert_file = fopen(optarg, "w");
if (! opt_cert_file)
perror("could not open certificate file");
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 2)
usage();
if (argc > 0) {
if (freopen(argv[0], "r", stdin) == NULL) {
perror("could not open input file");
exit(1);
}
}
if (argc > 1) {
if (opt_quiet)
usage();
if (freopen(argv[1], "w", stdout) == NULL) {
perror("could not open output file");
exit(1);
}
}
// read cnf
while (getchar() == 'c') {
while (getchar() != '\n')
;
}
scanf(" cnf %d %d", &N, &M);
F.resize(M);
for (auto & c : F) {
int lit;
while (scanf("%d", &lit), lit != 0) {
c.push_back(lit);
}
}
bool sat = solve();
// follow sat competition's output format
if (! sat) {
if (opt_cert_file)
fputs("0\n", opt_cert_file);
if (! opt_quiet)
puts("s UNSATISFIABLE");
return 20;
}
check_model();
if (! opt_quiet) {
puts("s SATISFIABLE");
printf("v ");
for (uint v = 1; v <= N; ++v) {
if (defined(v))
printf("%d ", ev(v));
}
puts("0");
}
return 10;
}