18 #include "absl/strings/str_format.h"
34 return absl::StrFormat(
"[%g, %g]", lb, ub);
38 double trunc(
double d) {
return d > 0 ? floor(d) : ceil(d); }
48 in_mip_context_(false),
49 infinite_time_limit_(
TimeLimit::Infinite()),
50 time_limit_(infinite_time_limit_.get()) {}
57 #define RUN_PREPROCESSOR(name) \
58 RunAndPushIfRelevant(std::unique_ptr<Preprocessor>(new name(¶meters_)), \
59 #name, time_limit_, lp)
71 const int kMaxNumPasses = 20;
72 for (
int i = 0; i < kMaxNumPasses; ++i) {
73 const int old_stack_size = preprocessors_.size();
86 if (preprocessors_.size() == old_stack_size) {
88 VLOG(1) <<
"Reached fixed point after presolve pass #" << i;
102 const int old_stack_size = preprocessors_.size();
108 if (old_stack_size != preprocessors_.size()) {
124 return !preprocessors_.empty();
127 #undef RUN_PREPROCESSOR
129 void MainLpPreprocessor::RunAndPushIfRelevant(
130 std::unique_ptr<Preprocessor> preprocessor,
const std::string&
name,
136 const double start_time =
time_limit->GetElapsedTime();
146 if (preprocessor->Run(lp)) {
147 const EntryIndex new_num_entries = lp->
num_entries();
148 const double preprocess_time =
time_limit->GetElapsedTime() - start_time;
149 VLOG(1) << absl::StrFormat(
150 "%s(%fs): %d(%d) rows, %d(%d) columns, %d(%d) entries.",
name,
156 static_cast<int64>(new_num_entries.value()),
157 static_cast<int64>(new_num_entries.value() -
158 initial_num_entries_.value()));
159 status_ = preprocessor->status();
160 preprocessors_.push_back(std::move(preprocessor));
165 status_ = preprocessor->status();
167 VLOG(1) <<
name <<
" detected that the problem is "
175 while (!preprocessors_.empty()) {
176 preprocessors_.back()->RecoverSolution(solution);
177 preprocessors_.pop_back();
186 is_column_deleted_.
clear();
187 stored_value_.
clear();
197 if (
col >= is_column_deleted_.
size()) {
198 is_column_deleted_.
resize(
col + 1,
false);
202 is_column_deleted_[
col] =
true;
203 stored_value_[
col] = fixed_value;
204 stored_status_[
col] = status;
211 ColIndex old_index(0);
212 for (ColIndex
col(0);
col < is_column_deleted_.
size(); ++
col) {
213 if (is_column_deleted_[
col]) {
226 for (; old_index < num_cols; ++old_index) {
242 if (
row >= is_row_deleted_.
size()) {
245 is_row_deleted_[
row] =
true;
249 if (
row >= is_row_deleted_.
size())
return;
250 is_row_deleted_[
row] =
false;
254 return is_row_deleted_;
260 RowIndex old_index(0);
261 const RowIndex end = is_row_deleted_.
size();
262 for (RowIndex
row(0);
row < end; ++
row) {
263 if (is_row_deleted_[
row]) {
277 for (; old_index < num_rows; ++old_index) {
296 if (lower_bound == upper_bound) {
301 if (
value == lower_bound) {
305 if (
value == upper_bound) {
328 Fractional ComputeMaxVariableBoundsMagnitude(
const LinearProgram& lp) {
330 const ColIndex num_cols = lp.num_variables();
331 for (ColIndex
col(0);
col < num_cols; ++
col) {
333 max_bounds_magnitude,
334 std::max(MagnitudeOrZeroIfInfinite(lp.variable_lower_bounds()[
col]),
335 MagnitudeOrZeroIfInfinite(lp.variable_upper_bounds()[
col])));
337 return max_bounds_magnitude;
345 column_deletion_helper_.
Clear();
347 for (ColIndex
col(0);
col < num_cols; ++
col) {
354 if (objective_coefficient == 0) {
366 value = objective_coefficient > 0 ? lower_bound : upper_bound;
368 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, empty column " <<
col
369 <<
" has a minimization cost of " << objective_coefficient
371 <<
" [" << lower_bound <<
"," << upper_bound <<
"]";
372 status_ = ProblemStatus::INFEASIBLE_OR_UNBOUNDED;
379 col,
value, ComputeVariableStatus(
value, lower_bound, upper_bound));
383 return !column_deletion_helper_.
IsEmpty();
401 void SubtractColumnMultipleFromConstraintBound(ColIndex
col,
405 const RowIndex
row = e.row();
419 struct ColumnWithRepresentativeAndScaledCost {
420 ColumnWithRepresentativeAndScaledCost(ColIndex _col, ColIndex _representative,
427 bool operator<(
const ColumnWithRepresentativeAndScaledCost& other)
const {
430 return col < other.col;
451 int num_proportionality_classes = 0;
452 std::vector<ColIndex> proportional_columns;
458 ++num_proportionality_classes;
461 proportional_columns.push_back(
col);
464 if (proportional_columns.empty())
return false;
465 VLOG(1) <<
"The problem contains " << proportional_columns.size()
466 <<
" columns which belong to " << num_proportionality_classes
467 <<
" proportionality classes.";
471 column_factors_.
assign(num_cols, 0.0);
472 for (
const ColIndex
col : proportional_columns) {
486 for (
const ColIndex
col : proportional_columns) {
490 const bool is_rc_positive_or_zero =
492 const bool is_rc_negative_or_zero =
494 bool is_slope_upper_bounded = is_rc_positive_or_zero;
495 bool is_slope_lower_bounded = is_rc_negative_or_zero;
496 if (column_factors_[
col] < 0.0) {
497 std::swap(is_slope_lower_bounded, is_slope_upper_bounded);
501 column_factors_[
col];
502 if (is_slope_lower_bounded) {
506 if (is_slope_upper_bounded) {
513 for (
const ColIndex
col : proportional_columns) {
521 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, no feasible dual values"
522 <<
" can satisfy the constraints of the proportional columns"
524 <<
" the associated quantity must be in ["
527 status_ = ProblemStatus::INFEASIBLE_OR_UNBOUNDED;
534 for (
const ColIndex
col : proportional_columns) {
538 column_factors_[
col];
541 bool variable_can_be_fixed =
false;
549 variable_can_be_fixed =
true;
550 target_bound = (column_factors_[
col] >= 0.0) ? upper_bound : lower_bound;
554 variable_can_be_fixed =
true;
555 target_bound = (column_factors_[
col] >= 0.0) ? lower_bound : upper_bound;
558 if (variable_can_be_fixed) {
563 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED.";
564 status_ = ProblemStatus::INFEASIBLE_OR_UNBOUNDED;
570 ComputeVariableStatus(
target_bound, lower_bound, upper_bound));
576 std::vector<ColumnWithRepresentativeAndScaledCost> sorted_columns;
577 for (
const ColIndex
col : proportional_columns) {
582 sorted_columns.push_back(ColumnWithRepresentativeAndScaledCost(
587 std::sort(sorted_columns.begin(), sorted_columns.end());
596 for (
int i = 0; i < sorted_columns.size();) {
597 const ColIndex target_col = sorted_columns[i].col;
598 const ColIndex target_representative = sorted_columns[i].representative;
599 const Fractional target_scaled_cost = sorted_columns[i].scaled_cost;
606 for (++i; i < sorted_columns.size(); ++i) {
607 if (sorted_columns[i].
representative != target_representative)
break;
608 if (std::abs(sorted_columns[i].
scaled_cost - target_scaled_cost) >=
613 const ColIndex
col = sorted_columns[i].col;
616 lower_bounds_[
col] = lower_bound;
617 upper_bounds_[
col] = upper_bound;
618 merged_columns_[
col] = target_col;
623 column_factors_[
col] / column_factors_[target_col];
634 MinInMagnitudeOrZeroIfInfinite(lower_bound, upper_bound);
635 Fractional lower_diff = (lower_bound - target_value) * bound_factor;
636 Fractional upper_diff = (upper_bound - target_value) * bound_factor;
637 if (bound_factor < 0.0) {
638 std::swap(lower_diff, upper_diff);
643 SubtractColumnMultipleFromConstraintBound(
col, target_value, lp);
646 ComputeVariableStatus(target_value, lower_bound, upper_bound));
652 if (num_merged > 0) {
653 merged_columns_[target_col] = target_col;
654 const Fractional target_value = MinInMagnitudeOrZeroIfInfinite(
655 lower_bounds_[target_col], upper_bounds_[target_col]);
659 SubtractColumnMultipleFromConstraintBound(target_col, target_value, lp);
666 return !column_deletion_helper_.
IsEmpty();
677 const ColIndex num_cols = merged_columns_.
size();
680 DenseRow distance_to_bound(num_cols, 0.0);
681 DenseRow wanted_value(num_cols, 0.0);
685 for (ColIndex
col(0);
col < num_cols; ++
col) {
686 if (merged_columns_[
col] ==
col) {
690 if (distance_to_upper_bound < distance_to_lower_bound) {
691 distance_to_bound[
col] = distance_to_upper_bound;
692 is_distance_to_upper_bound[
col] =
true;
694 distance_to_bound[
col] = distance_to_lower_bound;
695 is_distance_to_upper_bound[
col] =
false;
697 is_representative_basic[
col] =
704 lower_bounds_[
col], upper_bounds_[
col]);
711 for (ColIndex
col(0);
col < num_cols; ++
col) {
723 const bool to_upper_bound =
724 (bound_factor > 0.0) == is_distance_to_upper_bound[
representative];
725 if (width <= scaled_distance) {
727 to_upper_bound ? lower_bounds_[
col] : upper_bounds_[
col];
730 lower_bounds_[
col], upper_bounds_[
col]);
731 distance_to_bound[
representative] -= width * std::abs(bound_factor);
734 to_upper_bound ? upper_bounds_[
col] - scaled_distance
735 : lower_bounds_[
col] + scaled_distance;
758 const bool use_this_variable =
759 (error * bound_factor > 0.0) ? (upper_bounds_[
col] ==
kInfinity)
761 if (use_this_variable) {
770 solution->
status != ProblemStatus::PRIMAL_FEASIBLE);
794 row_factors_.
assign(num_rows, 0.0);
795 for (RowIndex
row(0);
row < num_rows; ++
row) {
797 if (!row_transpose.
IsEmpty()) {
816 transpose,
parameters_.preprocessor_zero_tolerance());
818 int num_proportional_rows = 0;
819 for (RowIndex
row(0);
row < num_rows; ++
row) {
822 mapping[representative_row_as_col] = representative_row_as_col;
823 is_a_representative[
ColToRowIndex(representative_row_as_col)] =
true;
824 ++num_proportional_rows;
830 for (RowIndex
row(0);
row < num_rows; ++
row) {
836 const RowIndex representative_row =
ColToRowIndex(mapping[row_as_col]);
839 row_factors_[representative_row] / row_factors_[
row];
843 std::swap(implied_lb, implied_ub);
849 lower_bound_sources_[representative_row] =
row;
853 upper_bound_sources_[representative_row] =
row;
861 for (RowIndex
row(0);
row < num_rows; ++
row) {
862 if (!is_a_representative[
row])
continue;
863 const RowIndex lower_source = lower_bound_sources_[
row];
864 const RowIndex upper_source = upper_bound_sources_[
row];
869 if (lower_source == upper_source) {
873 row_deletion_helper_.
UnmarkRow(lower_source);
879 status_ = ProblemStatus::PRIMAL_INFEASIBLE;
886 row_deletion_helper_.
UnmarkRow(lower_source);
891 row_deletion_helper_.
UnmarkRow(upper_source);
899 RowIndex new_representative = lower_source;
900 RowIndex other = upper_source;
901 if (std::abs(row_factors_[new_representative]) <
902 std::abs(row_factors_[other])) {
903 std::swap(new_representative, other);
908 row_factors_[new_representative] / row_factors_[other];
912 std::swap(new_lb, new_ub);
915 lower_bound_sources_[new_representative] = new_representative;
916 upper_bound_sources_[new_representative] = new_representative;
919 lower_bound_sources_[new_representative] = other;
923 if (new_ub < lp->constraint_upper_bounds()[new_representative]) {
924 upper_bound_sources_[new_representative] = other;
928 const RowIndex new_lower_source =
929 lower_bound_sources_[new_representative];
930 if (new_lower_source == upper_bound_sources_[new_representative]) {
931 row_deletion_helper_.
UnmarkRow(new_lower_source);
932 lower_bound_sources_[new_representative] =
kInvalidRow;
933 upper_bound_sources_[new_representative] =
kInvalidRow;
942 if (new_lb > new_ub) {
943 if (lower_bound_sources_[new_representative] == new_representative) {
949 row_deletion_helper_.
UnmarkRow(new_representative);
956 return !row_deletion_helper_.
IsEmpty();
969 for (RowIndex
row(0);
row < num_rows; ++
row) {
970 const RowIndex lower_source = lower_bound_sources_[
row];
971 const RowIndex upper_source = upper_bound_sources_[
row];
986 const Fractional corrected_dual_value = lp_is_maximization_problem_
989 if (corrected_dual_value != 0.0) {
1000 const Fractional factor = row_factors_[
row] / row_factors_[lower_source];
1010 const Fractional factor = row_factors_[
row] / row_factors_[upper_source];
1037 for (ColIndex
col(0);
col < num_cols; ++
col) {
1040 if (lower_bound == upper_bound) {
1045 SubtractColumnMultipleFromConstraintBound(
col, fixed_value, lp);
1052 return !column_deletion_helper_.
IsEmpty();
1076 for (ColIndex
col(0);
col < num_cols; ++
col) {
1080 const RowIndex
row = e.row();
1083 implied_lower_bounds[
row] += lower * coeff;
1084 implied_upper_bounds[
row] += upper * coeff;
1086 implied_lower_bounds[
row] += upper * coeff;
1087 implied_upper_bounds[
row] += lower * coeff;
1095 int num_implied_free_constraints = 0;
1096 int num_forcing_constraints = 0;
1097 is_forcing_up_.
assign(num_rows,
false);
1099 for (RowIndex
row(0);
row < num_rows; ++
row) {
1100 if (row_degree[
row] == 0)
continue;
1106 implied_upper_bounds[
row]) ||
1109 VLOG(1) <<
"implied bound " << implied_lower_bounds[
row] <<
" "
1110 << implied_upper_bounds[
row];
1111 VLOG(1) <<
"constraint bound " << lower <<
" " << upper;
1112 status_ = ProblemStatus::PRIMAL_INFEASIBLE;
1120 is_forcing_down[
row] =
true;
1121 ++num_forcing_constraints;
1125 implied_lower_bounds[
row])) {
1126 is_forcing_up_[
row] =
true;
1127 ++num_forcing_constraints;
1138 implied_lower_bounds[
row]) &&
1142 ++num_implied_free_constraints;
1146 if (num_implied_free_constraints > 0) {
1147 VLOG(1) << num_implied_free_constraints <<
" implied free constraints.";
1150 if (num_forcing_constraints > 0) {
1151 VLOG(1) << num_forcing_constraints <<
" forcing constraints.";
1154 costs_.
resize(num_cols, 0.0);
1155 for (ColIndex
col(0);
col < num_cols; ++
col) {
1159 bool is_forced =
false;
1162 if (is_forcing_down[e.row()]) {
1163 const Fractional candidate = e.coefficient() < 0.0 ? lower : upper;
1172 target_bound = std::abs(lower) < std::abs(upper) ? lower : upper;
1175 VLOG(1) <<
"A variable is forced in both directions! bounds: ["
1176 << std::fixed << std::setprecision(10) << lower <<
", "
1177 << upper <<
"]. coeff:" << e.coefficient();
1178 status_ = ProblemStatus::PRIMAL_INFEASIBLE;
1184 if (is_forcing_up_[e.row()]) {
1185 const Fractional candidate = e.coefficient() < 0.0 ? upper : lower;
1190 target_bound = std::abs(lower) < std::abs(upper) ? lower : upper;
1193 VLOG(1) <<
"A variable is forced in both directions! bounds: ["
1194 << std::fixed << std::setprecision(10) << lower <<
", "
1195 << upper <<
"]. coeff:" << e.coefficient();
1196 status_ = ProblemStatus::PRIMAL_INFEASIBLE;
1214 for (RowIndex
row(0);
row < num_rows; ++
row) {
1221 if (is_forcing_down[
row] || is_forcing_up_[
row]) {
1229 return !column_deletion_helper_.
IsEmpty();
1240 const ColIndex num_cols = deleted_columns_.
num_cols();
1242 for (ColIndex
col(0);
col < num_cols; ++
col) {
1245 const RowIndex
row = e.row();
1247 last_deleted_row[
col] =
row;
1265 for (RowIndex
row(0);
row < num_rows; ++
row) {
1271 if (last_deleted_row[
col] !=
row)
continue;
1274 const Fractional reduced_cost = costs_[
col] - scalar_product;
1276 if (is_forcing_up_[
row] == !lp_is_maximization_problem_) {
1277 if (
bound < new_dual_value) {
1278 new_dual_value =
bound;
1279 new_basic_column =
col;
1282 if (
bound > new_dual_value) {
1283 new_dual_value =
bound;
1284 new_basic_column =
col;
1304 struct ColWithDegree {
1307 ColWithDegree(ColIndex c, EntryIndex n) :
col(c),
num_entries(n) {}
1308 bool operator<(
const ColWithDegree& other)
const {
1310 return col < other.col;
1327 const int size = num_rows.value();
1336 for (ColIndex
col(0);
col < num_cols; ++
col) {
1340 Fractional entry_lb = e.coefficient() * lower_bound;
1341 Fractional entry_ub = e.coefficient() * upper_bound;
1342 if (e.coefficient() < 0.0) std::swap(entry_lb, entry_ub);
1343 lb_sums[e.row()].Add(entry_lb);
1344 ub_sums[e.row()].Add(entry_ub);
1354 for (RowIndex
row(0);
row < num_rows; ++
row) {
1363 variable_offsets_.
assign(num_cols, 0.0);
1380 std::vector<ColWithDegree> col_by_degree;
1381 for (ColIndex
col(0);
col < num_cols; ++
col) {
1382 col_by_degree.push_back(
1385 std::sort(col_by_degree.begin(), col_by_degree.end());
1388 int num_already_free_variables = 0;
1389 int num_implied_free_variables = 0;
1390 int num_fixed_variables = 0;
1391 for (ColWithDegree col_with_degree : col_by_degree) {
1392 const ColIndex
col = col_with_degree.col;
1398 ++num_already_free_variables;
1401 if (lower_bound == upper_bound)
continue;
1409 if (used_rows[e.row()])
continue;
1415 if (coeff < 0.0) std::swap(entry_lb, entry_ub);
1421 Fractional implied_lb = -ub_sums[e.row()].SumWithout(entry_ub) / coeff;
1422 Fractional implied_ub = -lb_sums[e.row()].SumWithout(entry_lb) / coeff;
1423 if (coeff < 0.0) std::swap(implied_lb, implied_ub);
1424 overall_implied_lb =
std::max(overall_implied_lb, implied_lb);
1425 overall_implied_ub =
std::min(overall_implied_ub, implied_ub);
1432 overall_implied_ub)) {
1433 status_ = ProblemStatus::PRIMAL_INFEASIBLE;
1440 overall_implied_lb) ||
1446 ++num_fixed_variables;
1449 overall_implied_lb)) {
1455 ++num_fixed_variables;
1462 overall_implied_lb) &&
1465 ++num_implied_free_variables;
1468 used_rows[e.row()] =
true;
1491 MinInMagnitudeOrZeroIfInfinite(lower_bound, upper_bound);
1492 if (offset != 0.0) {
1493 variable_offsets_[
col] = offset;
1494 SubtractColumnMultipleFromConstraintBound(
col, offset, lp);
1496 postsolve_status_of_free_variables_[
col] =
1497 ComputeVariableStatus(offset, lower_bound, upper_bound);
1500 VLOG(1) << num_already_free_variables <<
" free variables in the problem.";
1501 VLOG(1) << num_implied_free_variables <<
" implied free columns.";
1502 VLOG(1) << num_fixed_variables <<
" variables can be fixed.";
1503 return num_implied_free_variables > 0;
1510 for (ColIndex
col(0);
col < num_cols; ++
col) {
1518 postsolve_status_of_free_variables_[
col];
1540 for (ColIndex doubleton_col(0); doubleton_col < num_cols; ++doubleton_col) {
1549 r.col = doubleton_col;
1553 if (row_deletion_helper_.
IsRowMarked(e.row()))
break;
1554 r.row[
index] = e.row();
1555 r.coeff[
index] = e.coefficient();
1559 if (
index != NUM_ROWS)
continue;
1571 if (std::abs(r.coeff[DELETED]) < std::abs(r.coeff[MODIFIED])) {
1572 std::swap(r.coeff[DELETED], r.coeff[MODIFIED]);
1573 std::swap(r.row[DELETED], r.row[MODIFIED]);
1580 r.deleted_row_as_column.Swap(
1589 new_variable_lb /= r.coeff[DELETED];
1590 new_variable_ub /= r.coeff[DELETED];
1591 if (r.coeff[DELETED] < 0.0) std::swap(new_variable_lb, new_variable_ub);
1597 r.deleted_row_as_column.AddMultipleToSparseVectorAndIgnoreCommonIndex(
1598 -r.coeff[MODIFIED] / r.coeff[DELETED],
ColToRowIndex(r.col),
1604 if (r.objective_coefficient != 0.0) {
1607 if (
col == r.col)
continue;
1610 e.coefficient() * r.objective_coefficient / r.coeff[DELETED];
1616 if (std::abs(new_objective) >
parameters_.drop_tolerance()) {
1624 restore_stack_.push_back(r);
1627 if (!row_deletion_helper_.
IsEmpty()) {
1640 for (
const RestoreInfo& r :
Reverse(restore_stack_)) {
1672 if (
col == r.col)
continue;
1673 new_variable_value -= (e.coefficient() / r.coeff[DELETED]) *
1686 r.objective_coefficient -
1687 r.coeff[MODIFIED] * solution->
dual_values[r.row[MODIFIED]];
1690 current_reduced_cost / r.coeff[DELETED];
1717 if (deleted_rows_as_column_.
IsEmpty()) {
1730 const RowIndex
row = e.row();
1738 const bool is_constraint_upper_bound_relevant =
1739 e.coefficient() > 0.0 ? !is_unbounded_up : is_unbounded_up;
1740 activity_sign_correction_[
row] =
1741 is_constraint_upper_bound_relevant ? 1.0 : -1.0;
1742 rhs_[
row] = is_constraint_upper_bound_relevant
1750 is_unbounded_[
col] =
true;
1751 Fractional initial_feasible_value = MinInMagnitudeOrZeroIfInfinite(
1755 col, initial_feasible_value,
1756 ComputeVariableStatus(initial_feasible_value,
1779 for (RowIndex
row(0);
row < num_rows; ++
row) {
1781 dual_ub_[
row] = 0.0;
1784 dual_lb_[
row] = 0.0;
1789 may_have_participated_lb_.
assign(num_cols,
false);
1790 may_have_participated_ub_.
assign(num_cols,
false);
1793 std::deque<ColIndex> columns_to_process;
1795 std::vector<RowIndex> changed_rows;
1796 for (ColIndex
col(0);
col < num_cols; ++
col) {
1797 columns_to_process.push_back(
col);
1803 const int limit = 5 * num_cols.value();
1804 for (
int count = 0; !columns_to_process.empty() && count < limit; ++count) {
1805 const ColIndex
col = columns_to_process.front();
1806 columns_to_process.pop_front();
1807 in_columns_to_process[
col] =
false;
1819 rc_lb.
Add(col_cost);
1820 rc_ub.
Add(col_cost);
1822 if (row_deletion_helper_.
IsRowMarked(e.row()))
continue;
1825 rc_lb.
Add(-coeff * dual_ub_[e.row()]);
1826 rc_ub.
Add(-coeff * dual_lb_[e.row()]);
1828 rc_lb.
Add(-coeff * dual_lb_[e.row()]);
1829 rc_ub.
Add(-coeff * dual_ub_[e.row()]);
1837 bool can_be_removed =
false;
1839 bool rc_is_away_from_zero;
1840 if (rc_ub.
Sum() <= low_tolerance) {
1841 can_be_removed =
true;
1843 rc_is_away_from_zero = rc_ub.
Sum() <= -high_tolerance;
1844 can_be_removed = !may_have_participated_ub_[
col];
1846 if (rc_lb.
Sum() >= -low_tolerance) {
1850 can_be_removed =
true;
1852 rc_is_away_from_zero = rc_lb.
Sum() >= high_tolerance;
1853 can_be_removed = !may_have_participated_lb_[
col];
1857 if (can_be_removed) {
1869 if (rc_is_away_from_zero) {
1870 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, variable " <<
col
1872 <<
" and its reduced cost is in [" << rc_lb.
Sum() <<
", "
1873 << rc_ub.
Sum() <<
"]";
1874 status_ = ProblemStatus::INFEASIBLE_OR_UNBOUNDED;
1885 if (col_cost != 0.0)
continue;
1890 if (IsConstraintBlockingVariable(*lp, e.coefficient(), e.row())) {
1915 changed_rows.clear();
1917 if (row_deletion_helper_.
IsRowMarked(e.row()))
continue;
1919 const RowIndex
row = e.row();
1923 if (candidate < dual_ub_[
row]) {
1924 dual_ub_[
row] = candidate;
1925 may_have_participated_lb_[
col] =
true;
1926 changed_rows.push_back(
row);
1930 if (candidate > dual_lb_[
row]) {
1931 dual_lb_[
row] = candidate;
1932 may_have_participated_lb_[
col] =
true;
1933 changed_rows.push_back(
row);
1940 if (candidate > dual_lb_[
row]) {
1941 dual_lb_[
row] = candidate;
1942 may_have_participated_ub_[
col] =
true;
1943 changed_rows.push_back(
row);
1947 if (candidate < dual_ub_[
row]) {
1948 dual_ub_[
row] = candidate;
1949 may_have_participated_ub_[
col] =
true;
1950 changed_rows.push_back(
row);
1956 if (!changed_rows.empty()) {
1958 for (
const RowIndex
row : changed_rows) {
1962 if (!in_columns_to_process[
col]) {
1963 columns_to_process.push_back(
col);
1964 in_columns_to_process[
col] =
true;
1976 for (ColIndex
col(0);
col < end; ++
col) {
1986 return !column_deletion_helper_.
IsEmpty() || !row_deletion_helper_.
IsEmpty();
1999 for (RowIndex
row(0);
row < num_rows; ++
row) {
2004 if (is_unbounded_[
col]) {
2005 last_deleted_column[
row] =
col;
2012 const ColIndex num_cols = is_unbounded_.
size();
2013 for (ColIndex
col(0);
col < num_cols; ++
col) {
2014 if (!is_unbounded_[
col])
continue;
2018 const RowIndex
row = e.row();
2034 if (activity * activity_sign_correction_[
row] < 0.0) {
2036 if (std::abs(
bound) > std::abs(primal_value_shift)) {
2037 primal_value_shift =
bound;
2046 activity_sign_correction_[row_at_bound] == 1.0
2061 for (RowIndex
row(0);
row < num_rows; ++
row) {
2069 return !row_deletion_helper_.
IsEmpty();
2091 for (ColIndex
col(0);
col < num_cols; ++
col) {
2098 for (RowIndex
row(0);
row < num_rows; ++
row) {
2099 if (degree[
row] == 0) {
2106 VLOG(1) <<
"Problem PRIMAL_INFEASIBLE, constraint " <<
row
2107 <<
" is empty and its range ["
2110 status_ = ProblemStatus::PRIMAL_INFEASIBLE;
2117 return !row_deletion_helper_.
IsEmpty();
2134 is_maximization_(lp.IsMaximizationProblem()),
2136 cost_(lp.objective_coefficients()[e.
col]),
2137 variable_lower_bound_(lp.variable_lower_bounds()[e.
col]),
2138 variable_upper_bound_(lp.variable_upper_bounds()[e.
col]),
2139 constraint_lower_bound_(lp.constraint_lower_bounds()[e.
row]),
2140 constraint_upper_bound_(lp.constraint_upper_bounds()[e.
row]),
2141 constraint_status_(status) {}
2149 SingletonRowUndo(deleted_columns, solution);
2152 ZeroCostSingletonColumnUndo(
parameters, deleted_rows, solution);
2155 SingletonColumnInEqualityUndo(
parameters, deleted_rows, solution);
2158 MakeConstraintAnEqualityUndo(solution);
2163 void SingletonPreprocessor::DeleteSingletonRow(MatrixEntry e,
2169 if (e.coeff < 0.0) {
2170 std::swap(implied_lower_bound, implied_upper_bound);
2177 std::abs(
parameters_.preprocessor_zero_tolerance() / e.coeff);
2179 implied_lower_bound - potential_error > old_lower_bound
2180 ? implied_lower_bound
2183 implied_upper_bound + potential_error < old_upper_bound
2184 ? implied_upper_bound
2187 if (new_upper_bound < new_lower_bound) {
2190 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2191 "row causes the bound of the variable "
2192 << e.col <<
" to be infeasible by "
2193 << new_lower_bound - new_upper_bound;
2194 status_ = ProblemStatus::PRIMAL_INFEASIBLE;
2199 new_upper_bound = new_lower_bound;
2202 new_lower_bound = new_upper_bound;
2204 DCHECK_EQ(new_lower_bound, new_upper_bound);
2218 void SingletonUndo::SingletonRowUndo(
const SparseMatrix& deleted_columns,
2219 ProblemSolution* solution)
const {
2220 DCHECK_EQ(0, solution->dual_values[e_.row]);
2225 const VariableStatus status = solution->variable_statuses[e_.col];
2229 Fractional implied_lower_bound = constraint_lower_bound_ / e_.coeff;
2230 Fractional implied_upper_bound = constraint_upper_bound_ / e_.coeff;
2231 if (e_.coeff < 0.0) {
2232 std::swap(implied_lower_bound, implied_upper_bound);
2234 const bool lower_bound_changed = implied_lower_bound > variable_lower_bound_;
2235 const bool upper_bound_changed = implied_upper_bound < variable_upper_bound_;
2237 if (!lower_bound_changed && !upper_bound_changed)
return;
2245 ScalarProduct(solution->dual_values, deleted_columns.column(e_.col));
2246 const Fractional reduced_cost_for_minimization =
2247 is_maximization_ ? -reduced_cost : reduced_cost;
2250 DCHECK(lower_bound_changed || upper_bound_changed);
2251 if (reduced_cost_for_minimization >= 0.0 && !lower_bound_changed) {
2255 if (reduced_cost_for_minimization <= 0.0 && !upper_bound_changed) {
2266 solution->dual_values[e_.row] = reduced_cost / e_.coeff;
2269 (!lower_bound_changed || !upper_bound_changed)) {
2270 new_constraint_status = lower_bound_changed
2274 if (e_.coeff < 0.0) {
2282 solution->constraint_statuses[e_.row] = new_constraint_status;
2285 void SingletonPreprocessor::UpdateConstraintBoundsWithVariableBounds(
2286 MatrixEntry e, LinearProgram* lp) {
2287 Fractional lower_delta = -e.coeff * lp->variable_upper_bounds()[e.col];
2288 Fractional upper_delta = -e.coeff * lp->variable_lower_bounds()[e.col];
2289 if (e.coeff < 0.0) {
2290 std::swap(lower_delta, upper_delta);
2292 lp->SetConstraintBounds(e.row,
2293 lp->constraint_lower_bounds()[e.row] + lower_delta,
2294 lp->constraint_upper_bounds()[e.row] + upper_delta);
2297 bool SingletonPreprocessor::IntegerSingletonColumnIsRemovable(
2298 const MatrixEntry& matrix_entry,
const LinearProgram& lp)
const {
2300 DCHECK(lp.IsVariableInteger(matrix_entry.col));
2301 const SparseMatrix& transpose = lp.GetTransposeSparseMatrix();
2313 coefficient_ratio,
parameters_.solution_feasibility_tolerance())) {
2318 lp.constraint_lower_bounds()[matrix_entry.row];
2320 const Fractional lower_bound_ratio = constraint_lb / matrix_entry.coeff;
2322 lower_bound_ratio,
parameters_.solution_feasibility_tolerance())) {
2327 lp.constraint_upper_bounds()[matrix_entry.row];
2329 const Fractional upper_bound_ratio = constraint_ub / matrix_entry.coeff;
2331 upper_bound_ratio,
parameters_.solution_feasibility_tolerance())) {
2338 void SingletonPreprocessor::DeleteZeroCostSingletonColumn(
2339 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2341 const SparseColumn& column = transpose.column(transpose_col);
2348 UpdateConstraintBoundsWithVariableBounds(e, lp);
2353 void SingletonUndo::ZeroCostSingletonColumnUndo(
2354 const GlopParameters&
parameters,
const SparseMatrix& deleted_rows,
2355 ProblemSolution* solution)
const {
2358 if (variable_upper_bound_ == variable_lower_bound_) {
2359 solution->primal_values[e_.col] = variable_lower_bound_;
2371 solution->primal_values[e_.col] = variable_lower_bound_;
2375 solution->primal_values[e_.col] = variable_upper_bound_;
2378 if (constraint_upper_bound_ == constraint_lower_bound_) {
2388 ScalarProduct(solution->primal_values, deleted_rows.column(row_as_col));
2395 const auto is_smaller_with_tolerance = [tolerance](
Fractional a,
2399 if (variable_lower_bound_ != -
kInfinity) {
2401 activity + e_.coeff * variable_lower_bound_;
2402 if (is_smaller_with_tolerance(constraint_lower_bound_, activity_at_lb) &&
2403 is_smaller_with_tolerance(activity_at_lb, constraint_upper_bound_)) {
2404 solution->primal_values[e_.col] = variable_lower_bound_;
2409 if (variable_upper_bound_ !=
kInfinity) {
2411 activity + e_.coeff * variable_upper_bound_;
2412 if (is_smaller_with_tolerance(constraint_lower_bound_, actibity_at_ub) &&
2413 is_smaller_with_tolerance(actibity_at_ub, constraint_upper_bound_)) {
2414 solution->primal_values[e_.col] = variable_upper_bound_;
2423 if (constraint_lower_bound_ == -
kInfinity &&
2425 solution->primal_values[e_.col] = 0.0;
2433 if (constraint_lower_bound_ == constraint_upper_bound_) {
2434 solution->primal_values[e_.col] =
2435 (constraint_lower_bound_ - activity) / e_.coeff;
2440 bool set_constraint_to_lower_bound;
2441 if (constraint_lower_bound_ == -
kInfinity) {
2442 set_constraint_to_lower_bound =
false;
2443 }
else if (constraint_upper_bound_ ==
kInfinity) {
2444 set_constraint_to_lower_bound =
true;
2448 const Fractional to_lb = (constraint_lower_bound_ - activity) / e_.coeff;
2449 const Fractional to_ub = (constraint_upper_bound_ - activity) / e_.coeff;
2450 set_constraint_to_lower_bound =
2451 std::max(variable_lower_bound_ - to_lb, to_lb - variable_upper_bound_) <
2452 std::max(variable_lower_bound_ - to_ub, to_ub - variable_upper_bound_);
2455 if (set_constraint_to_lower_bound) {
2456 solution->primal_values[e_.col] =
2457 (constraint_lower_bound_ - activity) / e_.coeff;
2460 solution->primal_values[e_.col] =
2461 (constraint_upper_bound_ - activity) / e_.coeff;
2466 void SingletonPreprocessor::DeleteSingletonColumnInEquality(
2467 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2470 const SparseColumn& row_as_column = transpose.column(transpose_col);
2471 undo_stack_.push_back(
2482 const Fractional rhs = lp->constraint_upper_bounds()[e.row];
2485 lp->SetObjectiveOffset(lp->objective_offset() + rhs * multiplier);
2490 lp->objective_coefficients()[
col] - e.coefficient() * multiplier;
2497 if (std::abs(new_cost) <
parameters_.preprocessor_zero_tolerance()) {
2500 lp->SetObjectiveCoefficient(
col, new_cost);
2505 UpdateConstraintBoundsWithVariableBounds(e, lp);
2509 void SingletonUndo::SingletonColumnInEqualityUndo(
2510 const GlopParameters&
parameters,
const SparseMatrix& deleted_rows,
2511 ProblemSolution* solution)
const {
2513 ZeroCostSingletonColumnUndo(
parameters, deleted_rows, solution);
2517 solution->dual_values[e_.row] += cost_ / e_.coeff;
2524 void SingletonUndo::MakeConstraintAnEqualityUndo(
2525 ProblemSolution* solution)
const {
2527 solution->constraint_statuses[e_.row] = constraint_status_;
2531 bool SingletonPreprocessor::MakeConstraintAnEqualityIfPossible(
2532 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2535 const Fractional cst_lower_bound = lp->constraint_lower_bounds()[e.row];
2536 const Fractional cst_upper_bound = lp->constraint_upper_bounds()[e.row];
2537 if (cst_lower_bound == cst_upper_bound)
return true;
2543 const DenseRow& variable_ubs = lp->variable_upper_bounds();
2544 const DenseRow& variable_lbs = lp->variable_lower_bounds();
2545 if (e.row >= row_sum_is_cached_.
size() || !row_sum_is_cached_[e.row]) {
2546 if (e.row >= row_sum_is_cached_.
size()) {
2547 const int new_size = e.row.value() + 1;
2548 row_sum_is_cached_.
resize(new_size);
2549 row_lb_sum_.resize(new_size);
2550 row_ub_sum_.resize(new_size);
2552 row_sum_is_cached_[e.row] =
true;
2553 row_lb_sum_[e.row].Add(cst_lower_bound);
2554 row_ub_sum_[e.row].Add(cst_upper_bound);
2565 if (column_deletion_helper_.
IsColumnMarked(row_as_col))
continue;
2566 if (entry.coefficient() > 0.0) {
2567 row_lb_sum_[e.row].Add(-entry.coefficient() * variable_ubs[row_as_col]);
2568 row_ub_sum_[e.row].Add(-entry.coefficient() * variable_lbs[row_as_col]);
2570 row_lb_sum_[e.row].Add(-entry.coefficient() * variable_lbs[row_as_col]);
2571 row_ub_sum_[e.row].Add(-entry.coefficient() * variable_ubs[row_as_col]);
2583 c > 0.0 ? row_lb_sum_[e.row].SumWithout(-c * variable_ubs[e.col]) / c
2584 : row_ub_sum_[e.row].SumWithout(-c * variable_ubs[e.col]) / c;
2586 c > 0.0 ? row_ub_sum_[e.row].SumWithout(-c * variable_lbs[e.col]) / c
2587 : row_lb_sum_[e.row].SumWithout(-c * variable_lbs[e.col]) / c;
2593 lp->GetObjectiveCoefficientForMinimizationVersion(e.col);
2600 ub, lp->variable_upper_bounds()[e.col])) {
2603 status_ = ProblemStatus::INFEASIBLE_OR_UNBOUNDED;
2606 lp->SetConstraintBounds(e.row, cst_upper_bound, cst_upper_bound);
2610 status_ = ProblemStatus::INFEASIBLE_OR_UNBOUNDED;
2613 lp->SetConstraintBounds(e.row, cst_lower_bound, cst_lower_bound);
2617 if (
status_ == ProblemStatus::INFEASIBLE_OR_UNBOUNDED) {
2619 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2621 << e.col <<
" has a cost (for minimization) of " <<
cost
2622 <<
" and is unbounded towards kInfinity.";
2639 lp->SetVariableBounds(e.col, lp->variable_lower_bounds()[e.col],
kInfinity);
2642 lp->variable_lower_bounds()[e.col], lb)) {
2645 status_ = ProblemStatus::INFEASIBLE_OR_UNBOUNDED;
2648 lp->SetConstraintBounds(e.row, cst_lower_bound, cst_lower_bound);
2652 status_ = ProblemStatus::INFEASIBLE_OR_UNBOUNDED;
2655 lp->SetConstraintBounds(e.row, cst_upper_bound, cst_upper_bound);
2659 if (
status_ == ProblemStatus::INFEASIBLE_OR_UNBOUNDED) {
2661 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2663 << e.col <<
" has a cost (for minimization) of " <<
cost
2664 <<
" and is unbounded towards -kInfinity.";
2669 lp->SetVariableBounds(e.col, -
kInfinity,
2670 lp->variable_upper_bounds()[e.col]);
2673 if (lp->constraint_lower_bounds()[e.row] ==
2674 lp->constraint_upper_bounds()[e.row]) {
2675 undo_stack_.push_back(SingletonUndo(
2689 ColIndex num_cols(matrix.
num_cols());
2690 RowIndex num_rows(matrix.
num_rows());
2695 std::vector<ColIndex> column_to_process;
2696 for (ColIndex
col(0);
col < num_cols; ++
col) {
2698 if (column_degree[
col] == 1) {
2699 column_to_process.push_back(
col);
2705 std::vector<RowIndex> row_to_process;
2706 for (RowIndex
row(0);
row < num_rows; ++
row) {
2708 if (row_degree[
row] == 1) {
2709 row_to_process.push_back(
row);
2715 (!column_to_process.empty() || !row_to_process.empty())) {
2717 const ColIndex
col = column_to_process.back();
2718 column_to_process.pop_back();
2719 if (column_degree[
col] <= 0)
continue;
2720 const MatrixEntry e = GetSingletonColumnMatrixEntry(
col, matrix);
2722 !IntegerSingletonColumnIsRemovable(e, *lp)) {
2729 DeleteZeroCostSingletonColumn(transpose, e, lp);
2730 }
else if (MakeConstraintAnEqualityIfPossible(transpose, e, lp)) {
2731 DeleteSingletonColumnInEquality(transpose, e, lp);
2735 --row_degree[e.row];
2736 if (row_degree[e.row] == 1) {
2737 row_to_process.push_back(e.row);
2741 const RowIndex
row = row_to_process.back();
2742 row_to_process.pop_back();
2743 if (row_degree[
row] <= 0)
continue;
2744 const MatrixEntry e = GetSingletonRowMatrixEntry(
row, transpose);
2746 DeleteSingletonRow(e, lp);
2747 --column_degree[e.col];
2748 if (column_degree[e.col] == 1) {
2749 column_to_process.push_back(e.col);
2757 return !column_deletion_helper_.
IsEmpty() || !row_deletion_helper_.
IsEmpty();
2775 for (
int i = undo_stack_.size() - 1; i >= 0; --i) {
2776 undo_stack_[i].Undo(
parameters_, deleted_columns_, deleted_rows_, solution);
2780 MatrixEntry SingletonPreprocessor::GetSingletonColumnMatrixEntry(
2785 return MatrixEntry(e.row(),
col, e.coefficient());
2789 LOG(DFATAL) <<
"No unmarked entry in a column that is supposed to have one.";
2791 return MatrixEntry(RowIndex(0), ColIndex(0), 0.0);
2794 MatrixEntry SingletonPreprocessor::GetSingletonRowMatrixEntry(
2795 RowIndex
row,
const SparseMatrix& transpose) {
2800 return MatrixEntry(
row,
col, e.coefficient());
2804 LOG(DFATAL) <<
"No unmarked entry in a row that is supposed to have one.";
2806 return MatrixEntry(RowIndex(0), ColIndex(0), 0.0);
2817 if (num_cols == 0)
return false;
2823 Fractional num_non_zero_objective_coefficients = 0.0;
2824 for (ColIndex
col(0);
col < num_cols; ++
col) {
2826 row_degree[e.row()] += 1.0;
2829 num_non_zero_objective_coefficients += 1.0;
2841 const EntryIndex initial_num_entries = lp->
num_entries();
2842 int num_zeroed_objective_coefficients = 0;
2843 for (ColIndex
col(0);
col < num_cols; ++
col) {
2851 std::max(std::abs(lower_bound), std::abs(upper_bound));
2852 if (max_magnitude ==
kInfinity || max_magnitude == 0)
continue;
2853 const Fractional threshold = allowed_impact / max_magnitude;
2855 threshold, row_degree);
2858 num_non_zero_objective_coefficients *
2862 ++num_zeroed_objective_coefficients;
2869 <<
" near-zero entries.";
2871 if (num_zeroed_objective_coefficients > 0) {
2872 VLOG(1) <<
"Removed " << num_zeroed_objective_coefficients
2873 <<
" near-zero objective coefficients.";
2891 if (num_cols == 0)
return false;
2893 changed_columns_.clear();
2894 int num_singletons = 0;
2895 for (ColIndex
col(0);
col < num_cols; ++
col) {
2907 changed_columns_.push_back(
col);
2910 VLOG(1) <<
"Changed the sign of " << changed_columns_.size() <<
" columns.";
2911 VLOG(1) << num_singletons <<
" singleton columns left.";
2912 return !changed_columns_.empty();
2919 for (
int i = 0; i < changed_columns_.size(); ++i) {
2920 const ColIndex
col = changed_columns_[i];
2954 for (RowIndex
row(0);
row < num_rows; ++
row) {
2969 int entry_index = 0;
2973 r.col[entry_index] =
col;
2974 r.coeff[entry_index] = e.coefficient();
2982 if (entry_index < 2)
continue;
2988 for (
int col_choice = 0; col_choice < NUM_DOUBLETON_COLS; ++col_choice) {
2989 const ColIndex
col = r.col[col_choice];
2998 if (r.lb[DELETED] == r.ub[DELETED] || r.lb[MODIFIED] == r.ub[MODIFIED]) {
3015 const Fractional carry_over_offset = r.rhs / r.coeff[MODIFIED];
3017 -r.coeff[DELETED] / r.coeff[MODIFIED];
3019 carry_over_factor == 0.0) {
3027 r.lb[DELETED] * carry_over_factor + carry_over_offset;
3029 r.ub[DELETED] * carry_over_factor + carry_over_offset;
3030 if (carry_over_factor < 0) {
3031 std::swap(carried_over_lb, carried_over_ub);
3033 if (carried_over_lb <= lb) {
3038 lb = carried_over_lb;
3043 carry_over_factor > 0 ? r.lb[DELETED] : r.ub[DELETED]);
3045 if (carried_over_ub >= ub) {
3050 ub = carried_over_ub;
3055 carry_over_factor > 0 ? r.ub[DELETED] : r.lb[DELETED]);
3064 restore_stack_.push_back(r);
3073 -r.coeff[MODIFIED] / r.coeff[DELETED];
3074 const Fractional constant_offset_factor = r.rhs / r.coeff[DELETED];
3076 if (!
IsFinite(substitution_factor) || substitution_factor == 0.0 ||
3077 !
IsFinite(constant_offset_factor)) {
3081 r.column[DELETED].AddMultipleToSparseVectorAndDeleteCommonIndex(
3090 r.objective_coefficient[MODIFIED] +
3091 substitution_factor * r.objective_coefficient[DELETED];
3092 if (std::abs(new_objective) >
parameters_.drop_tolerance()) {
3102 SubtractColumnMultipleFromConstraintBound(r.col[DELETED],
3103 constant_offset_factor, lp);
3112 return !column_deletion_helper_.
IsEmpty();
3121 for (
const RestoreInfo& r :
Reverse(restore_stack_)) {
3124 LOG(DFATAL) <<
"FIXED variable produced by DoubletonPreprocessor!";
3131 ABSL_FALLTHROUGH_INTENDED;
3138 ABSL_FALLTHROUGH_INTENDED;
3146 ? r.bound_backtracking_at_lower_bound
3147 : r.bound_backtracking_at_upper_bound;
3148 const ColIndex bounded_var = r.col[bound_backtracking.
col_choice];
3149 const ColIndex basic_var =
3150 r.col[OtherColChoice(bound_backtracking.
col_choice)];
3165 solution->
primal_values[r.col[MODIFIED]] * r.coeff[MODIFIED]) /
3178 const ColChoice col_choice =
3185 r.objective_coefficient[col_choice] -
3187 solution->
dual_values[r.row] = current_reduced_cost / r.coeff[col_choice];
3192 saved_row_upper_bounds_, solution);
3201 for (RowIndex
row(0);
row < num_rows; ++
row) {
3205 if (row_lower_bounds[
row] == row_upper_bounds[
row])
continue;
3217 void DoubletonEqualityRowPreprocessor::
3218 SwapDeletedAndModifiedVariableRestoreInfo(RestoreInfo* r) {
3220 swap(r->col[DELETED], r->col[MODIFIED]);
3221 swap(r->coeff[DELETED], r->coeff[MODIFIED]);
3222 swap(r->lb[DELETED], r->lb[MODIFIED]);
3223 swap(r->ub[DELETED], r->ub[MODIFIED]);
3224 swap(r->column[DELETED], r->column[MODIFIED]);
3225 swap(r->objective_coefficient[DELETED], r->objective_coefficient[MODIFIED]);
3235 if (
parameters_.solve_dual_problem() == GlopParameters::NEVER_DO) {
3262 if (
parameters_.solve_dual_problem() == GlopParameters::LET_SOLVER_DECIDE) {
3263 if (1.0 * primal_num_rows_.value() <
3264 parameters_.dualizer_threshold() * primal_num_cols_.value()) {
3274 variable_lower_bounds_.
assign(num_cols, 0.0);
3275 variable_upper_bounds_.
assign(num_cols, 0.0);
3276 for (ColIndex
col(0);
col < num_cols; ++
col) {
3281 variable_lower_bounds_[
col] = lower;
3282 variable_upper_bounds_[
col] = upper;
3283 const Fractional value = MinInMagnitudeOrZeroIfInfinite(lower, upper);
3286 SubtractColumnMultipleFromConstraintBound(
col,
value, lp);
3294 dual_status_correspondence_.
clear();
3295 for (RowIndex
row(0);
row < primal_num_rows_; ++
row) {
3298 if (lower_bound == upper_bound) {
3305 LOG(DFATAL) <<
"There should be no free constraint in this lp.";
3308 slack_or_surplus_mapping_.
clear();
3309 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3319 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3352 DenseRow new_primal_values(primal_num_cols_, 0.0);
3356 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3363 const Fractional shift = MinInMagnitudeOrZeroIfInfinite(lower, upper);
3374 new_variable_statuses[
col] = ComputeVariableStatus(shift, lower, upper);
3383 const ColIndex end = dual_status_correspondence_.
size();
3388 const ColIndex
col = slack_or_surplus_mapping_[
index - begin];
3396 new_primal_values[
col] = variable_upper_bounds_[
col];
3399 new_primal_values[
col] = variable_lower_bounds_[
col];
3407 DenseColumn new_dual_values(primal_num_rows_, 0.0);
3414 Fractional sign = primal_is_maximization_problem_ ? -1 : 1;
3415 for (RowIndex
row(0);
row < primal_num_rows_; ++
row) {
3433 new_constraint_statuses[
row] =
3440 new_dual_values[
row] +=
3460 case ProblemStatus::PRIMAL_INFEASIBLE:
3461 return ProblemStatus::DUAL_INFEASIBLE;
3462 case ProblemStatus::DUAL_INFEASIBLE:
3463 return ProblemStatus::PRIMAL_INFEASIBLE;
3464 case ProblemStatus::PRIMAL_UNBOUNDED:
3465 return ProblemStatus::DUAL_UNBOUNDED;
3466 case ProblemStatus::DUAL_UNBOUNDED:
3467 return ProblemStatus::PRIMAL_UNBOUNDED;
3468 case ProblemStatus::PRIMAL_FEASIBLE:
3469 return ProblemStatus::DUAL_FEASIBLE;
3470 case ProblemStatus::DUAL_FEASIBLE:
3471 return ProblemStatus::PRIMAL_FEASIBLE;
3486 bool all_variable_domains_contain_zero =
true;
3488 variable_initial_lbs_.
assign(num_cols, 0.0);
3489 variable_initial_ubs_.
assign(num_cols, 0.0);
3490 for (ColIndex
col(0);
col < num_cols; ++
col) {
3493 if (0.0 < variable_initial_lbs_[
col] || 0.0 > variable_initial_ubs_[
col]) {
3494 all_variable_domains_contain_zero =
false;
3497 VLOG(1) <<
"Maximum variable bounds magnitude (before shift): "
3498 << ComputeMaxVariableBoundsMagnitude(*lp);
3501 if (all_variable_domains_contain_zero)
return false;
3505 int num_bound_shifts = 0;
3509 offsets_.
assign(num_cols, 0.0);
3510 for (ColIndex
col(0);
col < num_cols; ++
col) {
3511 if (0.0 < variable_initial_lbs_[
col] || 0.0 > variable_initial_ubs_[
col]) {
3512 Fractional offset = MinInMagnitudeOrZeroIfInfinite(
3513 variable_initial_lbs_[
col], variable_initial_ubs_[
col]);
3521 offset = trunc(offset);
3525 offsets_[
col] = offset;
3527 variable_initial_ubs_[
col] - offset);
3530 row_offsets[e.row()].Add(e.coefficient() * offset);
3536 VLOG(1) <<
"Maximum variable bounds magnitude (after " << num_bound_shifts
3537 <<
" shifts): " << ComputeMaxVariableBoundsMagnitude(*lp);
3540 for (RowIndex
row(0);
row < num_rows; ++
row) {
3554 for (ColIndex
col(0);
col < num_cols; ++
col) {
3560 ABSL_FALLTHROUGH_INTENDED;
3588 variable_lower_bounds_.
assign(num_cols, 0.0);
3589 variable_upper_bounds_.
assign(num_cols, 0.0);
3590 for (ColIndex
col(0);
col < num_cols; ++
col) {
3622 for (ColIndex
col(0);
col < num_cols; ++
col) {
3625 ABSL_FALLTHROUGH_INTENDED;
3633 ABSL_FALLTHROUGH_INTENDED;
3684 for (RowIndex
row(0);
row < num_rows; ++
row) {
3691 switch (variable_status) {