-
Notifications
You must be signed in to change notification settings - Fork 0
/
alternatiu.cpp
222 lines (188 loc) · 6.57 KB
/
alternatiu.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
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
#define UNDEF -1
#define TRUE 1
#define FALSE 0
uint numVars;
uint numClauses;
vector<vector<int> > clauses;
vector<int> model;
vector<int> modelStack;
uint indexOfNextLitToPropagate;
uint decisionLevel;
vector<vector<uint> > positius; //clausules on apareix positiu
vector<vector<uint> > negatius; //clausules on apareix negatiu
vector<uint> conflictes_tots;
uint conflictes;
int start_s;
int stop_s;
void conflictes_max(int lit){
if(30000<=conflictes){
conflictes=0;
for(int i = 1; i <= numVars; ++i){
conflictes_tots[i] = 0;
}
}
conflictes_tots[abs(lit)]+=positius[abs(lit)].size()+negatius[abs(lit)].size();
}
void readClauses( ){
// Skip comments
char c = cin.get();
while (c == 'c') {
while (c != '\n') c = cin.get();
c = cin.get();
}
// Read "cnf numVars numClauses"
string aux;
cin >> aux >> numVars >> numClauses;
clauses.resize(numClauses);
positius.resize(numVars+1); //la posició 0 és inútil
negatius.resize(numVars+1); //la posició 0 és inútil
conflictes=0;
conflictes_tots.resize(numVars+1,0);
//conflictes_neg.resize(numVars+1,0);
// Read clauses
for (uint i = 0; i < numClauses; ++i) {
int lit;
while (cin >> lit and lit != 0){
clauses[i].push_back(lit);
if(lit<0) negatius[-lit].push_back(i);
else positius[lit].push_back(i);
}
}
}
int currentValueInModel(int lit){
if (lit >= 0) return model[lit];
else {
if (model[-lit] == UNDEF) return UNDEF;
else return 1 - model[-lit];
}
}
void setLiteralToTrue(int lit){
modelStack.push_back(lit);
if (lit > 0) model[lit] = TRUE;
else model[-lit] = FALSE;
}
bool propagateGivesConflict ( ) {
while ( indexOfNextLitToPropagate < modelStack.size() ) {
int ultim = modelStack[indexOfNextLitToPropagate];
if(ultim>0){ //si es positiu
for (uint i = 0; i < negatius[ultim].size(); ++i) {
int cl = negatius[ultim][i];
bool someLitTrue = false;
int numUndefs = 0;
int lastLitUndef = 0;
for (uint k = 0; not someLitTrue and k < clauses[cl].size(); ++k){
int val = currentValueInModel(clauses[cl][k]);
if (val == TRUE) someLitTrue = true;
else if (val == UNDEF){ ++numUndefs; lastLitUndef = clauses[cl][k]; }
}
if (not someLitTrue and numUndefs == 0){
conflictes_max(ultim);
return true;} // conflict! all lits false
else if (not someLitTrue and numUndefs == 1) setLiteralToTrue(lastLitUndef);
}
}
else{ //si es negatiu
for (uint i = 0; i < positius[-ultim].size(); ++i) {
int cl = positius[-ultim][i];
bool someLitTrue = false;
int numUndefs = 0;
int lastLitUndef = 0;
for (uint k = 0; not someLitTrue and k < clauses[cl].size(); ++k){
int val = currentValueInModel(clauses[cl][k]);
if (val == TRUE) someLitTrue = true;
else if (val == UNDEF){ ++numUndefs; lastLitUndef = clauses[cl][k]; }
}
if (not someLitTrue and numUndefs == 0){
conflictes_max(ultim);
return true;} // conflict! all lits false
else if (not someLitTrue and numUndefs == 1) setLiteralToTrue(lastLitUndef);
}
}
++indexOfNextLitToPropagate;
}
return false;
}
void backtrack(){
uint i = modelStack.size() -1;
int lit = 0;
while (modelStack[i] != 0){ // 0 is the DL mark
lit = modelStack[i];
model[abs(lit)] = UNDEF;
modelStack.pop_back();
--i;
}
// at this point, lit is the last decision
modelStack.pop_back(); // remove the DL mark
--decisionLevel;
indexOfNextLitToPropagate = modelStack.size();
setLiteralToTrue(-lit); // reverse last decision
}
// Heuristic for finding the next decision literal:
int getNextDecisionLiteral(){
int max = 0;
int lit = 0;
for (uint i = 0; i < model.size(); ++i){
if (i!=0 and model[i] == UNDEF){
int n = conflictes_tots[i] + positius[i].size()+ negatius[i].size();
if(n>max){
max = n; lit = i;}
//n = conflictes_pos[i] + positius[i].size()+ negatius[i].size();
//if(n>max){
//max = n; lit = -i;}
}
}
return lit;
}
void checkmodel(){
for (int i = 0; i < numClauses; ++i){
bool someTrue = false;
for (int j = 0; not someTrue and j < clauses[i].size(); ++j)
someTrue = (currentValueInModel(clauses[i][j]) == TRUE);
if (not someTrue) {
cout << "Error in model, clause is not satisfied:";
for (int j = 0; j < clauses[i].size(); ++j) cout << clauses[i][j] << " ";
cout << endl;
exit(1);
}
}
}
void stop_time(){
stop_s=clock();
cout << " time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC) << endl;
}
int main(){
start_s=clock();
readClauses(); // reads numVars, numClauses and clauses
model.resize(numVars+1,UNDEF);
indexOfNextLitToPropagate = 0;
decisionLevel = 0;
// Take care of initial unit clauses, if any
for (uint i = 0; i < numClauses; ++i)
if (clauses[i].size() == 1) {
int lit = clauses[i][0];
int val = currentValueInModel(lit);
if (val == FALSE) {cout << "UNSATISFIABLE"; stop_time(); return 10;}
else if (val == UNDEF) setLiteralToTrue(lit);
}
// DPLL algorithm
while (true) {
while ( propagateGivesConflict() ) {
if ( decisionLevel == 0) { cout << "UNSATISFIABLE"; stop_time(); return 10; }
backtrack();
}
int decisionLit = getNextDecisionLiteral();
if (decisionLit == 0) { checkmodel(); cout << "SATISFIABLE"; stop_time(); return 20; }
// start new decision level:
modelStack.push_back(0); // push mark indicating new DL
++indexOfNextLitToPropagate;
++decisionLevel;
setLiteralToTrue(decisionLit); // now push decisionLit on top of the mark
}
stop_time();
}