-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathbranch_and_bound.cpp
More file actions
3802 lines (3345 loc) · 155 KB
/
branch_and_bound.cpp
File metadata and controls
3802 lines (3345 loc) · 155 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
/* clang-format off */
/*
* SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* clang-format on */
#include <branch_and_bound/branch_and_bound.hpp>
#include <branch_and_bound/mip_node.hpp>
#include <branch_and_bound/pseudo_costs.hpp>
#include <cuts/cuts.hpp>
#include <mip_heuristics/presolve/conflict_graph/clique_table.cuh>
#include <dual_simplex/basis_solves.hpp>
#include <dual_simplex/bounds_strengthening.hpp>
#include <dual_simplex/crossover.hpp>
#include <dual_simplex/initial_basis.hpp>
#include <dual_simplex/logger.hpp>
#include <dual_simplex/phase2.hpp>
#include <dual_simplex/presolve.hpp>
#include <dual_simplex/random.hpp>
#include <dual_simplex/tic_toc.hpp>
#include <dual_simplex/user_problem.hpp>
#include <raft/core/nvtx.hpp>
#include <utilities/hashing.hpp>
#include <omp.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <future>
#include <limits>
#include <map>
#include <optional>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
namespace cuopt::linear_programming::dual_simplex {
namespace {
template <typename f_t>
bool is_fractional(f_t x, variable_type_t var_type, f_t integer_tol)
{
if (var_type == variable_type_t::CONTINUOUS) {
return false;
} else {
f_t x_integer = std::round(x);
return (std::abs(x_integer - x) > integer_tol);
}
}
template <typename i_t, typename f_t>
i_t fractional_variables(const simplex_solver_settings_t<i_t, f_t>& settings,
const std::vector<f_t>& x,
const std::vector<variable_type_t>& var_types,
std::vector<i_t>& fractional)
{
const i_t n = x.size();
assert(x.size() == var_types.size());
for (i_t j = 0; j < n; ++j) {
if (is_fractional(x[j], var_types[j], settings.integer_tol)) { fractional.push_back(j); }
}
return fractional.size();
}
template <typename i_t, typename f_t>
void full_variable_types(const user_problem_t<i_t, f_t>& original_problem,
const lp_problem_t<i_t, f_t>& original_lp,
std::vector<variable_type_t>& var_types)
{
var_types = original_problem.var_types;
if (original_lp.num_cols > original_problem.num_cols) {
var_types.resize(original_lp.num_cols);
for (i_t k = original_problem.num_cols; k < original_lp.num_cols; k++) {
var_types[k] = variable_type_t::CONTINUOUS;
}
}
}
template <typename i_t, typename f_t>
bool check_guess(const lp_problem_t<i_t, f_t>& original_lp,
const simplex_solver_settings_t<i_t, f_t>& settings,
const std::vector<variable_type_t>& var_types,
const std::vector<f_t>& guess,
f_t& primal_error,
f_t& bound_error,
i_t& num_fractional)
{
bool feasible = false;
std::vector<f_t> residual(original_lp.num_rows);
residual = original_lp.rhs;
matrix_vector_multiply(original_lp.A, 1.0, guess, -1.0, residual);
primal_error = vector_norm_inf<i_t, f_t>(residual);
bound_error = 0.0;
constexpr bool verbose = false;
for (i_t j = 0; j < original_lp.num_cols; j++) {
// l_j <= x_j infeas means x_j < l_j or l_j - x_j > 0
const f_t low_bound_err = std::max(0.0, original_lp.lower[j] - guess[j]);
// x_j <= u_j infeas means u_j < x_j or x_j - u_j > 0
const f_t up_bound_err = std::max(0.0, guess[j] - original_lp.upper[j]);
if (verbose && (low_bound_err > settings.primal_tol || up_bound_err > settings.primal_tol)) {
settings.log.printf(
"Bound error %d variable value %e. Low %e Upper %e. Low Error %e Up Error %e\n",
j,
guess[j],
original_lp.lower[j],
original_lp.upper[j],
low_bound_err,
up_bound_err);
}
bound_error = std::max(bound_error, std::max(low_bound_err, up_bound_err));
}
if (verbose) { settings.log.printf("Bounds infeasibility %e\n", bound_error); }
std::vector<i_t> fractional;
num_fractional = fractional_variables(settings, guess, var_types, fractional);
if (verbose) { settings.log.printf("Fractional in solution %d\n", num_fractional); }
if (bound_error < settings.primal_tol && primal_error < 2 * settings.primal_tol &&
num_fractional == 0) {
if (verbose) { settings.log.printf("Solution is feasible\n"); }
feasible = true;
}
return feasible;
}
template <typename i_t, typename f_t>
void set_uninitialized_steepest_edge_norms(const lp_problem_t<i_t, f_t>& lp,
const std::vector<i_t>& basic_list,
std::vector<f_t>& edge_norms)
{
if (edge_norms.size() != lp.num_cols) { edge_norms.resize(lp.num_cols, -1.0); }
for (i_t k = 0; k < lp.num_rows; k++) {
const i_t j = basic_list[k];
if (edge_norms[j] <= 0.0) { edge_norms[j] = 1e-4; }
}
}
dual::status_t convert_lp_status_to_dual_status(lp_status_t status)
{
if (status == lp_status_t::OPTIMAL) {
return dual::status_t::OPTIMAL;
} else if (status == lp_status_t::INFEASIBLE) {
return dual::status_t::DUAL_UNBOUNDED;
} else if (status == lp_status_t::ITERATION_LIMIT) {
return dual::status_t::ITERATION_LIMIT;
} else if (status == lp_status_t::TIME_LIMIT) {
return dual::status_t::TIME_LIMIT;
} else if (status == lp_status_t::WORK_LIMIT) {
return dual::status_t::WORK_LIMIT;
} else if (status == lp_status_t::NUMERICAL_ISSUES) {
return dual::status_t::NUMERICAL;
} else if (status == lp_status_t::CUTOFF) {
return dual::status_t::CUTOFF;
} else if (status == lp_status_t::CONCURRENT_LIMIT) {
return dual::status_t::CONCURRENT_LIMIT;
} else if (status == lp_status_t::UNSET) {
return dual::status_t::UNSET;
} else {
return dual::status_t::NUMERICAL;
}
}
template <typename f_t>
f_t sgn(f_t x)
{
return x < 0 ? -1 : 1;
}
template <typename f_t>
f_t relative_gap(f_t obj_value, f_t lower_bound)
{
f_t user_mip_gap = obj_value == 0.0
? (lower_bound == 0.0 ? 0.0 : std::numeric_limits<f_t>::infinity())
: std::abs(obj_value - lower_bound) / std::abs(obj_value);
if (std::isnan(user_mip_gap)) { return std::numeric_limits<f_t>::infinity(); }
return user_mip_gap;
}
template <typename i_t, typename f_t>
f_t user_relative_gap(const lp_problem_t<i_t, f_t>& lp, f_t obj_value, f_t lower_bound)
{
f_t user_obj = compute_user_objective(lp, obj_value);
f_t user_lower_bound = compute_user_objective(lp, lower_bound);
f_t user_mip_gap = user_obj == 0.0
? (user_lower_bound == 0.0 ? 0.0 : std::numeric_limits<f_t>::infinity())
: std::abs(user_obj - user_lower_bound) / std::abs(user_obj);
if (std::isnan(user_mip_gap)) { return std::numeric_limits<f_t>::infinity(); }
return user_mip_gap;
}
template <typename f_t>
std::string user_mip_gap(f_t obj_value, f_t lower_bound)
{
const f_t user_mip_gap = relative_gap(obj_value, lower_bound);
if (user_mip_gap == std::numeric_limits<f_t>::infinity()) {
return " - ";
} else {
constexpr int BUFFER_LEN = 32;
char buffer[BUFFER_LEN];
snprintf(buffer, BUFFER_LEN - 1, "%5.1f%%", user_mip_gap * 100);
return std::string(buffer);
}
}
#ifdef SHOW_DIVING_TYPE
inline char feasible_solution_symbol(search_strategy_t strategy)
{
switch (strategy) {
case search_strategy_t::BEST_FIRST: return 'B';
case search_strategy_t::COEFFICIENT_DIVING: return 'C';
case search_strategy_t::LINE_SEARCH_DIVING: return 'L';
case search_strategy_t::PSEUDOCOST_DIVING: return 'P';
case search_strategy_t::GUIDED_DIVING: return 'G';
default: return 'U';
}
}
#else
inline char feasible_solution_symbol(search_strategy_t strategy)
{
switch (strategy) {
case search_strategy_t::BEST_FIRST: return 'B';
case search_strategy_t::COEFFICIENT_DIVING: return 'D';
case search_strategy_t::LINE_SEARCH_DIVING: return 'D';
case search_strategy_t::PSEUDOCOST_DIVING: return 'D';
case search_strategy_t::GUIDED_DIVING: return 'D';
default: return 'U';
}
}
#endif
} // namespace
template <typename i_t, typename f_t>
branch_and_bound_t<i_t, f_t>::branch_and_bound_t(
const user_problem_t<i_t, f_t>& user_problem,
const simplex_solver_settings_t<i_t, f_t>& solver_settings,
f_t start_time,
std::shared_ptr<detail::clique_table_t<i_t, f_t>> clique_table)
: original_problem_(user_problem),
settings_(solver_settings),
clique_table_(std::move(clique_table)),
original_lp_(user_problem.handle_ptr, 1, 1, 1),
Arow_(1, 1, 0),
incumbent_(1),
root_relax_soln_(1, 1),
root_crossover_soln_(1, 1),
pc_(1),
solver_status_(mip_status_t::UNSET)
{
exploration_stats_.start_time = start_time;
#ifdef PRINT_CONSTRAINT_MATRIX
settings_.log.printf("A");
original_problem_.A.print_matrix();
#endif
dualize_info_t<i_t, f_t> dualize_info;
convert_user_problem(original_problem_, settings_, original_lp_, new_slacks_, dualize_info);
full_variable_types(original_problem_, original_lp_, var_types_);
// Check slack
#ifdef CHECK_SLACKS
assert(new_slacks_.size() == original_lp_.num_rows);
for (i_t slack : new_slacks_) {
const i_t col_start = original_lp_.A.col_start[slack];
const i_t col_end = original_lp_.A.col_start[slack + 1];
const i_t col_len = col_end - col_start;
if (col_len != 1) {
settings_.log.printf("Slack %d has %d nzs\n", slack, col_len);
assert(col_len == 1);
}
const i_t i = original_lp_.A.i[col_start];
const f_t x = original_lp_.A.x[col_start];
if (std::abs(x) != 1.0) {
settings_.log.printf("Slack %d row %d has non-unit coefficient %e\n", slack, i, x);
assert(std::abs(x) == 1.0);
}
}
#endif
upper_bound_ = inf;
root_objective_ = std::numeric_limits<f_t>::quiet_NaN();
root_lp_current_lower_bound_ = -inf;
}
template <typename i_t, typename f_t>
f_t branch_and_bound_t<i_t, f_t>::get_lower_bound()
{
f_t lower_bound = lower_bound_ceiling_.load();
f_t heap_lower_bound = node_queue_.get_lower_bound();
lower_bound = std::min(heap_lower_bound, lower_bound);
lower_bound = std::min(worker_pool_.get_lower_bound(), lower_bound);
if (std::isfinite(lower_bound)) {
return lower_bound;
} else if (std::isfinite(root_objective_)) {
return root_objective_;
} else {
return -inf;
}
}
template <typename i_t, typename f_t>
void branch_and_bound_t<i_t, f_t>::report_heuristic(f_t obj)
{
if (is_running_) {
f_t user_obj = compute_user_objective(original_lp_, obj);
f_t user_lower = compute_user_objective(original_lp_, get_lower_bound());
std::string user_gap = user_mip_gap<f_t>(user_obj, user_lower);
settings_.log.printf(
"H %+13.6e %+10.6e %s %9.2f\n",
user_obj,
user_lower,
user_gap.c_str(),
toc(exploration_stats_.start_time));
} else {
if (solving_root_relaxation_.load()) {
f_t user_obj = compute_user_objective(original_lp_, obj);
f_t user_lower = root_lp_current_lower_bound_.load();
std::string user_gap = user_mip_gap<f_t>(user_obj, user_lower);
settings_.log.printf(
"New solution from primal heuristics. Objective %+.6e. Gap %s. Time %.2f\n",
user_obj,
user_gap.c_str(),
toc(exploration_stats_.start_time));
} else {
settings_.log.printf("New solution from primal heuristics. Objective %+.6e. Time %.2f\n",
compute_user_objective(original_lp_, obj),
toc(exploration_stats_.start_time));
}
}
}
template <typename i_t, typename f_t>
void branch_and_bound_t<i_t, f_t>::report(
char symbol, f_t obj, f_t lower_bound, i_t node_depth, i_t node_int_infeas, double work_time)
{
update_user_bound(lower_bound);
const i_t nodes_explored = exploration_stats_.nodes_explored;
const i_t nodes_unexplored = exploration_stats_.nodes_unexplored;
const f_t user_obj = compute_user_objective(original_lp_, obj);
const f_t user_lower = compute_user_objective(original_lp_, lower_bound);
const f_t iters = static_cast<f_t>(exploration_stats_.total_lp_iters);
const f_t iter_node = nodes_explored > 0 ? iters / nodes_explored : iters;
const std::string user_gap = user_mip_gap<f_t>(user_obj, user_lower);
if (work_time >= 0) {
settings_.log.printf(
"%c %10d %10lu %+13.6e %+10.6e %6d %6d %7.1e %s %9.2f %9.2f\n",
symbol,
nodes_explored,
nodes_unexplored,
user_obj,
user_lower,
node_int_infeas,
node_depth,
iter_node,
user_gap.c_str(),
work_time,
toc(exploration_stats_.start_time));
} else {
settings_.log.printf("%c %10d %10lu %+13.6e %+10.6e %6d %6d %7.1e %s %9.2f\n",
symbol,
nodes_explored,
nodes_unexplored,
user_obj,
user_lower,
node_int_infeas,
node_depth,
iter_node,
user_gap.c_str(),
toc(exploration_stats_.start_time));
}
}
template <typename i_t, typename f_t>
i_t branch_and_bound_t<i_t, f_t>::find_reduced_cost_fixings(f_t upper_bound,
std::vector<f_t>& lower_bounds,
std::vector<f_t>& upper_bounds)
{
std::vector<f_t> reduced_costs = root_relax_soln_.z;
lower_bounds = original_lp_.lower;
upper_bounds = original_lp_.upper;
std::vector<bool> bounds_changed(original_lp_.num_cols, false);
const f_t root_obj = compute_objective(original_lp_, root_relax_soln_.x);
const f_t threshold = 100.0 * settings_.integer_tol;
const f_t weaken = settings_.integer_tol;
const f_t fixed_tol = settings_.fixed_tol;
i_t num_improved = 0;
i_t num_fixed = 0;
i_t num_cols_to_check = reduced_costs.size(); // Reduced costs will be smaller than the original
// problem because we have added slacks for cuts
for (i_t j = 0; j < num_cols_to_check; j++) {
if (std::isfinite(reduced_costs[j]) && std::abs(reduced_costs[j]) > threshold) {
const f_t lower_j = original_lp_.lower[j];
const f_t upper_j = original_lp_.upper[j];
const f_t abs_gap = upper_bound - root_obj;
f_t reduced_cost_upper_bound = upper_j;
f_t reduced_cost_lower_bound = lower_j;
if (lower_j > -inf && reduced_costs[j] > 0) {
const f_t new_upper_bound = lower_j + abs_gap / reduced_costs[j];
reduced_cost_upper_bound = var_types_[j] == variable_type_t::INTEGER
? std::floor(new_upper_bound + weaken)
: new_upper_bound;
if (reduced_cost_upper_bound < upper_j && var_types_[j] == variable_type_t::INTEGER) {
num_improved++;
upper_bounds[j] = reduced_cost_upper_bound;
bounds_changed[j] = true;
}
}
if (upper_j < inf && reduced_costs[j] < 0) {
const f_t new_lower_bound = upper_j + abs_gap / reduced_costs[j];
reduced_cost_lower_bound = var_types_[j] == variable_type_t::INTEGER
? std::ceil(new_lower_bound - weaken)
: new_lower_bound;
if (reduced_cost_lower_bound > lower_j && var_types_[j] == variable_type_t::INTEGER) {
num_improved++;
lower_bounds[j] = reduced_cost_lower_bound;
bounds_changed[j] = true;
}
}
if (var_types_[j] == variable_type_t::INTEGER &&
reduced_cost_upper_bound <= reduced_cost_lower_bound + fixed_tol) {
num_fixed++;
}
}
}
if (num_fixed > 0 || num_improved > 0) {
settings_.log.printf(
"Reduced costs: Found %d improved bounds and %d fixed variables\n", num_improved, num_fixed);
}
return num_fixed;
}
template <typename i_t, typename f_t>
void branch_and_bound_t<i_t, f_t>::update_user_bound(f_t lower_bound)
{
if (user_bound_callback_ == nullptr) { return; }
f_t user_lower = compute_user_objective(original_lp_, lower_bound);
user_bound_callback_(user_lower);
}
template <typename i_t, typename f_t>
void branch_and_bound_t<i_t, f_t>::set_new_solution(const std::vector<f_t>& solution)
{
mutex_original_lp_.lock();
if (solution.size() != original_problem_.num_cols) {
settings_.log.printf(
"Solution size mismatch %ld %d\n", solution.size(), original_problem_.num_cols);
}
std::vector<f_t> crushed_solution;
crush_primal_solution<i_t, f_t>(
original_problem_, original_lp_, solution, new_slacks_, crushed_solution);
f_t obj = compute_objective(original_lp_, crushed_solution);
mutex_original_lp_.unlock();
bool is_feasible = false;
bool attempt_repair = false;
mutex_upper_.lock();
f_t current_upper_bound = upper_bound_;
mutex_upper_.unlock();
if (obj < current_upper_bound) {
f_t primal_err;
f_t bound_err;
i_t num_fractional;
mutex_original_lp_.lock();
if (crushed_solution.size() != original_lp_.num_cols) {
// original problem has been modified since the solution was crushed
// we need to re-crush the solution
crush_primal_solution<i_t, f_t>(
original_problem_, original_lp_, solution, new_slacks_, crushed_solution);
}
is_feasible = check_guess(
original_lp_, settings_, var_types_, crushed_solution, primal_err, bound_err, num_fractional);
mutex_original_lp_.unlock();
mutex_upper_.lock();
if (is_feasible && obj < upper_bound_) {
upper_bound_ = obj;
incumbent_.set_incumbent_solution(obj, crushed_solution);
} else {
attempt_repair = true;
constexpr bool verbose = false;
if (verbose) {
settings_.log.printf(
"Injected solution infeasible. Constraint error %e bound error %e integer infeasible "
"%d\n",
primal_err,
bound_err,
num_fractional);
}
}
mutex_upper_.unlock();
} else {
settings_.log.debug("Solution objective not better than current upper_bound_. Not accepted.\n");
}
if (is_feasible) { report_heuristic(obj); }
if (attempt_repair) {
mutex_repair_.lock();
repair_queue_.push_back(solution);
mutex_repair_.unlock();
}
}
template <typename i_t, typename f_t>
void branch_and_bound_t<i_t, f_t>::queue_external_solution_deterministic(
const std::vector<f_t>& solution, double work_unit_ts)
{
// In deterministic mode, queue the solution to be processed at the correct work unit timestamp
// This ensures deterministic ordering of solution events
if (solution.size() != original_problem_.num_cols) {
settings_.log.printf(
"Solution size mismatch %ld %d\n", solution.size(), original_problem_.num_cols);
return;
}
mutex_original_lp_.lock();
std::vector<f_t> crushed_solution;
crush_primal_solution<i_t, f_t>(
original_problem_, original_lp_, solution, new_slacks_, crushed_solution);
f_t obj = compute_objective(original_lp_, crushed_solution);
// Validate solution before queueing
f_t primal_err;
f_t bound_err;
i_t num_fractional;
bool is_feasible = check_guess(
original_lp_, settings_, var_types_, crushed_solution, primal_err, bound_err, num_fractional);
mutex_original_lp_.unlock();
if (!is_feasible) {
// Queue the uncrushed solution for repair; it will be crushed at
// consumption time so that the crush reflects the current LP state
// (which may have gained slack columns from cuts added after this point).
mutex_repair_.lock();
repair_queue_.push_back(solution);
mutex_repair_.unlock();
return;
}
// Queue the solution with its work unit timestamp
mutex_heuristic_queue_.lock();
heuristic_solution_queue_.push_back({obj, std::move(crushed_solution), 0, -1, 0, work_unit_ts});
mutex_heuristic_queue_.unlock();
}
template <typename i_t, typename f_t>
bool branch_and_bound_t<i_t, f_t>::repair_solution(const std::vector<f_t>& edge_norms,
const std::vector<f_t>& potential_solution,
f_t& repaired_obj,
std::vector<f_t>& repaired_solution) const
{
bool feasible = false;
repaired_obj = std::numeric_limits<f_t>::quiet_NaN();
i_t n = original_lp_.num_cols;
assert(potential_solution.size() == n);
lp_problem_t repair_lp = original_lp_;
// Fix integer variables
for (i_t j = 0; j < n; ++j) {
if (var_types_[j] == variable_type_t::INTEGER) {
const f_t fixed_val = std::round(potential_solution[j]);
repair_lp.lower[j] = fixed_val;
repair_lp.upper[j] = fixed_val;
}
}
lp_solution_t<i_t, f_t> lp_solution(original_lp_.num_rows, original_lp_.num_cols);
i_t iter = 0;
f_t lp_start_time = tic();
simplex_solver_settings_t lp_settings = settings_;
std::vector<variable_status_t> vstatus = root_vstatus_;
lp_settings.set_log(false);
lp_settings.inside_mip = true;
std::vector<f_t> leaf_edge_norms = edge_norms;
// should probably set the cut off here lp_settings.cut_off
dual::status_t lp_status = dual_phase2(
2, 0, lp_start_time, repair_lp, lp_settings, vstatus, lp_solution, iter, leaf_edge_norms);
repaired_solution = lp_solution.x;
if (lp_status == dual::status_t::OPTIMAL) {
f_t primal_error;
f_t bound_error;
i_t num_fractional;
feasible = check_guess(original_lp_,
settings_,
var_types_,
lp_solution.x,
primal_error,
bound_error,
num_fractional);
repaired_obj = compute_objective(original_lp_, repaired_solution);
constexpr bool verbose = false;
if (verbose) {
settings_.log.printf(
"After repair: feasible %d primal error %e bound error %e fractional %d. Objective %e\n",
feasible,
primal_error,
bound_error,
num_fractional,
repaired_obj);
}
}
return feasible;
}
template <typename i_t, typename f_t>
void branch_and_bound_t<i_t, f_t>::repair_heuristic_solutions()
{
raft::common::nvtx::range scope("BB::repair_heuristics");
// Check if there are any solutions to repair
std::vector<std::vector<f_t>> to_repair;
mutex_repair_.lock();
if (repair_queue_.size() > 0) {
to_repair = repair_queue_;
repair_queue_.clear();
}
mutex_repair_.unlock();
if (to_repair.size() > 0) {
settings_.log.debug("Attempting to repair %ld injected solutions\n", to_repair.size());
for (const std::vector<f_t>& uncrushed_solution : to_repair) {
std::vector<f_t> crushed_solution;
crush_primal_solution<i_t, f_t>(
original_problem_, original_lp_, uncrushed_solution, new_slacks_, crushed_solution);
std::vector<f_t> repaired_solution;
f_t repaired_obj;
bool is_feasible =
repair_solution(edge_norms_, crushed_solution, repaired_obj, repaired_solution);
if (is_feasible) {
mutex_upper_.lock();
if (repaired_obj < upper_bound_) {
upper_bound_ = repaired_obj;
incumbent_.set_incumbent_solution(repaired_obj, repaired_solution);
report_heuristic(repaired_obj);
if (settings_.solution_callback != nullptr) {
std::vector<f_t> original_x;
uncrush_primal_solution(original_problem_, original_lp_, repaired_solution, original_x);
settings_.solution_callback(original_x, repaired_obj);
}
}
mutex_upper_.unlock();
}
}
}
}
template <typename i_t, typename f_t>
void branch_and_bound_t<i_t, f_t>::set_solution_at_root(mip_solution_t<i_t, f_t>& solution,
const cut_info_t<i_t, f_t>& cut_info)
{
mutex_upper_.lock();
incumbent_.set_incumbent_solution(root_objective_, root_relax_soln_.x);
upper_bound_ = root_objective_;
mutex_upper_.unlock();
print_cut_info(settings_, cut_info);
// We should be done here
uncrush_primal_solution(original_problem_, original_lp_, incumbent_.x, solution.x);
solution.objective = incumbent_.objective;
solution.lower_bound = root_objective_;
solution.nodes_explored = 0;
solution.simplex_iterations = root_relax_soln_.iterations;
settings_.log.printf("Optimal solution found at root node. Objective %.16e. Time %.2f.\n",
compute_user_objective(original_lp_, root_objective_),
toc(exploration_stats_.start_time));
if (settings_.solution_callback != nullptr) {
settings_.solution_callback(solution.x, solution.objective);
}
if (settings_.heuristic_preemption_callback != nullptr) {
settings_.heuristic_preemption_callback();
}
}
template <typename i_t, typename f_t>
void branch_and_bound_t<i_t, f_t>::set_final_solution(mip_solution_t<i_t, f_t>& solution,
f_t lower_bound)
{
if (solver_status_ == mip_status_t::NUMERICAL) {
settings_.log.printf("Numerical issue encountered. Stopping the solver...\n");
}
if (solver_status_ == mip_status_t::TIME_LIMIT) {
settings_.log.printf("Time limit reached. Stopping the solver...\n");
}
if (solver_status_ == mip_status_t::WORK_LIMIT) {
settings_.log.printf("Work limit reached. Stopping the solver...\n");
}
if (solver_status_ == mip_status_t::NODE_LIMIT) {
settings_.log.printf("Node limit reached. Stopping the solver...\n");
}
if (settings_.heuristic_preemption_callback != nullptr) {
settings_.heuristic_preemption_callback();
}
f_t gap = upper_bound_ - lower_bound;
f_t obj = compute_user_objective(original_lp_, upper_bound_.load());
f_t user_bound = compute_user_objective(original_lp_, lower_bound);
f_t gap_rel = user_relative_gap(original_lp_, upper_bound_.load(), lower_bound);
bool is_maximization = original_lp_.obj_scale < 0.0;
settings_.log.printf("Explored %d nodes in %.2fs.\n",
exploration_stats_.nodes_explored,
toc(exploration_stats_.start_time));
settings_.log.printf("Absolute Gap %e Objective %.16e %s Bound %.16e\n",
gap,
obj,
is_maximization ? "Upper" : "Lower",
user_bound);
{
const f_t root_lp_obj = root_lp_current_lower_bound_.load();
if (std::isfinite(root_lp_obj)) {
settings_.log.printf("Root LP dual objective (last): %.16e\n", root_lp_obj);
}
}
if (gap <= settings_.absolute_mip_gap_tol || gap_rel <= settings_.relative_mip_gap_tol) {
solver_status_ = mip_status_t::OPTIMAL;
#ifdef CHECK_CUTS_AGAINST_SAVED_SOLUTION
if (settings_.sub_mip == 0) { write_solution_for_cut_verification(original_lp_, incumbent_.x); }
#endif
if (gap > 0 && gap <= settings_.absolute_mip_gap_tol) {
settings_.log.printf("Optimal solution found within absolute MIP gap tolerance (%.1e)\n",
settings_.absolute_mip_gap_tol);
} else if (gap > 0 && gap_rel <= settings_.relative_mip_gap_tol) {
settings_.log.printf("Optimal solution found within relative MIP gap tolerance (%.1e)\n",
settings_.relative_mip_gap_tol);
} else {
settings_.log.printf("Optimal solution found.\n");
}
if (settings_.heuristic_preemption_callback != nullptr) {
settings_.heuristic_preemption_callback();
}
}
if (solver_status_ == mip_status_t::UNSET) {
if (exploration_stats_.nodes_explored > 0 && exploration_stats_.nodes_unexplored == 0 &&
upper_bound_ == inf) {
settings_.log.printf("Integer infeasible.\n");
solver_status_ = mip_status_t::INFEASIBLE;
if (settings_.heuristic_preemption_callback != nullptr) {
settings_.heuristic_preemption_callback();
}
}
}
if (upper_bound_ != inf) {
assert(incumbent_.has_incumbent);
uncrush_primal_solution(original_problem_, original_lp_, incumbent_.x, solution.x);
}
solution.objective = incumbent_.objective;
solution.lower_bound = lower_bound;
solution.nodes_explored = exploration_stats_.nodes_explored;
solution.simplex_iterations = exploration_stats_.total_lp_iters;
}
template <typename i_t, typename f_t>
void branch_and_bound_t<i_t, f_t>::add_feasible_solution(f_t leaf_objective,
const std::vector<f_t>& leaf_solution,
i_t leaf_depth,
search_strategy_t thread_type)
{
bool send_solution = false;
settings_.log.debug("%c found a feasible solution with obj=%.10e.\n",
feasible_solution_symbol(thread_type),
compute_user_objective(original_lp_, leaf_objective));
mutex_upper_.lock();
if (leaf_objective < upper_bound_) {
incumbent_.set_incumbent_solution(leaf_objective, leaf_solution);
upper_bound_ = leaf_objective;
report(feasible_solution_symbol(thread_type), leaf_objective, get_lower_bound(), leaf_depth, 0);
send_solution = true;
}
if (send_solution && settings_.solution_callback != nullptr) {
std::vector<f_t> original_x;
uncrush_primal_solution(original_problem_, original_lp_, incumbent_.x, original_x);
settings_.solution_callback(original_x, upper_bound_);
}
mutex_upper_.unlock();
}
// Martin's criteria for the preferred rounding direction (see [1])
// [1] A. Martin, “Integer Programs with Block Structure,”
// Technische Universit¨at Berlin, Berlin, 1999. Accessed: Aug. 08, 2025.
// [Online]. Available: https://opus4.kobv.de/opus4-zib/frontdoor/index/index/docId/391
template <typename f_t>
rounding_direction_t martin_criteria(f_t val, f_t root_val)
{
const f_t down_val = std::floor(root_val);
const f_t up_val = std::ceil(root_val);
const f_t down_dist = val - down_val;
const f_t up_dist = up_val - val;
constexpr f_t eps = 1e-6;
if (down_dist < up_dist + eps) {
return rounding_direction_t::DOWN;
} else {
return rounding_direction_t::UP;
}
}
template <typename i_t, typename f_t>
branch_variable_t<i_t> branch_and_bound_t<i_t, f_t>::variable_selection(
mip_node_t<i_t, f_t>* node_ptr,
const std::vector<i_t>& fractional,
branch_and_bound_worker_t<i_t, f_t>* worker)
{
logger_t log;
log.log = false;
i_t branch_var = -1;
rounding_direction_t round_dir = rounding_direction_t::NONE;
std::vector<f_t> current_incumbent;
std::vector<f_t>& solution = worker->leaf_solution.x;
switch (worker->search_strategy) {
case search_strategy_t::BEST_FIRST:
if (settings_.reliability_branching != 0) {
branch_var = pc_.reliable_variable_selection(node_ptr,
fractional,
solution,
settings_,
var_types_,
worker,
exploration_stats_,
upper_bound_,
worker_pool_.num_idle_workers(),
log);
} else {
branch_var = pc_.variable_selection(fractional, solution, log);
}
round_dir = martin_criteria(solution[branch_var], root_relax_soln_.x[branch_var]);
return {branch_var, round_dir};
case search_strategy_t::COEFFICIENT_DIVING:
return coefficient_diving(
original_lp_, fractional, solution, var_up_locks_, var_down_locks_, log);
case search_strategy_t::LINE_SEARCH_DIVING:
return line_search_diving(fractional, solution, root_relax_soln_.x, log);
case search_strategy_t::PSEUDOCOST_DIVING:
return pseudocost_diving(pc_, fractional, solution, root_relax_soln_.x, log);
case search_strategy_t::GUIDED_DIVING:
mutex_upper_.lock();
current_incumbent = incumbent_.x;
mutex_upper_.unlock();
return guided_diving(pc_, fractional, solution, current_incumbent, log);
default:
log.debug("Unknown variable selection method: %d\n", worker->search_strategy);
return {-1, rounding_direction_t::NONE};
}
}
// ============================================================================
// Policies for update_tree
// These allow sharing the tree update logic between the default and deterministic codepaths
// ============================================================================
// Compiler is able to devirtualize the policy objects in update_tree_impl.
// This is for self-documenting purposes
template <typename i_t, typename f_t>
struct tree_update_policy_t {
virtual ~tree_update_policy_t() = default;
virtual f_t upper_bound() const = 0;
virtual void update_pseudo_costs(mip_node_t<i_t, f_t>* node, f_t obj) = 0;
virtual void handle_integer_solution(mip_node_t<i_t, f_t>* node,
f_t obj,
const std::vector<f_t>& x) = 0;
virtual branch_variable_t<i_t> select_branch_variable(mip_node_t<i_t, f_t>* node,
const std::vector<i_t>& fractional,
const std::vector<f_t>& x) = 0;
virtual void update_objective_estimate(mip_node_t<i_t, f_t>* node,
const std::vector<i_t>& fractional,
const std::vector<f_t>& x) = 0;
virtual void on_node_completed(mip_node_t<i_t, f_t>* node,
node_status_t status,
rounding_direction_t dir) = 0;
virtual void on_numerical_issue(mip_node_t<i_t, f_t>*) = 0;
virtual void graphviz(search_tree_t<i_t, f_t>&, mip_node_t<i_t, f_t>*, const char*, f_t) = 0;
virtual void on_optimal_callback(const std::vector<f_t>&, f_t) = 0;
};
template <typename i_t, typename f_t>
struct nondeterministic_policy_t : tree_update_policy_t<i_t, f_t> {
branch_and_bound_t<i_t, f_t>& bnb;
branch_and_bound_worker_t<i_t, f_t>* worker;
logger_t& log;
nondeterministic_policy_t(branch_and_bound_t<i_t, f_t>& bnb,
branch_and_bound_worker_t<i_t, f_t>* worker,
logger_t& log)
: bnb(bnb), worker(worker), log(log)
{
}
f_t upper_bound() const override { return bnb.upper_bound_.load(); }
void update_pseudo_costs(mip_node_t<i_t, f_t>* node, f_t leaf_obj) override
{
bnb.pc_.update_pseudo_costs(node, leaf_obj);
}
void handle_integer_solution(mip_node_t<i_t, f_t>* node,
f_t obj,
const std::vector<f_t>& x) override
{
bnb.add_feasible_solution(obj, x, node->depth, worker->search_strategy);
}
branch_variable_t<i_t> select_branch_variable(mip_node_t<i_t, f_t>* node,
const std::vector<i_t>& fractional,
const std::vector<f_t>&) override
{
return bnb.variable_selection(node, fractional, worker);
}
void update_objective_estimate(mip_node_t<i_t, f_t>* node,
const std::vector<i_t>& fractional,
const std::vector<f_t>& x) override
{
if (worker->search_strategy == search_strategy_t::BEST_FIRST) {
logger_t pc_log;
pc_log.log = false;
node->objective_estimate = bnb.pc_.obj_estimate(fractional, x, node->lower_bound, pc_log);
}
}
void on_numerical_issue(mip_node_t<i_t, f_t>* node) override
{
if (worker->search_strategy == search_strategy_t::BEST_FIRST) {
fetch_min(bnb.lower_bound_ceiling_, node->lower_bound);
log.printf("LP returned numerical issue on node %d. Best bound set to %+10.6e.\n",
node->node_id,
compute_user_objective(bnb.original_lp_, bnb.lower_bound_ceiling_.load()));
}
}
void graphviz(search_tree_t<i_t, f_t>& tree,
mip_node_t<i_t, f_t>* node,
const char* label,
f_t value) override
{
tree.graphviz_node(log, node, label, value);
}
void on_optimal_callback(const std::vector<f_t>& x, f_t objective) override
{
if (worker->search_strategy == search_strategy_t::BEST_FIRST &&
bnb.settings_.node_processed_callback != nullptr) {
std::vector<f_t> original_x;
uncrush_primal_solution(bnb.original_problem_, bnb.original_lp_, x, original_x);
bnb.settings_.node_processed_callback(original_x, objective);
}
}
void on_node_completed(mip_node_t<i_t, f_t>*, node_status_t, rounding_direction_t) override {}
};
template <typename i_t, typename f_t, typename WorkerT>
struct deterministic_policy_base_t : tree_update_policy_t<i_t, f_t> {
branch_and_bound_t<i_t, f_t>& bnb;
WorkerT& worker;
deterministic_policy_base_t(branch_and_bound_t<i_t, f_t>& bnb, WorkerT& worker)
: bnb(bnb), worker(worker)
{
}
f_t upper_bound() const override { return worker.local_upper_bound; }
void update_pseudo_costs(mip_node_t<i_t, f_t>* node, f_t leaf_obj) override
{
if (node->branch_var < 0) return;
f_t change = std::max(leaf_obj - node->lower_bound, f_t(0));