-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathHighsMipSolverData.cpp
More file actions
2790 lines (2494 loc) · 107 KB
/
HighsMipSolverData.cpp
File metadata and controls
2790 lines (2494 loc) · 107 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/HighsMipSolverData.h"
#include <random>
// #include "lp_data/HighsLpUtils.h"
#include "../extern/pdqsort/pdqsort.h"
#include "lp_data/HighsModelUtils.h"
#include "mip/HighsPseudocost.h"
#include "mip/HighsRedcostFixing.h"
#include "mip/MipTimer.h"
#include "parallel/HighsParallel.h"
#include "presolve/HPresolve.h"
#include "util/HighsIntegers.h"
std::string HighsMipSolverData::solutionSourceToString(
const int solution_source, const bool code) const {
if (solution_source == kSolutionSourceNone) {
if (code) return " ";
return "None";
} else if (solution_source == kSolutionSourceBranching) {
if (code) return "B";
return "Branching";
} else if (solution_source == kSolutionSourceCentralRounding) {
if (code) return "C";
return "Central rounding";
} else if (solution_source == kSolutionSourceFeasibilityPump) {
if (code) return "F";
return "Feasibility pump";
} else if (solution_source == kSolutionSourceFeasibilityJump) {
if (code) return "J";
return "Feasibility jump";
} else if (solution_source == kSolutionSourceHeuristic) {
if (code) return "H";
return "Heuristic";
// } else if (solution_source == kSolutionSourceInitial) {
// if (code) return "I";
// return "Initial";
} else if (solution_source == kSolutionSourceSubMip) {
if (code) return "L";
return "Sub-MIP";
} else if (solution_source == kSolutionSourceEmptyMip) {
if (code) return "P";
return "Empty MIP";
} else if (solution_source == kSolutionSourceRandomizedRounding) {
if (code) return "R";
return "Randomized rounding";
} else if (solution_source == kSolutionSourceZiRound) {
if (code) return "Z";
return "ZI Round";
} else if (solution_source == kSolutionSourceShifting) {
if (code) return "I";
return "Shifting";
} else if (solution_source == kSolutionSourceSolveLp) {
if (code) return "S";
return "Solve LP";
} else if (solution_source == kSolutionSourceEvaluateNode) {
if (code) return "T";
return "Evaluate node";
} else if (solution_source == kSolutionSourceUnbounded) {
if (code) return "U";
return "Unbounded";
} else if (solution_source == kSolutionSourceTrivialZ) {
if (code) return "z";
return "Trivial zero";
} else if (solution_source == kSolutionSourceTrivialL) {
if (code) return "l";
return "Trivial lower";
} else if (solution_source == kSolutionSourceTrivialU) {
if (code) return "u";
return "Trivial upper";
} else if (solution_source == kSolutionSourceTrivialP) {
if (code) return "p";
return "Trivial point";
} else if (solution_source == kSolutionSourceUserSolution) {
if (code) return "X";
return "User solution";
} else if (solution_source == kSolutionSourceCleanup) {
if (code) return " ";
return "";
} else {
printf("HighsMipSolverData::solutionSourceToString: Unknown source = %d\n",
solution_source);
assert(0 == 111);
if (code) return "*";
return "None";
}
}
bool HighsMipSolverData::checkSolution(
const std::vector<double>& solution) const {
for (HighsInt i = 0; i != mipsolver.model_->num_col_; ++i) {
if (solution[i] < mipsolver.model_->col_lower_[i] - feastol) return false;
if (solution[i] > mipsolver.model_->col_upper_[i] + feastol) return false;
if (mipsolver.variableType(i) == HighsVarType::kInteger &&
fractionality(solution[i]) > feastol)
return false;
}
for (HighsInt i = 0; i != mipsolver.model_->num_row_; ++i) {
double rowactivity = 0.0;
HighsInt start = ARstart_[i];
HighsInt end = ARstart_[i + 1];
for (HighsInt j = start; j != end; ++j)
rowactivity += solution[ARindex_[j]] * ARvalue_[j];
if (rowactivity > mipsolver.rowUpper(i) + feastol) return false;
if (rowactivity < mipsolver.rowLower(i) - feastol) return false;
}
return true;
}
std::vector<std::tuple<HighsInt, HighsInt, double>>
HighsMipSolverData::getInfeasibleRows(
const std::vector<double>& solution) const {
std::vector<std::tuple<HighsInt, HighsInt, double>> infeasibleRows;
for (HighsInt i = 0; i != mipsolver.model_->num_row_; ++i) {
HighsInt start = ARstart_[i];
HighsInt end = ARstart_[i + 1];
HighsCDouble row_activity_quad = 0.0;
for (HighsInt j = start; j != end; ++j)
row_activity_quad +=
static_cast<HighsCDouble>(solution[ARindex_[j]]) * ARvalue_[j];
double row_activity = static_cast<double>(row_activity_quad);
if (row_activity > mipsolver.rowUpper(i) + feastol) {
double difference = std::abs(row_activity - mipsolver.rowUpper(i));
infeasibleRows.push_back({i, +1, difference});
}
if (row_activity < mipsolver.rowLower(i) - feastol) {
double difference = std::abs(mipsolver.rowLower(i) - row_activity);
infeasibleRows.push_back({i, -1, difference});
}
}
return infeasibleRows;
}
bool HighsMipSolverData::trySolution(const std::vector<double>& solution,
const int solution_source) {
if (int(solution.size()) != mipsolver.model_->num_col_) return false;
HighsCDouble obj = 0;
for (HighsInt i = 0; i != mipsolver.model_->num_col_; ++i) {
if (solution[i] < mipsolver.model_->col_lower_[i] - feastol) return false;
if (solution[i] > mipsolver.model_->col_upper_[i] + feastol) return false;
if (mipsolver.variableType(i) == HighsVarType::kInteger &&
fractionality(solution[i]) > feastol)
return false;
obj += mipsolver.colCost(i) * solution[i];
}
for (HighsInt i = 0; i != mipsolver.model_->num_row_; ++i) {
double rowactivity = 0.0;
HighsInt start = ARstart_[i];
HighsInt end = ARstart_[i + 1];
for (HighsInt j = start; j != end; ++j)
rowactivity += solution[ARindex_[j]] * ARvalue_[j];
if (rowactivity > mipsolver.rowUpper(i) + feastol) return false;
if (rowactivity < mipsolver.rowLower(i) - feastol) return false;
}
return addIncumbent(solution, double(obj), solution_source);
}
bool HighsMipSolverData::solutionRowFeasible(
const std::vector<double>& solution) const {
for (HighsInt i = 0; i != mipsolver.model_->num_row_; ++i) {
HighsCDouble c_double_rowactivity = HighsCDouble(0.0);
HighsInt start = ARstart_[i];
HighsInt end = ARstart_[i + 1];
for (HighsInt j = start; j != end; ++j)
c_double_rowactivity += HighsCDouble(solution[ARindex_[j]] * ARvalue_[j]);
double rowactivity = double(c_double_rowactivity);
if (rowactivity > mipsolver.rowUpper(i) + feastol) return false;
if (rowactivity < mipsolver.rowLower(i) - feastol) return false;
}
return true;
}
HighsModelStatus HighsMipSolverData::trivialHeuristics() {
// printf("\nHighsMipSolverData::trivialHeuristics() Number of continuous
// columns is %d\n",
// int(continuous_cols.size()));
if (continuous_cols.size() > 0) return HighsModelStatus::kNotset;
const HighsInt num_try_heuristic = 4;
const std::vector<int> heuristic_source = {
kSolutionSourceTrivialZ, kSolutionSourceTrivialL, kSolutionSourceTrivialU,
kSolutionSourceTrivialP};
std::vector<double> col_lower = mipsolver.model_->col_lower_;
std::vector<double> col_upper = mipsolver.model_->col_upper_;
const std::vector<double>& row_lower = mipsolver.model_->row_lower_;
const std::vector<double>& row_upper = mipsolver.model_->row_upper_;
const HighsSparseMatrix& matrix = mipsolver.model_->a_matrix_;
// Determine the following properties, according to which some
// trivial heuristics are duplicated or fail immediately
bool all_integer_lower_non_positive = true;
bool all_integer_lower_zero = true;
bool all_integer_lower_finite = true;
bool all_integer_upper_finite = true;
for (HighsInt integer_col = 0; integer_col < numintegercols; integer_col++) {
HighsInt iCol = integer_cols[integer_col];
// Round bounds in to nearest integer
col_lower[iCol] = std::ceil(col_lower[iCol]);
col_upper[iCol] = std::floor(col_upper[iCol]);
const bool legal_bounds =
col_lower[iCol] <= col_upper[iCol] && col_lower[iCol] < kHighsInf &&
col_upper[iCol] > -kHighsInf && !std::isnan(col_lower[iCol]) &&
!std::isnan(col_upper[iCol]);
if (!legal_bounds) {
assert(legal_bounds);
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"HighsMipSolverData::trivialHeuristics() has detected "
"infeasible/illegal bounds [%g, %g] for column %d: MIP is "
"infeasible\n",
col_lower[iCol], col_upper[iCol], int(iCol));
return HighsModelStatus::kInfeasible;
}
// If bounds are inconsistent then MIP is infeasible
if (col_lower[iCol] > col_upper[iCol]) return HighsModelStatus::kInfeasible;
if (col_lower[iCol] > 0) all_integer_lower_non_positive = false;
if (col_lower[iCol]) all_integer_lower_zero = false;
if (col_lower[iCol] <= -kHighsInf) all_integer_lower_finite = false;
if (col_upper[iCol] >= kHighsInf) all_integer_upper_finite = false;
// Only continue if one of the properties still holds
if (!(all_integer_lower_non_positive || all_integer_lower_zero ||
all_integer_upper_finite))
break;
}
const bool all_integer_boxed =
all_integer_lower_finite && all_integer_upper_finite;
// printf(
// "Trying trivial heuristics\n"
// " all_integer_lower_non_positive = %d\n"
// " all_integer_lower_zero = %d\n"
// " all_integer_upper_finite = %d\n"
// " all_integer_boxed = %d\n",
// all_integer_lower_non_positive, all_integer_lower_zero,
// all_integer_upper_finite, all_integer_boxed);
const double feasibility_tolerance =
mipsolver.options_mip_->mip_feasibility_tolerance;
// Loop through the trivial heuristics
std::vector<double> solution(mipsolver.model_->num_col_);
for (HighsInt try_heuristic = 0; try_heuristic < num_try_heuristic;
try_heuristic++) {
if (try_heuristic == 0) {
// First heuristic is to see whether all-zero for integer
// variables is feasible
//
// If there is a positive lower bound then the heuristic fails
if (!all_integer_lower_non_positive) continue;
// Determine whether a zero row activity is feasible
bool heuristic_failed = false;
for (HighsInt iRow = 0; iRow < mipsolver.model_->num_row_; iRow++) {
if (row_lower[iRow] > feasibility_tolerance ||
row_upper[iRow] < -feasibility_tolerance) {
heuristic_failed = true;
break;
}
}
if (heuristic_failed) continue;
solution.assign(mipsolver.model_->num_col_, 0);
} else if (try_heuristic == 1) {
// Second heuristic is to see whether all-lower for integer
// variables (if distinct from all-zero) is feasible
if (all_integer_lower_zero) continue;
// Trivially feasible for columns
if (!solutionRowFeasible(col_lower)) continue;
solution = col_lower;
} else if (try_heuristic == 2) {
// Third heuristic is to see whether all-upper for integer
// variables is feasible
//
// If there is an infinite upper bound then the heuristic fails
if (!all_integer_upper_finite) continue;
// Trivially feasible for columns
if (!solutionRowFeasible(col_upper)) continue;
solution = col_upper;
} else if (try_heuristic == 3) {
// Fourth heuristic is to see whether the "lock point" is feasible
if (!all_integer_boxed) continue;
for (HighsInt integer_col = 0; integer_col < numintegercols;
integer_col++) {
HighsInt iCol = integer_cols[integer_col];
HighsInt num_positive_values = 0;
HighsInt num_negative_values = 0;
for (HighsInt iEl = matrix.start_[iCol]; iEl < matrix.start_[iCol + 1];
iEl++) {
if (matrix.value_[iEl] > 0)
num_positive_values++;
else
num_negative_values++;
}
solution[iCol] = num_positive_values > num_negative_values
? col_lower[iCol]
: col_upper[iCol];
}
// Trivially feasible for columns
if (!solutionRowFeasible(solution)) continue;
}
HighsCDouble cdouble_obj = 0.0;
for (HighsInt iCol = 0; iCol < mipsolver.model_->num_col_; iCol++)
cdouble_obj += mipsolver.colCost(iCol) * solution[iCol];
double obj = double(cdouble_obj);
const double save_upper_bound = upper_bound;
const bool new_incumbent =
addIncumbent(solution, obj, heuristic_source[try_heuristic]);
const bool lc_report = false;
if (lc_report) {
printf("Trivial heuristic %d has succeeded: objective = %g",
int(try_heuristic), obj);
if (new_incumbent) {
printf("; upper bound from %g to %g\n", save_upper_bound, upper_bound);
} else {
printf("\n");
}
}
}
return HighsModelStatus::kNotset;
}
void HighsMipSolverData::startAnalyticCenterComputation(
const highs::parallel::TaskGroup& taskGroup) {
taskGroup.spawn([&]() {
// first check if the analytic centre computation should be cancelled, e.g.
// due to early return in the root node evaluation
Highs ipm;
ipm.setOptionValue("solver", "ipm");
ipm.setOptionValue("run_crossover", kHighsOffString);
// ipm.setOptionValue("allow_pdlp_cleanup", false);
ipm.setOptionValue("presolve", kHighsOffString);
ipm.setOptionValue("output_flag", false);
// ipm.setOptionValue("output_flag", !mipsolver.submip);
ipm.setOptionValue("ipm_iteration_limit", 200);
HighsLp lpmodel(*mipsolver.model_);
lpmodel.col_cost_.assign(lpmodel.num_col_, 0.0);
ipm.passModel(std::move(lpmodel));
// if (!mipsolver.submip) {
// const std::string file_name = mipsolver.model_->model_name_ +
// "_ipm.mps"; printf("Calling ipm.writeModel(%s)\n",
// file_name.c_str()); fflush(stdout); ipm.writeModel(file_name);
// }
mipsolver.analysis_.mipTimerStart(kMipClockIpmSolveLp);
ipm.run();
mipsolver.analysis_.mipTimerStop(kMipClockIpmSolveLp);
const std::vector<double>& sol = ipm.getSolution().col_value;
if (HighsInt(sol.size()) != mipsolver.numCol()) return;
analyticCenterStatus = ipm.getModelStatus();
analyticCenter = sol;
});
}
void HighsMipSolverData::finishAnalyticCenterComputation(
const highs::parallel::TaskGroup& taskGroup) {
if (mipsolver.analysis_.analyse_mip_time) {
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - starting analytic centre synch\n",
mipsolver.analysis_.mipTimerRead());
fflush(stdout);
}
taskGroup.sync();
if (mipsolver.analysis_.analyse_mip_time) {
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - completed analytic centre synch\n",
mipsolver.analysis_.mipTimerRead());
fflush(stdout);
}
analyticCenterComputed = true;
if (analyticCenterStatus == HighsModelStatus::kOptimal) {
HighsInt nfixed = 0;
HighsInt nintfixed = 0;
for (HighsInt i = 0; i != mipsolver.numCol(); ++i) {
double boundRange = mipsolver.mipdata_->domain.col_upper_[i] -
mipsolver.mipdata_->domain.col_lower_[i];
if (boundRange == 0.0) continue;
double tolerance =
mipsolver.mipdata_->feastol * std::min(boundRange, 1.0);
if (analyticCenter[i] <= mipsolver.model_->col_lower_[i] + tolerance) {
mipsolver.mipdata_->domain.changeBound(
HighsBoundType::kUpper, i, mipsolver.model_->col_lower_[i],
HighsDomain::Reason::unspecified());
if (mipsolver.mipdata_->domain.infeasible()) return;
++nfixed;
if (mipsolver.variableType(i) == HighsVarType::kInteger) ++nintfixed;
} else if (analyticCenter[i] >=
mipsolver.model_->col_upper_[i] - tolerance) {
mipsolver.mipdata_->domain.changeBound(
HighsBoundType::kLower, i, mipsolver.model_->col_upper_[i],
HighsDomain::Reason::unspecified());
if (mipsolver.mipdata_->domain.infeasible()) return;
++nfixed;
if (mipsolver.variableType(i) == HighsVarType::kInteger) ++nintfixed;
}
}
if (nfixed > 0)
highsLogDev(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"Fixing %d columns (%d integers) sitting at bound at "
"analytic center\n",
int(nfixed), int(nintfixed));
mipsolver.mipdata_->domain.propagate();
if (mipsolver.mipdata_->domain.infeasible()) return;
}
}
void HighsMipSolverData::startSymmetryDetection(
const highs::parallel::TaskGroup& taskGroup,
std::unique_ptr<SymmetryDetectionData>& symData) {
symData = std::unique_ptr<SymmetryDetectionData>(new SymmetryDetectionData());
symData->symDetection.loadModelAsGraph(
mipsolver.mipdata_->presolvedModel,
mipsolver.options_mip_->small_matrix_value);
detectSymmetries = symData->symDetection.initializeDetection();
if (detectSymmetries) {
taskGroup.spawn([&]() {
double startTime = mipsolver.timer_.getWallTime();
symData->symDetection.run(symData->symmetries);
symData->detectionTime = mipsolver.timer_.getWallTime() - startTime;
});
} else
symData.reset();
}
void HighsMipSolverData::finishSymmetryDetection(
const highs::parallel::TaskGroup& taskGroup,
std::unique_ptr<SymmetryDetectionData>& symData) {
taskGroup.sync();
symmetries = std::move(symData->symmetries);
std::string symmetry_time =
mipsolver.options_mip_->timeless_log
? ""
: highsFormatToString(" %.1fs", symData->detectionTime);
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"\nSymmetry detection completed in%s\n", symmetry_time.c_str());
if (symmetries.numGenerators == 0) {
detectSymmetries = false;
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"No symmetry present\n\n");
} else if (symmetries.orbitopes.size() == 0) {
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"Found %d generator(s)\n\n", int(symmetries.numGenerators));
} else {
if (symmetries.numPerms != 0) {
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"Found %d generator(s) and %d full orbitope(s) acting on %d "
"columns\n\n",
int(symmetries.numPerms), int(symmetries.orbitopes.size()),
int(symmetries.columnToOrbitope.size()));
} else {
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"Found %d full orbitope(s) acting on %d columns\n\n",
int(symmetries.orbitopes.size()),
int(symmetries.columnToOrbitope.size()));
}
}
symData.reset();
for (HighsOrbitopeMatrix& orbitope : symmetries.orbitopes)
orbitope.determineOrbitopeType(cliquetable);
if (symmetries.numPerms != 0)
globalOrbits = symmetries.computeStabilizerOrbits(domain);
}
double HighsMipSolverData::limitsToGap(const double use_lower_bound,
const double use_upper_bound, double& lb,
double& ub) const {
double offset = mipsolver.model_->offset_;
lb = use_lower_bound + offset;
if (std::abs(lb) <= epsilon) lb = 0;
ub = kHighsInf;
double gap = kHighsInf;
if (use_upper_bound != kHighsInf) {
ub = use_upper_bound + offset;
if (std::fabs(ub) <= epsilon) ub = 0;
lb = std::min(ub, lb);
if (ub == 0.0)
gap = lb == 0.0 ? 0.0 : kHighsInf;
else
gap = (ub - lb) / fabs(ub);
}
return gap;
}
double HighsMipSolverData::computeNewUpperLimit(double ub, double mip_abs_gap,
double mip_rel_gap) const {
double new_upper_limit;
if (objectiveFunction.isIntegral()) {
new_upper_limit =
(std::floor(objectiveFunction.integralScale() * ub - 0.5) /
objectiveFunction.integralScale());
if (mip_rel_gap != 0.0)
new_upper_limit = std::min(
new_upper_limit,
ub - std::ceil(mip_rel_gap * fabs(ub + mipsolver.model_->offset_) *
objectiveFunction.integralScale() -
mipsolver.mipdata_->epsilon) /
objectiveFunction.integralScale());
if (mip_abs_gap != 0.0)
new_upper_limit = std::min(
new_upper_limit,
ub - std::ceil(mip_abs_gap * objectiveFunction.integralScale() -
mipsolver.mipdata_->epsilon) /
objectiveFunction.integralScale());
// add feasibility tolerance so that the next best integer feasible solution
// is definitely included in the remaining search
new_upper_limit += feastol;
} else {
new_upper_limit = std::min(ub - feastol, std::nextafter(ub, -kHighsInf));
if (mip_rel_gap != 0.0)
new_upper_limit =
std::min(new_upper_limit,
ub - mip_rel_gap * fabs(ub + mipsolver.model_->offset_));
if (mip_abs_gap != 0.0)
new_upper_limit = std::min(new_upper_limit, ub - mip_abs_gap);
}
return new_upper_limit;
}
bool HighsMipSolverData::moreHeuristicsAllowed() const {
// in the beginning of the search and in sub-MIP heuristics we only allow
// what is proportionally for the currently spent effort plus an initial
// offset. This is because in a sub-MIP we usually do a truncated search and
// therefore should not extrapolate the time we spent for heuristics as in
// the other case. Moreover, since we estimate the total effort for
// exploring the tree based on the weight of the already pruned nodes, the
// estimated effort the is not expected to be a good prediction in the
// beginning.
if (mipsolver.submip) {
return heuristic_lp_iterations < total_lp_iterations * heuristic_effort;
} else if (pruned_treeweight < 1e-3 &&
num_leaves - num_leaves_before_run < 10 &&
num_nodes - num_nodes_before_run < 1000) {
// in the main MIP solver allow an initial offset of 10000 heuristic LP
// iterations
if (heuristic_lp_iterations <
total_lp_iterations * heuristic_effort + 10000)
return true;
} else if (heuristic_lp_iterations <
100000 + ((total_lp_iterations - heuristic_lp_iterations -
sb_lp_iterations) >>
1)) {
// compute the node LP iterations in the current run as only those should be
// used when estimating the total required LP iterations to complete the
// search
int64_t heur_iters_curr_run =
heuristic_lp_iterations - heuristic_lp_iterations_before_run;
int64_t sb_iters_curr_run = sb_lp_iterations - sb_lp_iterations_before_run;
int64_t node_iters_curr_run = total_lp_iterations -
total_lp_iterations_before_run -
heur_iters_curr_run - sb_iters_curr_run;
// now estimate the total fraction of LP iterations that we have spent on
// heuristics by assuming the node iterations of the current run will
// grow proportional to the pruned weight of the current tree and the
// iterations spent for anything else are just added as an offset
double total_heuristic_effort_estim =
heuristic_lp_iterations /
((total_lp_iterations - node_iters_curr_run) +
node_iters_curr_run / std::max(0.01, double(pruned_treeweight)));
// since heuristics help most in the beginning of the search, we want to
// spent the time we have for heuristics in the first 80% of the tree
// exploration. Additionally we want to spent the proportional effort
// of heuristics that is allowed in the first 30% of tree exploration as
// fast as possible, which is why we have the max(0.3/0.8,...).
// Hence, in the first 30% of the tree exploration we allow to spent all
// effort available for heuristics in that part of the search as early as
// possible, whereas after that we allow the part that is proportionally
// adequate when we want to spent all available time in the first 80%.
if (total_heuristic_effort_estim <
std::max(0.3 / 0.8, std::min(double(pruned_treeweight), 0.8) / 0.8) *
heuristic_effort) {
// printf(
// "heuristic lp iterations: %ld, total_lp_iterations: %ld, "
// "total_heur_effort_estim = %.3f%%\n",
// heuristic_lp_iterations, total_lp_iterations,
// total_heuristic_effort_estim);
return true;
}
}
return false;
}
void HighsMipSolverData::removeFixedIndices() {
integral_cols.erase(
std::remove_if(integral_cols.begin(), integral_cols.end(),
[&](HighsInt col) { return domain.isFixed(col); }),
integral_cols.end());
integer_cols.erase(
std::remove_if(integer_cols.begin(), integer_cols.end(),
[&](HighsInt col) { return domain.isFixed(col); }),
integer_cols.end());
implint_cols.erase(
std::remove_if(implint_cols.begin(), implint_cols.end(),
[&](HighsInt col) { return domain.isFixed(col); }),
implint_cols.end());
continuous_cols.erase(
std::remove_if(continuous_cols.begin(), continuous_cols.end(),
[&](HighsInt col) { return domain.isFixed(col); }),
continuous_cols.end());
}
void HighsMipSolverData::init() {
postSolveStack.initializeIndexMaps(mipsolver.model_->num_row_,
mipsolver.model_->num_col_);
mipsolver.orig_model_ = mipsolver.model_;
feastol = mipsolver.options_mip_->mip_feasibility_tolerance;
epsilon = mipsolver.options_mip_->small_matrix_value;
if (mipsolver.clqtableinit)
cliquetable.buildFrom(mipsolver.orig_model_, *mipsolver.clqtableinit);
cliquetable.setMinEntriesForParallelism(
highs::parallel::num_threads() > 1
? mipsolver.options_mip_->mip_min_cliquetable_entries_for_parallelism
: kHighsIInf);
if (mipsolver.implicinit) implications.buildFrom(*mipsolver.implicinit);
heuristic_effort = mipsolver.options_mip_->mip_heuristic_effort;
detectSymmetries = mipsolver.options_mip_->mip_detect_symmetry;
firstlpsolobj = -kHighsInf;
rootlpsolobj = -kHighsInf;
analyticCenterComputed = false;
analyticCenterStatus = HighsModelStatus::kNotset;
maxTreeSizeLog2 = 0;
numRestarts = 0;
numRestartsRoot = 0;
numImprovingSols = 0;
pruned_treeweight = 0;
avgrootlpiters = 0;
num_nodes = 0;
num_nodes_before_run = 0;
num_leaves = 0;
num_leaves_before_run = 0;
total_repair_lp = 0;
total_repair_lp_feasible = 0;
total_repair_lp_iterations = 0;
total_lp_iterations = 0;
heuristic_lp_iterations = 0;
sepa_lp_iterations = 0;
sb_lp_iterations = 0;
total_lp_iterations_before_run = 0;
heuristic_lp_iterations_before_run = 0;
sepa_lp_iterations_before_run = 0;
sb_lp_iterations_before_run = 0;
num_disp_lines = 0;
numCliqueEntriesAfterPresolve = 0;
numCliqueEntriesAfterFirstPresolve = 0;
cliquesExtracted = false;
rowMatrixSet = false;
lower_bound = -kHighsInf;
upper_bound = kHighsInf;
upper_limit = mipsolver.options_mip_->objective_bound;
optimality_limit = mipsolver.options_mip_->objective_bound;
primal_dual_integral.initialise();
if (mipsolver.options_mip_->mip_report_level == 0)
dispfreq = 0;
else if (mipsolver.options_mip_->mip_report_level == 1)
dispfreq = 2000;
else
dispfreq = 100;
}
void HighsMipSolverData::runPresolve(const HighsInt presolve_reduction_limit) {
mipsolver.timer_.start(mipsolver.timer_.presolve_clock);
presolve::HPresolve presolve;
if (!presolve.okSetInput(mipsolver, presolve_reduction_limit)) {
mipsolver.modelstatus_ = HighsModelStatus::kMemoryLimit;
presolve_status = HighsPresolveStatus::kOutOfMemory;
} else {
mipsolver.modelstatus_ = presolve.run(postSolveStack);
presolve_status = presolve.getPresolveStatus();
}
mipsolver.timer_.stop(mipsolver.timer_.presolve_clock);
}
void HighsMipSolverData::runSetup() {
const HighsLp& model = *mipsolver.model_;
last_disptime = -kHighsInf;
disptime = 0;
// Transform the reference of the objective limit and lower/upper
// bounds from the original model to the current model, undoing the
// transformation done before restart so that the offset change due
// to presolve is incorporated. Bound changes are transitory, so no
// real gap change, and no update to P-D integral is necessary
upper_limit -= mipsolver.model_->offset_;
optimality_limit -= mipsolver.model_->offset_;
lower_bound -= mipsolver.model_->offset_;
upper_bound -= mipsolver.model_->offset_;
if (mipsolver.solution_objective_ != kHighsInf) {
incumbent = postSolveStack.getReducedPrimalSolution(mipsolver.solution_);
// return the objective value in the transformed space
double solobj =
mipsolver.solution_objective_ * (int)mipsolver.orig_model_->sense_ -
mipsolver.model_->offset_;
bool feasible = mipsolver.bound_violation_ <=
mipsolver.options_mip_->mip_feasibility_tolerance &&
mipsolver.integrality_violation_ <=
mipsolver.options_mip_->mip_feasibility_tolerance &&
mipsolver.row_violation_ <=
mipsolver.options_mip_->mip_feasibility_tolerance;
if (numRestarts == 0) {
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"\nMIP start solution is %s, objective value is %.12g\n",
feasible ? "feasible" : "infeasible",
mipsolver.solution_objective_);
}
if (feasible && solobj < upper_bound) {
double prev_upper_bound = upper_bound;
upper_bound = solobj;
bool bound_change = upper_bound != prev_upper_bound;
if (!mipsolver.submip && bound_change)
updatePrimalDualIntegral(lower_bound, lower_bound, prev_upper_bound,
upper_bound);
double new_upper_limit = computeNewUpperLimit(solobj, 0.0, 0.0);
saveReportMipSolution(new_upper_limit);
if (new_upper_limit < upper_limit) {
upper_limit = new_upper_limit;
optimality_limit =
computeNewUpperLimit(solobj, mipsolver.options_mip_->mip_abs_gap,
mipsolver.options_mip_->mip_rel_gap);
nodequeue.setOptimalityLimit(optimality_limit);
}
}
if (!mipsolver.submip && feasible && mipsolver.callback_->user_callback &&
mipsolver.callback_->active[kCallbackMipSolution]) {
assert(!mipsolver.submip);
mipsolver.callback_->clearHighsCallbackOutput();
mipsolver.callback_->data_out.mip_solution = mipsolver.solution_;
const bool interrupt = interruptFromCallbackWithData(
kCallbackMipSolution, mipsolver.solution_objective_,
"Feasible solution");
assert(!interrupt);
}
}
if (mipsolver.numCol() == 0)
addIncumbent(std::vector<double>(), 0, kSolutionSourceEmptyMip);
redcostfixing = HighsRedcostFixing();
pseudocost = HighsPseudocost(mipsolver);
nodequeue.setNumCol(mipsolver.numCol());
nodequeue.setOptimalityLimit(optimality_limit);
continuous_cols.clear();
integer_cols.clear();
implint_cols.clear();
integral_cols.clear();
rowMatrixSet = false;
if (!rowMatrixSet) {
rowMatrixSet = true;
highsSparseTranspose(model.num_row_, model.num_col_, model.a_matrix_.start_,
model.a_matrix_.index_, model.a_matrix_.value_,
ARstart_, ARindex_, ARvalue_);
// (re-)initialize number of uplocks and downlocks
uplocks.assign(model.num_col_, 0);
downlocks.assign(model.num_col_, 0);
for (HighsInt i = 0; i != model.num_col_; ++i) {
HighsInt start = model.a_matrix_.start_[i];
HighsInt end = model.a_matrix_.start_[i + 1];
for (HighsInt j = start; j != end; ++j) {
HighsInt row = model.a_matrix_.index_[j];
if (model.row_lower_[row] != -kHighsInf) {
if (model.a_matrix_.value_[j] < 0)
++uplocks[i];
else
++downlocks[i];
}
if (model.row_upper_[row] != kHighsInf) {
if (model.a_matrix_.value_[j] < 0)
++downlocks[i];
else
++uplocks[i];
}
}
}
}
rowintegral.resize(mipsolver.model_->num_row_);
// compute the maximal absolute coefficients to filter propagation
maxAbsRowCoef.resize(mipsolver.model_->num_row_);
for (HighsInt i = 0; i != mipsolver.model_->num_row_; ++i) {
double maxabsval = 0.0;
HighsInt start = ARstart_[i];
HighsInt end = ARstart_[i + 1];
bool integral = true;
for (HighsInt j = start; j != end; ++j) {
integral =
integral &&
mipsolver.variableType(ARindex_[j]) != HighsVarType::kContinuous &&
fractionality(ARvalue_[j]) <= epsilon;
maxabsval = std::max(maxabsval, std::abs(ARvalue_[j]));
}
if (integral) {
if (presolvedModel.row_lower_[i] != -kHighsInf)
presolvedModel.row_lower_[i] =
std::ceil(presolvedModel.row_lower_[i] - feastol);
if (presolvedModel.row_upper_[i] != kHighsInf)
presolvedModel.row_upper_[i] =
std::floor(presolvedModel.row_upper_[i] + feastol);
}
rowintegral[i] = integral;
maxAbsRowCoef[i] = maxabsval;
}
// compute row activities and propagate all rows once
objectiveFunction.setupCliquePartition(domain, cliquetable);
domain.setupObjectivePropagation();
domain.computeRowActivities();
domain.propagate();
if (domain.infeasible()) {
mipsolver.modelstatus_ = HighsModelStatus::kInfeasible;
double prev_lower_bound = lower_bound;
lower_bound = kHighsInf;
bool bound_change = lower_bound != prev_lower_bound;
if (!mipsolver.submip && bound_change)
updatePrimalDualIntegral(prev_lower_bound, lower_bound, upper_bound,
upper_bound);
pruned_treeweight = 1.0;
return;
}
if (model.num_col_ == 0) {
mipsolver.modelstatus_ = HighsModelStatus::kOptimal;
return;
}
if (checkLimits()) return;
// extract cliques if they have not been extracted before
for (HighsInt col : domain.getChangedCols())
implications.cleanupVarbounds(col);
domain.clearChangedCols();
lp.getLpSolver().setOptionValue("presolve", kHighsOffString);
// lp.getLpSolver().setOptionValue("dual_simplex_cost_perturbation_multiplier",
// 0.0); lp.getLpSolver().setOptionValue("parallel", kHighsOnString);
lp.getLpSolver().setOptionValue("simplex_initial_condition_check", false);
checkObjIntegrality();
rootlpsol.clear();
firstlpsol.clear();
HighsInt num_binary = 0;
HighsInt num_domain_fixed = 0;
maxTreeSizeLog2 = 0;
for (HighsInt i = 0; i != mipsolver.numCol(); ++i) {
switch (mipsolver.variableType(i)) {
case HighsVarType::kContinuous:
if (domain.isFixed(i)) {
num_domain_fixed++;
continue;
}
continuous_cols.push_back(i);
break;
case HighsVarType::kImplicitInteger:
if (domain.isFixed(i)) {
num_domain_fixed++;
continue;
}
implint_cols.push_back(i);
integral_cols.push_back(i);
break;
case HighsVarType::kInteger:
if (domain.isFixed(i)) {
num_domain_fixed++;
if (fractionality(domain.col_lower_[i]) > feastol) {
// integer variable is fixed to a fractional value -> infeasible
mipsolver.modelstatus_ = HighsModelStatus::kInfeasible;
double prev_lower_bound = lower_bound;
lower_bound = kHighsInf;
bool bound_change = lower_bound != prev_lower_bound;
if (!mipsolver.submip && bound_change)
updatePrimalDualIntegral(prev_lower_bound, lower_bound,
upper_bound, upper_bound);
pruned_treeweight = 1.0;
return;
}
continue;
}
integer_cols.push_back(i);
integral_cols.push_back(i);
maxTreeSizeLog2 += (HighsInt)std::ceil(
std::log2(std::min(1024.0, 1.0 + mipsolver.model_->col_upper_[i] -
mipsolver.model_->col_lower_[i])));
// NB Since this is for counting the number of times the
// condition is true using the bitwise operator avoids having
// any conditional branch whereas using the logical operator
// would require a branch due to short circuit
// evaluation. Semantically both is equivalent and correct. If
// there was any code to be executed for the condition being
// true then there would be a conditional branch in any case
// and I would have used the logical to begin with.
//
// Hence any compiler warning can be ignored safely
num_binary +=
(static_cast<HighsInt>(mipsolver.model_->col_lower_[i] == 0.0) &
static_cast<HighsInt>(mipsolver.model_->col_upper_[i] == 1.0));
break;
case HighsVarType::kSemiContinuous:
case HighsVarType::kSemiInteger:
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kError,
"Semicontinuous or semiinteger variables should have been "
"reformulated away before HighsMipSolverData::runSetup() "
"is called.");
throw std::logic_error("Unexpected variable type");
}
}
basisTransfer();
numintegercols = integer_cols.size();
detectSymmetries = detectSymmetries && num_binary > 0;
numCliqueEntriesAfterPresolve = cliquetable.getNumEntries();
HighsInt num_col = mipsolver.numCol();
HighsInt num_general_integer = numintegercols - num_binary;
HighsInt num_implied_integer = implint_cols.size();
HighsInt num_continuous = continuous_cols.size();
assert(num_col == num_continuous + num_binary + num_general_integer +
num_implied_integer + num_domain_fixed);
if (numRestarts == 0) {
numCliqueEntriesAfterFirstPresolve = cliquetable.getNumEntries();
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
// clang-format off
"\nSolving MIP model with:\n"
" %" HIGHSINT_FORMAT " rows\n"
" %" HIGHSINT_FORMAT " cols ("
"%" HIGHSINT_FORMAT" binary, "
"%" HIGHSINT_FORMAT " integer, "
"%" HIGHSINT_FORMAT" implied int., "
"%" HIGHSINT_FORMAT " continuous, "
"%" HIGHSINT_FORMAT " domain fixed)\n"
" %" HIGHSINT_FORMAT " nonzeros\n",
// clang-format on
mipsolver.numRow(), num_col, num_binary, num_general_integer,
num_implied_integer, num_continuous, num_domain_fixed,
mipsolver.numNonzero());
} else {
highsLogUser(mipsolver.options_mip_->log_options, HighsLogType::kInfo,
"Model after restart has "
// clang-format off
"%" HIGHSINT_FORMAT " rows, "
"%" HIGHSINT_FORMAT " cols ("
"%" HIGHSINT_FORMAT " bin., "
"%" HIGHSINT_FORMAT " int., "