-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathbabysat.c
401 lines (285 loc) · 9.89 KB
/
babysat.c
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
Copyright (c) the Selfie Project authors. All rights reserved.
Please see the AUTHORS file for details. Use of this source code is
governed by a BSD license that can be found in the LICENSE file.
Selfie is a project of the Computational Systems Group at the
Department of Computer Sciences of the University of Salzburg
in Austria. For further information and code please refer to:
selfie.cs.uni-salzburg.at
Babysat is a naive implementation of a SAT solver for educational
purposes. It is arguably even more naive than Donald Knuth's sat0.
Babysat enumerates all possible variable assignments and checks
satisfiability for each without any optimizations whatsoever.
The implementation can therefore also be seen as an executable
specification of a SAT solver.
Babysat comes with a DIMACS CNF parser, is written in C*, and
uses code from the selfie system. See selfie's Makefile for
details on how to build babysat.
*/
// -----------------------------------------------------------------
// -------------------------- SAT Solver ---------------------------
// -----------------------------------------------------------------
uint64_t clause_may_be_true(uint64_t* clause_address, uint64_t depth);
uint64_t instance_may_be_true(uint64_t depth);
uint64_t babysat(uint64_t depth);
// ------------------------ GLOBAL CONSTANTS -----------------------
uint64_t FALSE = 0;
uint64_t TRUE = 1;
uint64_t UNSAT = 0;
uint64_t SAT = 1;
// ------------------------ GLOBAL VARIABLES -----------------------
char* dimacs_name = (char*) 0;
uint64_t number_of_sat_variables = 0;
// number_of_sat_variables
uint64_t* sat_assignment = (uint64_t*) 0;
uint64_t number_of_sat_clauses = 0;
// number_of_sat_clauses * 2 * number_of_sat_variables
uint64_t* sat_instance = (uint64_t*) 0;
// -----------------------------------------------------------------
// ----------------------- DIMACS CNF PARSER -----------------------
// -----------------------------------------------------------------
void selfie_print_dimacs();
void dimacs_find_next_character(uint64_t new_line);
void dimacs_get_symbol();
void dimacs_word(char* word);
uint64_t dimacs_number();
void dimacs_get_clause(uint64_t clause);
void dimacs_get_instance();
void selfie_load_dimacs();
void selfie_sat();
// -----------------------------------------------------------------
// -------------------------- SAT Solver ---------------------------
// -----------------------------------------------------------------
uint64_t clause_may_be_true(uint64_t* clause_address, uint64_t depth) {
uint64_t variable;
variable = 0;
while (variable <= depth) {
if (*(sat_assignment + variable) == TRUE) {
if (*(clause_address + 2 * variable))
return TRUE;
} else if (*(clause_address + 2 * variable + 1))
// variable must be FALSE because variable <= depth
return TRUE;
variable = variable + 1;
}
while (variable < number_of_sat_variables) {
// variable must be unassigned because variable > depth
if (*(clause_address + 2 * variable))
return TRUE;
else if (*(clause_address + 2 * variable + 1))
return TRUE;
variable = variable + 1;
}
return FALSE;
}
uint64_t instance_may_be_true(uint64_t depth) {
uint64_t clause;
clause = 0;
while (clause < number_of_sat_clauses) {
if (clause_may_be_true(sat_instance + clause * 2 * number_of_sat_variables, depth))
clause = clause + 1;
else
// clause is FALSE under current assignment
return FALSE;
}
return TRUE;
}
uint64_t babysat(uint64_t depth) {
if (depth == number_of_sat_variables)
return SAT;
*(sat_assignment + depth) = TRUE;
if (instance_may_be_true(depth)) if (babysat(depth + 1) == SAT)
return SAT;
*(sat_assignment + depth) = FALSE;
if (instance_may_be_true(depth)) if (babysat(depth + 1) == SAT)
return SAT;
return UNSAT;
}
// -----------------------------------------------------------------
// ----------------------- DIMACS CNF PARSER -----------------------
// -----------------------------------------------------------------
void selfie_print_dimacs() {
uint64_t clause;
uint64_t variable;
printf("p cnf %lu %lu\n", number_of_sat_variables, number_of_sat_clauses);
clause = 0;
while (clause < number_of_sat_clauses) {
variable = 0;
while (variable < number_of_sat_variables) {
if (*(sat_instance + clause * 2 * number_of_sat_variables + 2 * variable) == TRUE)
printf("%ld ", variable + 1);
else if (*(sat_instance + clause * 2 * number_of_sat_variables + 2 * variable + 1) == TRUE)
printf("%ld ", -(variable + 1));
variable = variable + 1;
}
printf("0\n");
clause = clause + 1;
}
}
void dimacs_find_next_character(uint64_t new_line) {
uint64_t in_comment;
// assuming we are not in a comment
in_comment = 0;
// read and discard all whitespace and comments until a character is found
// that is not whitespace and does not occur in a comment, or the file ends
while (1) {
if (in_comment) {
get_character();
if (is_character_new_line())
// comments end with new line
in_comment = 0;
else if (character == CHAR_EOF)
return;
else
// count the characters in comments as ignored characters
// line feed and carriage return are counted below
number_of_ignored_characters = number_of_ignored_characters + 1;
} else if (new_line) {
new_line = 0;
if (character == 'c') {
// 'c' at beginning of a line begins a comment
in_comment = 1;
// count the number of comments
number_of_comments = number_of_comments + 1;
}
} else if (is_character_whitespace()) {
if (is_character_new_line())
new_line = 1;
else
new_line = 0;
// count whitespace as ignored characters
number_of_ignored_characters = number_of_ignored_characters + 1;
get_character();
} else
// character found that is not whitespace and not occurring in a comment
return;
}
}
void dimacs_get_symbol() {
dimacs_find_next_character(0);
get_symbol();
}
void dimacs_word(char* word) {
if (symbol == SYM_IDENTIFIER) {
if (string_compare(identifier, word)) {
dimacs_get_symbol();
return;
} else
syntax_error_unexpected_identifier(word);
} else
syntax_error_expected_symbol(SYM_IDENTIFIER);
exit(EXITCODE_PARSERERROR);
}
uint64_t dimacs_number() {
uint64_t number;
if (symbol == SYM_INTEGER) {
number = literal;
dimacs_get_symbol();
return number;
} else
syntax_error_expected_symbol(SYM_INTEGER);
exit(EXITCODE_PARSERERROR);
}
void dimacs_get_clause(uint64_t clause) {
uint64_t not;
while (1) {
not = 0;
if (symbol == SYM_MINUS) {
not = 1;
dimacs_get_symbol();
}
if (symbol == SYM_INTEGER) {
if (literal == 0) {
dimacs_get_symbol();
return;
} else if (literal > number_of_sat_variables) {
syntax_error_message("clause exceeds declared number of variables");
exit(EXITCODE_PARSERERROR);
}
// literal encoding starts at 0
literal = literal - 1;
if (not)
*(sat_instance + clause * 2 * number_of_sat_variables + 2 * literal + 1) = TRUE;
else
*(sat_instance + clause * 2 * number_of_sat_variables + 2 * literal) = TRUE;
} else if (symbol == SYM_EOF)
return;
else
syntax_error_expected_symbol(SYM_INTEGER);
dimacs_get_symbol();
}
}
void dimacs_get_instance() {
uint64_t clauses;
clauses = 0;
while (clauses < number_of_sat_clauses)
if (symbol != SYM_EOF) {
dimacs_get_clause(clauses);
clauses = clauses + 1;
} else {
syntax_error_message("instance has fewer clauses than declared");
exit(EXITCODE_PARSERERROR);
}
if (symbol != SYM_EOF) {
syntax_error_message("instance has more clauses than declared");
exit(EXITCODE_PARSERERROR);
}
}
void selfie_load_dimacs() {
source_name = get_argument();
printf("%s: babysat loading SAT instance %s\n", selfie_name, source_name);
// assert: source_name is mapped and not longer than MAX_FILENAME_LENGTH
source_fd = open_read_only(source_name);
if (signed_less_than(source_fd, 0)) {
printf("%s: could not open input file %s\n", selfie_name, source_name);
exit(EXITCODE_IOERROR);
}
reset_scanner();
// ignore all comments before problem
dimacs_find_next_character(1);
dimacs_get_symbol();
dimacs_word("p");
dimacs_word("cnf");
number_of_sat_variables = dimacs_number();
sat_assignment = (uint64_t*) zmalloc(number_of_sat_variables * sizeof(uint64_t));
number_of_sat_clauses = dimacs_number();
sat_instance = (uint64_t*) zmalloc(number_of_sat_clauses * 2 * number_of_sat_variables * sizeof(uint64_t));
dimacs_get_instance();
printf("%s: %lu clauses with %lu declared variables loaded from %s\n", selfie_name,
number_of_sat_clauses,
number_of_sat_variables,
source_name);
dimacs_name = source_name;
}
void selfie_sat() {
uint64_t variable;
init_scanner();
selfie_load_dimacs();
if (dimacs_name == (char*) 0) {
printf("%s: nothing to SAT solve\n", selfie_name);
return;
}
selfie_print_dimacs();
if (babysat(0) == SAT) {
printf("%s: %s is satisfiable with ", selfie_name, dimacs_name);
variable = 0;
while (variable < number_of_sat_variables) {
if (*(sat_assignment + variable) == FALSE)
printf("-%lu ", variable + 1);
else
printf("%lu ", variable + 1);
variable = variable + 1;
}
} else
printf("%s: %s is unsatisfiable", selfie_name, dimacs_name);
println();
}
// -----------------------------------------------------------------
// ----------------------------- MAIN ------------------------------
// -----------------------------------------------------------------
int main(int argc, char** argv) {
init_selfie((uint64_t) argc, (uint64_t*) argv);
init_library();
selfie_sat();
return EXITCODE_NOERROR;
}