-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathHighsMipSolver.cpp
More file actions
875 lines (751 loc) · 33.7 KB
/
HighsMipSolver.cpp
File metadata and controls
875 lines (751 loc) · 33.7 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the HiGHS linear optimization suite */
/* */
/* Available as open-source under the MIT License */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "mip/HighsMipSolver.h"
#include "lp_data/HighsLpUtils.h"
#include "lp_data/HighsModelUtils.h"
#include "mip/HighsCliqueTable.h"
#include "mip/HighsCutPool.h"
#include "mip/HighsDomain.h"
#include "mip/HighsImplications.h"
#include "mip/HighsLpRelaxation.h"
#include "mip/HighsMipSolverData.h"
#include "mip/HighsPseudocost.h"
#include "mip/HighsSearch.h"
#include "mip/HighsSeparation.h"
#include "mip/MipTimer.h"
#include "presolve/HPresolve.h"
#include "presolve/HighsPostsolveStack.h"
#include "presolve/PresolveComponent.h"
#include "util/HighsCDouble.h"
#include "util/HighsIntegers.h"
using std::fabs;
HighsMipSolver::HighsMipSolver(HighsCallback& callback,
const HighsOptions& options, const HighsLp& lp,
const HighsSolution& solution, bool submip,
HighsInt submip_level)
: callback_(&callback),
options_mip_(&options),
model_(&lp),
orig_model_(&lp),
solution_objective_(kHighsInf),
submip(submip),
submip_level(submip_level),
rootbasis(nullptr),
pscostinit(nullptr),
clqtableinit(nullptr),
implicinit(nullptr) {
assert(!submip || submip_level > 0);
max_submip_level = 0;
if (solution.value_valid) {
// MIP solver doesn't check row residuals, but they should be OK
// so validate using assert
#ifndef NDEBUG
bool valid, integral, feasible;
assessLpPrimalSolution("For debugging: ", options, lp, solution, valid,
integral, feasible);
assert(valid);
#endif
bound_violation_ = 0;
row_violation_ = 0;
integrality_violation_ = 0;
HighsCDouble obj = orig_model_->offset_;
assert((HighsInt)solution.col_value.size() == orig_model_->num_col_);
for (HighsInt i = 0; i != orig_model_->num_col_; ++i) {
const double value = solution.col_value[i];
obj += orig_model_->col_cost_[i] * value;
if (orig_model_->integrality_[i] == HighsVarType::kInteger) {
integrality_violation_ =
std::max(fractionality(value), integrality_violation_);
}
const double lower = orig_model_->col_lower_[i];
const double upper = orig_model_->col_upper_[i];
double primal_infeasibility;
if (value < lower - options_mip_->mip_feasibility_tolerance) {
primal_infeasibility = lower - value;
} else if (value > upper + options_mip_->mip_feasibility_tolerance) {
primal_infeasibility = value - upper;
} else
continue;
bound_violation_ = std::max(bound_violation_, primal_infeasibility);
}
for (HighsInt i = 0; i != orig_model_->num_row_; ++i) {
const double value = solution.row_value[i];
const double lower = orig_model_->row_lower_[i];
const double upper = orig_model_->row_upper_[i];
double primal_infeasibility;
if (value < lower - options_mip_->mip_feasibility_tolerance) {
primal_infeasibility = lower - value;
} else if (value > upper + options_mip_->mip_feasibility_tolerance) {
primal_infeasibility = value - upper;
} else
continue;
row_violation_ = std::max(row_violation_, primal_infeasibility);
}
solution_objective_ = double(obj);
solution_ = solution.col_value;
}
}
HighsMipSolver::~HighsMipSolver() = default;
void HighsMipSolver::run() {
modelstatus_ = HighsModelStatus::kNotset;
if (submip) {
analysis_.analyse_mip_time = false;
} else {
analysis_.timer_ = &this->timer_;
analysis_.setup(*orig_model_, *options_mip_);
}
timer_.start();
improving_solution_file_ = nullptr;
if (!submip && options_mip_->mip_improving_solution_file != "")
improving_solution_file_ =
fopen(options_mip_->mip_improving_solution_file.c_str(), "w");
mipdata_ = decltype(mipdata_)(new HighsMipSolverData(*this));
analysis_.mipTimerStart(kMipClockPresolve);
analysis_.mipTimerStart(kMipClockInit);
mipdata_->init();
analysis_.mipTimerStop(kMipClockInit);
analysis_.mipTimerStart(kMipClockRunPresolve);
mipdata_->runPresolve(options_mip_->presolve_reduction_limit);
analysis_.mipTimerStop(kMipClockRunPresolve);
analysis_.mipTimerStop(kMipClockPresolve);
if (analysis_.analyse_mip_time & !submip)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - completed presolve\n", timer_.read());
// Identify whether time limit has been reached (in presolve)
if (modelstatus_ == HighsModelStatus::kNotset &&
timer_.read() >= options_mip_->time_limit)
modelstatus_ = HighsModelStatus::kTimeLimit;
if (modelstatus_ != HighsModelStatus::kNotset) {
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"Presolve: %s\n",
utilModelStatusToString(modelstatus_).c_str());
if (modelstatus_ == HighsModelStatus::kOptimal) {
mipdata_->lower_bound = 0;
mipdata_->upper_bound = 0;
mipdata_->transformNewIntegerFeasibleSolution(std::vector<double>());
mipdata_->saveReportMipSolution();
}
cleanupSolve();
return;
}
analysis_.mipTimerStart(kMipClockSolve);
if (analysis_.analyse_mip_time & !submip)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - starting setup\n", timer_.read());
analysis_.mipTimerStart(kMipClockRunSetup);
mipdata_->runSetup();
analysis_.mipTimerStop(kMipClockRunSetup);
if (analysis_.analyse_mip_time & !submip)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - completed setup\n", timer_.read());
restart:
if (modelstatus_ == HighsModelStatus::kNotset) {
// Check limits have not been reached before evaluating root node
if (mipdata_->checkLimits()) {
cleanupSolve();
return;
}
// Apply the trivial heuristics
analysis_.mipTimerStart(kMipClockTrivialHeuristics);
HighsModelStatus model_status = mipdata_->trivialHeuristics();
if (modelstatus_ == HighsModelStatus::kNotset &&
model_status == HighsModelStatus::kInfeasible) {
// trivialHeuristics can spot trivial infeasibility, so act on it
modelstatus_ = model_status;
cleanupSolve();
return;
}
analysis_.mipTimerStop(kMipClockTrivialHeuristics);
if (analysis_.analyse_mip_time & !submip)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - starting evaluate root node\n",
timer_.read());
analysis_.mipTimerStart(kMipClockEvaluateRootNode);
mipdata_->evaluateRootNode();
analysis_.mipTimerStop(kMipClockEvaluateRootNode);
// Sometimes the analytic centre calculation is not completed when
// evaluateRootNode returns, so stop its clock if it's running
if (analysis_.analyse_mip_time &&
analysis_.mipTimerRunning(kMipClockIpmSolveLp))
analysis_.mipTimerStop(kMipClockIpmSolveLp);
if (analysis_.analyse_mip_time & !submip)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - completed evaluate root node\n",
timer_.read());
// age 5 times to remove stored but never violated cuts after root
// separation
analysis_.mipTimerStart(kMipClockPerformAging0);
mipdata_->cutpool.performAging();
mipdata_->cutpool.performAging();
mipdata_->cutpool.performAging();
mipdata_->cutpool.performAging();
mipdata_->cutpool.performAging();
analysis_.mipTimerStop(kMipClockPerformAging0);
}
if (mipdata_->nodequeue.empty() || mipdata_->checkLimits()) {
cleanupSolve();
return;
}
std::shared_ptr<const HighsBasis> basis;
HighsSearch search{*this, mipdata_->pseudocost};
mipdata_->debugSolution.registerDomain(search.getLocalDomain());
HighsSeparation sepa(*this);
search.setLpRelaxation(&mipdata_->lp);
sepa.setLpRelaxation(&mipdata_->lp);
double prev_lower_bound = mipdata_->lower_bound;
mipdata_->lower_bound = mipdata_->nodequeue.getBestLowerBound();
bool bound_change = mipdata_->lower_bound != prev_lower_bound;
if (!submip && bound_change)
mipdata_->updatePrimalDualIntegral(prev_lower_bound, mipdata_->lower_bound,
mipdata_->upper_bound,
mipdata_->upper_bound);
mipdata_->printDisplayLine();
search.installNode(mipdata_->nodequeue.popBestBoundNode());
int64_t numStallNodes = 0;
int64_t lastLbLeave = 0;
int64_t numQueueLeaves = 0;
HighsInt numHugeTreeEstim = 0;
int64_t numNodesLastCheck = mipdata_->num_nodes;
int64_t nextCheck = mipdata_->num_nodes;
double treeweightLastCheck = 0.0;
double upperLimLastCheck = mipdata_->upper_limit;
double lowerBoundLastCheck = mipdata_->lower_bound;
analysis_.mipTimerStart(kMipClockSearch);
while (search.hasNode()) {
analysis_.mipTimerStart(kMipClockPerformAging1);
mipdata_->conflictPool.performAging();
analysis_.mipTimerStop(kMipClockPerformAging1);
// set iteration limit for each lp solve during the dive to 10 times the
// average nodes
HighsInt iterlimit = 10 * std::max(mipdata_->lp.getAvgSolveIters(),
mipdata_->avgrootlpiters);
iterlimit = std::max({HighsInt{10000}, iterlimit,
HighsInt((3 * mipdata_->firstrootlpiters) / 2)});
mipdata_->lp.setIterationLimit(iterlimit);
// perform the dive and put the open nodes to the queue
size_t plungestart = mipdata_->num_nodes;
bool limit_reached = false;
bool considerHeuristics = true;
analysis_.mipTimerStart(kMipClockDive);
while (true) {
// Possibly apply primal heuristics
if (considerHeuristics && mipdata_->moreHeuristicsAllowed()) {
analysis_.mipTimerStart(kMipClockEvaluateNode);
const HighsSearch::NodeResult evaluate_node_result =
search.evaluateNode();
analysis_.mipTimerStop(kMipClockEvaluateNode);
if (evaluate_node_result == HighsSearch::NodeResult::kSubOptimal) break;
if (search.currentNodePruned()) {
++mipdata_->num_leaves;
search.flushStatistics();
} else {
analysis_.mipTimerStart(kMipClockPrimalHeuristics);
if (mipdata_->incumbent.empty()) {
analysis_.mipTimerStart(kMipClockRandomizedRounding0);
mipdata_->heuristics.randomizedRounding(
mipdata_->lp.getLpSolver().getSolution().col_value);
analysis_.mipTimerStop(kMipClockRandomizedRounding0);
}
if (mipdata_->incumbent.empty()) {
analysis_.mipTimerStart(kMipClockRens);
mipdata_->heuristics.RENS(
mipdata_->lp.getLpSolver().getSolution().col_value);
analysis_.mipTimerStop(kMipClockRens);
} else {
analysis_.mipTimerStart(kMipClockRins);
mipdata_->heuristics.RINS(
mipdata_->lp.getLpSolver().getSolution().col_value);
analysis_.mipTimerStop(kMipClockRins);
}
mipdata_->heuristics.flushStatistics();
analysis_.mipTimerStop(kMipClockPrimalHeuristics);
}
}
considerHeuristics = false;
if (mipdata_->domain.infeasible()) break;
if (!search.currentNodePruned()) {
double this_dive_time = -analysis_.mipTimerRead(kMipClockTheDive);
analysis_.mipTimerStart(kMipClockTheDive);
const HighsSearch::NodeResult search_dive_result = search.dive();
analysis_.mipTimerStop(kMipClockTheDive);
if (analysis_.analyse_mip_time) {
this_dive_time += analysis_.mipTimerRead(kMipClockNodeSearch);
analysis_.dive_time.push_back(this_dive_time);
}
if (search_dive_result == HighsSearch::NodeResult::kSubOptimal) break;
++mipdata_->num_leaves;
search.flushStatistics();
}
if (mipdata_->checkLimits()) {
limit_reached = true;
break;
}
HighsInt numPlungeNodes = mipdata_->num_nodes - plungestart;
if (numPlungeNodes >= 100) break;
analysis_.mipTimerStart(kMipClockBacktrackPlunge);
const bool backtrack_plunge = search.backtrackPlunge(mipdata_->nodequeue);
analysis_.mipTimerStop(kMipClockBacktrackPlunge);
if (!backtrack_plunge) break;
assert(search.hasNode());
if (mipdata_->conflictPool.getNumConflicts() >
options_mip_->mip_pool_soft_limit) {
analysis_.mipTimerStart(kMipClockPerformAging2);
mipdata_->conflictPool.performAging();
analysis_.mipTimerStop(kMipClockPerformAging2);
}
search.flushStatistics();
mipdata_->printDisplayLine();
// printf("continue plunging due to good estimate\n");
} // while (true)
analysis_.mipTimerStop(kMipClockDive);
analysis_.mipTimerStart(kMipClockOpenNodesToQueue);
search.openNodesToQueue(mipdata_->nodequeue);
analysis_.mipTimerStop(kMipClockOpenNodesToQueue);
search.flushStatistics();
if (limit_reached) {
double prev_lower_bound = mipdata_->lower_bound;
mipdata_->lower_bound = std::min(mipdata_->upper_bound,
mipdata_->nodequeue.getBestLowerBound());
bool bound_change = mipdata_->lower_bound != prev_lower_bound;
if (!submip && bound_change)
mipdata_->updatePrimalDualIntegral(
prev_lower_bound, mipdata_->lower_bound, mipdata_->upper_bound,
mipdata_->upper_bound);
mipdata_->printDisplayLine();
break;
}
// the search datastructure should have no installed node now
assert(!search.hasNode());
// propagate the global domain
analysis_.mipTimerStart(kMipClockDomainPropgate);
mipdata_->domain.propagate();
analysis_.mipTimerStop(kMipClockDomainPropgate);
analysis_.mipTimerStart(kMipClockPruneInfeasibleNodes);
mipdata_->pruned_treeweight += mipdata_->nodequeue.pruneInfeasibleNodes(
mipdata_->domain, mipdata_->feastol);
analysis_.mipTimerStop(kMipClockPruneInfeasibleNodes);
// if global propagation detected infeasibility, stop here
if (mipdata_->domain.infeasible()) {
mipdata_->nodequeue.clear();
mipdata_->pruned_treeweight = 1.0;
double prev_lower_bound = mipdata_->lower_bound;
mipdata_->lower_bound = std::min(kHighsInf, mipdata_->upper_bound);
bool bound_change = mipdata_->lower_bound != prev_lower_bound;
if (!submip && bound_change)
mipdata_->updatePrimalDualIntegral(
prev_lower_bound, mipdata_->lower_bound, mipdata_->upper_bound,
mipdata_->upper_bound);
mipdata_->printDisplayLine();
break;
}
double prev_lower_bound = mipdata_->lower_bound;
mipdata_->lower_bound = std::min(mipdata_->upper_bound,
mipdata_->nodequeue.getBestLowerBound());
bool bound_change = mipdata_->lower_bound != prev_lower_bound;
if (!submip && bound_change)
mipdata_->updatePrimalDualIntegral(
prev_lower_bound, mipdata_->lower_bound, mipdata_->upper_bound,
mipdata_->upper_bound);
mipdata_->printDisplayLine();
if (mipdata_->nodequeue.empty()) break;
// if global propagation found bound changes, we update the local domain
if (!mipdata_->domain.getChangedCols().empty()) {
analysis_.mipTimerStart(kMipClockUpdateLocalDomain);
highsLogDev(options_mip_->log_options, HighsLogType::kInfo,
"added %" HIGHSINT_FORMAT " global bound changes\n",
(HighsInt)mipdata_->domain.getChangedCols().size());
mipdata_->cliquetable.cleanupFixed(mipdata_->domain);
for (HighsInt col : mipdata_->domain.getChangedCols())
mipdata_->implications.cleanupVarbounds(col);
mipdata_->domain.setDomainChangeStack(std::vector<HighsDomainChange>());
search.resetLocalDomain();
mipdata_->domain.clearChangedCols();
mipdata_->removeFixedIndices();
analysis_.mipTimerStop(kMipClockUpdateLocalDomain);
}
if (!submip && mipdata_->num_nodes >= nextCheck) {
auto nTreeRestarts = mipdata_->numRestarts - mipdata_->numRestartsRoot;
double currNodeEstim =
numNodesLastCheck - mipdata_->num_nodes_before_run +
(mipdata_->num_nodes - numNodesLastCheck) *
double(1.0 - mipdata_->pruned_treeweight) /
std::max(
double(mipdata_->pruned_treeweight - treeweightLastCheck),
mipdata_->epsilon);
// printf(
// "nTreeRestarts: %d, numNodesThisRun: %ld, numNodesLastCheck: %ld,
// " "currNodeEstim: %g, " "prunedTreeWeightDelta: %g,
// numHugeTreeEstim: %d, numLeavesThisRun:
// "
// "%ld\n",
// nTreeRestarts, mipdata_->num_nodes -
// mipdata_->num_nodes_before_run, numNodesLastCheck -
// mipdata_->num_nodes_before_run, currNodeEstim, 100.0 *
// double(mipdata_->pruned_treeweight - treeweightLastCheck),
// numHugeTreeEstim,
// mipdata_->num_leaves - mipdata_->num_leaves_before_run);
bool doRestart = false;
double activeIntegerRatio =
1.0 - mipdata_->percentageInactiveIntegers() / 100.0;
activeIntegerRatio *= activeIntegerRatio;
if (!doRestart) {
double gapReduction = 1.0;
if (mipdata_->upper_limit != kHighsInf) {
double oldGap = upperLimLastCheck - lowerBoundLastCheck;
double newGap = mipdata_->upper_limit - mipdata_->lower_bound;
gapReduction = oldGap / newGap;
}
if (gapReduction < 1.0 + (0.05 / activeIntegerRatio) &&
currNodeEstim >=
activeIntegerRatio * 20 *
(mipdata_->num_nodes - mipdata_->num_nodes_before_run)) {
nextCheck = mipdata_->num_nodes + 100;
++numHugeTreeEstim;
} else {
numHugeTreeEstim = 0;
treeweightLastCheck = double(mipdata_->pruned_treeweight);
numNodesLastCheck = mipdata_->num_nodes;
upperLimLastCheck = mipdata_->upper_limit;
lowerBoundLastCheck = mipdata_->lower_bound;
}
// Possibly prevent restart - necessary for debugging presolve
// errors: see #1553
if (options_mip_->mip_allow_restart) {
int64_t minHugeTreeOffset =
(mipdata_->num_leaves - mipdata_->num_leaves_before_run) / 1000;
int64_t minHugeTreeEstim = HighsIntegers::nearestInteger(
activeIntegerRatio * (10 + minHugeTreeOffset) *
std::pow(1.5, nTreeRestarts));
doRestart = numHugeTreeEstim >= minHugeTreeEstim;
} else {
doRestart = false;
}
} else {
// count restart due to many fixings within the first 1000 nodes as
// root restart
++mipdata_->numRestartsRoot;
}
if (doRestart) {
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"\nRestarting search from the root node\n");
mipdata_->performRestart();
analysis_.mipTimerStop(kMipClockSearch);
goto restart;
}
} // if (!submip && mipdata_->num_nodes >= nextCheck))
// remove the iteration limit when installing a new node
// mipdata_->lp.setIterationLimit();
// loop to install the next node for the search
double this_node_search_time = -analysis_.mipTimerRead(kMipClockNodeSearch);
analysis_.mipTimerStart(kMipClockNodeSearch);
while (!mipdata_->nodequeue.empty()) {
// printf("popping node from nodequeue (length = %" HIGHSINT_FORMAT ")\n",
// (HighsInt)nodequeue.size());
assert(!search.hasNode());
if (numQueueLeaves - lastLbLeave >= 10) {
search.installNode(mipdata_->nodequeue.popBestBoundNode());
lastLbLeave = numQueueLeaves;
} else {
HighsInt bestBoundNodeStackSize =
mipdata_->nodequeue.getBestBoundDomchgStackSize();
double bestBoundNodeLb = mipdata_->nodequeue.getBestLowerBound();
HighsNodeQueue::OpenNode nextNode(mipdata_->nodequeue.popBestNode());
if (nextNode.lower_bound == bestBoundNodeLb &&
(HighsInt)nextNode.domchgstack.size() == bestBoundNodeStackSize)
lastLbLeave = numQueueLeaves;
search.installNode(std::move(nextNode));
}
++numQueueLeaves;
if (search.getCurrentEstimate() >= mipdata_->upper_limit) {
++numStallNodes;
if (options_mip_->mip_max_stall_nodes != kHighsIInf &&
numStallNodes >= options_mip_->mip_max_stall_nodes) {
limit_reached = true;
modelstatus_ = HighsModelStatus::kSolutionLimit;
break;
}
} else
numStallNodes = 0;
assert(search.hasNode());
// we evaluate the node directly here instead of performing a dive
// because we first want to check if the node is not fathomed due to
// new global information before we perform separation rounds for the node
if (search.evaluateNode() == HighsSearch::NodeResult::kSubOptimal)
search.currentNodeToQueue(mipdata_->nodequeue);
// if the node was pruned we remove it from the search and install the
// next node from the queue
if (search.currentNodePruned()) {
search.backtrack();
++mipdata_->num_leaves;
++mipdata_->num_nodes;
search.flushStatistics();
mipdata_->domain.propagate();
mipdata_->pruned_treeweight += mipdata_->nodequeue.pruneInfeasibleNodes(
mipdata_->domain, mipdata_->feastol);
if (mipdata_->domain.infeasible()) {
mipdata_->nodequeue.clear();
mipdata_->pruned_treeweight = 1.0;
double prev_lower_bound = mipdata_->lower_bound;
mipdata_->lower_bound = std::min(kHighsInf, mipdata_->upper_bound);
bool bound_change = mipdata_->lower_bound != prev_lower_bound;
if (!submip && bound_change)
mipdata_->updatePrimalDualIntegral(
prev_lower_bound, mipdata_->lower_bound, mipdata_->upper_bound,
mipdata_->upper_bound);
break;
}
if (mipdata_->checkLimits()) {
limit_reached = true;
break;
}
double prev_lower_bound = mipdata_->lower_bound;
mipdata_->lower_bound = std::min(
mipdata_->upper_bound, mipdata_->nodequeue.getBestLowerBound());
bool bound_change = mipdata_->lower_bound != prev_lower_bound;
if (!submip && bound_change)
mipdata_->updatePrimalDualIntegral(
prev_lower_bound, mipdata_->lower_bound, mipdata_->upper_bound,
mipdata_->upper_bound);
mipdata_->printDisplayLine();
if (!mipdata_->domain.getChangedCols().empty()) {
highsLogDev(options_mip_->log_options, HighsLogType::kInfo,
"added %" HIGHSINT_FORMAT " global bound changes\n",
(HighsInt)mipdata_->domain.getChangedCols().size());
mipdata_->cliquetable.cleanupFixed(mipdata_->domain);
for (HighsInt col : mipdata_->domain.getChangedCols())
mipdata_->implications.cleanupVarbounds(col);
mipdata_->domain.setDomainChangeStack(
std::vector<HighsDomainChange>());
search.resetLocalDomain();
mipdata_->domain.clearChangedCols();
mipdata_->removeFixedIndices();
}
continue;
}
// the node is still not fathomed, so perform separation
sepa.separate(search.getLocalDomain());
if (mipdata_->domain.infeasible()) {
search.cutoffNode();
search.openNodesToQueue(mipdata_->nodequeue);
mipdata_->nodequeue.clear();
mipdata_->pruned_treeweight = 1.0;
double prev_lower_bound = mipdata_->lower_bound;
mipdata_->lower_bound = std::min(kHighsInf, mipdata_->upper_bound);
bool bound_change = mipdata_->lower_bound != prev_lower_bound;
if (!submip && bound_change)
mipdata_->updatePrimalDualIntegral(
prev_lower_bound, mipdata_->lower_bound, mipdata_->upper_bound,
mipdata_->upper_bound);
break;
}
// after separation we store the new basis and proceed with the outer loop
// to perform a dive from this node
if (mipdata_->lp.getStatus() != HighsLpRelaxation::Status::kError &&
mipdata_->lp.getStatus() != HighsLpRelaxation::Status::kNotSet)
mipdata_->lp.storeBasis();
basis = mipdata_->lp.getStoredBasis();
if (!basis || !isBasisConsistent(mipdata_->lp.getLp(), *basis)) {
HighsBasis b = mipdata_->firstrootbasis;
b.row_status.resize(mipdata_->lp.numRows(), HighsBasisStatus::kBasic);
basis = std::make_shared<const HighsBasis>(std::move(b));
mipdata_->lp.setStoredBasis(basis);
}
break;
} // while(!mipdata_->nodequeue.empty())
analysis_.mipTimerStop(kMipClockNodeSearch);
if (analysis_.analyse_mip_time) {
this_node_search_time += analysis_.mipTimerRead(kMipClockNodeSearch);
analysis_.node_search_time.push_back(this_node_search_time);
}
if (limit_reached) break;
} // while(search.hasNode())
analysis_.mipTimerStop(kMipClockSearch);
cleanupSolve();
}
void HighsMipSolver::cleanupSolve() {
// Force a final logging line
mipdata_->printDisplayLine(kSolutionSourceCleanup);
// Stop the solve clock - which won't be running if presolve
// determines the model status
if (analysis_.mipTimerRunning(kMipClockSolve))
analysis_.mipTimerStop(kMipClockSolve);
// Need to complete the calculation of P-D integral, checking for NO
// gap change
mipdata_->updatePrimalDualIntegral(
mipdata_->lower_bound, mipdata_->lower_bound, mipdata_->upper_bound,
mipdata_->upper_bound, false);
analysis_.mipTimerStart(kMipClockPostsolve);
bool havesolution = solution_objective_ != kHighsInf;
bool feasible;
if (havesolution)
feasible =
bound_violation_ <= options_mip_->mip_feasibility_tolerance &&
integrality_violation_ <= options_mip_->mip_feasibility_tolerance &&
row_violation_ <= options_mip_->mip_feasibility_tolerance;
else
feasible = false;
dual_bound_ = mipdata_->lower_bound;
if (mipdata_->objectiveFunction.isIntegral()) {
double rounded_lower_bound =
std::ceil(mipdata_->lower_bound *
mipdata_->objectiveFunction.integralScale() -
mipdata_->feastol) /
mipdata_->objectiveFunction.integralScale();
dual_bound_ = std::max(dual_bound_, rounded_lower_bound);
}
dual_bound_ += model_->offset_;
primal_bound_ = mipdata_->upper_bound + model_->offset_;
node_count_ = mipdata_->num_nodes;
total_lp_iterations_ = mipdata_->total_lp_iterations;
dual_bound_ = std::min(dual_bound_, primal_bound_);
primal_dual_integral_ = mipdata_->primal_dual_integral.value;
// adjust objective sense in case of maximization problem
if (orig_model_->sense_ == ObjSense::kMaximize) {
dual_bound_ = -dual_bound_;
primal_bound_ = -primal_bound_;
}
if (modelstatus_ == HighsModelStatus::kNotset ||
modelstatus_ == HighsModelStatus::kInfeasible) {
if (feasible && havesolution)
modelstatus_ = HighsModelStatus::kOptimal;
else
modelstatus_ = HighsModelStatus::kInfeasible;
}
analysis_.mipTimerStop(kMipClockPostsolve);
timer_.stop();
std::string solutionstatus = "-";
if (havesolution) {
bool feasible =
bound_violation_ <= options_mip_->mip_feasibility_tolerance &&
integrality_violation_ <= options_mip_->mip_feasibility_tolerance &&
row_violation_ <= options_mip_->mip_feasibility_tolerance;
solutionstatus = feasible ? "feasible" : "infeasible";
}
gap_ = fabs(primal_bound_ - dual_bound_);
if (primal_bound_ == 0.0)
gap_ = dual_bound_ == 0.0 ? 0.0 : kHighsInf;
else if (primal_bound_ != kHighsInf)
gap_ = fabs(primal_bound_ - dual_bound_) / fabs(primal_bound_);
else
gap_ = kHighsInf;
std::array<char, 128> gapString = {};
if (gap_ == kHighsInf)
std::strcpy(gapString.data(), "inf");
else {
double printTol = std::max(std::min(1e-2, 1e-1 * gap_), 1e-6);
auto gapValString = highsDoubleToString(100.0 * gap_, printTol);
double gapTol = options_mip_->mip_rel_gap;
if (options_mip_->mip_abs_gap > options_mip_->mip_feasibility_tolerance) {
gapTol = primal_bound_ == 0.0
? kHighsInf
: std::max(gapTol,
options_mip_->mip_abs_gap / fabs(primal_bound_));
}
if (gapTol == 0.0)
std::snprintf(gapString.data(), gapString.size(), "%s%%",
gapValString.data());
else if (gapTol != kHighsInf) {
printTol = std::max(std::min(1e-2, 1e-1 * gapTol), 1e-6);
auto gapTolString = highsDoubleToString(100.0 * gapTol, printTol);
std::snprintf(gapString.data(), gapString.size(),
"%s%% (tolerance: %s%%)", gapValString.data(),
gapTolString.data());
} else
std::snprintf(gapString.data(), gapString.size(), "%s%% (tolerance: inf)",
gapValString.data());
}
bool timeless_log = options_mip_->timeless_log;
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"\nSolving report\n");
if (this->orig_model_->model_name_.length())
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
" Model %s\n",
this->orig_model_->model_name_.c_str());
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
" Status %s\n"
" Primal bound %.12g\n"
" Dual bound %.12g\n"
" Gap %s\n",
utilModelStatusToString(modelstatus_).c_str(), primal_bound_,
dual_bound_, gapString.data());
if (!timeless_log)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
" P-D integral %.12g\n",
mipdata_->primal_dual_integral.value);
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
" Solution status %s\n", solutionstatus.c_str());
if (solutionstatus != "-")
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
" %.12g (objective)\n"
" %.12g (bound viol.)\n"
" %.12g (int. viol.)\n"
" %.12g (row viol.)\n",
solution_objective_, bound_violation_, integrality_violation_,
row_violation_);
if (!timeless_log)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
" Timing %.2f (total)\n"
" %.2f (presolve)\n"
" %.2f (solve)\n"
" %.2f (postsolve)\n",
timer_.read(), analysis_.mipTimerRead(kMipClockPresolve),
analysis_.mipTimerRead(kMipClockSolve),
analysis_.mipTimerRead(kMipClockPostsolve));
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
" Max sub-MIP depth %d\n"
" Nodes %llu\n"
" Repair LPs %llu (%llu feasible; %llu iterations)\n"
" LP iterations %llu (total)\n"
" %llu (strong br.)\n"
" %llu (separation)\n"
" %llu (heuristics)\n",
int(max_submip_level), (long long unsigned)mipdata_->num_nodes,
(long long unsigned)mipdata_->total_repair_lp,
(long long unsigned)mipdata_->total_repair_lp_feasible,
(long long unsigned)mipdata_->total_repair_lp_iterations,
(long long unsigned)mipdata_->total_lp_iterations,
(long long unsigned)mipdata_->sb_lp_iterations,
(long long unsigned)mipdata_->sepa_lp_iterations,
(long long unsigned)mipdata_->heuristic_lp_iterations);
if (!timeless_log) analysis_.reportMipTimer();
assert(modelstatus_ != HighsModelStatus::kNotset);
}
// Only called in Highs::runPresolve
void HighsMipSolver::runPresolve(const HighsInt presolve_reduction_limit) {
mipdata_ = decltype(mipdata_)(new HighsMipSolverData(*this));
mipdata_->init();
mipdata_->runPresolve(presolve_reduction_limit);
}
const HighsLp& HighsMipSolver::getPresolvedModel() const {
return mipdata_->presolvedModel;
}
HighsPresolveStatus HighsMipSolver::getPresolveStatus() const {
return mipdata_->presolve_status;
}
presolve::HighsPostsolveStack HighsMipSolver::getPostsolveStack() const {
return mipdata_->postSolveStack;
}
void HighsMipSolver::callbackGetCutPool() const {
assert(callback_->user_callback);
assert(callback_->callbackActive(kCallbackMipGetCutPool));
HighsCallbackDataOut& data_out = callback_->data_out;
std::vector<double> cut_lower;
std::vector<double> cut_upper;
HighsSparseMatrix cut_matrix;
mipdata_->lp.getCutPool(data_out.cutpool_num_col, data_out.cutpool_num_cut,
cut_lower, cut_upper, cut_matrix);
data_out.cutpool_num_nz = cut_matrix.numNz();
data_out.cutpool_start = cut_matrix.start_.data();
data_out.cutpool_index = cut_matrix.index_.data();
data_out.cutpool_value = cut_matrix.value_.data();
data_out.cutpool_lower = cut_lower.data();
data_out.cutpool_upper = cut_upper.data();
callback_->user_callback(kCallbackMipGetCutPool, "MIP cut pool",
&callback_->data_out, &callback_->data_in,
callback_->user_callback_data);
}