-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPLL.h
382 lines (332 loc) · 9.09 KB
/
DPLL.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
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
379
380
381
382
//
// DPLL algorithm.
//
#ifndef DPLL_DPLL_H
#define DPLL_DPLL_H
#include <utility>
#include "common.h"
struct ImplicationGraph {
std::vector<int> decisions;
std::vector<int> parity;
std::vector<std::vector<bool>> adjacent;
int last_atom;
int num_variables;
explicit ImplicationGraph(int num_variables) : last_atom(0), num_variables(num_variables) {
decisions.resize(num_variables + 1);
adjacent.resize(num_variables + 1);
parity.resize(num_variables + 1);
for (int i = 0; i <= num_variables; i++) {
decisions[i] = 0;
parity[i] = 0;
adjacent[i].resize(num_variables + 1);
for (int j = 0; j <= num_variables; j++) {
adjacent[i][j] = false;
}
}
}
void connect(literal from, literal to) {
adjacent[VAR(from)][VAR(to)] = true;
last_atom = VAR(to);
}
void setDecision(literal l, int d) {
decisions[VAR(l)] = d;
parity[VAR(l)] = l;
}
void span(const clause &c, literal unit) {
int latest_decision = 0;
for (literal l: c) {
if (VAR(l) != VAR(unit)) {
connect(l, unit);
if (decisions[VAR(l)] > latest_decision) {
latest_decision = decisions[VAR(l)];
}
}
}
setDecision(unit, latest_decision);
}
void clear(int i) {
for (int j = 0; j <= num_variables; j++) {
adjacent[i][j] = false;
adjacent[j][i] = false;
}
decisions[i] = 0;
parity[i] = 0;
}
void tidyup(const std::vector<literal> &m) {
std::vector<bool> appear;
appear.resize(num_variables + 1);
for (int i = 0; i <= num_variables; i++) {
appear[i] = false;
}
for (literal l: m) {
appear[VAR(l)] = true;
}
for (int i = 1; i <= num_variables; i++) {
if (!appear[i]) {
clear(i);
}
}
}
std::pair<int, int> find_reason() {
std::stack<int> s;
s.push(last_atom);
std::vector<bool> visited;
visited.resize(num_variables + 1);
for (int i = 0; i <= num_variables; i++) {
visited[i] = false;
}
std::vector<int> ans;
while (!s.empty()) {
int x = s.top();
s.pop();
bool hasPrev = false;
for (int i = 1; i <= num_variables; i++) {
if (i != x && adjacent[i][x]) {
hasPrev = true;
if (!visited[i]) {
s.push(i);
visited[i] = true;
}
}
}
if (!hasPrev) {
ans.push_back(x);
}
}
if (ans.size() >= 2) {
if (decisions[ans[0]] >= decisions[ans[1]]) {
return std::make_pair(ans[0], ans[1]);
} else {
return std::make_pair(ans[1], ans[0]);
}
} else {
return std::make_pair(0, 0);
}
}
};
class Interpretation {
private:
interp decision;
atoms remain;
void remove_remain(int x) {
auto iter = std::remove(remain.begin(), remain.end(), x);
remain.erase(iter, remain.end());
}
void assign_(literal l) {
decision.push_back(l);
remove_remain(VAR(l));
}
int state(literal l) {
for (literal d: decision) {
if (l == d) {
return 1;
}
if (-l == d) {
return -1;
}
}
return 0;
}
bool belongs(literal l) {
return (state(l) == 1);
}
bool satisfy(const clause &c) {
for (literal l: c) {
if (belongs(l)) {
return true;
}
}
return false;
}
bool unsatisfy(const clause &c) {
for (literal l: c) {
if (!belongs(-l)) {
return false;
}
}
return true;
}
bool unsatisfy_backjump(const clause &c, ImplicationGraph &g) {
for (literal l: c) {
if (!belongs(-l)) {
return false;
}
}
g.span(c, g.last_atom);
return true;
}
bool unassigned(literal l) {
return (!state(l));
}
literal check_unit(const clause &c) {
int num_unassigned = 0;
literal unit = 0;
for (literal l: c) {
if (unassigned(l)) {
num_unassigned++;
unit = l;
}
if (num_unassigned > 1) {
return 0;
}
if (state(l) == 1) {
return 0;
}
}
if (num_unassigned == 1) {
return unit;
}
return 0;
}
literal check_unit_backjump(const clause &c, ImplicationGraph &g) {
int num_unassigned = 0;
literal unit = 0;
for (literal l: c) {
if (unassigned(l)) {
num_unassigned++;
unit = l;
}
if (num_unassigned > 1) {
return 0;
}
if (state(l) == 1) {
return 0;
}
}
if (num_unassigned == 1) {
g.span(c, unit);
return unit;
}
return 0;
}
void pop_() {
literal removed = decision.back();
decision.pop_back();
remain.push_back(VAR(removed));
}
public:
explicit Interpretation(int num_variable = 0) {
init(num_variable);
}
void init(int num_variable) {
remain.resize(num_variable);
for (int i = 0; i < num_variable; i++) {
remain[i] = i + 1;
}
}
Interpretation assign(literal l) {
Interpretation new_interp = *this;
new_interp.assign_(l);
return new_interp;
}
Interpretation pop() {
Interpretation new_interp = *this;
new_interp.pop_();
return new_interp;
}
int first_atom() {
if (remain.empty()) {
throw std::logic_error("no remaining atom");
}
return remain[0];
}
bool satisfy(const formula &f) {
for (const clause &c: f.clauses) {
if (!satisfy(c)) {
return false;
}
}
return true;
}
bool unsatisfy(const formula &f) {
for (const clause &c: f.clauses) {
if (unsatisfy(c)) {
return true;
}
}
return false;
}
bool unsatisfy_backjump(const formula &f, ImplicationGraph &g) {
for (const clause &c: f.clauses) {
if (unsatisfy_backjump(c, g)) {
return true;
}
}
return false;
}
literal check_unit(const formula &f) {
for (const clause &c: f.clauses) {
literal result = check_unit(c);
if (result != 0) {
return result;
}
}
return 0;
}
literal check_unit_backjump(const formula &f, ImplicationGraph &g) {
for (const clause &c: f.clauses) {
literal result = check_unit_backjump(c, g);
if (result != 0) {
return result;
}
}
return 0;
}
model export_model() {
model answer;
for (literal l: decision) {
answer.insert(std::make_pair(VAR(l), POSITIVE(l)));
}
return answer;
}
std::vector<literal> &getDecision() {
return decision;
}
literal back() {
return decision.back();
}
bool exhausted() {
return remain.empty();
}
};
struct SearchState {
Interpretation interp;
int last_decide;
int decide_level;
ImplicationGraph graph;
SearchState(Interpretation i, int l, int d, ImplicationGraph g): interp(std::move(i)), last_decide(l), decide_level(d), graph(std::move(g)) {}
};
class DPLL {
public:
/**
* Constructor.
*
* @param phi the formula to be checked
* @note Please DON'T CHANGE this signature because the grading script will directly call this function!
*/
explicit DPLL(formula phi);
/**
* Check if the formula is satisfiable.
*
* @return true if satisfiable, and false if unsatisfiable
* @note Please DON'T CHANGE this signature because the grading script will directly call this function!
*/
bool check_sat();
/**
* Get a satisfying model (interpretation) of the formula, the model must be *complete*, that is,
* it must assign every variable a truth value.
* This function will be called if and only if `check_sat()` returns true.
*
* @return an arbitrary (if there exist many) satisfying model
* @note Please DON'T CHANGE this signature because the grading script will directly call this function!
*/
model get_model();
private:
formula phi;
Interpretation answer;
ImplicationGraph gamma;
bool dfs(Interpretation d);
bool dfs_stack();
bool dfs_backjump();
};
#endif //DPLL_DPLL_H