-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathpb_solver.cpp
More file actions
3800 lines (3409 loc) · 126 KB
/
Copy pathpb_solver.cpp
File metadata and controls
3800 lines (3409 loc) · 126 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
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
/*++
Copyright (c) 2017 Microsoft Corporation
Module Name:
pb_solver.cpp
Abstract:
Extension for cardinality reasoning.
Author:
Nikolaj Bjorner (nbjorner) 2017-01-30
--*/
#include <cmath>
#include <numeric>
#include "sat/sat_types.h"
#include "sat/smt/pb_solver.h"
#include "sat/smt/euf_solver.h"
#include "sat/sat_simplifier_params.hpp"
#include "sat/sat_scc.h"
namespace pb {
static unsigned _bad_id = 11111111; // 2759; //
#define BADLOG(_cmd_) if (p.id() == _bad_id) { _cmd_; }
// -----------------------
// constraint
void solver::set_conflict(constraint& c, literal lit) {
m_stats.m_num_conflicts++;
TRACE(pb, display(tout, c, true); );
if (!validate_conflict(c)) {
IF_VERBOSE(0, display(verbose_stream(), c, true));
UNREACHABLE();
}
SASSERT(validate_conflict(c));
SASSERT(value(lit) == l_false);
set_conflict(sat::justification::mk_ext_justification(s().scope_lvl(), c.cindex()), ~lit);
SASSERT(inconsistent());
}
void solver::assign(constraint& c, literal lit) {
if (inconsistent()) return;
switch (value(lit)) {
case l_true:
break;
case l_false:
set_conflict(c, lit);
break;
default:
m_stats.m_num_propagations++;
m_num_propagations_since_pop++;
//TRACE(pb, tout << "#prop: " << m_stats.m_num_propagations << " - " << c.lit() << " => " << lit << "\n";);
SASSERT(validate_unit_propagation(c, lit));
assign(lit, sat::justification::mk_ext_justification(s().scope_lvl(), c.cindex()));
break;
}
}
// -------------------
// pb_base
void solver::simplify(constraint& p) {
SASSERT(s().at_base_lvl());
if (p.lit() != sat::null_literal && value(p.lit()) == l_false) {
TRACE(pb, tout << "pb: flip sign " << p << "\n";);
IF_VERBOSE(2, verbose_stream() << "sign is flipped " << p << "\n";);
return;
}
bool nullify = p.lit() != sat::null_literal && value(p.lit()) == l_true;
if (nullify) {
IF_VERBOSE(100, display(verbose_stream() << "nullify tracking literal\n", p, true););
SASSERT(lvl(p.lit()) == 0);
p.nullify_tracking_literal(*this);
init_watch(p);
}
SASSERT(p.lit() == sat::null_literal || value(p.lit()) != l_false);
unsigned true_val = 0, slack = 0, num_false = 0;
for (unsigned i = 0; i < p.size(); ++i) {
literal l = p.get_lit(i);
bool_var v = l.var();
if (s().was_eliminated(v)) {
VERIFY(p.learned());
remove_constraint(p, "contains eliminated");
return;
}
switch (value(l)) {
case l_true: true_val += p.get_coeff(i); break;
case l_false: ++num_false; break;
default: slack += p.get_coeff(i); break;
}
}
if (p.k() == 1 && p.lit() == sat::null_literal) {
literal_vector lits(p.literals());
s().mk_clause(lits.size(), lits.data(), sat::status::th(p.learned(), get_id()));
IF_VERBOSE(100, display(verbose_stream() << "add clause: " << lits << "\n", p, true););
remove_constraint(p, "implies clause");
}
else if (true_val == 0 && num_false == 0) {
if (p.lit() == sat::null_literal || value(p.lit()) == l_true) {
init_watch(p);
}
}
else if (true_val >= p.k()) {
IF_VERBOSE(100, display(verbose_stream() << "assign true literal ", p, true););
if (p.lit() != sat::null_literal)
s().assign_scoped(p.lit());
else
remove_constraint(p, "is true");
}
else if (slack + true_val < p.k()) {
if (p.lit() != sat::null_literal) {
IF_VERBOSE(3, display(verbose_stream() << "assign false literal ", p, true););
s().assign_scoped(~p.lit());
}
else {
IF_VERBOSE(1, verbose_stream() << "unsat during simplification\n");
s().set_conflict(sat::justification(0));
}
}
else if (slack + true_val == p.k()) {
literal_vector lits(p.literals());
assert_unconstrained(p.lit(), lits);
remove_constraint(p, "is tight");
}
else {
unsigned sz = p.size();
clear_watch(p);
unsigned j = 0;
for (unsigned i = 0; i < sz; ++i) {
literal l = p.get_lit(i);
if (value(l) == l_undef) {
if (i != j)
p.swap(i, j);
++j;
}
}
sz = j;
// _bad_id = p.id();
BADLOG(display(verbose_stream() << "simplify ", p, true));
unsigned k = p.k() - true_val;
if (k == 1 && p.lit() == sat::null_literal) {
literal_vector lits(sz, p.literals().data());
s().mk_clause(sz, lits.data(), sat::status::th(p.learned(), get_id()));
remove_constraint(p, "is clause");
return;
}
p.set_size(sz);
p.set_k(k);
if (p.lit() == sat::null_literal || value(p.lit()) == l_true) {
init_watch(p);
}
else {
SASSERT(value(p.lit()) == l_undef);
}
BADLOG(display(verbose_stream() << "simplified ", p, true); verbose_stream() << "\n");
// display(verbose_stream(), c, true);
_bad_id = 11111111;
SASSERT(p.well_formed());
m_simplify_change = true;
}
}
/*
\brief split PB constraint into two because root is reused in arguments.
x <=> a*x + B*y >= k
x => a*x + By >= k
~x => a*x + By < k
k*~x + a*x + By >= k
(B+a-k + 1)*x + a*~x + B*~y >= B + a - k + 1
(k - a) * ~x + By >= k - a
k' * x + B'y >= k'
*/
void solver::split_root(constraint& p) {
SASSERT(p.lit() != sat::null_literal);
SASSERT(!p.learned());
m_weights.resize(2*s().num_vars(), 0);
unsigned k = p.k();
unsigned w, w1, w2;
literal root = p.lit();
m_weights[(~root).index()] = k;
for (unsigned i = 0; i < p.size(); ++i) {
m_weights[p.get_lit(i).index()] += p.get_coeff(i);
}
literal_vector lits(p.literals());
lits.push_back(~root);
for (literal l : lits) {
w1 = m_weights[l.index()];
w2 = m_weights[(~l).index()];
if (w1 >= w2) {
if (w2 >= k) {
for (literal l2 : lits) m_weights[l2.index()] = 0;
// constraint is true
return;
}
k -= w2;
m_weights[(~l).index()] = 0;
m_weights[l.index()] = w1 - w2;
}
}
SASSERT(k > 0);
// ~root * (k - a) + p >= k - a
m_wlits.reset();
for (literal l : lits) {
w = m_weights[l.index()];
if (w != 0) {
m_wlits.push_back(wliteral(w, l));
}
m_weights[l.index()] = 0;
}
add_pb_ge(sat::null_literal, m_wlits, k, false);
}
// -------------------
// pb
/*
Chai Kuhlmann:
Lw - set of watched literals
Lu - set of unwatched literals that are not false
Lw = Lw \ { alit }
Sw -= value
a_max = max { a | l in Lw u Lu, l = undef }
while (Sw < k + a_max & Lu != 0) {
a_s = max { a | l in Lu }
Sw += a_s
Lw = Lw u {l_s}
Lu = Lu \ {l_s}
}
if (Sw < k) return conflict
for (li in Lw | Sw < k + ai)
assign li
return no-conflict
a_max index: index of non-false literal with maximal weight.
*/
void solver::add_index(pbc& p, unsigned index, literal lit) {
if (value(lit) == l_undef) {
m_pb_undef.push_back(index);
auto [w, l] = p[index];
if (w > m_a_max) {
m_a_max = w;
}
}
}
/*
\brief propagate assignment to alit in constraint p.
TBD:
- consider reordering literals in watch list so that the search for watched literal takes average shorter time.
- combine with caching literals that are assigned to 'true' to a cold store where they are not being revisited.
Since 'true' literals may be unassigned (unless they are assigned at level 0) the cache has to be backtrack
friendly (and the overhead of backtracking has to be taken into account).
*/
lbool solver::add_assign(pbc& p, literal alit) {
BADLOG(display(verbose_stream() << "assign: " << alit << " watch: " << p.num_watch() << " size: " << p.size(), p, true));
TRACE(pb, display(tout << "assign: " << alit << "\n", p, true););
SASSERT(!inconsistent());
unsigned sz = p.size();
unsigned bound = p.k();
unsigned num_watch = p.num_watch();
unsigned slack = p.slack();
SASSERT(value(alit) == l_false);
SASSERT(p.lit() == sat::null_literal || value(p.lit()) == l_true);
SASSERT(num_watch <= sz);
SASSERT(num_watch > 0);
SASSERT(validate_watch(p, sat::null_literal));
unsigned index = 0;
m_a_max = 0;
m_pb_undef.reset();
for (; index < num_watch; ++index) {
auto [w, lit] = p[index];
if (lit == alit) {
break;
}
add_index(p, index, lit);
}
if (index == num_watch || num_watch == 0) {
_bad_id = p.id();
BADLOG(
verbose_stream() << "BAD: " << p.id() << "\n";
display(verbose_stream(), p, true);
verbose_stream() << "alit: " << alit << "\n";
verbose_stream() << "num watch " << num_watch << "\n");
UNREACHABLE();
return l_undef;
}
SASSERT(index < num_watch);
unsigned index1 = index + 1;
for (; m_a_max == 0 && index1 < num_watch; ++index1) {
auto [w, lit] = p[index1];
add_index(p, index1, lit);
}
auto [val, alit_lit] = p[index];
SASSERT(value(alit_lit) == l_false);
SASSERT(val <= slack);
slack -= val;
// find literals to swap with:
for (unsigned j = num_watch; j < sz && slack < bound + m_a_max; ++j) {
auto [w, lit] = p[j];
if (value(lit) != l_false) {
slack += w;
SASSERT(!p.is_watched(*this, lit));
p.watch_literal(*this, lit);
p.swap(num_watch, j);
add_index(p, num_watch, lit);
++num_watch;
}
}
SASSERT(!inconsistent());
DEBUG_CODE(for (auto idx : m_pb_undef) { SASSERT(value(p[idx].second) == l_undef); });
if (slack < bound) {
// maintain watching the literal
slack += val;
p.set_slack(slack);
p.set_num_watch(num_watch);
SASSERT(validate_watch(p, sat::null_literal));
BADLOG(display(verbose_stream() << "conflict: " << alit << " watch: " << p.num_watch() << " size: " << p.size(), p, true));
SASSERT(bound <= slack);
TRACE(pb, tout << "conflict " << alit << "\n";);
set_conflict(p, alit);
return l_false;
}
if (num_watch == 1) { _bad_id = p.id(); }
BADLOG(verbose_stream() << "size: " << p.size() << " index: " << index << " num watch: " << num_watch << "\n");
// swap out the watched literal.
--num_watch;
SASSERT(num_watch > 0);
p.set_slack(slack);
p.set_num_watch(num_watch);
p.swap(num_watch, index);
//
// slack >= bound, but slack - w(l) < bound
// l must be true.
//
if (slack < bound + m_a_max) {
BADLOG(verbose_stream() << "slack " << slack << " " << bound << " " << m_a_max << "\n";);
TRACE(pb, tout << p << "\n"; for(auto j : m_pb_undef) tout << j << " "; tout << "\n";);
for (unsigned index1 : m_pb_undef) {
if (index1 == num_watch) {
index1 = index;
}
auto [w, lit] = p[index1];
SASSERT(value(lit) == l_undef);
if (slack < bound + w) {
BADLOG(verbose_stream() << "Assign " << lit << " " << w << "\n");
assign(p, lit);
}
}
}
SASSERT(validate_watch(p, alit)); // except that alit is still watched.
TRACE(pb, display(tout << "assign: " << alit << "\n", p, true););
BADLOG(verbose_stream() << "unwatch " << alit << " watch: " << p.num_watch() << " size: " << p.size() << " slack: " << p.slack() << " " << inconsistent() << "\n");
return l_undef;
}
std::pair<unsigned, unsigned> solver::normalize(wliteral* begin, wliteral* end, unsigned k) {
m_weights.resize(2 * s().num_vars(), 0);
for (auto it = begin; it != end; ++it) {
auto [w, lit] = *it;
m_weights[lit.index()] += w;
}
auto j = begin;
unsigned sz = 0;
for (auto it = begin; it != end; ++it) {
auto [w, l] = *it;
unsigned w1 = m_weights[l.index()];
unsigned w2 = m_weights[(~l).index()];
if (w1 == 0 || w1 < w2) {
continue;
}
else if (k <= w2) {
k = 0;
break;
}
else {
SASSERT(w2 <= w1 && w2 < k);
k -= w2;
w1 -= w2;
m_weights[l.index()] = 0;
m_weights[(~l).index()] = 0;
if (w1 == 0) {
continue;
}
else {
*j = wliteral(w1, l);
++j;
++sz;
}
}
}
// clear weights
while (begin != end) {
auto [w, l] = *begin;
m_weights[l.index()] = 0;
m_weights[(~l).index()] = 0;
++begin;
}
return {sz, k};
}
void solver::recompile(pbc& p) {
// IF_VERBOSE(2, verbose_stream() << "re: " << p << "\n";);
auto [sz, k] = normalize(p.data(), p.data() + p.size(), p.k());
p.set_size(sz);
auto all_units = all_of(p, [](wliteral const& wl) { return wl.first == 1; });
if (k == 0) {
if (p.lit() != sat::null_literal) {
s().assign_scoped(p.lit());
}
remove_constraint(p, "recompiled to true");
return;
}
else if (k == 1 && p.lit() == sat::null_literal) {
literal_vector lits(sz, p.literals().data());
s().mk_clause(sz, lits.data(), sat::status::th(p.learned(), get_id()));
remove_constraint(p, "recompiled to clause");
return;
}
else if (all_units) {
literal_vector lits(sz, p.literals().data());
add_at_least(p.lit(), lits, k, p.learned());
remove_constraint(p, "recompiled to cardinality");
return;
}
else {
p.update_max_sum();
if (p.max_sum() < k) {
if (p.lit() == sat::null_literal) {
s().set_conflict(sat::justification(0));
}
else {
s().assign_scoped(~p.lit());
}
remove_constraint(p, "recompiled to false");
return;
}
p.set_k(k);
SASSERT(p.well_formed());
if (clausify(p)) {
return;
}
if (p.lit() == sat::null_literal || value(p.lit()) == l_true) {
init_watch(p);
}
}
}
// ---------------------------
// conflict resolution
void solver::inc_coeff(literal l, unsigned offset) {
SASSERT(offset > 0);
bool_var v = l.var();
SASSERT(v != sat::null_bool_var);
m_coeffs.reserve(v + 1, 0);
TRACE(ba_verbose, tout << l << " " << offset << "\n";);
int64_t coeff0 = m_coeffs[v];
if (coeff0 == 0) {
m_active_vars.push_back(v);
}
int64_t loffset = static_cast<int64_t>(offset);
int64_t inc = l.sign() ? -loffset : loffset;
int64_t coeff1 = inc + coeff0;
m_coeffs[v] = coeff1;
if (coeff1 > INT_MAX || coeff1 < INT_MIN) {
m_overflow = true;
return;
}
if (coeff0 > 0 && inc < 0) {
inc_bound(std::max((int64_t)0, coeff1) - coeff0);
}
else if (coeff0 < 0 && inc > 0) {
inc_bound(coeff0 - std::min((int64_t)0, coeff1));
}
int64_t lbound = static_cast<int64_t>(m_bound);
// reduce coefficient to be no larger than bound.
if (coeff1 > lbound) {
m_coeffs[v] = lbound;
}
else if (coeff1 < 0 && -coeff1 > lbound) {
m_coeffs[v] = -lbound;
}
}
int64_t solver::get_coeff(bool_var v) const {
return m_coeffs.get(v, 0);
}
uint64_t solver::get_coeff(literal lit) const {
int64_t c1 = get_coeff(lit.var());
SASSERT((c1 < 0) == lit.sign());
int64_t c = std::abs(c1);
m_overflow |= (c != c1);
return static_cast<uint64_t>(c);
}
wliteral solver::get_wliteral(bool_var v) {
int64_t c1 = get_coeff(v);
literal l = literal(v, c1 < 0);
c1 = std::abs(c1);
unsigned c = static_cast<unsigned>(c1);
// TRACE(pb, tout << l << " " << c << "\n";);
m_overflow |= c != c1;
return wliteral(c, l);
}
unsigned solver::get_abs_coeff(bool_var v) const {
int64_t c1 = std::abs(get_coeff(v));
unsigned c = static_cast<unsigned>(c1);
m_overflow |= c != c1;
return c;
}
int solver::get_int_coeff(bool_var v) const {
int64_t c1 = m_coeffs.get(v, 0);
int c = static_cast<int>(c1);
m_overflow |= c != c1;
return c;
}
void solver::inc_bound(int64_t i) {
int64_t new_bound = m_bound;
new_bound += i;
unsigned nb = static_cast<unsigned>(new_bound);
m_overflow |= new_bound < 0 || nb != new_bound;
m_bound = nb;
}
void solver::reset_coeffs() {
for (unsigned i = m_active_vars.size(); i-- > 0; ) {
m_coeffs[m_active_vars[i]] = 0;
}
m_active_vars.reset();
}
static bool _debug_conflict = false;
static literal _debug_consequent = sat::null_literal;
static unsigned_vector _debug_var2position;
// #define DEBUG_CODE(_x_) _x_
void solver::bail_resolve_conflict(unsigned idx) {
literal_vector const& lits = s().m_trail;
while (m_num_marks > 0) {
bool_var v = lits[idx].var();
if (s().is_marked(v)) {
s().reset_mark(v);
--m_num_marks;
}
if (idx == 0 && !_debug_conflict && m_num_marks > 0) {
_debug_conflict = true;
_debug_var2position.reserve(s().num_vars());
for (unsigned i = 0; i < lits.size(); ++i) {
_debug_var2position[lits[i].var()] = i;
}
IF_VERBOSE(0, verbose_stream() << "num marks: " << m_num_marks << "\n");
IF_VERBOSE(0,
active2pb(m_A);
uint64_t c = 0;
for (auto [w, l] : m_A.m_wlits) c += w;
verbose_stream() << "sum of coefficients: " << c << "\n";
display(verbose_stream(), m_A, true);
verbose_stream() << "conflicting literal: " << s().m_not_l << "\n";);
for (literal l : lits) {
if (s().is_marked(l.var())) {
IF_VERBOSE(0, verbose_stream() << "missing mark: " << l << "\n";);
s().reset_mark(l.var());
}
}
m_num_marks = 0;
resolve_conflict();
exit(0);
}
--idx;
}
}
lbool solver::resolve_conflict() {
if (0 == m_num_propagations_since_pop)
return l_undef;
if (s().m_config.m_pb_resolve == sat::PB_ROUNDING)
return resolve_conflict_rs();
m_overflow = false;
reset_coeffs();
m_num_marks = 0;
m_bound = 0;
literal consequent = s().m_not_l;
sat::justification js = s().m_conflict;
TRACE(pb, tout << consequent << " " << js << "\n";);
bool unique_max;
m_conflict_lvl = s().get_max_lvl(consequent, js, unique_max);
if (m_conflict_lvl == 0) {
return l_undef;
}
if (consequent != sat::null_literal) {
consequent.neg();
process_antecedent(consequent, 1);
}
literal_vector const& lits = s().m_trail;
unsigned idx = lits.size() - 1;
unsigned offset = 1;
DEBUG_CODE(active2pb(m_A););
do {
if (m_overflow || offset > (1 << 12)) {
IF_VERBOSE(20, verbose_stream() << "offset: " << offset << "\n";
DEBUG_CODE(active2pb(m_A); display(verbose_stream(), m_A);););
goto bail_out;
}
if (offset == 0) {
goto process_next_resolvent;
}
DEBUG_CODE(TRACE(sat_verbose, display(tout, m_A);););
TRACE(pb, tout << "process consequent: " << consequent << " : "; s().display_justification(tout, js) << "\n";);
SASSERT(offset > 0);
if (_debug_conflict) {
IF_VERBOSE(0,
verbose_stream() << consequent << "\n";
s().display_justification(verbose_stream(), js);
verbose_stream() << "\n";);
_debug_consequent = consequent;
}
switch(js.get_kind()) {
case sat::justification::NONE:
SASSERT (consequent != sat::null_literal);
inc_bound(offset);
break;
case sat::justification::BINARY:
inc_bound(offset);
SASSERT (consequent != sat::null_literal);
inc_coeff(consequent, offset);
process_antecedent(js.get_literal(), offset);
break;
case sat::justification::CLAUSE: {
inc_bound(offset);
sat::clause & c = s().get_clause(js);
unsigned i = 0;
if (consequent != sat::null_literal) {
inc_coeff(consequent, offset);
if (c[0] == consequent) {
i = 1;
}
else {
SASSERT(c[1] == consequent);
process_antecedent(c[0], offset);
i = 2;
}
}
unsigned sz = c.size();
for (; i < sz; ++i)
process_antecedent(c[i], offset);
break;
}
case sat::justification::EXT_JUSTIFICATION: {
auto cindex = js.get_ext_justification_idx();
auto* ext = sat::constraint_base::to_extension(cindex);
if (ext != this)
goto bail_out;
constraint& cnstr = index2constraint(cindex);
++m_stats.m_num_resolves;
switch (cnstr.tag()) {
case pb::tag_t::card_t: {
card& c = cnstr.to_card();
inc_bound(static_cast<int64_t>(offset) * c.k());
process_card(c, offset);
break;
}
case pb::tag_t::pb_t: {
pbc& p = cnstr.to_pb();
m_lemma.reset();
inc_bound(offset);
inc_coeff(consequent, offset);
get_antecedents(consequent, p, m_lemma);
TRACE(pb, display(tout, p, true); tout << m_lemma << "\n";);
if (_debug_conflict) {
verbose_stream() << consequent << " ";
verbose_stream() << "antecedents: " << m_lemma << "\n";
}
for (literal l : m_lemma) process_antecedent(~l, offset);
break;
}
default:
UNREACHABLE();
break;
}
break;
}
default:
UNREACHABLE();
break;
}
SASSERT(validate_lemma());
DEBUG_CODE(
active2pb(m_C);
VERIFY(validate_resolvent());
m_A = m_C;
TRACE(pb, display(tout << "conflict: ", m_A);););
cut();
process_next_resolvent:
// find the next marked variable in the assignment stack
bool_var v;
while (true) {
consequent = lits[idx];
v = consequent.var();
if (s().is_marked(v)) {
if (s().lvl(v) == m_conflict_lvl) {
break;
}
}
if (idx == 0) {
IF_VERBOSE(2, verbose_stream() << "did not find marked literal\n";);
goto bail_out;
}
SASSERT(idx > 0);
--idx;
}
SASSERT(lvl(v) == m_conflict_lvl);
s().reset_mark(v);
--idx;
TRACE(sat_verbose, tout << "Unmark: v" << v << "\n";);
--m_num_marks;
js = s().m_justification[v];
offset = get_abs_coeff(v);
if (offset > m_bound) {
int64_t bound64 = static_cast<int64_t>(m_bound);
m_coeffs[v] = (get_coeff(v) < 0) ? -bound64 : bound64;
offset = m_bound;
DEBUG_CODE(active2pb(m_A););
}
SASSERT(value(consequent) == l_true);
}
while (m_num_marks > 0);
DEBUG_CODE(for (bool_var i = 0; i < static_cast<bool_var>(s().num_vars()); ++i) SASSERT(!s().is_marked(i)););
SASSERT(validate_lemma());
if (!create_asserting_lemma()) {
goto bail_out;
}
active2lemma();
DEBUG_CODE(VERIFY(validate_conflict(m_lemma, m_A)););
return l_true;
bail_out:
if (m_overflow) {
++m_stats.m_num_overflow;
m_overflow = false;
}
bail_resolve_conflict(idx);
return l_undef;
}
unsigned solver::ineq::bv_coeff(bool_var v) const {
for (unsigned i = size(); i-- > 0; ) {
if (lit(i).var() == v) return coeff(i);
}
UNREACHABLE();
return 0;
}
void solver::ineq::divide(unsigned c) {
if (c <= 1) return;
for (unsigned i = size(); i-- > 0; ) {
m_wlits[i].first = (coeff(i) + c - 1) / c;
}
m_k = (m_k + c - 1) / c;
}
/**
* Remove literal at position i, subtract coefficient from bound.
*/
void solver::ineq::weaken(unsigned i) {
unsigned ci = coeff(i);
SASSERT(m_k >= ci);
m_k -= ci;
m_wlits[i] = m_wlits.back();
m_wlits.pop_back();
}
/**
* Round coefficient of inequality to 1.
*/
void solver::round_to_one(ineq& ineq, bool_var v) {
unsigned c = ineq.bv_coeff(v);
if (c <= 1) return;
unsigned sz = ineq.size();
for (unsigned i = 0; i < sz; ++i) {
unsigned ci = ineq.coeff(i);
unsigned q = ci % c;
if (q != 0 && !is_false(ineq.lit(i))) {
#if 1
// code review by Elffers:
ineq.weaken(i);
--i;
--sz;
#else
if (q == ci) {
ineq.weaken(i);
--i;
--sz;
}
else {
ineq.m_wlits[i].first -= q;
ineq.m_k -= q;
}
#endif
}
}
ineq.divide(c);
TRACE(pb, display(tout << "var: " << v << " " << c << ": ", ineq, true););
}
void solver::round_to_one(bool_var w) {
unsigned c = get_abs_coeff(w);
if (c == 1 || c == 0) return;
for (bool_var v : m_active_vars) {
auto [coeff, l] = get_wliteral(v);
unsigned q = coeff % c;
if (q != 0 && !is_false(l)) {
m_coeffs[v] = coeff - q;
m_bound -= q;
SASSERT(m_bound > 0);
}
}
SASSERT(validate_lemma());
divide(c);
SASSERT(validate_lemma());
TRACE(pb, active2pb(m_B); display(tout, m_B, true););
}
void solver::divide(unsigned c) {
SASSERT(c != 0);
if (c == 1) return;
reset_active_var_set();
unsigned j = 0, sz = m_active_vars.size();
for (unsigned i = 0; i < sz; ++i) {
bool_var v = m_active_vars[i];
int ci = get_int_coeff(v);
if (!test_and_set_active(v) || ci == 0) continue;
if (ci > 0) {
m_coeffs[v] = (ci + c - 1) / c;
}
else {
m_coeffs[v] = -static_cast<int64_t>((-ci + c - 1) / c);
}
m_active_vars[j++] = v;
}
m_active_vars.shrink(j);
m_bound = static_cast<unsigned>((m_bound + c - 1) / c);
}
void solver::resolve_on(literal consequent) {
round_to_one(consequent.var());
m_coeffs[consequent.var()] = 0;
}
void solver::resolve_with(ineq const& ineq) {
TRACE(pb, display(tout, ineq, true););
inc_bound(ineq.m_k);
TRACE(pb, tout << "bound: " << m_bound << "\n";);
for (unsigned i = ineq.size(); i-- > 0; ) {
literal l = ineq.lit(i);
inc_coeff(l, static_cast<unsigned>(ineq.coeff(i)));
TRACE(pb, tout << "bound: " << m_bound << " lit: " << l << " coeff: " << ineq.coeff(i) << "\n";);
}
}
void solver::reset_marks(unsigned idx) {
while (m_num_marks > 0) {
SASSERT(idx > 0);
bool_var v = s().m_trail[idx].var();
if (s().is_marked(v)) {
s().reset_mark(v);
--m_num_marks;
}
--idx;
}
}
/**
* \brief mark variables that are on the assignment stack but
* below the current processing level.
*/
void solver::mark_variables(ineq const& ineq) {
for (auto [w, l] : ineq.m_wlits) {
if (!is_false(l)) continue;
bool_var v = l.var();
unsigned level = lvl(v);
if (!s().is_marked(v) && !is_visited(v) && level == m_conflict_lvl) {
s().mark(v);
++m_num_marks;
}
}
}
lbool solver::resolve_conflict_rs() {
if (0 == m_num_propagations_since_pop) {
return l_undef;
}
m_overflow = false;
reset_coeffs();
init_visited();
m_num_marks = 0;
m_bound = 0;
literal consequent = s().m_not_l;
sat::justification js = s().m_conflict;
bool unique_max;
m_conflict_lvl = s().get_max_lvl(consequent, js, unique_max);
if (m_conflict_lvl == 0) {
return l_undef;
}
if (consequent != sat::null_literal) {
consequent.neg();
process_antecedent(consequent, 1);
}
TRACE(pb, tout << consequent << " " << js << "\n";);
unsigned idx = s().m_trail.size() - 1;
do {
TRACE(pb, s().display_justification(tout << "process consequent: " << consequent << " : ", js) << "\n";
if (consequent != sat::null_literal) { active2pb(m_A); display(tout, m_A, true); }
);
switch (js.get_kind()) {