-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsat_solver.cpp
More file actions
executable file
·348 lines (296 loc) · 10.9 KB
/
sat_solver.cpp
File metadata and controls
executable file
·348 lines (296 loc) · 10.9 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
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cmath>
using namespace std;
int limit; // recursive call limit
int num_variables; // number of variables in clauses
int num_clauses; // total number of clauses
string infile; // cnf file
string outfile; // sat file
vector<vector<int> > set_of_clauses; // Set of clauses; global and immutable
bool DPLL(vector<int> variable_values);
void printValues(vector<int> variable_values);
// Read in values from cnf file. Populate set_of_clauses, num_variables, and num_clauses
vector<vector<int> > readInput(vector<vector<int> > set_of_clauses)
{
string line;
ifstream input;
input.open(infile);
while(std::getline(input,line))
{
if(line.substr(0,1) == "c" || line.substr(0,1) == "C") {
continue;
} else if(line.substr(0,1) == "p" || line.substr(0,1) == "P") {
// TODO: something in this case causes files with more than one
// digit in num_variables to be truncated, so 1,2,3,4,5,6,7,8,9 work,
// but anything greater than 10 gets truncated, causing an out_of_range exception
// later.
num_variables = stoi(line.substr(6,1));
cout << "num_variables: " << num_variables << endl;
num_clauses = stoi(line.substr(8));
cout << "num_clauses: " << num_clauses << endl;
} else {
int count = 0;
stringstream ss(line);
string num;
vector<int> row;
while(ss >> num) {
// cout << "num: " << num << endl;
if (stoi(num) != 0)
row.push_back(stoi(num));
}
// cout << "\n" << endl;
set_of_clauses.push_back(row);
count++;
}
}
input.close();
return set_of_clauses;
}
// Write results to output file
void writeOutput(vector<int> variable_values, bool sat)
{
ofstream output;
output.open(outfile);
output << "c This is the comments section" << endl;
if(sat) {
output << "s SATISFIABLE" << endl;
output << "v ";
for(int i = 1; i < variable_values.size(); i++)
{
cout << variable_values.at(i) << endl;
if (variable_values.at(i) == 0)
output << "-" << to_string(i) << " ";
else
output << to_string(i) << " ";
}
output << "0";
} else {
output << "s UNSATISFIABLE" << endl;
}
output.close();
}
int main(int argc, char* argv[])
{
// Read args (format: ./sat-solver [LIMIT] [INFILE.cnf] [OUTFILE.sat])
limit = atoi(argv[1]);
infile = argv[2];
outfile = argv[3];
// Read in cnf file and create set of clauses
set_of_clauses = readInput(set_of_clauses);
/* Stores variable values. Index 0 is empty; x1 maps to index 1, x2 to index 2, and so on */
vector<int> variable_values;
// Initialize all variable values to -1 (no values assigned yet).
for (int i = 0; i < num_variables + 1; i++)
variable_values.push_back(-1);
// Perform sat solving (DPLL algorithm)
bool sat;
sat = DPLL(variable_values);
}
vector<int> setVariable(int var, int val, vector<int> variable_values)
{
cout << "Setting x" << var << " to " << val << endl;
if (abs(var) <= num_variables)
{
variable_values.at(abs(var)) = val;
return variable_values;
}
// Trying to set a variable that we can't actually set
else
{
cout << "ERROR: Can't set this variable, outside the range x1 to x" << num_variables << endl;
exit(1);
}
}
// Returns the mapping of a variable to an index in variable_values, e.g. x1 maps to 0,
// x2 maps to 1, and so on.
int varToIndex(int var)
{
return abs(var);
}
// Evaluates a variable given an index (var) to be either true (1) or false (0) or unassigned (-1)
int evaluateVariable(int var, vector<int> variable_values)
{
int res;
// Variable in unassigned
if (variable_values.at(varToIndex(var)) < 0)
res = -1;
// If variable is assigned, evaluate it based on sign
else
res = (var < 0) ? !variable_values.at(varToIndex(var)) : variable_values.at(varToIndex(var));
return res;
}
// Check if clause is a unit clause. If so, return variable that causes it, otherwise return -1
pair<int, int> isUnitClause(vector<int> clause, vector<int> variable_values)
{
int num_unassigned = clause.size(); // Assume all varables are unassigned at first
int unassigned_id = -1;
// Every variable but one must be assigned
for (int var : clause)
{
// cout << "checking variable x" << var << endl;
if (evaluateVariable(var, variable_values) != -1) // This variable is assigned
{
// cout << "This variable is already assigned." << endl;
num_unassigned--;
}
else
{
// cout << "This variable is unassigned!" << endl;
unassigned_id = var; // Otherwise, record unassigned variable
}
}
int val_to_assign = -1;
// More than one variable is unassigned
if (num_unassigned != 1)
unassigned_id = -1;
// Otherwise, find the value to assign to this unassigned variable
else // If complement, assign 0; otherwise assign 1
val_to_assign = (unassigned_id < 0) ? 0 : 1;
// cout << "returning x" << unassigned_id << " and " << val_to_assign << endl;
// exit(0);
// Return unassigned_id if it's a unit clause, otherwise return -1
return make_pair(unassigned_id, val_to_assign);
}
// Determine if set_of_clauses contains a unit cause due to literal L
// and return literal L (as an integer) and its required value V if so.
// If there's no unit clause in set_of_clauses, return (-1, -1).
pair<int, int> containsUnitClaus(vector<int> variable_values)
{
for (vector<int> clause : set_of_clauses)
{
pair<int, int> res = isUnitClause(clause, variable_values);
if (res.first != -1)
return res; // Found a unit clause!
}
return pair<int, int>(0,0);
}
// Determine if clause evaluates to zero
bool checkIfClauseIsZero(vector<int> clause, vector<int> variable_values)
{
for (int var : clause)
{
// If any variable evaluates to 1 or is unassigned, the entire clause is 1
// (or unassigned), so return false
if ((evaluateVariable(var, variable_values) == -1) ||
(evaluateVariable(var, variable_values) == 1))
return false;
}
// If we've reached this point, no 1's or -1's have been found, so entire clause is 0
return true;
}
// TODO: Heuristically choose an unassigned variable x and heuristically choose a value v
int selectVar(vector<int> variable_values)
{
// For now, choose first variable in variable_values that's currently -1 (ignoring first index)
for (int i = 1; i < variable_values.size(); i++)
{
if (variable_values.at(i) == -1)
return i;
}
cout << "ERROR: Attempted to select a variable, but there are no variables left to assign."<<endl;
exit(1); // Should never reach this case -- it would mean there are no variables left to assign
}
// Determine if clause evaluates to one
bool checkIfClauseIsOne(vector<int> clause, vector<int> variable_values)
{
cout << "checking if clause is one: " << endl;
// If any variable has a value of 1, the entire clause is one
for (int var : clause)
{
if (evaluateVariable(var, variable_values) == 1)
return true;
}
return false;
}
// Return true if every clause in set_of_clauses is simplified to 1
bool allOnes(vector<int> variable_values)
{
for (vector<int> clause : set_of_clauses)
{
// Check individual clauses
if (!checkIfClauseIsOne(clause, variable_values))
return false;
}
return true;
}
// Return true if set_of_clauses contains any clause that evals to 0
bool containsZero( vector<int> variable_values)
{
for (vector<int> clause : set_of_clauses)
{
// Check individual clauses
if (checkIfClauseIsZero(clause, variable_values))
return true;
}
return false;
}
// Print the current variable values
void printValues(vector<int> variable_values)
{
for (int i = 1; i < variable_values.size(); i++)
{
cout << "\tx" << i << ": " << variable_values.at(i);
if (variable_values.at(i) == -1)
cout << " (unassigned)" << endl;
else if (variable_values.at(i) == 0)
cout << " (false)" << endl;
else if (variable_values.at(i) == 1)
cout << " (true)" << endl;
}
}
// Return true if satisfied, false if not satisfied
bool DPLL(vector<int> variable_values)
{
cout << "call to DPL. Current variable values: " << endl;
printValues(variable_values);
// Do BCP
pair<int, int> L_V = containsUnitClaus(variable_values);
// cout << "L = x" << L_V.first << ": " << L_V.second << endl;
// while (set_of_clausescontains a unit clause due to literal L)
while (L_V.first != 0)
{
cout << "Found unit clause! Setting x" << L_V.first << " to " << L_V.second << endl;
// TODO: 2-literal watching here
// Simplify set_of_clauses by setting variable for L to its required value
// in all clauses in set_of_clauses
variable_values = setVariable(L_V.first, L_V.second, variable_values);
// printValues(variable_values);
// Check for other unit clauses
L_V = containsUnitClaus(variable_values);
}
if (allOnes(variable_values))
{
cout << "All clauses evaluate to one! Return true" << endl;
printValues(variable_values);
writeOutput(variable_values,true);
return true; // return SAT; have simplified every clause to be 1
}
if (containsZero(variable_values))
{
cout << "Some clause evaluates to 0 :( return false" << endl;
printValues(variable_values);
writeOutput(variable_values,false);
return false; // return UNSAT; this is a conflict, this set of var assignment doesn't satisfy
}
// Otherwise, we need to recurse
// TODO: Heuristically choose an unassigned variable x and heuristically
// choose a value v (in selectVar function)
int x = selectVar(variable_values), v = 1;
variable_values = setVariable(x, v, variable_values);
cout << "set variable x" << x << " to " << v << endl;
if (DPLL(variable_values)) // if( DPLL( set_of_clauses = simplified by setting x=v ) == SAT )
{
cout << "DPLL has been satisfied!" << endl;
return true;
}
else // else return( DPLL( set_of_clauses = simplified by setting x= ¬v ) )
{
cout << "original choice didn't work. trying x" << x << " = " << 1-v << endl;
variable_values = setVariable(x, 1-v, variable_values);
return (DPLL(variable_values));
}
}