forked from ERGO-Code/HiGHS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighsLpRelaxation.cpp
More file actions
1611 lines (1389 loc) · 55.3 KB
/
HighsLpRelaxation.cpp
File metadata and controls
1611 lines (1389 loc) · 55.3 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 "mip/HighsLpRelaxation.h"
#include <algorithm>
#include "lp_data/HighsSolve.h" // For useIpm()
#include "mip/HighsCutPool.h"
#include "mip/HighsDomain.h"
#include "mip/HighsMipSolver.h"
#include "mip/HighsMipSolverData.h"
#include "mip/HighsPseudocost.h"
#include "mip/MipTimer.h"
#include "util/HighsCDouble.h"
#include "util/HighsHash.h"
void HighsLpRelaxation::getCutPool(HighsInt& num_col, HighsInt& num_cut,
std::vector<double>& cut_lower,
std::vector<double>& cut_upper,
HighsSparseMatrix& cut_matrix) const {
// NB RESTORE reference
// const HighsLp& lp = lpsolver.getLp();
HighsLp lp = lpsolver.getLp();
num_col = lp.num_col_;
HighsInt num_lp_row = lp.num_row_;
HighsInt num_model_row = mipsolver.numRow();
num_cut = num_lp_row - num_model_row;
cut_lower.resize(num_cut);
cut_upper.resize(num_cut);
// Get a map from row index to cut row index
std::vector<HighsInt> cut_row_index;
cut_row_index.assign(num_lp_row, -1);
HighsInt cut_num = 0;
for (HighsInt iRow = 0; iRow < lp.num_row_; iRow++) {
if (lprows[iRow].origin != LpRow::Origin::kCutPool) continue;
cut_row_index[iRow] = cut_num;
cut_lower[cut_num] = lp.row_lower_[iRow];
cut_upper[cut_num] = lp.row_upper_[iRow];
cut_num++;
}
assert(cut_num == num_cut);
cut_matrix.num_col_ = lp.num_col_;
cut_matrix.num_row_ = num_cut;
cut_matrix.format_ = MatrixFormat::kRowwise;
std::vector<HighsInt> cut_matrix_length;
cut_matrix_length.assign(num_cut, 0);
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++) {
for (HighsInt iEl = lp.a_matrix_.start_[iCol];
iEl < lp.a_matrix_.start_[iCol + 1]; iEl++) {
HighsInt iCut = cut_row_index[lp.a_matrix_.index_[iEl]];
if (iCut >= 0) cut_matrix_length[iCut]++;
}
}
cut_matrix.start_.resize(num_cut + 1);
cut_matrix.start_[0] = 0;
HighsInt num_cut_nz = 0;
for (HighsInt iCut = 0; iCut < num_cut; iCut++) {
HighsInt length = cut_matrix_length[iCut];
cut_matrix_length[iCut] = cut_matrix.start_[iCut];
num_cut_nz += length;
cut_matrix.start_[iCut + 1] = num_cut_nz;
}
cut_matrix.index_.resize(num_cut_nz);
cut_matrix.value_.resize(num_cut_nz);
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++) {
for (HighsInt iEl = lp.a_matrix_.start_[iCol];
iEl < lp.a_matrix_.start_[iCol + 1]; iEl++) {
HighsInt iCut = cut_row_index[lp.a_matrix_.index_[iEl]];
if (iCut >= 0) {
cut_matrix.index_[cut_matrix_length[iCut]] = iCol;
cut_matrix.value_[cut_matrix_length[iCut]] = lp.a_matrix_.value_[iEl];
cut_matrix_length[iCut]++;
}
}
}
}
void HighsLpRelaxation::LpRow::get(const HighsMipSolver& mipsolver,
HighsInt& len, const HighsInt*& inds,
const double*& vals) const {
switch (origin) {
case kCutPool:
mipsolver.mipdata_->cutpool.getCut(index, len, inds, vals);
break;
case kModel:
mipsolver.mipdata_->getRow(index, len, inds, vals);
};
}
HighsInt HighsLpRelaxation::LpRow::getRowLen(
const HighsMipSolver& mipsolver) const {
switch (origin) {
case kCutPool:
return mipsolver.mipdata_->cutpool.getRowLength(index);
case kModel:
return mipsolver.mipdata_->ARstart_[index + 1] -
mipsolver.mipdata_->ARstart_[index];
};
assert(false);
return -1;
}
bool HighsLpRelaxation::LpRow::isIntegral(
const HighsMipSolver& mipsolver) const {
switch (origin) {
case kCutPool:
return mipsolver.mipdata_->cutpool.cutIsIntegral(index);
case kModel:
return (mipsolver.mipdata_->rowintegral[index] != 0);
};
assert(false);
return false;
}
double HighsLpRelaxation::LpRow::getMaxAbsVal(
const HighsMipSolver& mipsolver) const {
switch (origin) {
case kCutPool:
return mipsolver.mipdata_->cutpool.getMaxAbsCutCoef(index);
case kModel:
return mipsolver.mipdata_->maxAbsRowCoef[index];
};
assert(false);
return 0.0;
}
double HighsLpRelaxation::slackLower(HighsInt row) const {
switch (lprows[row].origin) {
case LpRow::kCutPool:
return mipsolver.mipdata_->domain.getMinCutActivity(
mipsolver.mipdata_->cutpool, lprows[row].index);
case LpRow::kModel:
double rowlower = rowLower(row);
if (rowlower != -kHighsInf) return rowlower;
return mipsolver.mipdata_->domain.getMinActivity(lprows[row].index);
};
assert(false);
return -kHighsInf;
}
double HighsLpRelaxation::slackUpper(HighsInt row) const {
double rowupper = rowUpper(row);
switch (lprows[row].origin) {
case LpRow::kCutPool:
return rowupper;
case LpRow::kModel:
if (rowupper != kHighsInf) return rowupper;
return mipsolver.mipdata_->domain.getMaxActivity(lprows[row].index);
};
assert(false);
return kHighsInf;
}
// Used when creating the instance of HighsMipSolverData in
// HighsMipSolver::run()
//
// Parameter creating_mip_solver_data is false by default, and is only
// set true when the HighsLpRelaxation instance is created as part of
// a new HighsMipSolverData instance
HighsLpRelaxation::HighsLpRelaxation(const HighsMipSolver& mipsolver)
: mipsolver(mipsolver) {
lpsolver.setOptionValue("output_flag", false);
lpsolver.setOptionValue("random_seed", mipsolver.options_mip_->random_seed);
// Set primal feasibility tolerance for LP solves according to
// mip_feasibility_tolerance, and smaller tolerance for dual
// feasibility
double mip_primal_feasibility_tolerance =
mipsolver.options_mip_->mip_feasibility_tolerance;
double mip_dual_feasibility_tolerance =
mipsolver.options_mip_->mip_feasibility_tolerance * 0.1;
lpsolver.setOptionValue("primal_feasibility_tolerance",
mip_primal_feasibility_tolerance);
lpsolver.setOptionValue("dual_feasibility_tolerance",
mip_dual_feasibility_tolerance);
status = Status::kNotSet;
numlpiters = 0;
avgSolveIters = 0;
numSolved = 0;
epochs = 0;
maxNumFractional = 0;
lastAgeCall = 0;
objective = -kHighsInf;
currentbasisstored = false;
adjustSymBranchingCol = true;
solved_first_lp = true;
row_ep.size = 0;
}
// Used in LP-based primal heuristics (RINS, RENS, tryRoundedPoint,
// randomizedRounding, shifting, feasibilityPump) to create a local LP
// relaxation using the MIP solver's LP relaxation
HighsLpRelaxation::HighsLpRelaxation(const HighsLpRelaxation& other)
: mipsolver(other.mipsolver),
lprows(other.lprows),
fractionalints(other.fractionalints),
objective(other.objective),
basischeckpoint(other.basischeckpoint),
currentbasisstored(other.currentbasisstored),
adjustSymBranchingCol(other.adjustSymBranchingCol) {
lpsolver.setOptionValue("output_flag", false);
lpsolver.passOptions(other.lpsolver.getOptions());
lpsolver.passModel(other.lpsolver.getLp());
lpsolver.setBasis(other.lpsolver.getBasis());
colLbBuffer.resize(mipsolver.numCol());
colUbBuffer.resize(mipsolver.numCol());
status = Status::kNotSet;
numlpiters = 0;
avgSolveIters = 0;
numSolved = 0;
epochs = 0;
maxNumFractional = 0;
lastAgeCall = 0;
objective = -kHighsInf;
solved_first_lp = true;
row_ep.size = 0;
}
void HighsLpRelaxation::loadModel() {
HighsLp lpmodel = *mipsolver.model_;
lpmodel.col_lower_ = mipsolver.mipdata_->domain.col_lower_;
lpmodel.col_upper_ = mipsolver.mipdata_->domain.col_upper_;
lpmodel.offset_ = 0;
lprows.clear();
lprows.reserve(lpmodel.num_row_);
for (HighsInt i = 0; i != lpmodel.num_row_; ++i)
lprows.push_back(LpRow::model(i));
lpmodel.integrality_.clear();
HighsInt num_col = lpmodel.num_col_;
lpsolver.clearSolver();
lpsolver.clearModel();
lpsolver.passModel(std::move(lpmodel));
colLbBuffer.resize(num_col);
colUbBuffer.resize(num_col);
}
void HighsLpRelaxation::resetToGlobalDomain() {
lpsolver.changeColsBounds(0, mipsolver.numCol() - 1,
mipsolver.mipdata_->domain.col_lower_.data(),
mipsolver.mipdata_->domain.col_upper_.data());
}
void HighsLpRelaxation::computeBasicDegenerateDuals(double threshold,
HighsDomain* localdom) {
if (!lpsolver.hasInvert()) return;
HighsInt k = 0;
const HighsLp& lp = lpsolver.getLp();
const HighsBasis& basis = lpsolver.getBasis();
HighsSolution& solution = const_cast<HighsSolution&>(lpsolver.getSolution());
for (HighsInt col : mipsolver.mipdata_->integral_cols) {
if (basis.col_status[col] != HighsBasisStatus::kBasic) continue;
const double lb = lp.col_lower_[col];
const double ub = lp.col_upper_[col];
if (ub - lb < mipsolver.mipdata_->feastol) continue;
if (solution.col_value[col] - lb < ub - solution.col_value[col]) {
if (solution.col_value[col] > lb + mipsolver.mipdata_->feastol) continue;
solution.col_dual[col] = 1.0;
++k;
} else {
if (solution.col_value[col] < ub - mipsolver.mipdata_->feastol) continue;
solution.col_dual[col] = -1.0;
++k;
}
}
if (k == 0) return;
const HighsInt num_row = lp.num_row_;
const HighsInt num_col = lp.num_col_;
if (row_ep.size < num_row) {
row_ep.setup(num_row);
if ((HighsInt)row_ap.values.size() < num_col) {
row_ap.setDimension(num_col);
dualproofvals.reserve(num_col);
dualproofinds.reserve(num_col);
}
}
const HighsInt* basicIndex = lpsolver.getBasicVariablesArray();
for (HighsInt row = 0; k > 0; row++) {
HighsInt var = basicIndex[row];
if (var >= num_col) continue;
if (std::fabs(solution.col_dual[var]) != 1.0) continue;
--k;
lpsolver.getBasisInverseRowSparse(row, row_ep);
double sign = solution.col_dual[var];
solution.col_dual[var] = 0.0;
double degenerateColDual = kHighsInf;
for (HighsInt i = 0; i < row_ep.count; ++i) {
HighsInt iRow = row_ep.index[i];
const double lb = lp.row_lower_[iRow];
const double ub = lp.row_upper_[iRow];
if (lb == ub) continue;
const double dual = solution.row_dual[iRow];
double val = -sign * row_ep.array[iRow];
if (val > 0) {
if (solution.row_value[iRow] - lb > mipsolver.mipdata_->feastol) {
degenerateColDual = std::min(degenerateColDual, -dual / val);
if (degenerateColDual < threshold) break;
}
} else {
if (ub - solution.row_value[iRow] > mipsolver.mipdata_->feastol) {
degenerateColDual = std::min(degenerateColDual, -dual / val);
if (degenerateColDual < threshold) break;
}
}
}
if (degenerateColDual < threshold) continue;
row_ap.clear();
for (HighsInt i = 0; i < row_ep.count; ++i) {
HighsInt iRow = row_ep.index[i];
HighsInt len;
const HighsInt* inds;
const double* vals;
getRow(iRow, len, inds, vals);
for (HighsInt j = 0; j < len; ++j)
row_ap.add(inds[j], row_ep.array[iRow] * vals[j]);
}
double eps = mipsolver.mipdata_->epsilon;
row_ap.cleanup(
[eps](HighsInt, double val) { return std::fabs(val) <= eps; });
for (HighsInt iCol : row_ap.nonzeroinds) {
if (iCol == var) continue;
const double lb = lp.col_lower_[iCol];
const double ub = lp.col_upper_[iCol];
if (lb == ub) continue;
const double dual = solution.col_dual[iCol];
double val = sign * row_ap.getValue(iCol);
if (val > mipsolver.mipdata_->epsilon) {
if (solution.col_value[iCol] - lb > mipsolver.mipdata_->feastol) {
degenerateColDual = std::min(degenerateColDual, -dual / val);
if (degenerateColDual < threshold) break;
}
} else if (val < -mipsolver.mipdata_->epsilon) {
if (ub - solution.col_value[iCol] > mipsolver.mipdata_->feastol) {
degenerateColDual = std::min(degenerateColDual, -dual / val);
if (degenerateColDual < threshold) break;
}
}
}
if (degenerateColDual < threshold) continue;
if (degenerateColDual == kHighsInf && localdom) {
HighsCDouble rhs = 0;
for (HighsInt i = 0; i < row_ep.count; ++i) {
HighsInt iRow = row_ep.index[i];
const double lb = lp.row_lower_[iRow];
const double ub = lp.row_upper_[iRow];
double val = sign * row_ep.array[iRow];
if (ub == lb || val > mipsolver.mipdata_->epsilon) {
rhs += val * ub;
} else if (val < -mipsolver.mipdata_->epsilon) {
rhs += val * lb;
} else {
rhs += val * solution.row_value[iRow];
}
}
dualproofvals.resize(row_ap.nonzeroinds.size());
for (HighsInt i = 0; i < (HighsInt)row_ap.nonzeroinds.size(); ++i)
dualproofvals[i] = sign * row_ap.getValue(row_ap.nonzeroinds[i]);
HighsDomainChange domchg;
domchg.column = var;
if (sign == 1.0) {
domchg.boundtype = HighsBoundType::kUpper;
domchg.boundval = lp.col_lower_[var];
} else {
domchg.boundtype = HighsBoundType::kLower;
domchg.boundval = lp.col_upper_[var];
}
localdom->conflictAnalyzeReconvergence(
domchg, row_ap.nonzeroinds.data(), dualproofvals.data(),
row_ap.nonzeroinds.size(), double(rhs),
mipsolver.mipdata_->conflictPool);
continue;
}
solution.col_dual[var] = sign * double(degenerateColDual);
}
}
double HighsLpRelaxation::computeBestEstimate(const HighsPseudocost& ps) const {
HighsCDouble estimate = objective;
if (!fractionalints.empty()) {
// because the pseudocost may be zero, we add an offset to the pseudocost so
// that we always have some part of the estimate depending on the
// fractionality.
HighsCDouble increase = 0.0;
double offset =
mipsolver.mipdata_->feastol * std::max(std::abs(objective), 1.0) /
static_cast<double>(mipsolver.mipdata_->integral_cols.size());
for (const std::pair<HighsInt, double>& f : fractionalints) {
increase += std::min(ps.getPseudocostUp(f.first, f.second, offset),
ps.getPseudocostDown(f.first, f.second, offset));
}
estimate += double(increase);
}
return double(estimate);
}
double HighsLpRelaxation::computeLPDegneracy(
const HighsDomain& localdomain) const {
if (!lpsolver.getSolution().dual_valid || !lpsolver.getBasis().valid) {
return 1.0;
}
double dualFeasTol = lpsolver.getInfo().max_dual_infeasibility;
const HighsBasis& basis = lpsolver.getBasis();
const HighsSolution& sol = lpsolver.getSolution();
HighsInt numFixedRows = 0;
HighsInt numInequalities = 0;
HighsInt numBasicEqualities = 0;
for (HighsInt i = 0; i < numRows(); ++i) {
if (lpsolver.getLp().row_lower_[i] != lpsolver.getLp().row_upper_[i]) {
++numInequalities;
if (basis.row_status[i] != HighsBasisStatus::kBasic) {
if (std::abs(sol.row_dual[i]) > dualFeasTol) ++numFixedRows;
}
} else
numBasicEqualities += basis.row_status[i] == HighsBasisStatus::kBasic;
}
HighsInt numAlreadyFixedCols = 0;
HighsInt numFixedCols = 0;
for (HighsInt i = 0; i < numCols(); ++i) {
if (basis.col_status[i] != HighsBasisStatus::kBasic) {
if (std::abs(sol.col_dual[i]) > dualFeasTol)
++numFixedCols;
else if (localdomain.col_lower_[i] == localdomain.col_upper_[i])
++numAlreadyFixedCols;
}
}
HighsInt base = numCols() - numAlreadyFixedCols + numInequalities +
numBasicEqualities - numRows();
double degenerateColumnShare =
base > 0 ? 1.0 - double(numFixedCols + numFixedRows) / base : 0.0;
double varConsRatio =
numRows() > 0
? double(numCols() + numInequalities + numBasicEqualities -
numFixedCols - numFixedRows - numAlreadyFixedCols) /
numRows()
: 1.0;
double fac1 = degenerateColumnShare < 0.8
? 1.0
: std::pow(10.0, 10 * (degenerateColumnShare - 0.7));
double fac2 = varConsRatio < 2.0 ? 1.0 : 10.0 * varConsRatio;
return fac1 * fac2;
}
void HighsLpRelaxation::addCuts(HighsCutSet& cutset) {
HighsInt numcuts = cutset.numCuts();
assert(lpsolver.getLp().num_row_ ==
(HighsInt)lpsolver.getLp().row_lower_.size());
assert(lpsolver.getLp().num_row_ == (HighsInt)lprows.size());
if (numcuts > 0) {
status = Status::kNotSet;
currentbasisstored = false;
basischeckpoint.reset();
lprows.reserve(lprows.size() + numcuts);
for (HighsInt i = 0; i != numcuts; ++i)
lprows.push_back(LpRow::cut(cutset.cutindices[i]));
bool success =
lpsolver.addRows(numcuts, cutset.lower_.data(), cutset.upper_.data(),
cutset.ARvalue_.size(), cutset.ARstart_.data(),
cutset.ARindex_.data(),
cutset.ARvalue_.data()) == HighsStatus::kOk;
assert(success);
(void)success;
assert(lpsolver.getLp().num_row_ ==
(HighsInt)lpsolver.getLp().row_lower_.size());
cutset.clear();
}
}
void HighsLpRelaxation::removeObsoleteRows(bool notifyPool) {
HighsInt nlprows = numRows();
HighsInt nummodelrows = getNumModelRows();
std::vector<HighsInt> deletemask;
HighsInt ndelcuts = 0;
for (HighsInt i = nummodelrows; i != nlprows; ++i) {
assert(lprows[i].origin == LpRow::Origin::kCutPool);
if (lpsolver.getBasis().row_status[i] == HighsBasisStatus::kBasic) {
if (ndelcuts == 0) deletemask.resize(nlprows);
++ndelcuts;
deletemask[i] = 1;
if (notifyPool) mipsolver.mipdata_->cutpool.lpCutRemoved(lprows[i].index);
}
}
removeCuts(ndelcuts, deletemask);
}
void HighsLpRelaxation::removeCuts(HighsInt ndelcuts,
std::vector<HighsInt>& deletemask) {
assert(lpsolver.getLp().num_row_ ==
(HighsInt)lpsolver.getLp().row_lower_.size());
if (ndelcuts > 0) {
HighsBasis basis = lpsolver.getBasis();
HighsInt nlprows = lpsolver.getNumRow();
lpsolver.deleteRows(deletemask.data());
for (HighsInt i = mipsolver.numRow(); i != nlprows; ++i) {
if (deletemask[i] >= 0) {
lprows[deletemask[i]] = lprows[i];
basis.row_status[deletemask[i]] = basis.row_status[i];
}
}
assert(lpsolver.getLp().num_row_ ==
(HighsInt)lpsolver.getLp().row_lower_.size());
basis.row_status.resize(basis.row_status.size() - ndelcuts);
lprows.resize(lprows.size() - ndelcuts);
assert(lpsolver.getLp().num_row_ == (HighsInt)lprows.size());
basis.debug_origin_name = "HighsLpRelaxation::removeCuts";
lpsolver.setBasis(basis);
lpsolver.run();
if (!mipsolver.submip) {
const HighsSubSolverCallTime& sub_solver_call_time =
lpsolver.getSubSolverCallTime();
mipsolver.analysis_.addSubSolverCallTime(sub_solver_call_time);
// Go through sub_solver_call_time to update any MIP clocks
const bool valid_basis = true;
const bool use_presolve = false;
mipsolver.analysis_.mipTimerUpdate(sub_solver_call_time, valid_basis,
use_presolve);
}
}
}
void HighsLpRelaxation::removeCuts() {
assert(lpsolver.getLp().num_row_ ==
(HighsInt)lpsolver.getLp().row_lower_.size());
HighsInt nlprows = lpsolver.getNumRow();
HighsInt modelrows = mipsolver.numRow();
lpsolver.deleteRows(modelrows, nlprows - 1);
for (HighsInt i = modelrows; i != nlprows; ++i) {
if (lprows[i].origin == LpRow::Origin::kCutPool)
mipsolver.mipdata_->cutpool.lpCutRemoved(lprows[i].index);
}
lprows.resize(modelrows);
assert(lpsolver.getLp().num_row_ ==
(HighsInt)lpsolver.getLp().row_lower_.size());
}
void HighsLpRelaxation::performAging(bool deleteRows) {
assert(lpsolver.getLp().num_row_ ==
(HighsInt)lpsolver.getLp().row_lower_.size());
HighsInt agelimit;
if (lpsolver.getInfo().basis_validity == kBasisValidityInvalid ||
lpsolver.getInfo().max_dual_infeasibility > mipsolver.mipdata_->feastol ||
!lpsolver.getSolution().dual_valid)
return;
if (deleteRows) {
agelimit = mipsolver.options_mip_->mip_lp_age_limit;
++epochs;
if (epochs % std::max(agelimit >> 1, HighsInt{2}) != 0)
agelimit = kHighsIInf;
else if ((HighsInt)epochs < agelimit)
agelimit = epochs;
} else {
if (lastAgeCall == numlpiters) return;
agelimit = kHighsIInf;
}
lastAgeCall = numlpiters;
HighsInt nlprows = numRows();
HighsInt nummodelrows = getNumModelRows();
std::vector<HighsInt> deletemask;
HighsInt ndelcuts = 0;
for (HighsInt i = nummodelrows; i != nlprows; ++i) {
assert(lprows[i].origin == LpRow::Origin::kCutPool);
if (lpsolver.getBasis().row_status[i] == HighsBasisStatus::kBasic) {
lprows[i].age += (deleteRows || lprows[i].age != 0);
if (lprows[i].age > agelimit) {
if (ndelcuts == 0) deletemask.resize(nlprows);
++ndelcuts;
deletemask[i] = 1;
mipsolver.mipdata_->cutpool.lpCutRemoved(lprows[i].index);
}
} else if (std::abs(lpsolver.getSolution().row_dual[i]) >
lpsolver.getOptions().dual_feasibility_tolerance) {
lprows[i].age = 0;
}
}
removeCuts(ndelcuts, deletemask);
}
void HighsLpRelaxation::resetAges() {
assert(lpsolver.getLp().num_row_ ==
(HighsInt)lpsolver.getLp().row_lower_.size());
if (lpsolver.getInfo().basis_validity == kBasisValidityInvalid ||
lpsolver.getInfo().max_dual_infeasibility > mipsolver.mipdata_->feastol ||
!lpsolver.getSolution().dual_valid)
return;
HighsInt nlprows = numRows();
HighsInt nummodelrows = getNumModelRows();
for (HighsInt i = nummodelrows; i != nlprows; ++i) {
assert(lprows[i].origin == LpRow::Origin::kCutPool);
if (lpsolver.getBasis().row_status[i] != HighsBasisStatus::kBasic &&
std::abs(lpsolver.getSolution().row_dual[i]) >
lpsolver.getOptions().dual_feasibility_tolerance)
lprows[i].age = 0;
}
}
void HighsLpRelaxation::flushDomain(HighsDomain& domain, bool continuous) {
if (!domain.getChangedCols().empty()) {
if (&domain == &mipsolver.mipdata_->domain) continuous = true;
currentbasisstored = false;
if (!continuous) domain.removeContinuousChangedCols();
HighsInt numChgCols = domain.getChangedCols().size();
if (numChgCols == 0) return;
const HighsInt* chgCols = domain.getChangedCols().data();
for (HighsInt i = 0; i < numChgCols; ++i) {
HighsInt col = chgCols[i];
colLbBuffer[i] = domain.col_lower_[col];
colUbBuffer[i] = domain.col_upper_[col];
}
lpsolver.changeColsBounds(numChgCols, domain.getChangedCols().data(),
colLbBuffer.data(), colUbBuffer.data());
domain.clearChangedCols();
}
}
bool HighsLpRelaxation::computeDualProof(const HighsDomain& globaldomain,
double upperbound,
std::vector<HighsInt>& inds,
std::vector<double>& vals, double& rhs,
bool extractCliques) const {
#if 0
const HighsBasis& basis = lpsolver.getBasis();
const HighsSolution& sol = lpsolver.getSolution();
HighsCDouble proofRhs = upperbound;
assert(lpsolver.getInfo().max_dual_infeasibility <=
mipsolver.mipdata_->feastol);
proofRhs -= lpsolver.getInfo().objective_function_value;
inds.clear();
vals.clear();
double maxVal = 0.0;
double maxValGlb = 0.0;
double sumLocal = 0.0;
HighsInt numLocalCols = 0;
const HighsInt numCol = lpsolver.getNumCol();
for (HighsInt i : mipsolver.mipdata_->integral_cols) {
if (basis.col_status[i] == HighsBasisStatus::kBasic) continue;
if (sol.col_dual[i] > kHighsTiny) {
if (sol.col_value[i] != globaldomain.col_lower_[i]) {
maxVal = std::max(sol.col_dual[i], maxVal);
sumLocal += sol.col_dual[i];
++numLocalCols;
}
} else if (sol.col_dual[i] < -kHighsTiny) {
if (sol.col_value[i] != globaldomain.col_upper_[i]) {
maxVal = std::max(-sol.col_dual[i], maxVal);
sumLocal += -sol.col_dual[i];
++numLocalCols;
}
} else
continue;
proofRhs += sol.col_value[i] * sol.col_dual[i];
vals.push_back(sol.col_dual[i]);
inds.push_back(i);
}
int expShift;
std::frexp(maxVal - mipsolver.mipdata_->epsilon, &expShift);
expShift = -expShift;
HighsInt len = vals.size();
double minGlbVal = numLocalCols == 0
? 0.0
: std::ldexp(sumLocal / numLocalCols, expShift) -
mipsolver.mipdata_->feastol;
for (HighsInt i = len - 1; i >= 0; --i) {
HighsInt iCol = inds[i];
double val = std::ldexp(vals[i], expShift);
bool remove = false;
double absVal = std::fabs(val);
if (absVal <= mipsolver.mipdata_->feastol || globaldomain.isFixed(iCol)) {
if (vals[i] > 0) {
if (globaldomain.col_lower_[iCol] == -kHighsInf) return false;
proofRhs -= vals[i] * globaldomain.col_lower_[iCol];
} else {
if (globaldomain.col_upper_[iCol] == kHighsInf) return false;
proofRhs -= vals[i] * globaldomain.col_upper_[iCol];
}
remove = true;
} else if (absVal < minGlbVal) {
if (vals[i] > 0)
remove = sol.col_value[iCol] == globaldomain.col_lower_[iCol];
else
remove = sol.col_value[iCol] == globaldomain.col_upper_[iCol];
if (remove) proofRhs -= vals[i] * sol.col_value[iCol];
}
if (remove) {
--len;
vals[i] = vals[len];
inds[i] = inds[len];
} else {
vals[i] = val;
}
}
vals.resize(len);
inds.resize(len);
rhs = std::ldexp(double(proofRhs), expShift);
globaldomain.tightenCoefficients(inds.data(), vals.data(), inds.size(), rhs);
mipsolver.mipdata_->debugSolution.checkCut(inds.data(), vals.data(),
inds.size(), rhs);
if (extractCliques)
mipsolver.mipdata_->cliquetable.extractCliquesFromCut(
mipsolver, inds.data(), vals.data(), inds.size(), rhs);
return true;
#else
std::vector<double> row_dual = lpsolver.getSolution().row_dual;
const HighsLp& lp = lpsolver.getLp();
assert(std::isfinite(upperbound));
HighsCDouble upper = upperbound;
for (HighsInt i = 0; i != lp.num_row_; ++i) {
// @FlipRowDual row_dual[i] < 0 became row_dual[i] > 0
if (row_dual[i] > 0) {
if (lp.row_lower_[i] != -kHighsInf)
// @FlipRowDual += became -=
upper -= row_dual[i] * lp.row_lower_[i];
else
row_dual[i] = 0;
// @FlipRowDual row_dual[i] > 0 became row_dual[i] < 0
} else if (row_dual[i] < 0) {
if (lp.row_upper_[i] != kHighsInf)
// @FlipRowDual += became -=
upper -= row_dual[i] * lp.row_upper_[i];
else
row_dual[i] = 0;
}
}
inds.clear();
vals.clear();
inds.reserve(lp.num_col_);
vals.reserve(lp.num_col_);
for (HighsInt i = 0; i != lp.num_col_; ++i) {
HighsInt start = lp.a_matrix_.start_[i];
HighsInt end = lp.a_matrix_.start_[i + 1];
HighsCDouble sum = lp.col_cost_[i];
for (HighsInt j = start; j != end; ++j) {
if (row_dual[lp.a_matrix_.index_[j]] == 0) continue;
// @FlipRowDual += became -=
sum -= lp.a_matrix_.value_[j] * row_dual[lp.a_matrix_.index_[j]];
}
double val = double(sum);
if (std::fabs(val) <= mipsolver.options_mip_->small_matrix_value) continue;
bool removeValue = std::fabs(val) <= mipsolver.mipdata_->feastol;
if (!removeValue &&
(globaldomain.col_lower_[i] == globaldomain.col_upper_[i] ||
mipsolver.isColContinuous(i))) {
if (val > 0)
removeValue =
lpsolver.getSolution().col_value[i] - globaldomain.col_lower_[i] <=
mipsolver.mipdata_->feastol;
else
removeValue =
globaldomain.col_upper_[i] - lpsolver.getSolution().col_value[i] <=
mipsolver.mipdata_->feastol;
}
if (removeValue) {
if (val < 0) {
if (globaldomain.col_upper_[i] == kHighsInf) return false;
upper -= val * globaldomain.col_upper_[i];
} else {
if (globaldomain.col_lower_[i] == -kHighsInf) return false;
upper -= val * globaldomain.col_lower_[i];
}
continue;
}
vals.push_back(val);
inds.push_back(i);
}
rhs = double(upper);
assert(std::isfinite(rhs));
globaldomain.tightenCoefficients(inds.data(), vals.data(), inds.size(), rhs);
mipsolver.mipdata_->debugSolution.checkCut(inds.data(), vals.data(),
inds.size(), rhs);
if (extractCliques)
mipsolver.mipdata_->cliquetable.extractCliquesFromCut(
mipsolver, inds.data(), vals.data(), inds.size(), rhs);
return true;
#endif
}
void HighsLpRelaxation::storeDualInfProof() {
assert(lpsolver.getModelStatus() == HighsModelStatus::kInfeasible);
hasdualproof = false;
if (lpsolver.getInfo().basis_validity == kBasisValidityInvalid) return;
HighsInt num_row = lpsolver.getNumRow();
HighsInt num_col = lpsolver.getNumCol();
if (row_ep.size < num_row) {
row_ep.setup(num_row);
if ((HighsInt)row_ap.values.size() < num_col) {
row_ap.setDimension(num_col);
dualproofvals.reserve(num_col);
dualproofinds.reserve(num_col);
}
}
lpsolver.getDualRaySparse(hasdualproof, row_ep);
if (!hasdualproof) {
highsLogDev(mipsolver.options_mip_->log_options, HighsLogType::kVerbose,
"no dual ray stored\n");
return;
}
dualproofinds.clear();
dualproofvals.clear();
dualproofrhs = kHighsInf;
const HighsLp& lp = lpsolver.getLp();
assert(hasdualproof);
HighsCDouble upper = 0.0;
row_ap.clear();
for (HighsInt i = 0; i < row_ep.count; ++i) {
HighsInt iRow = row_ep.index[i];
const double weight = -row_ep.array[iRow];
if (std::fabs(weight) * getMaxAbsRowVal(iRow) <=
mipsolver.mipdata_->epsilon)
continue;
else if (weight > 0) {
if (lp.row_upper_[iRow] == kHighsInf) continue;
upper += weight * lp.row_upper_[iRow];
} else {
if (lp.row_lower_[iRow] == -kHighsInf) continue;
upper += weight * lp.row_lower_[iRow];
}
HighsInt len;
const HighsInt* inds;
const double* vals;
getRow(iRow, len, inds, vals);
for (HighsInt j = 0; j < len; ++j) row_ap.add(inds[j], weight * vals[j]);
}
const HighsDomain& globaldomain = mipsolver.mipdata_->domain;
for (HighsInt i : row_ap.getNonzeros()) {
double val = row_ap.getValue(i);
if (std::fabs(val) <= mipsolver.mipdata_->epsilon) continue;
bool removeValue = std::abs(val) <= mipsolver.mipdata_->feastol;
if (!removeValue &&
(globaldomain.col_lower_[i] == globaldomain.col_upper_[i] ||
mipsolver.isColContinuous(i))) {
// remove continuous entries and globally fixed entries whenever the
// local LP's bound is not tighter than the global bound
if (val > 0)
removeValue = lp.col_lower_[i] - globaldomain.col_lower_[i] <=
mipsolver.mipdata_->feastol;
else
removeValue = globaldomain.col_upper_[i] - lp.col_upper_[i] <=
mipsolver.mipdata_->feastol;
}
if (removeValue) {
if (val < 0) {
if (globaldomain.col_upper_[i] == kHighsInf) {
hasdualproof = false;
return;
}
upper -= val * globaldomain.col_upper_[i];
} else {
if (globaldomain.col_lower_[i] == -kHighsInf) {
hasdualproof = false;
return;
}
upper -= val * globaldomain.col_lower_[i];
}
continue;
}
dualproofvals.push_back(val);
dualproofinds.push_back(i);
}
dualproofrhs = double(upper);
mipsolver.mipdata_->domain.tightenCoefficients(
dualproofinds.data(), dualproofvals.data(), dualproofinds.size(),
dualproofrhs);
mipsolver.mipdata_->debugSolution.checkCut(
dualproofinds.data(), dualproofvals.data(), dualproofinds.size(),
dualproofrhs);
mipsolver.mipdata_->cliquetable.extractCliquesFromCut(
mipsolver, dualproofinds.data(), dualproofvals.data(),
dualproofinds.size(), dualproofrhs);
}
void HighsLpRelaxation::storeDualUBProof() {
assert(lpsolver.getModelStatus() == HighsModelStatus::kObjectiveBound);