-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDPLL.cpp
More file actions
152 lines (146 loc) · 3.3 KB
/
DPLL.cpp
File metadata and controls
152 lines (146 loc) · 3.3 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
//
// Created by WADwi on 2021/3/1.
//
#include "DPLL.h"
DPLL::DPLL(const CNF & src) : origin(src){};
Status DPLL::rsc(CNF &cnf) {
Clause *pre,* cur;
bool has_unit = true;
while(has_unit){
has_unit = false;
pre = cnf.clauses;
cur = cnf.clauses->next;
while(cur){
if(!cur->size())
return unholdable;
else if(cur->size() == 1){
has_unit = true;
auto &unit = cur->literals[0];
cnf.literals[unit->id - 1].val = unit->val;
Status res = assign(cnf, unit->id);
if(res == holdable || res == unholdable)
return res;
break;
}
pre = cur;
cur = cur->next;
}
}
if(pre == cur)
return holdable;
return pending;
}
Status DPLL::assign(CNF &cnf, int id) {
Value val = cnf.literals[id - 1].val;
auto &clauses = cnf.clauses;
Clause *pre = clauses, *cur = clauses->next;
while(cur) {
for (int i = 0; i < cur->size(); i++) {
if (id != cur->literals[i]->id) {
continue;
}
if (val == cur->literals[i]->val){
cnf.remove_clauses(pre);
cur = pre;
cnf.clauses_len--;
// printf("%d %d\n", cnf.clauses_len, cnf.real_len());
// fflush(stdout);
if (cnf.clauses->next == nullptr) {
return Status::holdable;
}
break;
} else {
for (int x = i + 1; x < cur->size(); x++)
cur->literals[x - 1] = cur->literals[x];
cur->count--;
if (!cur->count) {
return Status::unholdable;
}
break;
}
}
pre = cur;
cur = cur->next;
}
return Status::pending;
}
Status DPLL::solve(){
CNF cnf = origin; // keep original cnf clean
auto status = perform_dpll(cnf);
if (status == pending || check(result) == unholdable) {
return Status::unholdable;
} else {
this->isSatisfly = 1;
return status;
}
}
Status DPLL::perform_dpll(CNF &cnf) {
Status status = rsc(cnf);
if(status == holdable){
this->result = cnf;
return done;
}else if(status == unholdable)
return pending;
int var = choose(cnf);
Value boolean[] = {positive, negative};
for(int j = 0; j < 2; j++){
CNF cur_cnf = cnf;
Literal &var_ref = cur_cnf.literals[var - 1];
var_ref.val = boolean[(var_ref.pol >= 0) ? j : ((j + 1) % 2)];
status = assign(cur_cnf, var);
if (status == holdable){
save_result(cur_cnf, status);
return done;
}else if(status == unholdable){
continue;
}
status = perform_dpll(cur_cnf);
if(status == done)
return status;
}
return pending;
}
void DPLL::save_result(CNF &cnf, int status) {
if (status == holdable) {
result = cnf;
}
}
int DPLL::choose(CNF &cnf){
return CNF_Strategies::frequential(cnf);
}
Status DPLL::check(const CNF & src){
auto cur = origin.clauses->next;
while(cur){
bool flag = false;
for(int i = 0; i < cur->count; i++)
if(cur->literals[i]->val == src.literals[cur->literals[i]->id - 1].val){
flag = true;
break;
}
if(!flag)
return unholdable;
cur = cur->next;
}
printf("Solution Passed!\n");
return holdable;
}
void DPLL::print_res(char * path, float time){
int i = strlen(path);
path[i - 3] = 'r';
path[i - 2] = 'e';
path[i - 1] = 's';
FILE * fp;
if((fp = fopen(path, "w")) == nullptr){
printf("error occurred while opening file %s", path);
exit(-1);
}
fprintf(fp, "S %d", isSatisfly);
if(isSatisfly){
fprintf(fp, "\nV ");
for(int i = 0; i < result.literals_len; i++){
fprintf(fp, "%d ", result.literals[i].val);
}
}
fprintf(fp, "\nT %f ms", time);
fclose(fp);
};