-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat.cpp
259 lines (247 loc) · 7.76 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
//
// Created by admin on 15/04/2017.
//
#include "sat.h"
void Sat::addClause(vector<int> &t, bool rlx) {
int index = data.size();
clause tmp;
tmp.sat = false;
tmp.relax = rlx;
tmp.data.clear();
data.push_back(tmp);
for(vector<int>::iterator v = t.begin(); v != t.end(); v++){
int num = *v;
if(num > 0) posWatches[toVar(num)].push_back(index);
else negWatches[toVar(num)].push_back(index);
data[index].push_back(num);
}
assert(data.size() < 2147483647);
}
void Sat::init(bool firstTime) {//after add Clause
aliveClauses = data.size();
for(vector<clause>::iterator t = data.begin(); t != data.end(); t++) (*t).alive = (*t).data.size();
for(int i = 0; i < alive.size(); i++) alive[i] = true;
reason.resize(nodes);
failed = -1;
}
bool Sat::up(int t) {
nodeSat tmp;
nodeQueue tmpQ;
tmp.var = toVar(t);
tmp.reason = 0;
assert(t != 0);
queue<nodeQueue> q;
tmpQ.lit = t;
tmpQ.reasonClause = -1;
q.push(tmpQ);
while(!q.empty()) {
nodeQueue n_ = q.front();
int n = n_.lit;
#if SAT_DEBUG
cout << "Current:" << n << " Reason Clause:" << n_.reasonClause << endl;
//debug();
#endif
q.pop();
tmp.var = toVar(n);
if (alive[toVar(n)]) {
reason[toVar(n)] = n_.reasonClause;
vector<vector<int>> *_posWatches = n > 0 ? &posWatches : &negWatches;
vector<vector<int>> *_negWatches = n > 0 ? &negWatches : &posWatches;
alive[toVar(n)] = false;
#if SAT_DEBUG
cout << "UP:" << n << endl;
#endif
for (vector<int>::iterator v = (*_posWatches)[toVar(n)].begin(); v != (*_posWatches)[toVar(n)].end(); v++) {
int cindex = *v;
#if SAT_DEBUG
cout << "Pos watch:" << cindex << endl;
#endif
if (!data[cindex].sat) data[cindex].sat = true;
aliveClauses--;
if (aliveClauses == 0) {
#if SAT_DEBUG
cout << "ALIVE:0" << endl;
#endif
return true;
}
if (data[cindex].relax) {
int lit = 0;
bool res = true;
//tmp.reason = cindex;
for (vector<int>::iterator t = data[cindex].data.begin(); t != data[cindex].data.end(); t++) {
if (toVar(*t) != toVar(n)) {
//reason[toVar(*t)] = tmp;
#if SAT_DEBUG
cout << "Relax Option:" << endl;
assert(*t != 0);
cout << -*t << " entered the queue with reason clause" << cindex << endl;
#endif
tmpQ.lit = -*t;
tmpQ.reasonClause = cindex;
q.push(tmpQ);
}
}
}
}
for (vector<int>::iterator v = (*_negWatches)[toVar(n)].begin(); v != (*_negWatches)[toVar(n)].end(); v++) {
int cindex = *v;
if (!data[cindex].sat) {
#if SAT_DEBUG
cout << "Neg watch:" << cindex << endl;
#endif
if (data[cindex].alive >= 3) {
data[cindex].alive--;
} else if (data[cindex].alive == 2) {
int lit = 0;
for (vector<int>::iterator t = data[cindex].data.begin(); t != data[cindex].data.end(); t++)
if (alive[toVar(*t)] && toVar(*t) != toVar(n)) lit = *t;
assert(lit != 0);
//tmp.reason = cindex;
//reason[toVar(lit)] = tmp;
data[cindex].alive--;
#if SAT_DEBUG
assert(lit != 0);
cout << lit << " entered the queue with reason clause" << cindex << endl;
#endif
tmpQ.lit = lit;
tmpQ.reasonClause = cindex;
q.push(tmpQ);
} else {
failed = toVar(n);
failed_index = cindex;
return false;
}
}
}
}
}
return true;
}
void Sat::relax(int upLit) {
int start = nodes;
int startNodes = nodes;
int newvar = 0;
vector<bool> seen(data.size(), false);
vector<int> newClause;
queue<int> q;
q.push(failed_index);
#if SAT_DEBUG
cout << "Conflict Clause:";
#endif
while(!q.empty()) {
int f = q.front();
#if SAT_DEBUG
cout << "Popped clause:" << f << endl;
#endif
q.pop();
if(!seen[f]) {
int clauseSize = data[f].data.size();
seen[f] = true;
for (int i = 0; i < clauseSize; i++) {
int lit = data[f].data[i];
int var = toVar(lit);
#if SAT_DEBUG
cout << endl << "relax variable " << var << " from clauses " << f << " whose size is "
<< data[f].data.size() << endl;
assert(var < nodes);
#endif
int cindex = reason[var];
if (cindex >= 0 && !seen[cindex]) {
q.push(cindex);
#if SAT_DEBUG
cout << cindex << " is pushed" << endl;
} else
cout << cindex << " is skipped" << endl;
#else
}
#endif
}
newvar = newVar();
#if SAT_DEBUG
cout << "clause " << f << " is adding new variable " << newvar << endl;
#endif
data[f].data.push_back(newvar + 1);
newClause.push_back(newvar + 1);
}
}
cout << endl;
assert(newvar != 0);
newvar = newVar();
newClause.push_back(newvar + 1);
addClause(newClause, true);
newClause.clear();
newClause.push_back(newvar + 1);
newClause.push_back(upLit);
addClause(newClause, false);
}
int Sat::newVar() {
int newvar = nodes;
#if SAT_DEBUG
cout << "generated new node:" << newvar << endl;
#endif
nodes++;
posWatches.resize(nodes);
negWatches.resize(nodes);
posWatches[newvar].clear();
negWatches[newvar].clear();
alive.resize(nodes);
alive[newvar] = true;
reason.resize(nodes);
return newvar;
}
void Sat::debug() {
cout << "-----------------------------------------------------" << endl;
cout << "Var:" << nodes << " Clause:" << data.size() << endl;
cout << "Alive:";
for(int i = 0; i < alive.size(); i++) if (alive[i]) cout << i << " ";
cout << endl;
cout << "Clauses:" << endl;
for(int i = 0; i < data.size(); i++){
cout << "(" << i << ")";
cout << (data[i].sat ? "S" : " ") << (data[i].relax ? "R":" ") << "[" << data[i].alive << "]";
for (vector<int>::iterator v = data[i].data.begin(); v != data[i].data.end(); v++) if (alive[toVar(*v)]) cout << *v << " ";
cout << endl;
}
cout << "Conflict:" << endl;
for(int i = 0; i < nodes; i++){
cout << i << " var:" << i << " reason clause index:" << reason[i] << endl;
}
}
void satTest(){
vector<int> data;
ifstream satFile;
int lits, clauses, up;
int failed;
satFile.open("sat.txt");
satFile >> lits >> clauses >> up;
Sat s(lits);
for(int i = 0; i < clauses; i++){
data.clear();
int tmp;
satFile >> tmp;
while(tmp != 0){
data.push_back(tmp);
satFile >> tmp;
}
s.addClause(data);
}
s.init(true);
#if SAT_DEBUG
s.debug();
#endif
if(!s.up(up)){
#if SAT_DEBUG
cout << "RELAX!" << endl;
s.debug();
#endif
s.relax(up);
#if SAT_DEBUG
cout << "RELAX FINISHED." << endl;
#endif
s.init(false);
#if SAT_DEBUG
s.debug();
#endif
}
satFile.close();
}