forked from ERGO-Code/HiGHS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHPresolve.cpp
More file actions
6957 lines (6048 loc) · 265 KB
/
HPresolve.cpp
File metadata and controls
6957 lines (6048 loc) · 265 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the HiGHS linear optimization suite */
/* */
/* Available as open-source under the MIT License */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "presolve/HPresolve.h"
#include <algorithm>
#include <atomic>
#include <cmath>
#include <limits>
#include "../extern/pdqsort/pdqsort.h"
#include "Highs.h"
#include "io/HighsIO.h"
#include "lp_data/HConst.h"
#include "lp_data/HStruct.h"
#include "lp_data/HighsLpUtils.h"
#include "lp_data/HighsSolution.h"
#include "mip/HighsCliqueTable.h"
#include "mip/HighsImplications.h"
#include "mip/HighsMipSolverData.h"
#include "mip/HighsObjectiveFunction.h"
#include "mip/MipTimer.h"
#include "presolve/HighsPostsolveStack.h"
#include "test_kkt/DevKkt.h"
#include "util/HFactor.h"
#include "util/HighsCDouble.h"
#include "util/HighsIntegers.h"
#include "util/HighsLinearSumBounds.h"
#include "util/HighsMemoryAllocation.h"
#include "util/HighsSplay.h"
#include "util/HighsUtils.h"
#define ENABLE_SPARSIFY_FOR_LP 0
#define HPRESOLVE_CHECKED_CALL(presolveCall) \
do { \
HPresolve::Result __result = presolveCall; \
if (__result != presolve::HPresolve::Result::kOk) return __result; \
} while (0)
namespace presolve {
#ifndef NDEBUG
void HPresolve::debugPrintRow(HighsPostsolveStack& postsolve_stack,
HighsInt row) {
printf("(row %" HIGHSINT_FORMAT ") %.15g (impl: %.15g) <= ",
postsolve_stack.getOrigRowIndex(row), model->row_lower_[row],
impliedRowBounds.getSumLower(row));
for (const HighsSliceNonzero& nonzero : getSortedRowVector(row)) {
// for (HighsInt rowiter = rowhead[row]; rowiter != -1; rowiter =
// ARnext[rowiter]) {
char colchar =
model->integrality_[nonzero.index()] == HighsVarType::kInteger ? 'y'
: 'x';
char signchar = nonzero.value() < 0 ? '-' : '+';
printf("%c%g %c%" HIGHSINT_FORMAT " ", signchar, std::abs(nonzero.value()),
colchar, postsolve_stack.getOrigColIndex(nonzero.index()));
}
printf("<= %.15g (impl: %.15g)\n", model->row_upper_[row],
impliedRowBounds.getSumUpper(row));
}
#endif
bool HPresolve::okSetInput(HighsLp& model_, const HighsOptions& options_,
const HighsInt presolve_reduction_limit,
HighsTimer* timer) {
model = &model_;
options = &options_;
this->timer = timer;
if (!okResize(colLowerSource, model->num_col_, HighsInt{-1})) return false;
if (!okResize(colUpperSource, model->num_col_, HighsInt{-1})) return false;
if (!okResize(implColLower, model->num_col_, -kHighsInf)) return false;
if (!okResize(implColUpper, model->num_col_, kHighsInf)) return false;
if (!okResize(colImplSourceByRow, model->num_row_)) return false;
if (!okResize(implRowDualSourceByCol, model->num_col_)) return false;
if (!okResize(rowDualLower, model->num_row_, -kHighsInf)) return false;
if (!okResize(rowDualUpper, model->num_row_, kHighsInf)) return false;
if (!okResize(implRowDualLower, model->num_row_, -kHighsInf)) return false;
if (!okResize(implRowDualUpper, model->num_row_, kHighsInf)) return false;
if (!okResize(rowDualUpperSource, model->num_row_, HighsInt{-1}))
return false;
if (!okResize(rowDualLowerSource, model->num_row_, HighsInt{-1}))
return false;
for (HighsInt i = 0; i != model->num_row_; ++i) {
if (model->row_lower_[i] == -kHighsInf) rowDualUpper[i] = 0;
if (model->row_upper_[i] == kHighsInf) rowDualLower[i] = 0;
}
if (mipsolver == nullptr) {
primal_feastol = options->primal_feasibility_tolerance;
model->integrality_.assign(model->num_col_, HighsVarType::kContinuous);
} else
primal_feastol = options->mip_feasibility_tolerance;
if (model_.a_matrix_.isRowwise()) {
// Does this even happen?
assert(model_.a_matrix_.isColwise());
if (!okFromCSR(model->a_matrix_.value_, model->a_matrix_.index_,
model->a_matrix_.start_))
return false;
} else {
if (!okFromCSC(model->a_matrix_.value_, model->a_matrix_.index_,
model->a_matrix_.start_))
return false;
}
// initialize everything as changed, but do not add all indices
// since the first thing presolve will do is a scan for easy reductions
// of each row and column and set the flag of processed columns to false
// from then on they are added to the vector whenever there are changes
if (!okResize(changedRowFlag, model->num_row_, uint8_t{1})) return false;
if (!okResize(rowDeleted, model->num_row_)) return false;
if (!okReserve(changedRowIndices, model->num_row_)) return false;
if (!okResize(changedColFlag, model->num_col_, uint8_t{1})) return false;
if (!okResize(colDeleted, model->num_col_)) return false;
if (!okReserve(changedColIndices, model->num_col_)) return false;
if (!okReserve(liftingOpportunities, model->num_row_)) return false;
numDeletedCols = 0;
numDeletedRows = 0;
// initialize substitution opportunities
for (HighsInt row = 0; row != model->num_row_; ++row) {
if (!isDualImpliedFree(row)) continue;
for (const HighsSliceNonzero& nonzero : getRowVector(row)) {
if (isImpliedFree(nonzero.index()))
substitutionOpportunities.emplace_back(row, nonzero.index());
}
}
// Take value passed in as reduction limit, allowing different
// values to be used for initial presolve, and after restart
reductionLimit =
presolve_reduction_limit < 0 ? kHighsSize_tInf : presolve_reduction_limit;
if (options->presolve != kHighsOffString &&
reductionLimit < kHighsSize_tInf) {
highsLogDev(options->log_options, HighsLogType::kInfo,
"HPresolve::okSetInput reductionLimit = %d\n",
static_cast<int>(reductionLimit));
}
return true;
}
// for MIP presolve
bool HPresolve::okSetInput(HighsMipSolver& mipsolver,
const HighsInt presolve_reduction_limit) {
this->mipsolver = &mipsolver;
probingContingent = 1000;
probingNumDelCol = 0;
numProbed = 0;
numProbes.assign(mipsolver.numCol(), 0);
if (mipsolver.model_ != &mipsolver.mipdata_->presolvedModel) {
mipsolver.mipdata_->presolvedModel = *mipsolver.model_;
mipsolver.model_ = &mipsolver.mipdata_->presolvedModel;
} else {
mipsolver.mipdata_->presolvedModel.col_lower_ =
mipsolver.mipdata_->domain.col_lower_;
mipsolver.mipdata_->presolvedModel.col_upper_ =
mipsolver.mipdata_->domain.col_upper_;
}
return okSetInput(mipsolver.mipdata_->presolvedModel, *mipsolver.options_mip_,
presolve_reduction_limit, &mipsolver.timer_);
}
bool HPresolve::rowCoefficientsIntegral(HighsInt row, double scale) const {
for (const HighsSliceNonzero& nz : getRowVector(row)) {
if (fractionality(nz.value() * scale) > options->small_matrix_value)
return false;
}
return true;
}
bool HPresolve::isLowerImplied(HighsInt col) const {
return (model->col_lower_[col] == -kHighsInf ||
implColLower[col] >= model->col_lower_[col] - primal_feastol);
}
bool HPresolve::isLowerStrictlyImplied(HighsInt col, double* tolerance) const {
return (model->col_lower_[col] == -kHighsInf ||
implColLower[col] >
model->col_lower_[col] +
(tolerance != nullptr ? *tolerance : primal_feastol));
}
bool HPresolve::isUpperImplied(HighsInt col) const {
return (model->col_upper_[col] == kHighsInf ||
implColUpper[col] <= model->col_upper_[col] + primal_feastol);
}
bool HPresolve::isUpperStrictlyImplied(HighsInt col, double* tolerance) const {
return (model->col_upper_[col] == kHighsInf ||
implColUpper[col] <
model->col_upper_[col] -
(tolerance != nullptr ? *tolerance : primal_feastol));
}
bool HPresolve::isImpliedFree(HighsInt col) const {
return isLowerImplied(col) && isUpperImplied(col);
}
bool HPresolve::isDualImpliedFree(HighsInt row) const {
return isEquation(row) ||
(model->row_upper_[row] != kHighsInf &&
implRowDualUpper[row] <= options->dual_feasibility_tolerance) ||
(model->row_lower_[row] != -kHighsInf &&
implRowDualLower[row] >= -options->dual_feasibility_tolerance);
}
void HPresolve::dualImpliedFreeGetRhsAndRowType(
HighsInt row, double& rhs, HighsPostsolveStack::RowType& rowType,
bool relaxRowDualBounds) {
assert(isDualImpliedFree(row));
if (isEquation(row)) {
rowType = HighsPostsolveStack::RowType::kEq;
rhs = model->row_upper_[row];
} else if (model->row_upper_[row] != kHighsInf &&
implRowDualUpper[row] <= options->dual_feasibility_tolerance) {
rowType = HighsPostsolveStack::RowType::kLeq;
rhs = model->row_upper_[row];
if (relaxRowDualBounds) changeRowDualUpper(row, kHighsInf);
} else {
rowType = HighsPostsolveStack::RowType::kGeq;
rhs = model->row_lower_[row];
if (relaxRowDualBounds) changeRowDualLower(row, -kHighsInf);
}
}
bool HPresolve::isEquation(HighsInt row) const {
return (model->row_lower_[row] == model->row_upper_[row]);
}
bool HPresolve::isImpliedEquationAtLower(HighsInt row) const {
// if the implied lower bound on a row dual is strictly positive then the row
// is an implied equation (using its lower bound) due to complementary
// slackness
bool isLbndPositive =
implRowDualLower[row] > options->dual_feasibility_tolerance;
assert(!isLbndPositive || model->row_lower_[row] != -kHighsInf);
return isLbndPositive;
}
bool HPresolve::isImpliedEquationAtUpper(HighsInt row) const {
// if the implied upper bound on a row dual is strictly negative then the row
// is an implied equation (using its upper bound) due to complementary
// slackness
bool isUbndNegative =
implRowDualUpper[row] < -options->dual_feasibility_tolerance;
assert(!isUbndNegative || model->row_upper_[row] != kHighsInf);
return isUbndNegative;
}
HPresolve::StatusResult HPresolve::isImpliedIntegral(HighsInt col) {
// check if the integer constraint on a variable is implied by the model
assert(model->integrality_[col] == HighsVarType::kInteger);
bool runDualDetection = true;
for (const HighsSliceNonzero& nz : getColumnVector(col)) {
// if not all other columns are integer, skip row and also do not try the
// dual detection in the second loop as it must hold for all rows
if (rowsize[nz.index()] < 2 ||
rowsizeInteger[nz.index()] < rowsize[nz.index()]) {
runDualDetection = false;
continue;
}
double rowLower = isImpliedEquationAtUpper(nz.index())
? model->row_upper_[nz.index()]
: model->row_lower_[nz.index()];
double rowUpper = isImpliedEquationAtLower(nz.index())
? model->row_lower_[nz.index()]
: model->row_upper_[nz.index()];
if (rowUpper == rowLower) {
// if there is an equation the dual detection does not need to be tried
runDualDetection = false;
double scale = 1.0 / nz.value();
if (!rowCoefficientsIntegral(nz.index(), scale)) continue;
if (fractionality(rowLower * scale) > primal_feastol)
return StatusResult(Result::kPrimalInfeasible);
return StatusResult(true);
}
}
if (!runDualDetection) return StatusResult(false);
for (const HighsSliceNonzero& nz : getColumnVector(col)) {
double scale = 1.0 / nz.value();
// if row coefficients are not integral, variable is not (implied) integral
if (!rowCoefficientsIntegral(nz.index(), scale)) return StatusResult(false);
if (model->row_upper_[nz.index()] != kHighsInf) {
// right-hand side: scale, round down and unscale again
double rUpper =
std::abs(nz.value()) *
std::floor(model->row_upper_[nz.index()] * std::abs(scale) +
primal_feastol);
// check if modification is large enough
if (std::abs(model->row_upper_[nz.index()] - rUpper) >
options->small_matrix_value) {
// update right-hand side and mark row as changed
model->row_upper_[nz.index()] = rUpper;
markChangedRow(nz.index());
}
}
if (model->row_lower_[nz.index()] != -kHighsInf) {
// left-hand side: scale, round up and unscale again
double rLower =
std::abs(nz.value()) *
std::ceil(model->row_lower_[nz.index()] * std::abs(scale) -
primal_feastol);
// check if modification is large enough
if (std::abs(model->row_lower_[nz.index()] - rLower) >
options->small_matrix_value) {
// update left-hand side and mark row as changed
model->row_lower_[nz.index()] = rLower;
markChangedRow(nz.index());
}
}
}
return StatusResult(true);
}
HPresolve::StatusResult HPresolve::isImpliedInteger(HighsInt col) const {
// check if a continuous variable is implied integer
assert(model->integrality_[col] == HighsVarType::kContinuous);
bool runDualDetection = true;
for (const HighsSliceNonzero& nz : getColumnVector(col)) {
// if not all other columns are integer, skip row and also do not try the
// dual detection in the second loop as it must hold for all rows
if (rowsize[nz.index()] < 2 ||
rowsizeInteger[nz.index()] + rowsizeImplInt[nz.index()] <
rowsize[nz.index()] - 1) {
runDualDetection = false;
continue;
}
double rowLower = isImpliedEquationAtUpper(nz.index())
? model->row_upper_[nz.index()]
: model->row_lower_[nz.index()];
double rowUpper = isImpliedEquationAtLower(nz.index())
? model->row_lower_[nz.index()]
: model->row_upper_[nz.index()];
if (rowUpper == rowLower) {
// if there is an equation the dual detection does not need to be tried
runDualDetection = false;
double scale = 1.0 / nz.value();
if (fractionality(rowLower * scale) > primal_feastol) continue;
if (!rowCoefficientsIntegral(nz.index(), scale)) continue;
return StatusResult(true);
}
}
if (!runDualDetection) return StatusResult(false);
if ((model->col_lower_[col] != -kHighsInf &&
fractionality(model->col_lower_[col]) > options->small_matrix_value) ||
(model->col_upper_[col] != kHighsInf &&
fractionality(model->col_upper_[col]) > options->small_matrix_value))
return StatusResult(false);
for (const HighsSliceNonzero& nz : getColumnVector(col)) {
double scale = 1.0 / nz.value();
if (model->row_upper_[nz.index()] != kHighsInf &&
fractionality(model->row_upper_[nz.index()] * scale) > primal_feastol)
return StatusResult(false);
if (model->row_lower_[nz.index()] != -kHighsInf &&
fractionality(model->row_lower_[nz.index()] * scale) > primal_feastol)
return StatusResult(false);
if (!rowCoefficientsIntegral(nz.index(), scale)) return StatusResult(false);
}
return StatusResult(true);
}
HPresolve::StatusResult HPresolve::convertImpliedInteger(HighsInt col,
HighsInt row,
bool skipInputChecks) {
// return if column was deleted
if (colDeleted[col]) return StatusResult(false);
// return if column is not continuous or cannot be converted to an implied
// integer
if (!skipInputChecks) {
if (model->integrality_[col] != HighsVarType::kContinuous)
return StatusResult(false);
StatusResult impliedInteger = isImpliedInteger(col);
if (!impliedInteger) return impliedInteger;
}
// convert to implied integer
model->integrality_[col] = HighsVarType::kImplicitInteger;
if (row != -1) {
// use row index supplied by caller (e.g. singleton)
++rowsizeImplInt[row];
} else {
// iterate over rows
for (const HighsSliceNonzero& nonzero : getColumnVector(col))
++rowsizeImplInt[nonzero.index()];
}
// round and update bounds
changeColLower(col, model->col_lower_[col]);
changeColUpper(col, model->col_upper_[col]);
return StatusResult(true);
}
void HPresolve::link(HighsInt pos) {
Anext[pos] = colhead[Acol[pos]];
Aprev[pos] = -1;
colhead[Acol[pos]] = pos;
if (Anext[pos] != -1) Aprev[Anext[pos]] = pos;
++colsize[Acol[pos]];
ARleft[pos] = -1;
ARright[pos] = -1;
auto get_row_left = [&](HighsInt pos) -> HighsInt& { return ARleft[pos]; };
auto get_row_right = [&](HighsInt pos) -> HighsInt& { return ARright[pos]; };
auto get_row_key = [&](HighsInt pos) { return Acol[pos]; };
highs_splay_link(pos, rowroot[Arow[pos]], get_row_left, get_row_right,
get_row_key);
impliedRowBounds.add(Arow[pos], Acol[pos], Avalue[pos]);
impliedDualRowBounds.add(Acol[pos], Arow[pos], Avalue[pos]);
++rowsize[Arow[pos]];
if (model->integrality_[Acol[pos]] == HighsVarType::kInteger)
++rowsizeInteger[Arow[pos]];
else if (model->integrality_[Acol[pos]] == HighsVarType::kImplicitInteger)
++rowsizeImplInt[Arow[pos]];
}
void HPresolve::unlink(HighsInt pos) {
HighsInt next = Anext[pos];
HighsInt prev = Aprev[pos];
if (next != -1) Aprev[next] = prev;
if (prev != -1)
Anext[prev] = next;
else
colhead[Acol[pos]] = next;
--colsize[Acol[pos]];
if (!colDeleted[Acol[pos]]) {
if (colsize[Acol[pos]] == 1)
singletonColumns.push_back(Acol[pos]);
else
markChangedCol(Acol[pos]);
impliedDualRowBounds.remove(Acol[pos], Arow[pos], Avalue[pos]);
}
auto get_row_left = [&](HighsInt pos) -> HighsInt& { return ARleft[pos]; };
auto get_row_right = [&](HighsInt pos) -> HighsInt& { return ARright[pos]; };
auto get_row_key = [&](HighsInt pos) { return Acol[pos]; };
highs_splay_unlink(pos, rowroot[Arow[pos]], get_row_left, get_row_right,
get_row_key);
--rowsize[Arow[pos]];
if (model->integrality_[Acol[pos]] == HighsVarType::kInteger)
--rowsizeInteger[Arow[pos]];
else if (model->integrality_[Acol[pos]] == HighsVarType::kImplicitInteger)
--rowsizeImplInt[Arow[pos]];
if (!rowDeleted[Arow[pos]]) {
if (rowsize[Arow[pos]] == 1)
singletonRows.push_back(Arow[pos]);
else
markChangedRow(Arow[pos]);
impliedRowBounds.remove(Arow[pos], Acol[pos], Avalue[pos]);
}
// remove implied bounds on row duals that where implied by this column's dual
// constraint
resetRowDualImpliedBoundsDerivedFromCol(Acol[pos]);
// remove implied bounds on columns that where implied by this row
resetColImpliedBoundsDerivedFromRow(Arow[pos]);
// modifications to row invalidate lifting opportunities
clearLiftingOpportunities(Arow[pos]);
// remove non-zero
Avalue[pos] = 0;
freeslots.push_back(pos);
}
void HPresolve::markChangedRow(HighsInt row) {
if (!changedRowFlag[row]) {
changedRowIndices.push_back(row);
changedRowFlag[row] = true;
}
}
void HPresolve::markChangedCol(HighsInt col) {
if (!changedColFlag[col]) {
changedColIndices.push_back(col);
changedColFlag[col] = true;
}
}
double HPresolve::getMaxAbsColVal(HighsInt col) const {
double maxVal = 0.0;
for (const auto& nz : getColumnVector(col))
maxVal = std::max(std::abs(nz.value()), maxVal);
return maxVal;
}
double HPresolve::getMaxAbsRowVal(HighsInt row) const {
double maxVal = 0.0;
for (const auto& nz : getRowVector(row))
maxVal = std::max(std::abs(nz.value()), maxVal);
return maxVal;
}
bool HPresolve::checkUpdateRowDualImpliedBounds(HighsInt col,
double* dualRowLower,
double* dualRowUpper) const {
// check if implied bounds of row duals in given column can be updated (i.e.
// dual row has finite bounds and number of infinite contributions to
// corresponding activity bounds is at most one)
// if the column has an infinite lower bound the reduced cost cannot be
// positive, i.e. the column corresponds to a <= constraint in the dual with
// right hand side -cost which becomes a >= constraint with side +cost.
// Furthermore, we can ignore strictly redundant primal
// column bounds and treat them as if they are infinite
double impliedMargin = colsize[col] != 1 ? primal_feastol : -primal_feastol;
double myDualRowLower = isLowerStrictlyImplied(col, &impliedMargin)
? model->col_cost_[col]
: -kHighsInf;
double myDualRowUpper = isUpperStrictlyImplied(col, &impliedMargin)
? model->col_cost_[col]
: kHighsInf;
if (dualRowLower != nullptr) *dualRowLower = myDualRowLower;
if (dualRowUpper != nullptr) *dualRowUpper = myDualRowUpper;
return (myDualRowLower != -kHighsInf &&
impliedDualRowBounds.getNumInfSumUpperOrig(col) <= 1) ||
(myDualRowUpper != kHighsInf &&
impliedDualRowBounds.getNumInfSumLowerOrig(col) <= 1);
}
void HPresolve::updateRowDualImpliedBounds(HighsInt row, HighsInt col,
double val) {
// propagate implied row dual bound
double dualRowLower, dualRowUpper;
if (!checkUpdateRowDualImpliedBounds(col, &dualRowLower, &dualRowUpper))
return;
const double threshold = 1000 * options->dual_feasibility_tolerance;
auto checkImpliedBound = [&](HighsInt row, HighsInt col, double val,
double dualRowBnd, double residualAct,
HighsInt direction) {
if (direction * residualAct <= -kHighsInf) return;
double impliedBound = static_cast<double>(
(static_cast<HighsCDouble>(dualRowBnd) - residualAct) / val);
if (std::abs(impliedBound) * kHighsTiny >
options->dual_feasibility_tolerance)
return;
if (direction * val > 0) {
// only tighten bound if it is tighter by a wide enough margin
if (impliedBound < implRowDualUpper[row] - threshold)
changeImplRowDualUpper(row, impliedBound, col);
} else {
if (impliedBound > implRowDualLower[row] + threshold)
changeImplRowDualLower(row, impliedBound, col);
}
};
if (dualRowUpper != kHighsInf)
checkImpliedBound(
row, col, val, dualRowUpper,
impliedDualRowBounds.getResidualSumLowerOrig(col, row, val),
HighsInt{1});
if (dualRowLower != -kHighsInf)
checkImpliedBound(
row, col, val, dualRowLower,
impliedDualRowBounds.getResidualSumUpperOrig(col, row, val),
HighsInt{-1});
}
void HPresolve::updateRowDualImpliedBounds(HighsInt col) {
// update dual implied bounds of all rows in given column
assert(col >= 0 && col < model->num_col_);
if (!checkUpdateRowDualImpliedBounds(col)) return;
for (const HighsSliceNonzero& nonzero : getColumnVector(col))
updateRowDualImpliedBounds(nonzero.index(), col, nonzero.value());
}
bool HPresolve::checkUpdateColImpliedBounds(HighsInt row, double* rowLower,
double* rowUpper) const {
// check if implied bounds of columns in given row can be updated (i.e. if
// row's left-hand or right-hand side is finite and number of infinite
// contributions to corresponding activity bounds is at most one)
double myRowLower = isImpliedEquationAtUpper(row) ? model->row_upper_[row]
: model->row_lower_[row];
double myRowUpper = isImpliedEquationAtLower(row) ? model->row_lower_[row]
: model->row_upper_[row];
assert(myRowLower != kHighsInf);
assert(myRowUpper != -kHighsInf);
if (rowLower != nullptr) *rowLower = myRowLower;
if (rowUpper != nullptr) *rowUpper = myRowUpper;
return (myRowLower != -kHighsInf &&
impliedRowBounds.getNumInfSumUpperOrig(row) <= 1) ||
(myRowUpper != kHighsInf &&
impliedRowBounds.getNumInfSumLowerOrig(row) <= 1);
}
void HPresolve::updateColImpliedBounds(HighsInt row, HighsInt col, double val) {
// propagate implied column bound upper bound if row has an upper bound
double rowLower, rowUpper;
if (!checkUpdateColImpliedBounds(row, &rowLower, &rowUpper)) return;
const double threshold = 1000 * primal_feastol;
auto checkImpliedBound = [&](HighsInt row, HighsInt col, double val,
double rowBnd, double residualAct,
HighsInt direction) {
if (direction * residualAct <= -kHighsInf) return;
double impliedBound = static_cast<double>(
(static_cast<HighsCDouble>(rowBnd) - residualAct) / val);
if (std::abs(impliedBound) * kHighsTiny > primal_feastol) return;
if (direction * val > 0) {
// bound is an upper bound
if (mipsolver != nullptr) {
// solving a MIP; keep tighter bounds on integer variables
if (model->integrality_[col] != HighsVarType::kContinuous &&
impliedBound < model->col_upper_[col] - primal_feastol)
changeColUpper(col, impliedBound);
// do not use the implied bound if this a not a model row, since the
// row can be removed and should not be used, e.g., to identify a
// column as implied free
if (mipsolver->mipdata_->postSolveStack.getOrigRowIndex(row) >=
mipsolver->orig_model_->num_row_) {
// keep implied bound (as column bound)
if (impliedBound < model->col_upper_[col] - threshold)
changeColUpper(col, impliedBound);
// set to +infinity, so that it is not stored as an implied bound
impliedBound = kHighsInf;
}
}
// only tighten bound if it is tighter by a wide enough margin
if (impliedBound < implColUpper[col] - threshold)
changeImplColUpper(col, impliedBound, row);
} else {
// bound is a lower bound
if (mipsolver != nullptr) {
// solving a MIP; keep tighter bounds on integer variables
if (model->integrality_[col] != HighsVarType::kContinuous &&
impliedBound > model->col_lower_[col] + primal_feastol)
changeColLower(col, impliedBound);
// do not use the implied bound if this a not a model row, since the
// row can be removed and should not be used, e.g., to identify a
// column as implied free
if (mipsolver->mipdata_->postSolveStack.getOrigRowIndex(row) >=
mipsolver->orig_model_->num_row_) {
// keep implied bound (as column bound)
if (impliedBound > model->col_lower_[col] + threshold)
changeColLower(col, impliedBound);
// set to -infinity, so that it is not stored as an implied bound
impliedBound = -kHighsInf;
}
}
// only tighten bound if it is tighter by a wide enough margin
if (impliedBound > implColLower[col] + threshold)
changeImplColLower(col, impliedBound, row);
}
};
if (rowUpper != kHighsInf)
checkImpliedBound(row, col, val, rowUpper,
impliedRowBounds.getResidualSumLowerOrig(row, col, val),
HighsInt{1});
if (rowLower != -kHighsInf)
checkImpliedBound(row, col, val, rowLower,
impliedRowBounds.getResidualSumUpperOrig(row, col, val),
HighsInt{-1});
}
void HPresolve::updateColImpliedBounds(HighsInt row) {
// update implied bounds of all columns in given row
assert(row >= 0 && row < model->num_row_);
if (!checkUpdateColImpliedBounds(row)) return;
for (const HighsSliceNonzero& nonzero : getRowVector(row))
updateColImpliedBounds(row, nonzero.index(), nonzero.value());
}
void HPresolve::resetColImpliedBounds(HighsInt col, HighsInt row) {
assert(row == -1 || colLowerSource[col] == row || colUpperSource[col] == row);
if (!colDeleted[col]) {
// set implied bounds to infinite values if (1) they were deduced from the
// given row or (2) no row was given
if (colLowerSource[col] != -1 && (row == -1 || colLowerSource[col] == row))
changeImplColLower(col, -kHighsInf, -1);
if (colUpperSource[col] != -1 && (row == -1 || colUpperSource[col] == row))
changeImplColUpper(col, kHighsInf, -1);
} else if (row != -1 && !rowDeleted[row]) {
// remove column from row-wise implied bound storage
colImplSourceByRow[row].erase(col);
}
}
void HPresolve::resetRowDualImpliedBounds(HighsInt row, HighsInt col) {
assert(col == -1 || rowDualLowerSource[row] == col ||
rowDualUpperSource[row] == col);
if (!rowDeleted[row]) {
// set implied bounds to infinite values if (1) they were deduced from the
// given column or (2) no column was given
if (rowDualLowerSource[row] != -1 &&
(col == -1 || rowDualLowerSource[row] == col))
changeImplRowDualLower(row, -kHighsInf, -1);
if (rowDualUpperSource[row] != -1 &&
(col == -1 || rowDualUpperSource[row] == col))
changeImplRowDualUpper(row, kHighsInf, -1);
} else if (col != -1 && !colDeleted[col]) {
// remove row from column-wise implied bound storage
implRowDualSourceByCol[col].erase(row);
}
}
void HPresolve::resetColImpliedBoundsDerivedFromRow(HighsInt row) {
// reset implied column bounds affected by a modification in a row
// (removed / added non-zeros, etc.)
if (colImplSourceByRow[row].empty()) return;
std::set<HighsInt> affectedCols(colImplSourceByRow[row]);
for (const HighsInt& col : affectedCols) {
// set implied bounds to infinite values if they were deduced from the
// given row
resetColImpliedBounds(col, row);
}
}
void HPresolve::resetRowDualImpliedBoundsDerivedFromCol(HighsInt col) {
// reset implied row dual bounds affected by a modification in a column
// (removed / added non-zeros, etc.)
if (implRowDualSourceByCol[col].empty()) return;
std::set<HighsInt> affectedRows(implRowDualSourceByCol[col]);
for (const HighsInt& row : affectedRows) {
// set implied bounds to infinite values if they were deduced from the
// given column
resetRowDualImpliedBounds(row, col);
}
}
HighsInt HPresolve::findNonzero(HighsInt row, HighsInt col) {
if (rowroot[row] == -1) return -1;
auto get_row_left = [&](HighsInt pos) -> HighsInt& { return ARleft[pos]; };
auto get_row_right = [&](HighsInt pos) -> HighsInt& { return ARright[pos]; };
auto get_row_key = [&](HighsInt pos) { return Acol[pos]; };
rowroot[row] =
highs_splay(col, rowroot[row], get_row_left, get_row_right, get_row_key);
if (Acol[rowroot[row]] == col) return rowroot[row];
return -1;
}
void HPresolve::shrinkProblem(HighsPostsolveStack& postsolve_stack) {
HighsInt oldNumCol = model->num_col_;
model->num_col_ = 0;
std::vector<HighsInt> newColIndex(oldNumCol);
const bool have_col_names = model->col_names_.size() > 0;
assert(!have_col_names ||
model->col_names_.size() == static_cast<size_t>(oldNumCol));
for (HighsInt i = 0; i != oldNumCol; ++i) {
if (colDeleted[i])
newColIndex[i] = -1;
else {
newColIndex[i] = model->num_col_++;
if (newColIndex[i] < i) {
model->col_cost_[newColIndex[i]] = model->col_cost_[i];
model->col_lower_[newColIndex[i]] = model->col_lower_[i];
model->col_upper_[newColIndex[i]] = model->col_upper_[i];
assert(!std::isnan(model->col_lower_[newColIndex[i]]));
assert(!std::isnan(model->col_upper_[newColIndex[i]]));
model->integrality_[newColIndex[i]] = model->integrality_[i];
implColLower[newColIndex[i]] = implColLower[i];
implColUpper[newColIndex[i]] = implColUpper[i];
colLowerSource[newColIndex[i]] = colLowerSource[i];
colUpperSource[newColIndex[i]] = colUpperSource[i];
implRowDualSourceByCol[newColIndex[i]] = implRowDualSourceByCol[i];
colhead[newColIndex[i]] = colhead[i];
colsize[newColIndex[i]] = colsize[i];
if (have_col_names)
model->col_names_[newColIndex[i]] = std::move(model->col_names_[i]);
changedColFlag[newColIndex[i]] = changedColFlag[i];
}
}
}
colDeleted.assign(model->num_col_, false);
model->col_cost_.resize(model->num_col_);
model->col_lower_.resize(model->num_col_);
model->col_upper_.resize(model->num_col_);
model->integrality_.resize(model->num_col_);
implColLower.resize(model->num_col_);
implColUpper.resize(model->num_col_);
colLowerSource.resize(model->num_col_);
colUpperSource.resize(model->num_col_);
implRowDualSourceByCol.resize(model->num_col_);
colhead.resize(model->num_col_);
colsize.resize(model->num_col_);
if (have_col_names) model->col_names_.resize(model->num_col_);
changedColFlag.resize(model->num_col_);
numDeletedCols = 0;
HighsInt oldNumRow = model->num_row_;
const bool have_row_names = model->row_names_.size() > 0;
assert(!have_row_names ||
model->row_names_.size() == static_cast<size_t>(oldNumRow));
model->num_row_ = 0;
std::vector<HighsInt> newRowIndex(oldNumRow);
for (HighsInt i = 0; i != oldNumRow; ++i) {
if (rowDeleted[i])
newRowIndex[i] = -1;
else {
newRowIndex[i] = model->num_row_++;
if (newRowIndex[i] < i) {
model->row_lower_[newRowIndex[i]] = model->row_lower_[i];
model->row_upper_[newRowIndex[i]] = model->row_upper_[i];
assert(!std::isnan(model->row_lower_[newRowIndex[i]]));
assert(!std::isnan(model->row_upper_[newRowIndex[i]]));
rowDualLower[newRowIndex[i]] = rowDualLower[i];
rowDualUpper[newRowIndex[i]] = rowDualUpper[i];
implRowDualLower[newRowIndex[i]] = implRowDualLower[i];
implRowDualUpper[newRowIndex[i]] = implRowDualUpper[i];
rowDualLowerSource[newRowIndex[i]] = rowDualLowerSource[i];
rowDualUpperSource[newRowIndex[i]] = rowDualUpperSource[i];
colImplSourceByRow[newRowIndex[i]] = colImplSourceByRow[i];
rowroot[newRowIndex[i]] = rowroot[i];
rowsize[newRowIndex[i]] = rowsize[i];
rowsizeInteger[newRowIndex[i]] = rowsizeInteger[i];
rowsizeImplInt[newRowIndex[i]] = rowsizeImplInt[i];
if (have_row_names)
model->row_names_[newRowIndex[i]] = std::move(model->row_names_[i]);
changedRowFlag[newRowIndex[i]] = changedRowFlag[i];
}
}
}
for (HighsInt i = 0; i != model->num_col_; ++i) {
if (colLowerSource[i] != -1)
colLowerSource[i] = newRowIndex[colLowerSource[i]];
if (colUpperSource[i] != -1)
colUpperSource[i] = newRowIndex[colUpperSource[i]];
}
for (HighsInt i = 0; i != model->num_row_; ++i) {
if (rowDualLowerSource[i] != -1)
rowDualLowerSource[i] = newColIndex[rowDualLowerSource[i]];
if (rowDualUpperSource[i] != -1)
rowDualUpperSource[i] = newColIndex[rowDualUpperSource[i]];
}
for (HighsInt i = 0; i != model->num_col_; ++i) {
std::set<HighsInt> newSet;
std::for_each(implRowDualSourceByCol[i].cbegin(),
implRowDualSourceByCol[i].cend(), [&](const HighsInt& row) {
if (newRowIndex[row] != -1)
newSet.emplace(newRowIndex[row]);
});
implRowDualSourceByCol[i] = std::move(newSet);
}
for (HighsInt i = 0; i != model->num_row_; ++i) {
std::set<HighsInt> newSet;
std::for_each(colImplSourceByRow[i].cbegin(), colImplSourceByRow[i].cend(),
[&](const HighsInt& col) {
if (newColIndex[col] != -1)
newSet.emplace(newColIndex[col]);
});
colImplSourceByRow[i] = std::move(newSet);
}
rowDeleted.assign(model->num_row_, false);
model->row_lower_.resize(model->num_row_);
model->row_upper_.resize(model->num_row_);
rowDualLower.resize(model->num_row_);
rowDualUpper.resize(model->num_row_);
implRowDualLower.resize(model->num_row_);
implRowDualUpper.resize(model->num_row_);
rowDualLowerSource.resize(model->num_row_);
rowDualUpperSource.resize(model->num_row_);
colImplSourceByRow.resize(model->num_row_);
rowroot.resize(model->num_row_);
rowsize.resize(model->num_row_);
rowsizeInteger.resize(model->num_row_);
rowsizeImplInt.resize(model->num_row_);
if (have_row_names) model->row_names_.resize(model->num_row_);
changedRowFlag.resize(model->num_row_);
numDeletedRows = 0;
postsolve_stack.compressIndexMaps(newRowIndex, newColIndex);
impliedRowBounds.shrink(newRowIndex, model->num_row_);
impliedDualRowBounds.shrink(newColIndex, model->num_col_);
HighsInt numNnz = Avalue.size();
for (HighsInt i = 0; i != numNnz; ++i) {
if (Avalue[i] == 0) continue;
assert(newColIndex[Acol[i]] != -1);
assert(newRowIndex[Arow[i]] != -1);
Acol[i] = newColIndex[Acol[i]];
Arow[i] = newRowIndex[Arow[i]];
}
// update index sets
for (HighsInt& singCol : singletonColumns) singCol = newColIndex[singCol];
singletonColumns.erase(
std::remove(singletonColumns.begin(), singletonColumns.end(), -1),
singletonColumns.end());
for (HighsInt& chgCol : changedColIndices) chgCol = newColIndex[chgCol];
changedColIndices.erase(
std::remove(changedColIndices.begin(), changedColIndices.end(), -1),
changedColIndices.end());
for (HighsInt& singRow : singletonRows) singRow = newRowIndex[singRow];
singletonRows.erase(
std::remove(singletonRows.begin(), singletonRows.end(), -1),
singletonRows.end());
for (HighsInt& chgRow : changedRowIndices) chgRow = newRowIndex[chgRow];
changedRowIndices.erase(
std::remove(changedRowIndices.begin(), changedRowIndices.end(), -1),
changedRowIndices.end());
for (auto& rowColPair : substitutionOpportunities) {
// skip deleted elements
if (rowColPair.first == -1) continue;
rowColPair.first = newRowIndex[rowColPair.first];
rowColPair.second = newColIndex[rowColPair.second];
}
substitutionOpportunities.erase(
std::remove_if(substitutionOpportunities.begin(),
substitutionOpportunities.end(),
[&](const std::pair<HighsInt, HighsInt>& p) {
return p.first == -1 || p.second == -1;
}),
substitutionOpportunities.end());
// todo remove equation set and replace with a vector of doubleton eqs
equations.clear();
eqiters.assign(model->num_row_, equations.end());
for (HighsInt i = 0; i != model->num_row_; ++i) {
if (isEquation(i)) eqiters[i] = equations.emplace(rowsize[i], i).first;
}
if (mipsolver != nullptr) {
mipsolver->mipdata_->rowMatrixSet = false;
mipsolver->mipdata_->objectiveFunction = HighsObjectiveFunction(*mipsolver);
mipsolver->mipdata_->domain = HighsDomain(*mipsolver);
mipsolver->mipdata_->cliquetable.rebuild(model->num_col_, postsolve_stack,
mipsolver->mipdata_->domain,
newColIndex, newRowIndex);
mipsolver->mipdata_->implications.rebuild(model->num_col_, newColIndex,
newRowIndex);
mipsolver->mipdata_->cutpool =
HighsCutPool(mipsolver->model_->num_col_,