Repository navigation
Expand file tree
/
Copy pathDPLL.cpp
More file actions
140 lines (127 loc) · 3.79 KB
/
Copy pathDPLL.cpp
File metadata and controls
140 lines (127 loc) · 3.79 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
//
// DPLL algorithm.
//
#include "DPLL.h"
#include <utility>
DPLL::DPLL(formula phi) : phi(std::move(phi)), answer(Interpretation(phi.num_variable)), gamma(ImplicationGraph(phi.num_variable)) {}
bool DPLL::check_sat() {
//return dfs(answer);
//return dfs_stack();
return dfs_backjump();
}
model DPLL::get_model() {
return answer.export_model();
}
bool DPLL::dfs_stack() {
std::stack<Interpretation> s;
s.push(answer);
while (!s.empty()) {
Interpretation d = s.top();
s.pop();
if (d.satisfy(phi)) {
answer = d;
return true;
}
if (d.unsatisfy(phi)) {
continue;
}
literal unit = d.check_unit(phi);
if (unit) {
if (DEBUG) {
printf("[trace] found unit %d\n", unit);
}
s.push(d.assign(unit));
continue;
}
literal split = d.first_atom();
if (DEBUG) {
printf("[trace] split on %d\n", split);
}
s.push(d.assign(-split));
s.push(d.assign(split));
}
return false;
}
bool DPLL::dfs(Interpretation d) {
if (d.satisfy(phi)) {
answer = d;
return true;
}
if (d.unsatisfy(phi)) {
return false;
}
literal unit = d.check_unit(phi);
if (unit) {
if (DEBUG) {
printf("[trace] found unit %d\n", unit);
}
return dfs(d.assign(unit));
}
literal split = d.first_atom();
if (DEBUG) {
printf("[trace] split on %d\n", split);
}
return (dfs(d.assign(split)) || dfs(d.assign(-split)));
}
bool DPLL::dfs_backjump() {
std::stack<SearchState> s;
s.push(SearchState(answer, 0, 0, gamma));
while (!s.empty()) {
SearchState state = s.top();
s.pop();
Interpretation d = state.interp;
int decide_level = state.decide_level;
int last_decide = state.last_decide;
ImplicationGraph g = state.graph;
if (last_decide != 0) {
g.setDecision(last_decide, decide_level);
if (DEBUG) {
printf("[trace] decide on %d\n", last_decide);
}
}
if (d.satisfy(phi)) {
answer = d;
return true;
}
if (d.unsatisfy_backjump(phi, g)) {
auto reason = g.find_reason();
if (reason.first == 0 || reason.second == 0) {
continue;
}
if (DEBUG) {
printf("[trace] backjump on %d, %d\n", reason.first, reason.second);
}
while (!s.empty()) {
SearchState top = s.top();
if (VAR(top.last_decide) != reason.second) {
s.pop();
} else {
break;
}
}
int initial_decide_level = g.decisions[reason.second];
while (g.decisions[VAR(d.back())] > initial_decide_level) {
d = d.pop();
}
literal new_assignment = -g.parity[reason.first];
g.tidyup(d.getDecision());
d = d.assign(new_assignment);
g.connect(reason.second, reason.first);
g.setDecision(new_assignment, g.decisions[reason.second]);
s.push(SearchState(d, 0, g.decisions[reason.second], g));
continue;
}
literal unit = d.check_unit_backjump(phi, g);
if (unit) {
if (DEBUG) {
printf("[trace] found unit %d\n", unit);
}
s.push(SearchState(d.assign(unit), 0, decide_level, g));
continue;
}
literal split = d.first_atom();
s.push(SearchState(d.assign(-split), split, decide_level + 1, g));
s.push(SearchState(d.assign(split), split, decide_level + 1, g));
}
return false;
}