-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathlar_solver.cpp
2337 lines (2082 loc) · 88.7 KB
/
lar_solver.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
/*
Copyright (c) 2017 Microsoft Corporation
Author: Nikolaj Bjorner, Lev Nachmanson
*/
#include "math/lp/lar_solver.h"
#include "smt/params/smt_params_helper.hpp"
namespace lp {
lp_settings& lar_solver::settings() { return m_settings; }
lp_settings const& lar_solver::settings() const { return m_settings; }
statistics& lar_solver::stats() { return m_settings.stats(); }
void lar_solver::updt_params(params_ref const& _p) {
smt_params_helper p(_p);
track_touched_rows(p.arith_bprop_on_pivoted_rows());
set_cut_strategy(p.arith_branch_cut_ratio());
m_settings.updt_params(_p);
}
lar_solver::lar_solver() :
m_mpq_lar_core_solver(m_settings, *this),
m_var_register(),
m_constraints(m_dependencies, *this) {}
// start or ends tracking the rows that were changed by solve()
void lar_solver::track_touched_rows(bool v) {
m_mpq_lar_core_solver.m_r_solver.m_touched_rows = v ? (&m_touched_rows) : nullptr;
}
// returns true iff the solver tracks the rows that were changed by solve()
bool lar_solver::touched_rows_are_tracked() const {
return m_mpq_lar_core_solver.m_r_solver.m_touched_rows != nullptr;
}
lar_solver::~lar_solver() {
for (auto t : m_terms)
delete t;
}
bool lar_solver::sizes_are_correct() const {
lp_assert(A_r().column_count() == m_mpq_lar_core_solver.m_r_solver.m_column_types.size());
lp_assert(A_r().column_count() == m_mpq_lar_core_solver.m_r_solver.m_costs.size());
lp_assert(A_r().column_count() == m_mpq_lar_core_solver.m_r_x.size());
return true;
}
std::ostream& lar_solver::print_implied_bound(const implied_bound& be, std::ostream& out) const {
out << "implied bound\n";
unsigned v = be.m_j;
if (column_has_term(v)) {
out << "term for column " << v << std::endl;
print_term(*m_columns[v].term(), out);
}
else {
out << get_variable_name(v);
}
out << " " << lconstraint_kind_string(be.kind()) << " " << be.m_bound << std::endl;
out << "end of implied bound" << std::endl;
return out;
}
std::ostream& lar_solver::print_values(std::ostream& out) const {
for (unsigned i = 0; i < m_mpq_lar_core_solver.m_r_x.size(); i++) {
const numeric_pair<mpq>& rp = m_mpq_lar_core_solver.m_r_x[i];
out << this->get_variable_name(i) << " -> " << rp << "\n";
}
return out;
}
bool lar_solver::implied_bound_is_correctly_explained(implied_bound const& be, const vector<std::pair<mpq, unsigned>>& explanation) const {
std::unordered_map<unsigned, mpq> coeff_map;
auto rs_of_evidence = zero_of_type<mpq>();
unsigned n_of_G = 0, n_of_L = 0;
bool strict = false;
for (auto& it : explanation) {
mpq coeff = it.first;
constraint_index con_ind = it.second;
const auto& constr = m_constraints[con_ind];
lconstraint_kind kind = coeff.is_pos() ? constr.kind() : flip_kind(constr.kind());
register_in_map(coeff_map, constr, coeff);
if (kind == GT || kind == LT)
strict = true;
if (kind == GE || kind == GT) n_of_G++;
else if (kind == LE || kind == LT) n_of_L++;
rs_of_evidence += coeff * constr.rhs();
}
lp_assert(n_of_G == 0 || n_of_L == 0);
lconstraint_kind kind = n_of_G ? GE : (n_of_L ? LE : EQ);
if (strict)
kind = static_cast<lconstraint_kind>((static_cast<int>(kind) / 2));
if (!column_has_term(be.m_j)) {
if (coeff_map.size() != 1)
return false;
auto it = coeff_map.find(be.m_j);
if (it == coeff_map.end()) return false;
mpq ratio = it->second;
if (ratio < zero_of_type<mpq>()) {
kind = static_cast<lconstraint_kind>(-kind);
}
rs_of_evidence /= ratio;
}
else {
lar_term const& t = get_term(be.m_j);
auto first_coeff = t.begin();
unsigned j = (*first_coeff).j();
auto it = coeff_map.find(j);
if (it == coeff_map.end())
return false;
mpq ratio = it->second / (*first_coeff).coeff();
for (auto p : t) {
it = coeff_map.find(p.j());
if (it == coeff_map.end())
return false;
if (p.coeff() * ratio != it->second)
return false;
}
if (ratio < zero_of_type<mpq>()) {
kind = static_cast<lconstraint_kind>(-kind);
}
rs_of_evidence /= ratio;
// rs_of_evidence += t->m_v * ratio;
}
return kind == be.kind() && rs_of_evidence == be.m_bound;
}
bool lar_solver::row_has_a_big_num(unsigned i) const {
for (const auto& c : A_r().m_rows[i])
if (c.coeff().is_big())
return true;
return false;
}
lp_status lar_solver::get_status() const { return m_status; }
void lar_solver::set_status(lp_status s) {
TRACE("lar_solver", tout << "setting status to " << s << "\n";);
m_status = s;
}
lp_status lar_solver::find_feasible_solution() {
stats().m_make_feasible++;
if (A_r().column_count() > stats().m_max_cols)
stats().m_max_cols = A_r().column_count();
if (A_r().row_count() > stats().m_max_rows)
stats().m_max_rows = A_r().row_count();
flet f(settings().simplex_strategy(), simplex_strategy_enum::tableau_rows);
m_mpq_lar_core_solver.m_r_solver.m_look_for_feasible_solution_only = true;
auto ret = solve();
return ret;
}
lp_status lar_solver::solve() {
if (m_status == lp_status::INFEASIBLE || m_status == lp_status::CANCELLED)
return m_status;
solve_with_core_solver();
if (m_status == lp_status::INFEASIBLE || m_status == lp_status::CANCELLED)
return m_status;
if (m_settings.bound_propagation())
detect_rows_with_changed_bounds();
clear_columns_with_changed_bounds();
return m_status;
}
void lar_solver::fill_explanation_from_crossed_bounds_column(explanation& evidence) const {
// this is the case when the lower bound is in conflict with the upper one
svector<constraint_index> deps;
SASSERT(m_crossed_bounds_deps != nullptr);
m_dependencies.linearize(m_crossed_bounds_deps, deps);
for (auto d : deps)
evidence.add_pair(d, -numeric_traits<mpq>::one());
}
void lar_solver::push() {
m_trail.push_scope();
m_simplex_strategy = m_settings.simplex_strategy();
m_simplex_strategy.push();
m_crossed_bounds_column = null_lpvar;
m_crossed_bounds_deps = nullptr;
m_mpq_lar_core_solver.push();
m_constraints.push();
m_usage_in_terms.push();
m_dependencies.push_scope();
}
void lar_solver::clean_popped_elements(unsigned n, indexed_uint_set& set) {
vector<int> to_remove;
for (unsigned j : set)
if (j >= n)
to_remove.push_back(j);
for (unsigned j : to_remove)
set.remove(j);
}
void lar_solver::clean_popped_elements_for_heap(unsigned n, lpvar_heap& heap) {
vector<int> to_remove;
for (unsigned j : heap)
if (j >= n)
to_remove.push_back(j);
for (unsigned j : to_remove)
heap.erase(j);
}
void lar_solver::pop(unsigned k) {
TRACE("lar_solver", tout << "k = " << k << std::endl;);
m_crossed_bounds_column = null_lpvar;
m_crossed_bounds_deps = nullptr;
m_trail.pop_scope(k);
unsigned n = m_columns.size();
m_var_register.shrink(n);
lp_assert(m_mpq_lar_core_solver.m_r_solver.m_costs.size() == A_r().column_count());
lp_assert(m_mpq_lar_core_solver.m_r_solver.m_basis.size() == A_r().row_count());
lp_assert(m_mpq_lar_core_solver.m_r_solver.basis_heading_is_correct());
lp_assert(A_r().column_count() == n);
TRACE("lar_solver_details", for (unsigned j = 0; j < n; j++) print_column_info(j, tout) << "\n";);
m_mpq_lar_core_solver.pop(k);
remove_non_fixed_from_fixed_var_table();
for (auto rid : m_row_bounds_to_replay)
add_touched_row(rid);
m_row_bounds_to_replay.reset();
unsigned m = A_r().row_count();
clean_popped_elements(m, m_touched_rows);
clean_inf_heap_of_r_solver_after_pop();
SASSERT(m_mpq_lar_core_solver.m_r_solver.reduced_costs_are_correct_tableau());
m_constraints.pop(k);
m_simplex_strategy.pop(k);
m_settings.simplex_strategy() = m_simplex_strategy;
lp_assert(sizes_are_correct());
lp_assert(m_mpq_lar_core_solver.m_r_solver.reduced_costs_are_correct_tableau());
m_usage_in_terms.pop(k);
m_dependencies.pop_scope(k);
// init the nbasis sorting
require_nbasis_sort();
set_status(lp_status::UNKNOWN);
}
bool lar_solver::maximize_term_on_tableau(const lar_term& term,
impq& term_max) {
flet f(m_mpq_lar_core_solver.m_r_solver.m_look_for_feasible_solution_only, false);
m_mpq_lar_core_solver.m_r_solver.set_status(lp_status::FEASIBLE);
m_mpq_lar_core_solver.solve();
lp_status st = m_mpq_lar_core_solver.m_r_solver.get_status();
TRACE("lar_solver", tout << st << "\n";);
SASSERT(m_mpq_lar_core_solver.m_r_solver.calc_current_x_is_feasible_include_non_basis());
if (st == lp_status::UNBOUNDED || st == lp_status::CANCELLED) {
return false;
}
else {
term_max = term.apply(m_mpq_lar_core_solver.m_r_x);
return true;
}
}
// get dependencies of the corresponding bounds from max_coeffs
u_dependency* lar_solver::get_dependencies_of_maximum(const vector<std::pair<mpq,lpvar>>& max_coeffs) {
// The linear combinations of d_j*x[j] = the term that got maximized, where (d_j, j) is in max_coeffs
// Every j with positive coeff is at its upper bound,
// and every j with negative coeff is at its lower bound: so the sum cannot be increased.
// All variables j in the sum are non-basic.
u_dependency* dep = nullptr;
for (const auto & [d_j, j]: max_coeffs) {
SASSERT (!d_j.is_zero());
TRACE("lar_solver_improve_bounds", tout << "d[" << j << "] = " << d_j << "\n";
this->m_mpq_lar_core_solver.m_r_solver.print_column_info(j, tout););
const column& ul = m_columns[j];
u_dependency * bound_dep;
if (d_j.is_pos())
bound_dep = ul.upper_bound_witness();
else
bound_dep = ul.lower_bound_witness();
TRACE("lar_solver_improve_bounds", {
svector<constraint_index> cs;
m_dependencies.linearize(bound_dep, cs);
for (auto c : cs)
m_constraints.display(tout, c) << "\n";
});
SASSERT(bound_dep != nullptr);
dep = m_dependencies.mk_join(dep, bound_dep);
}
return dep;
}
// returns nullptr if the bound is not improved, otherwise returns the witness of the bound
u_dependency* lar_solver::find_improved_bound(lpvar j, bool lower_bound, mpq& bound) {
SASSERT(is_feasible());
if (lower_bound && column_has_lower_bound(j) && get_column_value(j) == column_lower_bound(j))
return nullptr; // cannot do better
if (!lower_bound && column_has_upper_bound(j) && get_column_value(j) == column_upper_bound(j))
return nullptr; // cannot do better
lar_term term = get_term_to_maximize(j);
if (lower_bound)
term.negate();
vector<std::pair<mpq, unsigned>> max_coeffs;
TRACE("lar_solver_improve_bounds", tout << "j = " << j << ", "; print_term(term, tout << "term to maximize\n"););
impq term_max;
if (!maximize_term_on_feasible_r_solver(term, term_max, &max_coeffs))
return nullptr;
// term_max is equal to the sum of m_d[j]*x[j] over all non basic j.
// For the sum to be at the maximum all non basic variables should be at their bounds: if (m_d[j] > 0) x[j] = u[j], otherwise x[j] = l[j]. At upper bounds we have u[j].y <= 0, and at lower bounds we have l[j].y >= 0, therefore for the sum term_max.y <= 0.
SASSERT(!term_max.y.is_pos());
// To keep it simpler we ignore possible improvements from non-strict to strict bounds.
bound = term_max.x;
if (lower_bound) {
bound.neg();
if (column_is_int(j))
bound = ceil(bound);
if (column_has_lower_bound(j) && column_is_int(j) && bound <= column_lower_bound(j).x)
return nullptr;
TRACE("lar_solver_improve_bounds",
tout << "setting lower bound for " << j << " to " << bound << "\n";
if (column_has_lower_bound(j)) tout << "bound was = " << column_lower_bound(j) << "\n";);
}
else {
if (column_is_int(j))
bound = floor(bound);
if (column_has_upper_bound(j)) {
if (bound >= column_upper_bound(j).x)
return nullptr;
}
TRACE("lar_solver_improve_bounds",
tout << "setting upper bound for " << j << " to " << bound << "\n";
if (column_has_upper_bound(j)) tout << "bound was = " << column_upper_bound(j) << "\n";;);
}
return get_dependencies_of_maximum(max_coeffs);
}
bool lar_solver::costs_are_zeros_for_r_solver() const {
for (unsigned j = 0; j < m_mpq_lar_core_solver.m_r_solver.m_costs.size(); j++) {
lp_assert(is_zero(m_mpq_lar_core_solver.m_r_solver.m_costs[j]));
}
return true;
}
bool lar_solver::reduced_costs_are_zeroes_for_r_solver() const {
for (unsigned j = 0; j < m_mpq_lar_core_solver.m_r_solver.m_d.size(); j++) {
lp_assert(is_zero(m_mpq_lar_core_solver.m_r_solver.m_d[j]));
}
return true;
}
void lar_solver::set_costs_to_zero(const lar_term& term) {
auto& rslv = m_mpq_lar_core_solver.m_r_solver;
auto& d = rslv.m_d;
auto& costs = rslv.m_costs;
for (lar_term::ival p : term) {
unsigned j = p.j();
costs[j] = zero_of_type<mpq>();
int i = rslv.m_basis_heading[j];
if (i < 0)
d[j] = zero_of_type<mpq>();
else
for (const auto& rc : A_r().m_rows[i])
d[rc.var()] = zero_of_type<mpq>();
}
lp_assert(reduced_costs_are_zeroes_for_r_solver());
lp_assert(costs_are_zeros_for_r_solver());
}
void lar_solver::prepare_costs_for_r_solver(const lar_term& term) {
TRACE("lar_solver", print_term(term, tout << "prepare: ") << "\n";);
auto& rslv = m_mpq_lar_core_solver.m_r_solver;
lp_assert(costs_are_zeros_for_r_solver());
lp_assert(reduced_costs_are_zeroes_for_r_solver());
move_non_basic_columns_to_bounds();
rslv.m_costs.resize(A_r().column_count(), zero_of_type<mpq>());
for (lar_term::ival p : term) {
unsigned j = p.j();
rslv.m_costs[j] = p.coeff();
if (rslv.m_basis_heading[j] < 0)
rslv.m_d[j] += p.coeff();
else
rslv.update_reduced_cost_for_basic_column_cost_change(-p.coeff(), j);
}
if (settings().backup_costs)
rslv.m_costs_backup = rslv.m_costs;
lp_assert(rslv.reduced_costs_are_correct_tableau());
}
void lar_solver::move_non_basic_columns_to_bounds() {
auto& lcs = m_mpq_lar_core_solver;
bool change = false;
for (unsigned j : lcs.m_r_nbasis) {
if (move_non_basic_column_to_bounds(j))
change = true;
}
if (!change)
return;
if (settings().simplex_strategy() == simplex_strategy_enum::tableau_costs)
update_x_and_inf_costs_for_columns_with_changed_bounds_tableau();
find_feasible_solution();
}
bool lar_solver::move_non_basic_column_to_bounds(unsigned j) {
auto& lcs = m_mpq_lar_core_solver;
auto& val = lcs.m_r_x[j];
switch (lcs.m_column_types()[j]) {
case column_type::boxed: {
const auto& l = lcs.m_r_lower_bounds()[j];
if (val == l || val == lcs.m_r_upper_bounds()[j]) return false;
set_value_for_nbasic_column(j, l);
return true;
}
case column_type::lower_bound: {
const auto& l = lcs.m_r_lower_bounds()[j];
if (val != l) {
set_value_for_nbasic_column(j, l);
return true;
}
return false;
}
case column_type::fixed:
case column_type::upper_bound: {
const auto & u = lcs.m_r_upper_bounds()[j];
if (val != u) {
set_value_for_nbasic_column(j, u);
return true;
}
return false;
}
case column_type::free_column:
if (column_is_int(j) && !val.is_int()) {
set_value_for_nbasic_column(j, impq(floor(val)));
return true;
}
return false;
default:
SASSERT(false);
}
return false;
}
void lar_solver::set_value_for_nbasic_column(unsigned j, const impq& new_val) {
lp_assert(!is_base(j));
auto& x = m_mpq_lar_core_solver.m_r_x[j];
auto delta = new_val - x;
x = new_val;
TRACE("lar_solver_feas", tout << "setting " << j << " to "
<< new_val << (column_is_feasible(j)?"feas":"non-feas") << "\n";);
change_basic_columns_dependend_on_a_given_nb_column(j, delta);
}
bool lar_solver::maximize_term_on_feasible_r_solver(lar_term& term,
impq& term_max, vector<std::pair<mpq, lpvar>>* max_coeffs = nullptr) {
settings().backup_costs = false;
bool ret = false;
TRACE("lar_solver", print_term(term, tout << "maximize: ") << "\n"
<< constraints() << ", strategy = " << (int)settings().simplex_strategy() << "\n";);
if (settings().simplex_strategy() != simplex_strategy_enum::tableau_costs)
require_nbasis_sort();
flet f(settings().simplex_strategy(), simplex_strategy_enum::tableau_costs);
prepare_costs_for_r_solver(term);
ret = maximize_term_on_tableau(term, term_max);
if (ret && max_coeffs != nullptr) {
for (unsigned j = 0; j < column_count(); j++) {
const mpq& d_j = m_mpq_lar_core_solver.m_r_solver.m_d[j];
if (d_j.is_zero())
continue;
max_coeffs->push_back(std::make_pair(d_j, j));
TRACE("lar_solver", tout<<"m_d["<<j<<"] = " << d_j<< ",\n";print_column_info(j, tout) << "\n";);
}
}
set_costs_to_zero(term);
m_mpq_lar_core_solver.m_r_solver.set_status(lp_status::OPTIMAL);
return ret;
}
// returns true iff the row of j has a non-fixed column different from j
bool lar_solver::remove_from_basis(unsigned j) {
lp_assert(is_base(j));
unsigned i = row_of_basic_column(j);
for (const auto & c : A_r().m_rows[i])
if (j != c.var() && !column_is_fixed(c.var()))
return m_mpq_lar_core_solver.m_r_solver.remove_from_basis_core(c.var(), j);
return false;
}
lar_term lar_solver::get_term_to_maximize(unsigned j) const {
if (column_has_term(j)) {
return * m_columns[j].term();
}
if (j < m_mpq_lar_core_solver.m_r_x.size()) {
lar_term r;
r.add_monomial(one_of_type<mpq>(), j);
return r;
}
return lar_term(); // return an empty term
}
lp_status lar_solver::maximize_term(unsigned j,
impq& term_max) {
TRACE("lar_solver", print_values(tout););
SASSERT(m_mpq_lar_core_solver.m_r_solver.calc_current_x_is_feasible_include_non_basis());
lar_term term = get_term_to_maximize(j);
if (term.is_empty()) return lp_status::UNBOUNDED;
impq prev_value = term.apply(m_mpq_lar_core_solver.m_r_x);
auto backup = m_mpq_lar_core_solver.m_r_x;
if (!maximize_term_on_feasible_r_solver(term, term_max, nullptr)) {
m_mpq_lar_core_solver.m_r_x = backup;
return lp_status::UNBOUNDED;
}
impq opt_val = term_max;
bool change = false;
for (unsigned j = 0; j < m_mpq_lar_core_solver.m_r_x.size(); j++) {
if (!column_is_int(j))
continue;
if (column_value_is_integer(j))
continue;
if (m_int_solver->is_base(j)) {
if (!remove_from_basis(j)) { // consider a special version of remove_from_basis that would not remove inf_int columns
m_mpq_lar_core_solver.m_r_x = backup;
term_max = prev_value;
return lp_status::FEASIBLE; // it should not happen
}
}
if (!column_value_is_integer(j)) {
term_max = prev_value;
m_mpq_lar_core_solver.m_r_x = backup;
return lp_status::FEASIBLE;
}
change = true;
}
if (change) {
term_max = term.apply(m_mpq_lar_core_solver.m_r_x);
}
if (term_max < prev_value) {
term_max = prev_value;
m_mpq_lar_core_solver.m_r_x = backup;
}
TRACE("lar_solver", print_values(tout););
if (term_max == opt_val) {
set_status(lp_status::OPTIMAL);
return lp_status::OPTIMAL;
}
return lp_status::FEASIBLE;
}
void lar_solver::pop_core_solver_params() {
pop_core_solver_params(1);
}
void lar_solver::pop_core_solver_params(unsigned k) {
A_r().pop(k);
}
void lar_solver::set_upper_bound_witness(lpvar j, u_dependency* dep) {
m_trail.push(vector_value_trail(m_columns, j));
m_columns[j].upper_bound_witness() = dep;
}
void lar_solver::set_lower_bound_witness(lpvar j, u_dependency* dep) {
m_trail.push(vector_value_trail(m_columns, j));
m_columns[j].lower_bound_witness() = dep;
}
void lar_solver::register_monoid_in_map(std::unordered_map<lpvar, mpq>& coeffs, const mpq& a, unsigned j) {
auto it = coeffs.find(j);
if (it == coeffs.end())
coeffs[j] = a;
else
it->second += a;
}
void lar_solver::substitute_terms_in_linear_expression(const vector<std::pair<mpq, lpvar>>& left_side_with_terms,
vector<std::pair<mpq, lpvar>>& left_side) const {
std::unordered_map<lpvar, mpq> coeffs;
for (auto& t : left_side_with_terms) {
unsigned j = t.second;
if (!column_has_term(j)) {
register_monoid_in_map(coeffs, t.first, j);
}
else {
const lar_term& term = *m_columns[t.second].term();
for (auto p : term)
register_monoid_in_map(coeffs, t.first * p.coeff(), p.j());
}
}
for (auto& [v, c] : coeffs)
if (!is_zero(c))
left_side.push_back(std::make_pair(c, v));
}
void lar_solver::add_touched_row(unsigned rid) {
if (m_settings.bound_propagation())
m_touched_rows.insert(rid);
}
void lar_solver::remove_fixed_vars_from_base() {
// this will allow to disable and restore the tracking of the touched rows
flet<indexed_uint_set*> f(m_mpq_lar_core_solver.m_r_solver.m_touched_rows, nullptr);
unsigned num = A_r().column_count();
unsigned_vector to_remove;
for (unsigned j : m_fixed_base_var_set) {
if (j >= num || !is_base(j) || !column_is_fixed(j)) {
to_remove.push_back(j);
continue;
}
lp_assert(is_base(j) && column_is_fixed(j));
auto const& r = basic2row(j);
for (auto const& c : r) {
unsigned j_entering = c.var();
if (!column_is_fixed(j_entering)) {
pivot(j_entering, j);
to_remove.push_back(j);
lp_assert(is_base(j_entering));
break;
}
}
}
for (unsigned j : to_remove) {
m_fixed_base_var_set.remove(j);
}
lp_assert(fixed_base_removed_correctly());
}
#ifdef Z3DEBUG
bool lar_solver::fixed_base_removed_correctly() const {
for (unsigned i = 0; i < A_r().row_count(); i++) {
unsigned j = get_base_column_in_row(i);
if (column_is_fixed(j)) {
for (const auto & c : A_r().m_rows[i] ) {
if (!column_is_fixed(c.var())) {
TRACE("lar_solver", print_row(A_r().m_rows[i], tout) << "\n";
for(const auto & c : A_r().m_rows[i]) {
print_column_info(c.var(), tout) << "\n";
});
return false;
}
}
}
}
return true;
}
#endif
bool lar_solver::use_tableau_costs() const {
return m_settings.simplex_strategy() == simplex_strategy_enum::tableau_costs;
}
bool lar_solver::row_is_correct(unsigned i) const {
numeric_pair<mpq> r = zero_of_type<numeric_pair<mpq>>();
for (const auto& c : A_r().m_rows[i]) {
r += c.coeff() * m_mpq_lar_core_solver.m_r_x[c.var()];
}
CTRACE("lar_solver", !is_zero(r), tout << "row = " << i << ", j = " << m_mpq_lar_core_solver.m_r_basis[i] << "\n";
print_row(A_r().m_rows[i], tout); tout << " = " << r << "\n");
return is_zero(r);
}
unsigned lar_solver::row_of_basic_column(unsigned j) const {
return m_mpq_lar_core_solver.m_r_heading[j];
}
bool lar_solver::ax_is_correct() const {
for (unsigned i = 0; i < A_r().row_count(); i++) {
if (!row_is_correct(i)) {
return false;
}
}
return true;
}
bool lar_solver::tableau_with_costs() const {
return m_settings.simplex_strategy() == simplex_strategy_enum::tableau_costs;
}
bool lar_solver::costs_are_used() const {
return m_settings.simplex_strategy() != simplex_strategy_enum::tableau_rows;
}
void lar_solver::change_basic_columns_dependend_on_a_given_nb_column(unsigned j, const numeric_pair<mpq>& delta) {
for (const auto& c : A_r().m_columns[j]) {
unsigned bj = m_mpq_lar_core_solver.m_r_basis[c.var()];
if (tableau_with_costs()) {
m_basic_columns_with_changed_cost.insert(bj);
}
m_mpq_lar_core_solver.m_r_solver.add_delta_to_x_and_track_feasibility(bj, -A_r().get_val(c) * delta);
TRACE("change_x_del",
tout << "changed basis column " << bj << ", it is " <<
(column_is_feasible(bj) ? "feas" : "inf") << std::endl;);
}
}
void lar_solver::update_x_and_inf_costs_for_column_with_changed_bounds(unsigned j) {
if (m_mpq_lar_core_solver.m_r_heading[j] >= 0) {
if (costs_are_used()) {
bool was_infeas = m_mpq_lar_core_solver.m_r_solver.inf_heap_contains(j);
m_mpq_lar_core_solver.m_r_solver.track_column_feasibility(j);
if (was_infeas != m_mpq_lar_core_solver.m_r_solver.inf_heap_contains(j))
m_basic_columns_with_changed_cost.insert(j);
}
else {
m_mpq_lar_core_solver.m_r_solver.track_column_feasibility(j);
}
}
else {
numeric_pair<mpq> delta;
if (m_mpq_lar_core_solver.m_r_solver.make_column_feasible(j, delta))
change_basic_columns_dependend_on_a_given_nb_column(j, delta);
}
}
void lar_solver::detect_rows_with_changed_bounds_for_column(unsigned j) {
if (m_mpq_lar_core_solver.m_r_heading[j] >= 0)
add_touched_row(m_mpq_lar_core_solver.m_r_heading[j]);
else
add_column_rows_to_touched_rows(j);
}
void lar_solver::detect_rows_with_changed_bounds() {
for (auto j : m_columns_with_changed_bounds)
detect_rows_with_changed_bounds_for_column(j);
if (m_find_monics_with_changed_bounds_func)
m_find_monics_with_changed_bounds_func(m_columns_with_changed_bounds);
}
void lar_solver::update_x_and_inf_costs_for_columns_with_changed_bounds_tableau() {
for (auto j : m_columns_with_changed_bounds)
update_x_and_inf_costs_for_column_with_changed_bounds(j);
}
void lar_solver::solve_with_core_solver() {
m_mpq_lar_core_solver.prefix_r();
update_x_and_inf_costs_for_columns_with_changed_bounds_tableau();
m_mpq_lar_core_solver.solve();
set_status(m_mpq_lar_core_solver.m_r_solver.get_status());
lp_assert(((stats().m_make_feasible% 100) != 0) || m_status != lp_status::OPTIMAL || all_constraints_hold());
}
// this function just looks at the status
bool lar_solver::is_feasible() const {
switch (this->get_status()) {
case lp_status::OPTIMAL:
case lp_status::FEASIBLE:
case lp_status::UNBOUNDED:
SASSERT(m_mpq_lar_core_solver.m_r_solver.inf_heap().size() == 0);
return true;
default:
return false;
}
}
numeric_pair<mpq> lar_solver::get_basic_var_value_from_row(unsigned i) {
numeric_pair<mpq> r = zero_of_type<numeric_pair<mpq>>();
unsigned bj = m_mpq_lar_core_solver.m_r_solver.m_basis[i];
for (const auto& c : A_r().m_rows[i]) {
if (c.var() == bj) continue;
const auto& x = m_mpq_lar_core_solver.m_r_x[c.var()];
if (!is_zero(x))
r -= c.coeff() * x;
}
return r;
}
bool lar_solver::var_is_registered(lpvar vj) const {
return vj < A_r().column_count();
}
bool lar_solver::all_constrained_variables_are_registered(const vector<std::pair<mpq, lpvar>>& left_side) {
for (auto it : left_side) {
if (!var_is_registered(it.second))
return false;
}
return true;
}
bool lar_solver::all_constraints_hold() const {
if (m_settings.get_cancel_flag())
return true;
std::unordered_map<lpvar, mpq> var_map;
get_model_do_not_care_about_diff_vars(var_map);
for (auto const& c : m_constraints.active()) {
if (!constraint_holds(c, var_map)) {
TRACE("lar_solver",
m_constraints.display(tout, c) << "\n";
for (auto p : c.coeffs()) {
m_mpq_lar_core_solver.m_r_solver.print_column_info(p.second, tout);
});
return false;
}
}
return true;
}
bool lar_solver::constraint_holds(const lar_base_constraint& constr, std::unordered_map<lpvar, mpq>& var_map) const {
mpq left_side_val = get_left_side_val(constr, var_map);
switch (constr.kind()) {
case LE: return left_side_val <= constr.rhs();
case LT: return left_side_val < constr.rhs();
case GE: return left_side_val >= constr.rhs();
case GT: return left_side_val > constr.rhs();
case EQ: return left_side_val == constr.rhs();
default:
UNREACHABLE();
}
return false; // it is unreachable
}
void lar_solver::register_in_map(std::unordered_map<lpvar, mpq>& coeffs, const lar_base_constraint& cn, const mpq& a) {
for (auto& it : cn.coeffs()) {
unsigned j = it.second;
auto p = coeffs.find(j);
if (p == coeffs.end())
coeffs[j] = it.first * a;
else {
p->second += it.first * a;
if (p->second.is_zero())
coeffs.erase(p);
}
}
}
bool lar_solver::the_left_sides_sum_to_zero(const vector<std::pair<mpq, unsigned>>& evidence) const {
std::unordered_map<lpvar, mpq> coeff_map;
for (auto const & [coeff, con_ind] : evidence) {
lp_assert(m_constraints.valid_index(con_ind));
register_in_map(coeff_map, m_constraints[con_ind], coeff);
}
if (!coeff_map.empty()) {
return false;
}
return true;
}
bool lar_solver::explanation_is_correct(explanation& explanation) const {
return true;
#if 0
// disabled: kind is uninitialized
#ifdef Z3DEBUG
lconstraint_kind kind;
lp_assert(the_left_sides_sum_to_zero(explanation));
mpq rs = sum_of_right_sides_of_explanation(explanation);
switch (kind) {
case LE: lp_assert(rs < zero_of_type<mpq>());
break;
case LT: lp_assert(rs <= zero_of_type<mpq>());
break;
case GE: lp_assert(rs > zero_of_type<mpq>());
break;
case GT: lp_assert(rs >= zero_of_type<mpq>());
break;
case EQ: lp_assert(rs != zero_of_type<mpq>());
break;
default:
UNREACHABLE();
return false;
}
#endif
#endif
return true;
}
bool lar_solver::inf_explanation_is_correct() const {
#ifdef Z3DEBUG
explanation exp;
get_infeasibility_explanation(exp);
return explanation_is_correct(exp);
#endif
return true;
}
mpq lar_solver::sum_of_right_sides_of_explanation(explanation& exp) const {
mpq ret = numeric_traits<mpq>::zero();
for (auto it : exp) {
mpq coeff = it.coeff();
constraint_index con_ind = it.ci();
lp_assert(m_constraints.valid_index(con_ind));
ret += (m_constraints[con_ind].rhs() - m_constraints[con_ind].get_free_coeff_of_left_side()) * coeff;
}
return ret;
}
bool lar_solver::has_lower_bound(lpvar var, u_dependency*& ci, mpq& value, bool& is_strict) const {
if (var >= m_columns.size()) {
// TBD: bounds on terms could also be used, caller may have to track these.
return false;
}
const column& ul = m_columns[var];
ci = ul.lower_bound_witness();
if (ci != nullptr) {
auto& p = m_mpq_lar_core_solver.m_r_lower_bounds()[var];
value = p.x;
is_strict = p.y.is_pos();
return true;
}
else {
return false;
}
}
bool lar_solver::has_upper_bound(lpvar var, u_dependency*& ci, mpq& value, bool& is_strict) const {
if (var >= m_columns.size()) {
// TBD: bounds on terms could also be used, caller may have to track these.
return false;
}
const column& ul = m_columns[var];
ci = ul.upper_bound_witness();
if (ci != nullptr) {
auto& p = m_mpq_lar_core_solver.m_r_upper_bounds()[var];
value = p.x;
is_strict = p.y.is_neg();
return true;
}
else {
return false;
}
}
bool lar_solver::has_value(lpvar var, mpq& value) const {
if (column_has_term(var)) {
lar_term const& t = get_term(var);
value = 0;
for (lar_term::ival cv : t) {
impq const& r = get_column_value(cv.j());
if (!numeric_traits<mpq>::is_zero(r.y)) return false;
value += r.x * cv.coeff();
}
return true;
}
else {
impq const& r = get_column_value(var);
value = r.x;
return numeric_traits<mpq>::is_zero(r.y);
}
}
void lar_solver::get_infeasibility_explanation(explanation& exp) const {
exp.clear();
if (m_crossed_bounds_column != null_lpvar) {
fill_explanation_from_crossed_bounds_column(exp);
return;
}
if (m_mpq_lar_core_solver.get_infeasible_sum_sign() == 0) {
return;
}
// the infeasibility sign
int inf_sign;
auto inf_row = m_mpq_lar_core_solver.get_infeasibility_info(inf_sign);
get_infeasibility_explanation_for_inf_sign(exp, inf_row, inf_sign);
lp_assert(explanation_is_correct(exp));
}
void lar_solver::get_infeasibility_explanation_for_inf_sign(
explanation& exp,
const vector<std::pair<mpq, unsigned>>& inf_row,
int inf_sign) const {
for (auto& it : inf_row) {
mpq coeff = it.first;
unsigned j = it.second;
int adj_sign = coeff.is_pos() ? inf_sign : -inf_sign;
const column& ul = m_columns[j];
u_dependency* bound_constr_i = adj_sign < 0 ? ul.upper_bound_witness() : ul.lower_bound_witness();
svector<constraint_index> deps;