-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheliminate.c
526 lines (484 loc) · 16.2 KB
/
eliminate.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "eliminate.h"
#include "definition.h"
#include "macros.h"
#include "message.h"
#include "profile.h"
#include "simplify.h"
#include "trace.h"
#include "utilities.h"
#include <inttypes.h>
#include <string.h>
static size_t actual_occurrences (struct ruler *ruler,
struct clauses *clauses) {
size_t clause_size_limit = ruler->limits.clause_size_limit;
struct clause **begin = clauses->begin, **q = begin;
struct clause **end = clauses->end, **p = q;
uint64_t ticks = 1 + cache_lines (end, begin);
bool failed = false;
while (p != end) {
struct clause *clause = *q++ = *p++;
if (is_binary_pointer (clause))
continue;
ticks++;
if (clause->garbage)
q--;
else if (!failed && clause->size > clause_size_limit)
failed = true;
}
ruler->statistics.ticks.elimination += ticks;
clauses->end = q;
return failed ? UINT_MAX : q - begin;
}
static bool can_resolve_clause (struct simplifier *simplifier,
struct clause *clause, unsigned except) {
signed char *marks = simplifier->marks;
struct ruler *ruler = simplifier->ruler;
signed char *values = (signed char *) ruler->values;
if (is_binary_pointer (clause)) {
unsigned other = other_pointer (clause);
signed char value = values[other];
if (value > 0)
return false;
if (value < 0)
return true;
signed char mark = marked_literal (marks, other);
if (mark < 0)
return false;
return true;
} else {
assert (!clause->garbage);
assert (clause->size <= ruler->limits.clause_size_limit);
ruler->statistics.ticks.elimination++;
for (all_literals_in_clause (lit, clause)) {
if (lit == except)
continue;
signed char value = values[lit];
if (value > 0)
return false;
if (value < 0)
continue;
signed char mark = marked_literal (marks, lit);
if (mark < 0)
return false;
}
return true;
}
}
static bool is_elimination_candidate (struct simplifier *simplifier,
unsigned idx) {
if (simplifier->eliminated[idx])
return false;
struct ruler *ruler = simplifier->ruler;
if (!ruler->eliminate[idx])
return false;
unsigned pivot = LIT (idx);
if (ruler->values[pivot])
return false;
return true;
}
static bool can_eliminate_variable (struct simplifier *simplifier,
unsigned idx) {
if (!is_elimination_candidate (simplifier, idx))
return false;
struct ruler *ruler = simplifier->ruler;
ROG ("trying next elimination candidate %s", ROGVAR (idx));
ruler->eliminate[idx] = false;
size_t occurrence_limit = ruler->limits.occurrence_limit;
unsigned pivot = LIT (idx);
struct clauses *pos_clauses = &OCCURRENCES (pivot);
ROG ("flushing garbage clauses of %s", ROGLIT (pivot));
size_t pos_size = actual_occurrences (ruler, pos_clauses);
unsigned not_pivot = NOT (pivot);
struct clauses *neg_clauses = &OCCURRENCES (not_pivot);
ROG ("flushing garbage clauses of %s", ROGLIT (not_pivot));
size_t neg_size = actual_occurrences (ruler, neg_clauses);
if (!pos_size) {
ROG ("pure pivot literal %s", ROGLIT (pivot));
CLEAR (*simplifier->gate);
return true;
}
if (!neg_size) {
ROG ("pure negated pivot literal %s", ROGLIT (not_pivot));
CLEAR (*simplifier->gate);
return true;
}
size_t occurrences = pos_size + neg_size;
ROG ("candidate %s has %zu = %zu + %zu occurrences", ROGVAR (idx),
occurrences, pos_size, neg_size);
if (pos_size && neg_size && occurrences > occurrence_limit) {
ROG ("negative pivot literal %s occurs %zu times (limit %zu)",
ROGLIT (not_pivot), neg_size, occurrence_limit);
return false;
}
size_t resolvents = 0;
unsigned bound = ruler->limits.current_bound;
size_t limit = occurrences + bound;
ROG ("actual limit %zu = occurrences %zu + bound %u", limit, occurrences,
bound);
#ifdef LOGGING
uint64_t ticks_before = ruler->statistics.ticks.elimination;
size_t resolutions = 0;
#endif
if (find_definition (simplifier, pivot)) {
struct clauses *gate = simplifier->gate;
struct clauses *nogate = simplifier->nogate;
for (unsigned i = 0; i != 2; i++) {
for (all_clauses (pos_clause, gate[i])) {
ruler->statistics.ticks.elimination++;
mark_clause (simplifier->marks, pos_clause, pivot);
for (all_clauses (neg_clause, nogate[!i])) {
if (elimination_ticks_limit_hit (simplifier))
break;
#ifdef LOGGING
resolutions++;
#endif
if (can_resolve_clause (simplifier, neg_clause, not_pivot))
if (resolvents++ == limit)
break;
}
unmark_clause (simplifier->marks, pos_clause, pivot);
if (elimination_ticks_limit_hit (simplifier))
break;
}
SWAP (unsigned, pivot, not_pivot);
if (resolvents > limit)
break;
if (elimination_ticks_limit_hit (simplifier))
break;
}
} else {
for (all_clauses (pos_clause, *pos_clauses)) {
ruler->statistics.ticks.elimination++;
mark_clause (simplifier->marks, pos_clause, pivot);
for (all_clauses (neg_clause, *neg_clauses)) {
if (elimination_ticks_limit_hit (simplifier))
break;
#ifdef LOGGING
resolutions++;
#endif
if (can_resolve_clause (simplifier, neg_clause, not_pivot))
if (resolvents++ == limit)
break;
}
unmark_clause (simplifier->marks, pos_clause, pivot);
if (elimination_ticks_limit_hit (simplifier))
break;
}
CLEAR (*simplifier->gate);
}
ROG ("candidate %s has %zu = %zu + %zu occurrences "
"took %zu resolutions %" PRIu64 " ticks total %" PRIu64,
ROGLIT (pivot), limit, pos_size, neg_size, resolutions,
ruler->statistics.ticks.elimination - ticks_before,
ruler->statistics.ticks.elimination);
if (elimination_ticks_limit_hit (simplifier))
return false;
if (resolvents == limit)
ROG ("number of resolvents %zu matches limit %zu", resolvents, limit);
else if (resolvents < limit)
ROG ("number of resolvents %zu stays below limit %zu", resolvents,
limit);
else
ROG ("number of resolvents exceeds limit %zu", limit);
return resolvents <= limit;
}
static bool add_first_antecedent_literals (struct simplifier *simplifier,
struct clause *clause,
unsigned pivot) {
struct ruler *ruler = simplifier->ruler;
ROGCLAUSE (clause, "1st %s antecedent", ROGLIT (pivot));
signed char *values = (signed char *) ruler->values;
struct unsigneds *resolvent = &simplifier->resolvent;
if (is_binary_pointer (clause)) {
unsigned other = other_pointer (clause);
signed char value = values[other];
if (value > 0) {
ROG ("1st antecedent %s satisfied", ROGLIT (other));
return false;
}
if (value < 0)
return true;
PUSH (*resolvent, other);
} else {
assert (!clause->garbage);
bool found_pivot = false;
for (all_literals_in_clause (lit, clause)) {
if (lit == pivot) {
found_pivot = true;
continue;
}
signed char value = values[lit];
if (value > 0) {
ROG ("1st antecedent %s satisfied", ROGLIT (lit));
return false;
}
if (value < 0)
continue;
PUSH (*resolvent, lit);
}
assert (found_pivot), (void) found_pivot;
}
return true;
}
static bool add_second_antecedent_literals (struct simplifier *simplifier,
struct clause *clause,
unsigned not_pivot) {
struct ruler *ruler = simplifier->ruler;
ROGCLAUSE (clause, "2nd %s antecedent", ROGLIT (not_pivot));
signed char *values = (signed char *) ruler->values;
signed char *marks = simplifier->marks;
struct unsigneds *resolvent = &simplifier->resolvent;
if (is_binary_pointer (clause)) {
unsigned other = other_pointer (clause);
signed char value = values[other];
if (value > 0) {
ROG ("2nd antecedent %s satisfied", ROGLIT (other));
return false;
}
if (value < 0)
return true;
signed char mark = marked_literal (marks, other);
if (mark < 0) {
ROG ("2nd antecedent tautological through %s", ROGLIT (other));
return false;
}
if (mark > 0)
return true;
PUSH (*resolvent, other);
return true;
} else {
assert (!clause->garbage);
bool found_not_pivot = false;
for (all_literals_in_clause (lit, clause)) {
if (lit == not_pivot) {
found_not_pivot = true;
continue;
}
signed char value = values[lit];
if (value > 0) {
ROG ("2nd antecedent %s satisfied", ROGLIT (lit));
return false;
}
if (value < 0)
continue;
signed char mark = marked_literal (marks, lit);
if (mark < 0) {
ROG ("2nd antecedent tautological through %s", ROGLIT (lit));
return false;
}
if (mark > 0)
continue;
PUSH (*resolvent, lit);
}
assert (found_not_pivot), (void) found_not_pivot;
return true;
}
}
static void eliminate_variable (struct simplifier *simplifier,
unsigned idx) {
struct ruler *ruler = simplifier->ruler;
unsigned pivot = LIT (idx);
if (ruler->values[pivot])
return;
ROG ("eliminating %s", ROGVAR (idx));
assert (!simplifier->eliminated[idx]);
simplifier->eliminated[idx] = true;
ruler->statistics.eliminated++;
assert (ruler->statistics.active);
ruler->statistics.active--;
ROG ("adding resolvents on %s", ROGVAR (idx));
unsigned not_pivot = NOT (pivot);
struct clauses *pos_clauses = &OCCURRENCES (pivot);
struct clauses *neg_clauses = &OCCURRENCES (not_pivot);
#ifdef LOGGING
size_t resolvents = 0;
#endif
signed char *marks = simplifier->marks;
struct clauses *gate = simplifier->gate;
if (EMPTY (*gate)) {
if (SIZE (*pos_clauses) > SIZE (*neg_clauses)) {
SWAP (unsigned, pivot, not_pivot);
SWAP (struct clauses *, pos_clauses, neg_clauses);
}
for (all_clauses (pos_clause, *pos_clauses)) {
mark_clause (marks, pos_clause, pivot);
for (all_clauses (neg_clause, *neg_clauses)) {
assert (EMPTY (simplifier->resolvent));
if (add_first_antecedent_literals (simplifier, pos_clause, pivot) &&
add_second_antecedent_literals (simplifier, neg_clause,
not_pivot)) {
add_resolvent (simplifier);
#ifdef LOGGING
resolvents++;
#endif
}
CLEAR (simplifier->resolvent);
if (ruler->inconsistent)
break;
}
unmark_clause (marks, pos_clause, pivot);
if (ruler->inconsistent)
break;
}
} else {
ruler->statistics.definitions++;
struct clauses *gate = simplifier->gate;
struct clauses *nogate = simplifier->nogate;
for (unsigned i = 0; i != 2; i++) {
for (all_clauses (pos_clause, gate[i])) {
mark_clause (marks, pos_clause, pivot);
for (all_clauses (neg_clause, nogate[!i])) {
assert (EMPTY (simplifier->resolvent));
if (add_first_antecedent_literals (simplifier, pos_clause,
pivot) &&
add_second_antecedent_literals (simplifier, neg_clause,
not_pivot)) {
add_resolvent (simplifier);
#ifdef LOGGING
resolvents++;
#endif
}
CLEAR (simplifier->resolvent);
if (ruler->inconsistent)
break;
}
unmark_clause (marks, pos_clause, pivot);
if (ruler->inconsistent)
break;
}
SWAP (unsigned, pivot, not_pivot);
if (ruler->inconsistent)
break;
}
}
ROG ("added %zu resolvents on %s", resolvents, ROGVAR (idx));
if (ruler->inconsistent)
return;
size_t pos_size = SIZE (*pos_clauses);
size_t neg_size = SIZE (*neg_clauses);
if (pos_size > neg_size) {
SWAP (unsigned, pivot, not_pivot);
SWAP (size_t, pos_size, neg_size);
SWAP (struct clauses *, pos_clauses, neg_clauses);
}
ROG ("adding %zu clauses with %s to extension stack", pos_size,
ROGLIT (pivot));
struct unsigneds *extension = &ruler->extension[0];
unsigned *unmap = ruler->unmap;
for (all_clauses (clause, *pos_clauses)) {
ruler->statistics.weakened++;
ROGCLAUSE (clause, "pushing weakened[%zu] witness literal %s",
ruler->statistics.weakened, ROGLIT (pivot));
PUSH (*extension, INVALID);
PUSH (*extension, unmap_literal (unmap, pivot));
if (is_binary_pointer (clause)) {
unsigned other = other_pointer (clause);
PUSH (*extension, unmap_literal (unmap, other));
} else {
for (all_literals_in_clause (lit, clause))
if (lit != pivot)
PUSH (*extension, unmap_literal (unmap, lit));
}
}
ruler->statistics.weakened++;
ROG ("pushing weakened[%zu] unit %s", ruler->statistics.weakened,
ROGLIT (not_pivot));
PUSH (*extension, INVALID);
PUSH (*extension, unmap_literal (unmap, not_pivot));
recycle_clauses (simplifier, pos_clauses, pivot);
recycle_clauses (simplifier, neg_clauses, not_pivot);
}
static void gather_elimination_candidates (struct simplifier *simplifier,
struct unsigneds *candidates) {
struct ruler *ruler = simplifier->ruler;
unsigned idx = ruler->compact;
while (idx)
if (is_elimination_candidate (simplifier, --idx))
PUSH (*candidates, idx);
}
bool eliminate_variables (struct simplifier *simplifier, unsigned round) {
struct ruler *ruler = simplifier->ruler;
if (!ruler->options.eliminate)
return false;
if (elimination_ticks_limit_hit (simplifier))
return false;
#ifndef QUIET
double start_round = START (ruler, eliminate);
#endif
assert (!ruler->eliminating);
ruler->eliminating = true;
struct unsigneds candidates;
INIT (candidates);
gather_elimination_candidates (simplifier, &candidates);
#ifndef QUIET
unsigned variables = ruler->compact;
unsigned scheduled = SIZE (candidates);
verbose (0, "[%u] gathered %u elimination candidates %.0f%%", round,
scheduled, percent (scheduled, variables));
#endif
unsigned eliminated = 0;
while (!EMPTY (candidates)) {
if (ruler->inconsistent)
break;
if (ruler->terminate)
break;
if (elimination_ticks_limit_hit (simplifier))
break;
unsigned idx = POP (candidates);
if (can_eliminate_variable (simplifier, idx)) {
eliminate_variable (simplifier, idx);
eliminated++;
}
idx++;
}
#ifndef QUIET
unsigned remaining = SIZE (candidates);
#endif
RELEASE (candidates);
RELEASE (simplifier->resolvent);
RELEASE (simplifier->gate[0]);
RELEASE (simplifier->gate[1]);
RELEASE (simplifier->nogate[0]);
RELEASE (simplifier->nogate[1]);
#ifndef QUIET
unsigned old_bound = ruler->limits.current_bound;
double end_round = STOP (ruler, eliminate);
message (0,
"[%u] eliminated %u variables %.0f%% "
"with bound %u in %.2f seconds",
round, eliminated, percent (eliminated, ruler->compact),
old_bound, end_round - start_round);
if (remaining) {
unsigned completed = scheduled - remaining;
message (0,
"[%u] tried %u candidate variables %.0f%% "
"(%u remain %.0f%%)",
round, completed, percent (completed, variables), remaining,
percent (remaining, variables));
} else
message (0, "[%u] all candidate variables 100%% tried", round);
#endif
assert (ruler->eliminating);
ruler->eliminating = false;
return eliminated;
}
void try_to_increase_elimination_bound (struct ruler *ruler) {
unsigned max_bound = ruler->limits.max_bound;
unsigned old_bound = ruler->limits.current_bound;
unsigned new_bound = old_bound ? 2 * old_bound : 1;
if (new_bound > max_bound)
new_bound = max_bound;
assert (old_bound <= new_bound);
#ifndef QUIET
const char *reached_max_bound = new_bound == max_bound ? "maximum " : "";
#endif
if (old_bound == new_bound)
verbose (0, "keeping elimination bound at %s%u", reached_max_bound,
old_bound);
else {
message (0, "increasing elimination bound to %s%u", reached_max_bound,
new_bound);
memset (ruler->eliminate, 1, ruler->compact);
ruler->limits.current_bound = new_bound;
}
}