-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqbce_qrat_plus.c
1237 lines (1121 loc) · 42.3 KB
/
qbce_qrat_plus.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of QRATPre+.
Copyright 2019
Florian Lonsing, Stanford University, USA.
Copyright 2018
Florian Lonsing, Vienna University of Technology, Austria.
QRATPre+ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
QRATPre+ is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with QRATPre+. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdlib.h>
#include "stack.h"
#include "qbcp.h"
#include "util.h"
#include "qbce_qrat_plus.h"
#include "qratpreplus_internals.h"
enum QRATPlusCheckMode
{
QRATPLUS_CHECK_MODE_UNDEF = 0,
QRATPLUS_CHECK_MODE_QBCE = 1,
QRATPLUS_CHECK_MODE_AT = 2,
QRATPLUS_CHECK_MODE_QRAT = 3
};
typedef enum QRATPlusCheckMode QRATPlusCheckMode;
/* Check if soft time limit is exceeded every
'2^QRATPLUS_SOFT_TIME_LIMIT_CHECK_PERIOD' clause checks. With small
values the accuracy of the time limit will be higher but checking
the limit may be costly. With high values checks are infrequent and
hence cheap but program may run longer if clause checks are
expensive. */
#define QRATPLUS_SOFT_TIME_LIMIT_CHECK_PERIOD 10
/* Returns non-zero iff clause 'c' contains at least one literal of a
variable that appears in the outermost (i.e. leftmost) quantifier
block. */
static int
clause_has_outermost_qblock_literal (QRATPrePlus * qr, Clause *c)
{
LitID *p, *e;
for (p = c->lits, e = p + c->num_lits; p < e; p++)
{
LitID lit = *p;
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
if (var->qblock->nesting == 0)
return 1;
}
return 0;
}
/* Returns true if and only if 'lit' appears in the literal array
bounded by 'start' and 'end' (position 'end' is not part of the
array) AND 'lit' is from a qblock of nesting level equal to
'nesting' or smaller. Assumes that literal array is sorted by qblock
ordering. */
static int
find_literal_outer_tautology (QRATPrePlus * qr, LitID taut_lit, Nesting nesting,
LitID * start, LitID * end)
{
LitID *p;
for (p = start; p < end; p++)
{
LitID lit = *p;
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
if (var->qblock->nesting > nesting)
break;
if (lit == taut_lit)
return 1;
}
return 0;
}
/* Return nonzero iff resolvent of 'c' and 'occ' on literal 'lit' is
tautologous with respect to a variable that is smaller than or
equal to 'lit' in the prefix ordering. */
static int
check_outer_tautology (QRATPrePlus * qr, Clause *c, LitID lit, Clause *occ)
{
assert (!c->redundant);
assert (!occ->redundant);
assert (c->num_lits > 0);
assert (occ->num_lits > 0);
qr->clause_redundancy_or_checks++;
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
QBlock *qblock = var->qblock;
Nesting nesting = qblock->nesting;
/* Literal 'lit' must appear in complementary phases in clauses 'c'
and 'occ'. */
assert (find_literal (lit, c->lits, c->lits + c->num_lits));
assert (find_literal (-lit, occ->lits, occ->lits + occ->num_lits));
const unsigned int qbce_check_taut_by_nesting =
qr->options.qbce_check_taut_by_nesting;
LitID *cp, *ce;
for (cp = c->lits, ce = cp + c->num_lits; cp < ce; cp++)
{
qr->clause_redundancy_or_checks_lits_seen++;
LitID cl = *cp;
Var *cv = LIT2VARPTR (qr->pcnf.vars, cl);
/* Can ignore variables from qblocks larger than 'lit'. */
if (qbce_check_taut_by_nesting && cv->qblock->nesting > nesting)
break;
/* Must ignore potential blocking literal 'lit'. */
if (cl != lit)
{
/* Must consider only tautologies due to literals with
nesting level smaller than or equal to nesting of
'lit'. Check whether other clause 'occ' contains
complementary literal '-cl'. */
if ((!qbce_check_taut_by_nesting && (cv->qblock->nesting <= nesting)
&& find_literal (-cl, occ->lits, occ->lits + occ->num_lits)) ||
(qbce_check_taut_by_nesting && find_literal_outer_tautology
(qr, -cl, nesting, occ->lits, occ->lits + occ->num_lits)))
return 1;
}
}
return 0;
}
/* Return nonzero iff clause 'c' has qrat on literal 'lit'. */
static int
has_qrat_on_literal (QRATPrePlus * qr, Clause *c, LitID lit)
{
assert (!c->redundant);
assert (c->num_lits > 0);
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
assert (LIT_NEG (lit) || LIT_POS (lit));
/* Set pointer to stack of clauses containing literals complementary to 'lit'. */
ClausePtrStack *comp_occs = LIT_NEG (lit) ?
&(var->pos_occ_clauses) : &(var->neg_occ_clauses);
/* Check all possible resolution candidates on literal 'lit' and
clauses on 'comp_occs'. Must ignore already redundant
occurrences. */
Clause **occ_p, **occ_e;
for (occ_p = comp_occs->start, occ_e = comp_occs->top; occ_p < occ_e; occ_p++)
{
/* Do QRAT test either with or without EABS (controlled by option '--eabs'). */
Clause *occ = *occ_p;
if (occ->redundant)
continue;
qr->clause_redundancy_or_checks++;
qr->clause_redundancy_or_checks_lits_seen += occ->num_lits;
if (!qrat_qbcp_check (qr, c, lit, occ))
{
if (var->qblock->type == QTYPE_EXISTS)
{
/* Collect 'occ' as a witness for non-redundancy of 'c' (on
'lit'). */
if (!occ->witness)
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " clause ");
print_lits (qr, stderr, occ->lits, occ->num_lits, 1);
fprintf (stderr, " is witness of: ");
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
occ->witness = 1;
PUSH_STACK (qr->mm, qr->witness_clauses, occ);
}
}
return 0;
}
}
/* All candidates fulfill tautology property of QBCE, hence 'lit'
is blocking literal in clause 'c'. */
return 1;
}
/* Return nonzero iff 'lit' is a blocking literal in clause 'c'. */
static int
is_literal_blocking (QRATPrePlus * qr, Clause *c, LitID lit)
{
assert (!c->redundant);
assert (c->num_lits > 0);
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
assert(var->qblock->type == QTYPE_EXISTS);
assert (LIT_NEG (lit) || LIT_POS (lit));
/* Set pointer to stack of clauses containing literals complementary to 'lit'. */
ClausePtrStack *comp_occs = LIT_NEG (lit) ?
&(var->pos_occ_clauses) : &(var->neg_occ_clauses);
/* Check all possible resolution candidates on literal 'lit' and
clauses on 'comp_occs'. Must ignore already redundant
occurrences. */
Clause **occ_p, **occ_e;
for (occ_p = comp_occs->start, occ_e = comp_occs->top; occ_p < occ_e; occ_p++)
{
Clause *occ = *occ_p;
if (!occ->redundant &&
/* Syntactic check for tautology, i.e., QBCE check. */
!check_outer_tautology (qr, c, lit, occ))
{
/* Collect 'occ' as a witness for non-redundancy of 'c' (on
'lit'). */
if (!occ->witness)
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " clause ");
print_lits (qr, stderr, occ->lits, occ->num_lits, 1);
fprintf (stderr, " is witness of: ");
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
occ->witness = 1;
PUSH_STACK (qr->mm, qr->witness_clauses, occ);
}
return 0;
}
}
/* All candidates fulfill tautology property of QBCE, hence 'lit'
is blocking literal in clause 'c'. */
return 1;
}
/* Return nonzero iff clause 'c' has QRAT. */
static int
has_clause_qrat (QRATPrePlus * qr, Clause *c)
{
assert (!c->redundant);
LitID *p, *e;
for (p = c->lits, e = p + c->num_lits; p < e; p++)
{
qr->clause_redundancy_or_checks_lits_seen++;
LitID lit = *p;
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
/* Check if existential literal 'lit' is indeed a blocking literal. */
if (var->qblock->type == QTYPE_EXISTS)
{
if (has_qrat_on_literal (qr, c, lit))
return 1;
}
}
return 0;
}
/* Return nonzero iff 'lit' is blocked in clause 'c'. */
static int
is_literal_blocked (QRATPrePlus * qr, Clause *c, LitID lit)
{
assert (!c->redundant);
assert (c->num_lits > 0);
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
assert(var->qblock->type == QTYPE_FORALL);
assert (LIT_NEG (lit) || LIT_POS (lit));
/* Set pointer to stack of clauses containing literals complementary to 'lit'. */
ClausePtrStack *comp_occs = LIT_NEG (lit) ?
&(var->pos_occ_clauses) : &(var->neg_occ_clauses);
/* Check all possible resolution candidates on literal 'lit' and
clauses on 'comp_occs'. Must ignore already redundant
occurrences. */
Clause **occ_p, **occ_e;
for (occ_p = comp_occs->start, occ_e = comp_occs->top; occ_p < occ_e; occ_p++)
{
Clause *occ = *occ_p;
if (!occ->redundant &&
/* Syntactic check for tautology, i.e., QBCE check. */
!check_outer_tautology (qr, c, lit, occ))
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " clause ");
print_lits (qr, stderr, occ->lits, occ->num_lits, 1);
fprintf (stderr, " is witness of: ");
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
return 0;
}
}
/* All candidates fulfill tautology property of QBCE, hence 'lit'
is blocked in clause 'c'. */
return 1;
}
/* Return nonzero iff clause 'c' is blocked. */
static int
is_clause_blocked (QRATPrePlus * qr, Clause *c)
{
assert (!c->redundant);
LitID *p, *e;
for (p = c->lits, e = p + c->num_lits; p < e; p++)
{
qr->clause_redundancy_or_checks_lits_seen++;
LitID lit = *p;
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
/* Check if existential literal 'lit' is indeed a blocking literal. */
if (var->qblock->type == QTYPE_EXISTS)
{
if (is_literal_blocking (qr, c, lit))
return 1;
}
}
return 0;
}
/* Returns nonzero iff clause 'c' meets the current limits (if not, then 'c'
is not checked for redundancy). */
static int
reschedule_is_clause_within_limits (QRATPrePlus * qr, Clause *c)
{
if (c->num_lits < qr->limit_min_clause_len)
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, "Clause ID %u not rescheduled, length %u less than min-length %u: ",
c->id, c->num_lits, qr->limit_min_clause_len);
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
return 0;
}
if (qr->limit_max_clause_len < c->num_lits)
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, "Clause ID %u not rescheduled, length %u greater than max-length %u: ",
c->id, c->num_lits, qr->limit_max_clause_len);
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
return 0;
}
LitID *p, *e;
for (p = c->lits, e = p + c->num_lits; p < e; p++)
{
LitID lit = *p;
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
ClausePtrStack *compl_occs = LIT_NEG (lit) ?
&var->pos_occ_clauses : &var->neg_occ_clauses;
if (qr->limit_max_occ_cnt < (unsigned int) COUNT_STACK (*compl_occs))
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, "Clause ID %u not rescheduled, compl-occs count %u greater than max occ count %u: ",
c->id, (unsigned int) COUNT_STACK (*compl_occs), qr->limit_max_occ_cnt);
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
return 0;
}
}
if (qr->options.ignore_outermost_vars &&
clause_has_outermost_qblock_literal (qr, c))
return 0;
return 1;
}
/* Collect all non-redundant and not already collected clauses 'd' on stack
'rescheduled' such that 'd' potentially is now redundant due to having
identified 'c' as redundant before. Clauses 'd' are resolution partners of
'c'. That is, the now redundant clause 'c' may have prevented 'd' from
being redundant if the check of the outer resolvent of 'c' and 'd'
failed. That check may now succeed as 'c' was found redundant. */
static void
reschedule_from_redundant_clause (QRATPrePlus * qr, Clause *c,
ClausePtrStack *rescheduled)
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " Rescheduling from redundant clause: ");
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
assert (c->redundant);
LitID *p, *e;
for (p = c->lits, e = p + c->num_lits; p < e; p++)
{
LitID lit = *p;
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
ClausePtrStack *compl_occs = LIT_NEG (lit) ?
&var->pos_occ_clauses : &var->neg_occ_clauses;
Clause *oc, **cp, **ce;
for (cp = compl_occs->start, ce = compl_occs->top; cp < ce; cp++)
{
oc = *cp;
if (!oc->redundant)
if (!oc->rescheduled && reschedule_is_clause_within_limits (qr, oc))
{
oc->rescheduled = 1;
PUSH_STACK (qr->mm, *rescheduled, oc);
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " rescheduled clause: ");
print_lits (qr, stderr, oc->lits, oc->num_lits, 1);
}
}
}
}
}
static void
reset_witness_clauses (QRATPrePlus *qr)
{
Clause **cp, **ce;
for (cp = qr->witness_clauses.start, ce = qr->witness_clauses.top;
cp < ce; cp++)
{
Clause *c = *cp;
assert (c->witness);
c->witness = 0;
}
RESET_STACK (qr->witness_clauses);
}
static void
reschedule_from_redundant_witness_clauses (QRATPrePlus *qr,
ClausePtrStack *rescheduled)
{
if (qr->options.verbosity >= 2)
fprintf (stderr, "\nRescheduling from %u witness clauses\n",
(unsigned int) COUNT_STACK (qr->witness_clauses));
assert (EMPTY_STACK (*rescheduled));
Clause **cp, **ce;
for (cp = qr->witness_clauses.start, ce = qr->witness_clauses.top;
cp < ce; cp++)
{
Clause *c = *cp;
assert (c->witness);
if (c->redundant)
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " Redundant witness clause: ");
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
reschedule_from_redundant_clause (qr, c, rescheduled);
c->witness = 0;
Clause *last = POP_STACK (qr->witness_clauses);
*cp = last;
cp--;
ce--;
}
else
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " Non-redundant witness clause: ");
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
}
}
}
static int
compare_clauses_by_id (const void * cp1, const void * cp2)
{
Clause *c1 = (Clause *) cp1;
Clause *c2 = (Clause *) cp2;
if (c1->id < c2->id)
return -1;
else if (c1->id > c2->id)
return 1;
else return 0;
}
static void
permute_clauses_to_be_checked (QRATPrePlus * qr, ClausePtrStack *to_be_checked)
{
if (!EMPTY_STACK (*to_be_checked))
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, "Sequence before permuting: ");
Clause **cp, **ce;
for (cp = to_be_checked->start, ce = to_be_checked->top; cp < ce; cp++)
{
Clause *c = *cp;
fprintf (stderr, "%u ", c->id);
}
fprintf (stderr, "\n");
}
/* Permute set of clauses to be checked randomly according to the
following algorithm:
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle */
unsigned int i;
for (i = ((unsigned int)COUNT_STACK (*to_be_checked)) - 1; i >= 1; i--)
{
unsigned int j = rand_r (&qr->options.seed) % (i+1);
assert (i < COUNT_STACK (*to_be_checked));
assert (j < COUNT_STACK (*to_be_checked));
Clause *tmp = to_be_checked->start[i];
to_be_checked->start[i] = to_be_checked->start[j];
to_be_checked->start[j] = tmp;
}
if (qr->options.verbosity >= 2)
{
fprintf (stderr, "Sequence after permuting: ");
Clause **cp, **ce;
for (cp = to_be_checked->start, ce = to_be_checked->top; cp < ce; cp++)
{
Clause *c = *cp;
fprintf (stderr, "%u ", c->id);
}
fprintf (stderr, "\n");
}
}
}
static Clause *
find_non_redundant_occ (QRATPrePlus * qr, ClausePtrStack *occs)
{
Clause **occ_p, **occ_e;
for (occ_p = occs->start, occ_e = occs->top; occ_p < occ_e; occ_p++)
{
Clause *c = *occ_p;
if (!c->redundant)
return c;
}
return 0;
}
static void
reschedule_from_input_clauses (QRATPrePlus * qr, ClausePtrStack *rescheduled)
{
Clause *c;
for (c = qr->pcnf.clauses.first; c; c = c->link.next)
{
/* Mark 'c->rescheduled' set iff 'c' appears in set of rescheduled
clauses. */
if (!c->redundant && !c->rescheduled &&
reschedule_is_clause_within_limits (qr, c))
{
c->rescheduled = 1;
PUSH_STACK (qr->mm, *rescheduled, c);
}
}
}
/* Returns nonzero iff redundant clauses were found. */
static int
find_and_mark_redundant_clauses_aux (QRATPrePlus * qr,
ClausePtrStack *to_be_checked,
ClausePtrStack *rescheduled,
const QRATPlusCheckMode mode)
{
int result = 0;
assert (mode == QRATPLUS_CHECK_MODE_QBCE || mode == QRATPLUS_CHECK_MODE_AT ||
mode == QRATPLUS_CHECK_MODE_QRAT);
assert (!qr->options.no_qbce || !qr->options.no_qat ||
!qr->options.no_qrate);
assert (mode != QRATPLUS_CHECK_MODE_QRAT || !qr->options.no_qrate);
assert (mode != QRATPLUS_CHECK_MODE_AT || !qr->options.no_qat);
assert (EMPTY_STACK (qr->witness_clauses));
int exceeded = 0;
if ((exceeded = exceeded_soft_time_limit (qr)))
fprintf (stderr, "Exceeded soft time limit of %u sec\n", qr->soft_time_limit);
#ifndef NDEBUG
{
/* Clauses to be tested are expected to be sorted by ID. */
Clause **cp, **ce, **cpprev;
for (cp = cpprev = rescheduled->start, ce = rescheduled->top; cp < ce; cp++)
{
Clause *c = *cp;
Clause *cprev = *cpprev;
assert (cprev->id <= c->id);
cpprev = cp;
}
}
#endif
unsigned int cur_redundant_clauses = 0;
int changed = 1;
while (!exceeded && changed)
{
/* Statistics. */
qr->cnt_redundant_clauses += cur_redundant_clauses;
qr->cnt_qbce_iterations++;
/* Set up new iteration: swap 'rescheduled' and 'to_be_checked', reset. */
changed = 0;
Clause **cp, **ce;
for (cp = rescheduled->start, ce = rescheduled->top; cp < ce; cp++)
{
Clause *c = *cp;
/* NOTE: we may encounter clauses 'c' with 'c->redundant' true. This
may happen if we first reschedule a non-redundant clause 'c' which
in the same iteration is found redundant later. */
assert (c->rescheduled);
c->rescheduled = 0;
}
ClausePtrStack *stack_tmp = to_be_checked;
to_be_checked = rescheduled;
rescheduled = stack_tmp;
RESET_STACK (*rescheduled);
if (qr->options.verbosity >= 1)
{
fprintf (stderr, "\n======\n%s iteration %d: %d new redundant clauses in previous iteration %d\n",
mode == QRATPLUS_CHECK_MODE_QBCE ? "QBCE" : (mode == QRATPLUS_CHECK_MODE_AT ? "AT" : "QRATE"),
qr->cnt_qbce_iterations, cur_redundant_clauses, qr->cnt_qbce_iterations - 1);
fprintf (stderr, "Clauses to be checked (worst case): %u ( %f %% of original CNF)\n======\n",
(unsigned int) COUNT_STACK (*to_be_checked), 100 * (COUNT_STACK (*to_be_checked) / (float) qr->actual_num_clauses));
}
cur_redundant_clauses = 0;
/* Either randomly permute or sort clauses to be tested by ID. Note that
without sorting we may get different orderings due to the way we
reschedule clauses. */
if (mode != QRATPLUS_CHECK_MODE_QBCE && qr->options.permute)
permute_clauses_to_be_checked (qr, to_be_checked);
else
qsort (to_be_checked->start, COUNT_STACK (*to_be_checked),
sizeof (Clause *), compare_clauses_by_id);
for (cp = to_be_checked->start, ce = to_be_checked->top;
!exceeded && cp < ce; cp++)
{
Clause *c = *cp;
assert (!qr->options.ignore_outermost_vars ||
!clause_has_outermost_qblock_literal (qr, c));
/* NOTE: we may encounter clauses 'c' with 'c->redundant' true
because such clauses may appear on 'rescheduled' (see comment above)
and we just swap the sets at the beginning of each iteration. */
if (!c->redundant)
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, "\nRedundancy check on clause ");
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
qr->cnt_qbce_checks++;
/* Print progress information. */
if (qr->options.verbosity >= 1 &&
(qr->cnt_qbce_checks & ((1 << 15) - 1)) == 0)
fprintf (stderr, "progress -- clause checks: %llu\n",
qr->cnt_qbce_checks);
/* Periodically check if soft time limit reached, exit for-loop. */
if ((qr->cnt_qbce_checks &
((1 << QRATPLUS_SOFT_TIME_LIMIT_CHECK_PERIOD) - 1)) == 0 &&
(exceeded = exceeded_soft_time_limit (qr)))
{
fprintf (stderr, "Exceeded soft time limit of %u sec after %llu clause checks\n",
qr->soft_time_limit, qr->cnt_qbce_checks);
continue;
}
if ( (mode == QRATPLUS_CHECK_MODE_QBCE && is_clause_blocked (qr, c)) ||
(mode == QRATPLUS_CHECK_MODE_AT && qrat_qat_check (qr, c)) ||
(mode == QRATPLUS_CHECK_MODE_QRAT && has_clause_qrat (qr, c)) )
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " ==> Clause ");
print_lits (qr, stderr, c->lits, c->num_lits, 1);
fprintf (stderr, " is redundant.\n");
}
c->redundant = 1;
PUSH_STACK (qr->mm, qr->redundant_clauses, c);
cur_redundant_clauses++;
changed = 1;
result = 1;
}
}
}
/* Do not reschedule from incomplete iterations of above for-loop. */
if (exceeded)
continue;
/* Reschedule from redundant witness clauses in QBCE/QRAT mode. */
if (mode != QRATPLUS_CHECK_MODE_AT)
reschedule_from_redundant_witness_clauses (qr, rescheduled);
}
/* Must update statistics after exiting loop due to exceeding time limit. */
assert (exceeded || cur_redundant_clauses == 0);
qr->cnt_redundant_clauses += cur_redundant_clauses;
#ifndef NDEBUG
Clause *c;
for (c = qr->pcnf.clauses.first; c; c = c->link.next)
assert (!c->rescheduled);
#endif
return result;
}
static void
unlink_redundant_clauses_occs (QRATPrePlus * qr, ClausePtrStack *occs)
{
Clause **cp, **ce;
for (cp = occs->start, ce = occs->top; cp < ce; cp++)
{
Clause *c = *cp;
if (c->redundant)
{
Clause *last = POP_STACK (*occs);
*cp = last;
cp--;
ce--;
}
}
}
static void
remove_clause_from_occs (ClausePtrStack *occs, Clause *c)
{
Clause **p, **e;
for (p = occs->start, e = occs->top; p < e; p++)
{
if (*p == c)
{
*p = POP_STACK (*occs);
break;
}
}
/* Assuming that 'occs' contains 'c'. */
assert (p < e);
}
/* Clean up redundant universal literal 'red_lit' from clause 'c', update data
structures and watchers. */
static void
cleanup_redundant_universal_literal (QRATPrePlus * qr, Clause * c, LitID red_lit)
{
assert (c->num_lits >= 2);
assert (find_literal (red_lit, c->lits, c->lits + c->num_lits));
assert (LIT2VARPTR (qr->pcnf.vars, red_lit)->qblock->type == QTYPE_FORALL);
assert (c->rw_index != WATCHED_LIT_INVALID_INDEX);
assert (c->lw_index != WATCHED_LIT_INVALID_INDEX);
assert (c->lw_index != c->rw_index);
unsigned int update_watcher = 0;
Var *red_var = LIT2VARPTR (qr->pcnf.vars, red_lit);
/* Check for update of watched literals. Note: when not using EABS, then
right watcher may be set to a universal literal and hence we must handle
this case below. */
if (c->lits[c->lw_index] == red_lit || c->lits[c->rw_index] == red_lit)
{
/* Reset left and right watched literal, remove 'c' from watched occs. */
update_watcher = 1;
LitID lw_lit = c->lits[c->lw_index];
Var *lw_var = LIT2VARPTR (qr->pcnf.vars, lw_lit);
ClausePtrStack *occs = LIT_NEG (lw_lit) ?
&lw_var->watched_neg_occ_clauses : &lw_var->watched_pos_occ_clauses;
remove_clause_from_occs (occs, c);
c->lw_index = WATCHED_LIT_INVALID_INDEX;
LitID rw_lit = c->lits[c->rw_index];
Var *rw_var = LIT2VARPTR (qr->pcnf.vars, rw_lit);
occs = LIT_NEG (rw_lit) ?
&rw_var->watched_neg_occ_clauses : &rw_var->watched_pos_occ_clauses;
remove_clause_from_occs (occs, c);
c->rw_index = WATCHED_LIT_INVALID_INDEX;
}
/* Remove clause 'c' from occs of variable of 'red_lit'. */
ClausePtrStack *occs = LIT_NEG (red_lit) ?
&red_var->neg_occ_clauses : &red_var->pos_occ_clauses;
remove_clause_from_occs (occs, c);
assert (count_qtype_literals (qr, c, QTYPE_FORALL) +
count_qtype_literals (qr, c, QTYPE_EXISTS) == c->num_lits);
/* Remove 'red_lit' from 'c' while keeping ordering of literals. */
LitID *p, *e;
for (p = c->lits, e = p + c->num_lits; p < e; p++)
{
LitID lit = *p;
if (lit == red_lit)
{
/* Must update index of watched literals. */
if (!update_watcher)
{
unsigned int red_lit_index = p - c->lits;
assert (c->rw_index != red_lit_index);
assert (c->lw_index != red_lit_index);
if (c->lw_index > red_lit_index)
c->lw_index--;
if (c->rw_index > red_lit_index)
c->rw_index--;
}
LitID *to, *from;
for (to = p, from = p + 1; from < e; to++, from++)
*to = *from;
break;
}
}
c->num_lits--;
if (c->num_lits == 1)
PUSH_STACK (qr->mm, qr->unit_input_clauses, c);
else if (update_watcher)
{
/* NOTE / TODO: this code is repeated in 'retract_re_init_lit_watchers' in
file 'qbcp.c'. */
/* Set right watched literal. */
c->rw_index = c->num_lits - 1;
LitID lit = c->lits[c->rw_index];
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
assert (var->assignment == ASSIGNMENT_UNDEF);
assert (var->qblock->type == QTYPE_EXISTS);
/* Add 'c' to watched occurrences. */
if (LIT_NEG (lit))
PUSH_STACK (qr->mm, var->watched_neg_occ_clauses, c);
else
PUSH_STACK (qr->mm, var->watched_pos_occ_clauses, c);
/* Set left watched literal. */
c->lw_index = c->rw_index - 1;
lit = c->lits[c->lw_index];
var = LIT2VARPTR (qr->pcnf.vars, lit);
assert (var->assignment == ASSIGNMENT_UNDEF);
/* Add 'c' to watched occurrences. */
if (LIT_NEG (lit))
PUSH_STACK (qr->mm, var->watched_neg_occ_clauses, c);
else
PUSH_STACK (qr->mm, var->watched_pos_occ_clauses, c);
}
}
/* Return nonzero iff clause 'c' contains universal literals which have QRAT. */
static int
has_clause_qrat_literals (QRATPrePlus * qr, Clause * c)
{
assert (!c->redundant);
int result = 0;
LitID *p, *e;
for (p = c->lits, e = p + c->num_lits; p < e; p++)
{
LitID lit = *p;
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
/* Check if universal literal 'lit' has QRAT. */
if (var->qblock->type == QTYPE_FORALL)
{
if (has_qrat_on_literal (qr, c, lit))
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " ==> universal literal %d has QRAT in clause ", lit);
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
cleanup_redundant_universal_literal (qr, c, lit);
result = 1;
/* Must update pointers since removed redundant 'lit' from 'c'. */
p--;
e--;
}
}
}
return result;
}
/* Return nonzero iff clause 'c' contains blocked universal literals. */
static int
has_clause_blocked_literals (QRATPrePlus * qr, Clause * c)
{
int result = 0;
assert (!c->redundant);
LitID *p, *e;
for (p = c->lits, e = p + c->num_lits; p < e; p++)
{
LitID lit = *p;
Var *var = LIT2VARPTR (qr->pcnf.vars, lit);
/* Check if universal literal 'lit' is blocked. */
if (var->qblock->type == QTYPE_FORALL)
{
if (is_literal_blocked (qr, c, lit))
{
if (qr->options.verbosity >= 2)
{
fprintf (stderr, " ==> universal literal %d is blocked in clause ", lit);
print_lits (qr, stderr, c->lits, c->num_lits, 1);
}
cleanup_redundant_universal_literal (qr, c, lit);
result = 1;
/* Must update pointers since removed redundant 'lit' from 'c'. */
p--;
e--;
}
}
}
return result;
}
/* Returns nonzero iff redundant literals were found. */
static int
find_and_delete_redundant_literals_aux (QRATPrePlus * qr,
ClausePtrStack *to_be_checked,
ClausePtrStack *rescheduled,
const QRATPlusCheckMode mode)
{
int result = 0;
assert (!qr->options.no_ble || !qr->options.no_qratu);
assert (mode == QRATPLUS_CHECK_MODE_QBCE || mode == QRATPLUS_CHECK_MODE_QRAT);
assert (mode != QRATPLUS_CHECK_MODE_QBCE || !qr->options.no_ble);
assert (mode != QRATPLUS_CHECK_MODE_QRAT || !qr->options.no_qratu);
assert (EMPTY_STACK (qr->witness_clauses));
int exceeded = 0;
if ((exceeded = exceeded_soft_time_limit (qr)))
fprintf (stderr, "Exceeded soft time limit of %u sec\n", qr->soft_time_limit);
#ifndef NDEBUG
{
/* Clauses to be tested are expected to be sorted by ID. */
Clause **cp, **ce, **cpprev;
for (cp = cpprev = rescheduled->start, ce = rescheduled->top; cp < ce; cp++)
{
Clause *c = *cp;
Clause *cprev = *cpprev;
assert (cprev->id <= c->id);
cpprev = cp;
}
}
#endif
unsigned int cur_redundant_literals = 0;
int changed = 1;
while (!exceeded && changed)
{
/* Statistics. */
qr->cnt_redundant_literals += cur_redundant_literals;
qr->cnt_qratu_iterations++;
/* Set up new iteration: swap 'rescheduled' and 'to_be_checked', reset. */
changed = 0;
Clause **cp, **ce;
for (cp = rescheduled->start, ce = rescheduled->top; cp < ce; cp++)
{
Clause *c = *cp;
/* NOTE: we may encounter clauses 'c' with 'c->redundant' true. This
may happen if we first reschedule a non-redundant clause 'c' which
in the same iteration is found redundant later. */
assert (c->rescheduled);
c->rescheduled = 0;
}
ClausePtrStack *stack_tmp = to_be_checked;
to_be_checked = rescheduled;
rescheduled = stack_tmp;
RESET_STACK (*rescheduled);
if (qr->options.verbosity >= 1)
{
fprintf (stderr, "\n======\n%s iteration %d: %d new redundant literals in previous iteration %d\n",
mode == QRATPLUS_CHECK_MODE_QBCE ? "BLE" : "QRATU",
qr->cnt_qratu_iterations, cur_redundant_literals, qr->cnt_qratu_iterations - 1);
fprintf (stderr, "Clauses to be checked (worst case): %u ( %f %% of original CNF)\n======\n",
(unsigned int) COUNT_STACK (*to_be_checked), 100 * (COUNT_STACK (*to_be_checked) / (float) qr->actual_num_clauses));
}
cur_redundant_literals = 0;
/* Either randomly permute or sort clauses to be tested by ID. Sorting
makes sure that clauses are always tested in the same ordering, while
permuting allows to analyze impact of orderings and non-confluence QRAT.
Note that without sorting we may get different orderings due to
the way we reschedule clauses. */
if (mode != QRATPLUS_CHECK_MODE_QBCE && qr->options.permute)
permute_clauses_to_be_checked (qr, to_be_checked);
else
qsort (to_be_checked->start, COUNT_STACK (*to_be_checked),
sizeof (Clause *), compare_clauses_by_id);
for (cp = to_be_checked->start, ce = to_be_checked->top;
!exceeded && cp < ce; cp++)
{
Clause *c = *cp;
assert (!qr->options.ignore_outermost_vars ||
!clause_has_outermost_qblock_literal (qr, c));
/* NOTE: we may encounter clauses 'c' with 'c->redundant' true
because such clauses may appear on 'rescheduled' (see comment above)
and we just swap the sets at the beginning of each iteration. */
if (!c->redundant)
{
if (qr->options.verbosity >= 2)
{