-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathdioph_eq.cpp
2838 lines (2562 loc) · 114 KB
/
dioph_eq.cpp
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
#include "math/lp/dioph_eq.h"
#include <algorithm>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include "math/lp/int_solver.h"
#include "math/lp/lar_solver.h"
#include "math/lp/lp_utils.h"
#include "math/lp/var_register.h"
/*
Following paper: "A Practical Approach to Satisfiability Modulo Linear
Integer Arithmetic" by Alberto Griggio([email protected]).
Data structures are:
-- term_o: inherits lar_term and differs from it by having a constant, while
lar_term is just a sum of monomials
-- lra: pointer to lar_solver.
-- lia: point to int_solver.
-- m_sum_of_fixed: it keeps the contribution of the fixed variables to the row
-- m_e_matrix: i-th row of this matrix keeps the term corresponding to i-ith entry
-- m_l_matrix: the m_l_matrix[i] produces m_e_matrix[i] by using the terms definitions of lar_solver
-- m_k2s: when the variable k is substituted in the row s of m_e_matrix, the pair (k,s) is added to m_k2s.
m_k2s is a one to one mapping.
-- m_fresh_k2xt_terms: when a fresh definitions is created for a variable k in row s then the triple
(k,xt,(t,s)) is added to m_fresh_k2xt_terms, where xt is the fresh variable, and t it the term defining the substitution: something like k - xt + 5z + 6y = 0.
The set of pairs (k, xt) is a one to one mapping
m_row2fresh_defs[i]: is the list of all xt that were defined for row m_e_matrix[i].
Invariant: Every xt in m_row2fresh[i] must have a corresponding entry in m_fresh_k2xt_terms
The mapping between the columns of lar_solver and m_e_matrix is controlled by m_var_register.
local_to_lar_solver(lar_solver_to_local(j)) == j. If local_to_lar_solver(j) == -1
then j is a fresh variable, that is such that got introduced when normalizing a term like 3x-6y + 5z +11 = 0,
when no variable has a coefficient +-1.
-- get_term_from_entry(unsigned i) returns a term corresponding i-th entry.
If t = get_term_from_entry(i) then we have equality t = 0. Initially
get_term_from_entry(i) is set to initt(j) = lra.get_term(j) - j, for some
column j,where all fixed variables are replaced by their values. To track the
explanations of equality t = 0 we initially set m_l_matrix[i] = {(1,j)}. The
explanation is obtained by replacing term of the m_l_matrix[i] = sum(aj*j) by the linear
combination sum (aj*initt(j)) and joining the explanations of all fixed
variables in the latter sum. entry_invariant(i) guarantees the validity of
the i-th entry.
*/
namespace lp {
template <typename T, typename K>
bool contains(const T& t, K j) {
return t.find(j) != t.end();
}
struct iv {
mpq m_coeff;
unsigned m_j;
lpvar var() const { return m_j; }
const mpq& coeff() const { return m_coeff; }
mpq& coeff() { return m_coeff; }
iv() {}
iv(const mpq& v, unsigned j) : m_coeff(v), m_j(j) {}
};
class bijection {
std::unordered_map<unsigned, unsigned> m_map;
std::unordered_map<unsigned, unsigned> m_rev_map;
auto map_begin() const { return m_map.begin(); }
auto map_end() const { return m_map.end(); }
public:
// Iterator helper
struct map_it {
const bijection& m_bij;
auto begin() const { return m_bij.map_begin(); }
auto end() const { return m_bij.map_end(); }
};
// Add a method to get iterators over the key->val map (complementing key_val)
map_it key_val_pairs() const { return {*this}; }
void add(unsigned a, unsigned b) {
SASSERT(!contains(m_map, a) && !contains(m_rev_map, b));
m_map[a] = b;
m_rev_map[b] = a;
}
unsigned get_key(unsigned b) const {
SASSERT(has_val(b));
return m_rev_map.find(b)->second;
}
unsigned size() const { return static_cast<unsigned>(m_map.size()); }
void erase_val(unsigned b) {
VERIFY(contains(m_rev_map, b) && contains(m_map, m_rev_map[b]));
auto it = m_rev_map.find(b);
if (it == m_rev_map.end()) return;
unsigned key = it->second;
m_rev_map.erase(it);
VERIFY(has_key(key));
m_map.erase(key);
}
bool has_val(unsigned b) const {
return m_rev_map.find(b) != m_rev_map.end();
}
bool has_key(unsigned a) const {
return m_map.find(a) != m_map.end();
}
void transpose_val(unsigned b0, unsigned b1) {
bool has_b0 = has_val(b0);
bool has_b1 = has_val(b1);
// Both b0 and b1 exist
if (has_b0 && has_b1) {
unsigned a0 = m_rev_map.at(b0);
unsigned a1 = m_rev_map.at(b1);
erase_val(b0);
erase_val(b1);
add(a1, b0);
add(a0, b1);
}
// Only b0 exists
else if (has_b0) {
unsigned a0 = m_rev_map.at(b0);
erase_val(b0);
add(a0, b1);
}
// Only b1 exists
else if (has_b1) {
unsigned a1 = m_rev_map.at(b1);
erase_val(b1);
add(a1, b0);
}
// Neither b0 nor b1 exists; do nothing
}
unsigned operator[](unsigned i) const {
auto it = m_map.find(i);
VERIFY(it != m_map.end());
return it->second;
}
};
template <typename T>
struct bij_map {
// We store T indexed by 'b' in an std::unordered_map, and use bijection to map from 'a' to 'b'.
bijection m_bij;
std::unordered_map<unsigned, T> m_data;
// Adds a->b in m_bij, associates val with b.
void add(unsigned a, unsigned b, const T& val) {
// You might have some method in bijection such as 'insert(a, b)'
// or possibly you'd do:
// m_bij.add_key_val(a, b);
// For example:
m_bij.add(a, b);
m_data[b] = val;
}
unsigned get_second_of_key(unsigned a) const {
return m_bij[a];
}
void erase_by_second_key(unsigned b) {
SASSERT(m_bij.has_val(b));
m_bij.erase_val(b);
auto it = m_data.find(b);
VERIFY(it != m_data.end());
m_data.erase(it);
}
bool has_key(unsigned j) const { return m_bij.has_key(j); }
bool has_second_key(unsigned j) const { return m_bij.has_val(j); }
// Get the data by 'a', look up b in m_bij, then read from m_data
const T& get_by_key(unsigned a) const {
unsigned b = m_bij[a]; // relies on operator[](unsigned) from bijection
return get_by_val(b);
}
// Get the data by 'b' directly
const T& get_by_val(unsigned b) const {
auto it = m_data.find(b);
VERIFY(it != m_data.end());
return it->second;
}
};
class dioph_eq::imp {
// This class represents a term with an added constant number c, in form sum
// {x_i*a_i} + c.
class term_o : public lar_term {
mpq m_c;
public:
term_o clone() const {
term_o ret;
for (const auto& p : *this) {
ret.add_monomial(p.coeff(), p.j());
}
ret.c() = c();
ret.set_j(j());
return ret;
}
term_o(const lar_term& t) : lar_term(t), m_c(0) {
SASSERT(m_c.is_zero());
}
const mpq& c() const {
return m_c;
}
mpq& c() {
return m_c;
}
term_o() : m_c(0) {}
friend term_o operator*(const mpq& k, const term_o& term) {
term_o r;
for (const auto& p : term)
r.add_monomial(p.coeff() * k, p.j());
r.c() = k * term.c();
return r;
}
friend term_o operator+(const term_o& a, const term_o& b) {
term_o r = a.clone();
for (const auto& p : b) {
r.add_monomial(p.coeff(), p.j());
}
r.c() += b.c();
return r;
}
friend term_o operator-(const term_o& a, const term_o& b) {
term_o r = a.clone();
for (const auto& p : b)
r.sub_monomial(p.coeff(), p.j());
r.c() -= b.c();
return r;
}
#if Z3DEBUG
friend bool operator==(const term_o& a, const term_o& b) {
term_o t = a.clone();
t += mpq(-1) * b;
return t.c() == mpq(0) && t.size() == 0;
}
friend bool operator!=(const term_o& a, const term_o& b) {
return ! (a == b);
}
#endif
term_o& operator+=(const term_o& t) {
for (const auto& p : t) {
add_monomial(p.coeff(), p.j());
}
m_c += t.c();
return *this;
}
};
std::ostream& print_S(std::ostream& out) {
out << "S:\n";
for (unsigned ei = 0 ; ei < m_e_matrix.row_count(); ei++) {
print_entry(ei, out, false, false, true);
}
return out;
}
std::ostream& print_bounds(std::ostream& out) {
out << "bounds:\n";
for (unsigned v = 0; v < lra.column_count(); ++v) {
lra.print_column_info(v, out);
}
return out;
}
std::ostream& print_lar_term_L(const lar_term& t, std::ostream& out) const {
return print_linear_combination_customized(
t.coeffs_as_vector(),
[](int j) -> std::string { return "x" + std::to_string(j); }, out);
}
// used for debug only
std::ostream& print_lar_term_L(const std_vector<iv>& t, std::ostream& out) const {
vector<std::pair<mpq, unsigned>> tp;
for (const auto& p : t) {
tp.push_back(std::make_pair(p.coeff(), p.var()));
}
return print_linear_combination_customized(
tp, [](int j) -> std::string { return "x" + std::to_string(j); }, out);
}
std::ostream& print_term_o(term_o const& term, std::ostream& out) const {
if (term.size() == 0 && term.c().is_zero()) {
out << "0";
return out;
}
bool first = true;
// Copy term to a std_vector and sort by p.j()
std_vector<std::pair<mpq, unsigned>> sorted_term;
sorted_term.reserve(term.size());
for (const auto& p : term) {
sorted_term.emplace_back(p.coeff(), p.j());
}
std::sort(
sorted_term.begin(), sorted_term.end(),
[](const auto& a, const auto& b) {
return a.second < b.second;
});
// Print the sorted term
for (auto& [val, j] : sorted_term) {
if (first) {
first = false;
}
else if (is_pos(val)) {
out << " + ";
}
else {
out << " - ";
val = -val;
}
if (val == -numeric_traits<mpq>::one())
out << " - ";
else if (val != numeric_traits<mpq>::one())
out << T_to_string(val);
out << "x";
out << j;
}
// Handle the constant term separately
if (!term.c().is_zero()) {
if (!first) {
if (term.c().is_pos())
out << " + ";
else
out << " - ";
}
out << abs(term.c());
}
return out;
}
// the maximal size of the term to try to tighten the bounds:
// if the size of the term is large than the chances are that the GCD of the coefficients is one
unsigned m_tighten_size_max = 10;
bool m_some_terms_are_ignored = false;
std_vector<mpq> m_sum_of_fixed;
// we have to use m_var_register because of the fresh variables: otherwise they clash with the existing lar_solver column indices
var_register m_var_register;
// the terms are stored in m_A and m_c
static_matrix<mpq, mpq> m_e_matrix; // the rows of the matrix are the terms,
static_matrix<mpq, mpq> m_l_matrix; // the rows of the matrix are the l_terms providing the certificate to the entries modulo the constant part: look an entry_invariant that assures that the each two rows are in sync.
int_solver& lia;
lar_solver& lra;
explanation m_infeas_explanation;
bool m_report_branch = false;
// set F
// iterate over all rows from 0 to m_e_matrix.row_count() - 1 and return those i such !m_k2s.has_val(i)
// set S - iterate over bijection m_k2s
mpq m_c; // the constant of the equation
struct term_with_index {
// The invariant is
// 1) m_index[m_data[k].var()] = k, for each 0 <= k < m_data.size(), and
// 2) m_index[j] = -1, or m_data[m_index[j]].var() = j, for every 0 <= j < m_index.size().
// For example m_data = [(coeff, 5), (coeff, 3)]
// then m_index = [-1,-1, -1, 1, -1, 0, -1, ....].
std_vector<iv> m_data;
std_vector<int> m_index;
// used for debug only
lar_term to_term() const {
lar_term r;
for (const auto& p : m_data) {
r.add_monomial(p.coeff(), p.var());
}
return r;
}
auto size() const { return m_data.size(); }
bool has(unsigned k) const {
return k < m_index.size() && m_index[k] >= 0;
}
const mpq& operator[](unsigned j) const {
SASSERT(j >= 0 && j < m_index.size());
SASSERT(m_index[j] >= 0 && m_index[j] < (int)m_data.size());
return m_data[m_index[j]].coeff();
}
void erase(unsigned k) {
if (k >= m_index.size() || m_index[k] == -1)
return;
unsigned idx = m_index[k];
// If not last element, move last element to idx position
if (idx != m_data.size() - 1) {
m_data[idx] = m_data.back();
m_index[m_data[idx].var()] = idx;
}
m_data.pop_back();
m_index[k] = -1;
SASSERT(invariant());
}
void add(const mpq& a, unsigned j) {
SASSERT(!a.is_zero());
// Expand m_index if needed
if (j >= m_index.size()) {
m_index.resize(j + 1, -1);
}
int idx = m_index[j];
if (idx == -1) {
// Insert a new monomial { a, j } into m_data
m_data.push_back({a, j});
m_index[j] = static_cast<int>(m_data.size() - 1);
}
else {
// Accumulate the coefficient
m_data[idx].coeff() += a;
// If the coefficient becomes zero, remove the entry
if (m_data[idx].coeff().is_zero()) {
int last = static_cast<int>(m_data.size() - 1);
// Swap with the last element for efficient removal
if (idx != last) {
auto tmp = m_data[last];
m_data[idx] = tmp;
m_index[tmp.var()] = idx;
}
m_data.pop_back();
m_index[j] = -1;
}
}
SASSERT(invariant());
}
bool invariant() const {
// 1. For each j in [0..m_index.size()), if m_index[j] = -1, ensure no m_data[k].var() == j
// otherwise verify m_data[m_index[j]].var() == j
for (unsigned j = 0; j < m_index.size(); j++) {
int idx = m_index[j];
if (idx == -1) {
// Check that j is not in m_data
for (unsigned k = 0; k < m_data.size(); ++k) {
if (m_data[k].var() == j) {
return false;
}
}
}
else {
// Check that var() in m_data[idx] matches j
if (idx < 0 || static_cast<unsigned>(idx) >= m_data.size()) {
return false;
}
if (m_data[idx].var() != j || m_data[idx].coeff().is_zero()) {
return false;
}
}
}
// 2. For each item in m_data, check that m_index[m_data[k].var()] == k
// and that the coeff() is non-zero
for (unsigned k = 0; k < m_data.size(); ++k) {
unsigned var = m_data[k].var();
if (var >= m_index.size()) {
return false;
}
if (m_index[var] != static_cast<int>(k)) {
return false;
}
if (m_data[k].coeff().is_zero()) {
return false;
}
}
return true;
}
void clear() {
for (const auto& p : m_data) {
m_index[p.var()] = -1;
}
m_data.clear();
SASSERT(invariant());
}
auto begin() const { return m_data.begin();}
auto end() const { return m_data.end(); }
};
// m_lspace is for operations on l_terms - m_e_matrix rows
term_with_index m_lspace;
// m_espace is for operations on m_e_matrix rows
term_with_index m_espace;
bijection m_k2s;
bij_map<std::pair<lar_term, unsigned>> m_fresh_k2xt_terms;
// m_row2fresh_defs[i] is the set of all fresh variables xt
// such that pairs (xt, m_fresh_k2xt_terms[xt]) is a fresh definition introduced for row i
// When row i is changed all entries depending on m_fresh_k2xt_terms[xt] should be recalculated,
// and the corresponding fresh definitions removed.
std::unordered_map<unsigned, std_vector<unsigned>> m_row2fresh_defs;
indexed_uint_set m_changed_rows;
// m_changed_columns are the columns that just became fixed, or those that just stopped being fixed.
// If such a column appears in an entry it has to be recalculated.
indexed_uint_set m_changed_columns;
indexed_uint_set m_changed_terms; // represented by term columns
indexed_uint_set m_terms_to_tighten; // represented by term columns
// m_column_to_terms[j] is the set of all k such lra.get_term(k) depends on j
std::unordered_map<unsigned, std::unordered_set<unsigned>> m_columns_to_terms;
unsigned m_normalize_conflict_index = UINT_MAX; // the row index of the conflict
mpq m_normalize_conflict_gcd; // the gcd of the coefficients the m_e_matrix[m_normalize_conflict_gcd].
void reset_conflict() { m_normalize_conflict_index = UINT_MAX; }
bool has_conflict_index() const { return m_normalize_conflict_index != UINT_MAX; }
void set_rewrite_conflict(unsigned idx, const mpq& gcd) {
SASSERT(idx != UINT_MAX);
m_normalize_conflict_index = idx;
m_normalize_conflict_gcd = gcd;
lra.stats().m_dio_rewrite_conflicts++;
}
unsigned m_max_of_branching_iterations = 0;
unsigned m_number_of_branching_calls;
struct branch {
unsigned m_j = UINT_MAX;
mpq m_rs;
// if m_left is true, then the branch is interpreted
// as x[j] <= m_rs
// otherwise x[j] >= m_rs
bool m_left;
bool m_fully_explored = false;
void flip() {
SASSERT(m_fully_explored == false);
m_left = !m_left;
m_fully_explored = true;
}
};
struct variable_branch_stats {
std::vector<unsigned> m_ii_after_left;
// g_right[i] - the rumber of int infeasible after taking the i-ith
// right branch
std::vector<unsigned> m_ii_after_right;
double score() const {
double avm_lefts =
m_ii_after_left.size()
? static_cast<double>(std::accumulate(
m_ii_after_left.begin(), m_ii_after_left.end(), 0)) /
m_ii_after_left.size()
: std::numeric_limits<double>::infinity();
double avm_rights = m_ii_after_right.size()
? static_cast<double>(std::accumulate(
m_ii_after_right.begin(),
m_ii_after_right.end(), 0)) /
m_ii_after_right.size()
: std::numeric_limits<double>::infinity();
return std::min(avm_lefts, avm_rights);
}
};
void undo_add_term_method(const lar_term* t) {
TRACE("d_undo", tout << "t:" << t << ", t->j():" << t->j() << std::endl;);
TRACE("dioph_eq", lra.print_term(*t, tout); tout << ", t->j() =" << t->j() << std::endl;);
if (!contains(m_active_terms, t)) {
for (auto i = m_added_terms.size(); i-- > 0; ) {
if (m_added_terms[i] != t)
continue;
m_added_terms[i] = m_added_terms.back();
m_added_terms.pop_back();
break; // all is done since the term has not made it to m_active_terms
}
return;
}
// deregister the term that has been activated
for (const auto& p : t->ext_coeffs()) {
TRACE("dio_reg", tout << "derigister p.var():" << p.var() << "->" << t->j() << std::endl;);
auto it = m_columns_to_terms.find(p.var());
SASSERT(it != m_columns_to_terms.end());
it->second.erase(t->j());
if (it->second.size() == 0) {
m_columns_to_terms.erase(it);
}
}
SASSERT(std::find(m_added_terms.begin(), m_added_terms.end(), t) == m_added_terms.end());
SASSERT(contains(m_active_terms, t));
m_active_terms.erase(t);
TRACE("dioph_eq", tout << "the deleted term column in m_l_matrix" << std::endl; for (auto p : m_l_matrix.column(t->j())) { tout << "p.coeff():" << p.coeff() << ", row " << p.var() << std::endl; } tout << "m_l_matrix has " << m_l_matrix.column_count() << " columns" << std::endl; tout << "and " << m_l_matrix.row_count() << " rows" << std::endl; print_lar_term_L(*t, tout); tout << "; t->j()=" << t->j() << std::endl;);
shrink_matrices();
}
struct undo_add_term : public trail {
imp& m_s;
const lar_term* m_t;
undo_add_term(imp& s, const lar_term* t) : m_s(s), m_t(t) {}
void undo() {
m_s.undo_add_term_method(m_t);
}
};
struct protected_queue {
std::list<unsigned> m_q;
std::unordered_map<unsigned, std::list<unsigned>::iterator> m_positions;
bool empty() const {
return m_q.empty();
}
unsigned size() const {
return static_cast<unsigned>(m_q.size());
}
void push(unsigned j) {
if (m_positions.find(j) != m_positions.end()) return;
m_q.push_back(j);
m_positions[j] = std::prev(m_q.end());
}
unsigned pop_front() {
unsigned j = m_q.front();
m_q.pop_front();
m_positions.erase(j);
return j;
}
void remove(unsigned j) {
auto it = m_positions.find(j);
if (it != m_positions.end()) {
m_q.erase(it->second);
m_positions.erase(it);
}
SASSERT(invariant());
}
bool contains(unsigned j) const {
return m_positions.find(j) != m_positions.end();
}
void reset() {
m_q.clear();
m_positions.clear();
}
// Invariant method to ensure m_q and m_positions are aligned
bool invariant() const {
if (m_q.size() != m_positions.size())
return false;
for (auto it = m_q.begin(); it != m_q.end(); ++it) {
auto pos_it = m_positions.find(*it);
if (pos_it == m_positions.end())
return false;
if (pos_it->second != it)
return false;
}
return true;
}
};
protected_queue m_q;
struct undo_fixed_column : public trail {
imp& m_imp;
unsigned m_j; // the column that has been added
const mpq m_fixed_val;
undo_fixed_column(imp& s, unsigned j) : m_imp(s), m_j(j), m_fixed_val(s.lra.get_lower_bound(j).x) {
SASSERT(s.lra.column_is_fixed(j));
}
void undo() override {
m_imp.add_changed_column(m_j);
}
};
void remove_last_entry() {
unsigned ei = static_cast<unsigned>(m_e_matrix.row_count() - 1);
if (m_k2s.has_val(ei)) {
remove_from_S(ei);
}
m_sum_of_fixed.pop_back();
}
void eliminate_last_term_column() {
// change only the rows in m_l_matrix, and update m_e_matrix lazily
unsigned j = m_l_matrix.column_count() - 1;
make_sure_j_is_in_the_last_row_of_l_matrix();
const auto& last_e_row = m_l_matrix.m_rows.back();
mpq alpha;
for (const auto& p : last_e_row) {
if (p.var() == j) {
alpha = p.coeff();
break;
}
}
unsigned last_row_index = m_l_matrix.row_count() - 1;
m_l_matrix.divide_row(last_row_index, alpha); // divide the last row by alpha
auto& column = m_l_matrix.m_columns[j];
int pivot_col_cell_index = -1;
for (unsigned k = 0; k < column.size(); k++) {
if (column[k].var() == last_row_index) {
pivot_col_cell_index = k;
break;
}
}
SASSERT(pivot_col_cell_index >= 0);
if (pivot_col_cell_index != 0) {
SASSERT(column.size() > 1);
// swap the pivot column cell with the head cell
auto c = column[0];
column[0] = column[pivot_col_cell_index];
column[pivot_col_cell_index] = c;
m_l_matrix.m_rows[last_row_index][column[0].offset()].offset() = 0;
m_l_matrix.m_rows[c.var()][c.offset()].offset() = pivot_col_cell_index;
}
while (column.size() > 1) {
auto& c = column.back();
SASSERT(c.var() != last_row_index);
m_l_matrix.pivot_row_to_row_given_cell(last_row_index, c, j);
m_changed_rows.insert(c.var());
}
}
void make_sure_j_is_in_the_last_row_of_l_matrix() {
unsigned j = m_l_matrix.column_count() - 1;
const auto& last_e_row = m_l_matrix.m_rows.back();
if (any_of(last_e_row, [j](auto const& p) { return p.var() == j; }))
return;
SASSERT(m_l_matrix.m_columns.back().size() > 0);
unsigned i = m_l_matrix.m_columns[j][0].var();
m_l_matrix.add_rows(mpq(1), i, m_l_matrix.row_count() - 1);
// what is the post-condition? Is 'j' used in the post-condition or is it 'i'?
// SASSERT(any_of(m_l_matrix.m_rows.back(), [i](auto const& p) { return p.var() == i; }));
}
void shrink_matrices() {
unsigned i = m_l_matrix.row_count() - 1;
eliminate_last_term_column();
remove_last_row_in_matrix(m_l_matrix);
remove_last_row_in_matrix(m_e_matrix);
while (m_l_matrix.column_count() && m_l_matrix.m_columns.back().size() == 0) {
m_l_matrix.m_columns.pop_back();
}
while (m_e_matrix.column_count() && m_e_matrix.m_columns.back().size() == 0) {
m_e_matrix.m_columns.pop_back();
}
m_var_register.shrink(m_e_matrix.column_count());
remove_irrelevant_fresh_defs_for_row(i);
if (m_k2s.has_val(i))
remove_from_S(i);
m_sum_of_fixed.pop_back();
}
void remove_last_row_in_matrix(static_matrix<mpq, mpq>& m) {
auto& last_row = m.m_rows.back();
for (unsigned k = static_cast<unsigned>(last_row.size()); k-- > 0;) {
m.remove_element(last_row, last_row[k]);
}
m.m_rows.pop_back();
}
void remove_entry_index(std::list<unsigned>& l, unsigned ei) {
auto it = std::find(l.begin(), l.end(), ei);
if (it != l.end())
l.erase(it);
}
void add_changed_column(unsigned j) {
TRACE("dio", lra.print_column_info(j, tout););
m_changed_columns.insert(j);
}
std_vector<const lar_term*> m_added_terms;
std::unordered_set<const lar_term*> m_active_terms;
std_vector<variable_branch_stats> m_branch_stats;
std_vector<branch> m_branch_stack;
std_vector<constraint_index> m_explanation_of_branches;
bool term_has_big_number(const lar_term& t) const {
for (const auto& p : t) {
if (p.coeff().is_big())
return true;
if (is_fixed(p.var()) && lra.get_lower_bound(p.var()).x.is_big())
return true;
}
return false;
}
bool ignore_big_nums() const { return lra.settings().dio_ignore_big_nums(); }
void add_term_callback(const lar_term* t) {
unsigned j = t->j();
TRACE("dio", tout << "term column t->j():" << j << std::endl; lra.print_term(*t, tout) << std::endl;);
if (!lra.column_is_int(j)) {
TRACE("dio", tout << "ignored a non-integral column" << std::endl;);
m_some_terms_are_ignored = true;
return;
}
CTRACE("dio", !lra.column_has_term(j), tout << "added term that is not associated with a column yet" << std::endl;);
if (ignore_big_nums() && term_has_big_number(*t)) {
TRACE("dio", tout << "term_has_big_number\n";);
m_some_terms_are_ignored = true;
return;
}
m_added_terms.push_back(t);
mark_term_change(t->j());
auto undo = undo_add_term(*this, t);
lra.trail().push(undo);
}
void mark_term_change(unsigned j) {
TRACE("dio", tout << "marked term change j:" << j << std::endl;);
m_changed_terms.insert(j);
}
void update_column_bound_callback(unsigned j) {
if (!lra.column_is_int(j))
return;
if (lra.column_has_term(j) &&
ignore_big_nums() && !term_has_big_number(lra.get_term(j)))
m_terms_to_tighten.insert(j); // the boundary of the term has changed: we can be successful to tighten this term
if (!lra.column_is_fixed(j))
return;
if (ignore_big_nums() && lra.get_lower_bound(j).x.is_big())
return;
TRACE("dio", tout << "j:" << j << "\n"; lra.print_column_info(j, tout););
m_changed_columns.insert(j);
lra.trail().push(undo_fixed_column(*this, j));
}
public:
imp(int_solver& lia, lar_solver& lra) : lia(lia), lra(lra) {
lra.m_add_term_callback = [this](const lar_term* t) { add_term_callback(t); };
lra.m_update_column_bound_callback = [this](unsigned j) { update_column_bound_callback(j); };
}
term_o get_term_from_entry(unsigned i) const {
term_o t;
for (const auto& p : m_e_matrix.m_rows[i]) {
t.add_monomial(p.coeff(), p.var());
}
t.c() = m_sum_of_fixed[i];
return t;
}
// adds variable j of lar_solver to the local diophantine handler
unsigned add_var(unsigned j) {
return this->m_var_register.add_var(j, true);
}
unsigned local_to_lar_solver(unsigned j) const {
return m_var_register.local_to_external(j);
}
void register_columns_to_term(const lar_term& t) {
TRACE("dio_reg", tout << "register term:"; lra.print_term(t, tout); tout << ", t.j()=" << t.j() << std::endl;);
for (const auto& p : t.ext_coeffs()) {
auto it = m_columns_to_terms.find(p.var());
TRACE("dio_reg", tout << "register p.var():" << p.var() << "->" << t.j() << std::endl;);
if (it != m_columns_to_terms.end()) {
it->second.insert(t.j());
}
else {
std::unordered_set<unsigned> s;
s.insert(t.j());
m_columns_to_terms[p.var()] = s;
}
}
}
// the term has form sum(a_i*x_i) - t.j() = 0,
void fill_entry(const lar_term& t) {
unsigned entry_index = (unsigned)m_e_matrix.row_count();
m_sum_of_fixed.push_back(mpq(0));
mpq& fixed_sum = m_sum_of_fixed.back();
// fill m_l_matrix row
m_l_matrix.add_row();
// todo: consider to compress variables t.j() by using a devoted var_register for term columns
m_l_matrix.add_columns_up_to(t.j());
m_l_matrix.add_new_element(entry_index, t.j(), mpq(1));
// fill E-entry
m_e_matrix.add_row();
SASSERT(m_sum_of_fixed.size() == m_l_matrix.row_count() && m_e_matrix.row_count() == m_e_matrix.row_count());
for (const auto& p : t.ext_coeffs()) {
SASSERT(p.coeff().is_int());
if (is_fixed(p.var()))
fixed_sum += p.coeff() * lia.lower_bound(p.var()).x;
else {
unsigned lj = add_var(p.var());
m_e_matrix.add_columns_up_to(lj);
m_e_matrix.add_new_element(entry_index, lj, p.coeff());
}
}
subs_entry(entry_index);
SASSERT(entry_invariant(entry_index));
TRACE("dio_entry", print_entry(entry_index, tout) << std::endl;);
}
void subs_entry(unsigned ei) {
if (ei >= m_e_matrix.row_count()) return;
// m_q is the queue of variables that can be substituted in ei
m_q.reset();
for (const auto& p : m_e_matrix.m_rows[ei]) {
if (can_substitute(p.var()))
m_q.push(p.var());
}
if (m_q.size() == 0) {
TRACE("dio", tout << "nothing to subst on ei:" << ei << "\n";);
return;
}
substitute_on_q(ei);
SASSERT(entry_invariant(ei));
}
void substitute_on_q_with_entry_in_S(unsigned ei, unsigned j, const mpq& alpha) {
unsigned ei_to_sub = m_k2s[j];
int sign_j = get_sign_in_e_row(ei_to_sub, j);
// we need to eliminate alpha*j in ei's row
add_two_entries(-mpq(sign_j) * alpha, ei_to_sub, ei);
for (const auto& p : m_e_matrix.m_rows[ei]) {
unsigned jj = p.var();
if (can_substitute(jj))
m_q.push(jj);
}
}
void substitute_with_fresh_def(unsigned ei, unsigned j, const mpq& alpha) {
const lar_term& sub_term = m_fresh_k2xt_terms.get_by_key(j).first;
TRACE("dioph_eq", print_lar_term_L(sub_term, tout) << std::endl;);
SASSERT(sub_term.get_coeff(j).is_one());
// we need to eliminate alpha*j in ei's row
add_term_to_entry(-alpha, sub_term, ei);
for (const auto& p : m_e_matrix.m_rows[ei]) {
unsigned jj = p.var();
if (can_substitute(jj))
m_q.push(jj);
}
}
// q is the queue of variables that can be substituted in ei
void substitute_on_q(unsigned ei) {
while (!m_q.empty()) {
unsigned j = m_q.pop_front();
mpq alpha = get_coeff_in_e_row(ei, j);
if (alpha.is_zero()) continue;
if (m_k2s.has_key(j)) {
substitute_on_q_with_entry_in_S(ei, j, alpha);
}
else {
substitute_with_fresh_def(ei, j, alpha);
}
}
}
bool term_is_in_range(const lar_term& t) const {
for (const auto& p : t) {
if (p.var() >= m_e_matrix.column_count())
return false;
}
return true;
}
// adds the term multiplied by coeff to m_e_matrix row i
void add_term_to_entry(const mpq& coeff, const lar_term& t, unsigned i) {
SASSERT(term_is_in_range(t));
m_e_matrix.add_term_to_row(coeff, t, i);
}
// adds entry i0 multiplied by coeff to entry i1
void add_two_entries(const mpq& coeff, unsigned i0, unsigned i1) {
m_e_matrix.add_rows(coeff, i0, i1);
m_l_matrix.add_rows(coeff, i0, i1);
m_sum_of_fixed[i1] += coeff * m_sum_of_fixed[i0];
}
void clear_e_row(unsigned ei) {
auto& row = m_e_matrix.m_rows[ei];
while (row.size() > 0) {
auto& c = row.back();
m_e_matrix.remove_element(row, c);
}
}
void recalculate_entry(unsigned ei) {
TRACE("dio", print_entry(ei, tout) << std::endl;);
mpq& fixed_sum = m_sum_of_fixed[ei];
fixed_sum = mpq(0);
open_l_term_to_espace(ei, fixed_sum);
clear_e_row(ei);
mpq denom(1);
for (const auto& p : m_espace.m_data) {
unsigned lj = add_var(p.var());
m_e_matrix.add_columns_up_to(lj);
m_e_matrix.add_new_element(ei, lj, p.coeff());