19 #include "absl/strings/str_format.h"
36 return absl::StrFormat(
"[%g, %g]", lb, ub);
40 double trunc(
double d) {
return d > 0 ? floor(d) : ceil(d); }
50 in_mip_context_(false),
51 infinite_time_limit_(
TimeLimit::Infinite()),
52 time_limit_(infinite_time_limit_.get()) {}
59 #define RUN_PREPROCESSOR(name) \
60 RunAndPushIfRelevant(std::unique_ptr<Preprocessor>(new name(¶meters_)), \
61 #name, time_limit_, lp)
73 const int kMaxNumPasses = 20;
74 for (
int i = 0; i < kMaxNumPasses; ++i) {
75 const int old_stack_size = preprocessors_.size();
88 if (preprocessors_.size() == old_stack_size) {
91 LOG(
INFO) <<
"Reached fixed point after presolve pass #" << i;
106 const int old_stack_size = preprocessors_.size();
112 if (old_stack_size != preprocessors_.size()) {
128 return !preprocessors_.empty();
131 #undef RUN_PREPROCESSOR
133 void MainLpPreprocessor::RunAndPushIfRelevant(
134 std::unique_ptr<Preprocessor> preprocessor,
const std::string&
name,
140 const double start_time =
time_limit->GetElapsedTime();
151 if (preprocessor->Run(lp)) {
152 const EntryIndex new_num_entries = lp->
num_entries();
153 const double preprocess_time =
time_limit->GetElapsedTime() - start_time;
156 "%s(%fs): %d(%d) rows, %d(%d) columns, %d(%d) entries.",
name,
163 static_cast<int64_t
>(new_num_entries.value()),
164 static_cast<int64_t
>(new_num_entries.value() -
165 initial_num_entries_.value()));
167 status_ = preprocessor->status();
168 preprocessors_.push_back(std::move(preprocessor));
173 status_ = preprocessor->status();
175 LOG(
INFO) <<
name <<
" detected that the problem is "
183 while (!preprocessors_.empty()) {
184 preprocessors_.back()->RecoverSolution(solution);
185 preprocessors_.pop_back();
194 const int index = saved_columns_.size();
195 CHECK(saved_columns_index_.insert({col, index}).second);
196 saved_columns_.push_back(column);
201 const int index = saved_columns_.size();
202 const bool inserted = saved_columns_index_.insert({
col,
index}).second;
203 if (inserted) saved_columns_.push_back(column);
207 const auto it = saved_columns_index_.find(
col);
208 CHECK(it != saved_columns_index_.end());
209 return saved_columns_[it->second];
213 const auto it = saved_columns_index_.find(
col);
214 return it == saved_columns_index_.end() ? empty_column_
215 : saved_columns_[it->second];
219 is_column_deleted_.
clear();
220 stored_value_.
clear();
230 if (
col >= is_column_deleted_.
size()) {
231 is_column_deleted_.
resize(
col + 1,
false);
235 is_column_deleted_[
col] =
true;
236 stored_value_[
col] = fixed_value;
237 stored_status_[
col] = status;
244 ColIndex old_index(0);
245 for (ColIndex
col(0);
col < is_column_deleted_.
size(); ++
col) {
246 if (is_column_deleted_[
col]) {
259 for (; old_index < num_cols; ++old_index) {
275 if (
row >= is_row_deleted_.
size()) {
278 is_row_deleted_[
row] =
true;
282 if (
row >= is_row_deleted_.
size())
return;
283 is_row_deleted_[
row] =
false;
287 return is_row_deleted_;
293 RowIndex old_index(0);
294 const RowIndex end = is_row_deleted_.
size();
295 for (RowIndex
row(0);
row < end; ++
row) {
296 if (is_row_deleted_[
row]) {
310 for (; old_index < num_rows; ++old_index) {
361 Fractional ComputeMaxVariableBoundsMagnitude(
const LinearProgram& lp) {
363 const ColIndex num_cols = lp.num_variables();
364 for (ColIndex
col(0);
col < num_cols; ++
col) {
366 max_bounds_magnitude,
367 std::max(MagnitudeOrZeroIfInfinite(lp.variable_lower_bounds()[
col]),
368 MagnitudeOrZeroIfInfinite(lp.variable_upper_bounds()[
col])));
370 return max_bounds_magnitude;
378 column_deletion_helper_.
Clear();
380 for (ColIndex
col(0);
col < num_cols; ++
col) {
387 if (objective_coefficient == 0) {
401 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, empty column " <<
col
402 <<
" has a minimization cost of " << objective_coefficient
416 return !column_deletion_helper_.
IsEmpty();
434 void SubtractColumnMultipleFromConstraintBound(ColIndex
col,
440 const RowIndex
row = e.row();
454 struct ColumnWithRepresentativeAndScaledCost {
455 ColumnWithRepresentativeAndScaledCost(ColIndex _col, ColIndex _representative,
462 bool operator<(
const ColumnWithRepresentativeAndScaledCost& other)
const {
465 return col < other.col;
486 int num_proportionality_classes = 0;
487 std::vector<ColIndex> proportional_columns;
493 ++num_proportionality_classes;
496 proportional_columns.push_back(
col);
499 if (proportional_columns.empty())
return false;
500 VLOG(1) <<
"The problem contains " << proportional_columns.size()
501 <<
" columns which belong to " << num_proportionality_classes
502 <<
" proportionality classes.";
506 column_factors_.
assign(num_cols, 0.0);
507 for (
const ColIndex
col : proportional_columns) {
521 for (
const ColIndex
col : proportional_columns) {
525 const bool is_rc_positive_or_zero =
527 const bool is_rc_negative_or_zero =
529 bool is_slope_upper_bounded = is_rc_positive_or_zero;
530 bool is_slope_lower_bounded = is_rc_negative_or_zero;
531 if (column_factors_[
col] < 0.0) {
532 std::swap(is_slope_lower_bounded, is_slope_upper_bounded);
536 column_factors_[
col];
537 if (is_slope_lower_bounded) {
541 if (is_slope_upper_bounded) {
548 for (
const ColIndex
col : proportional_columns) {
556 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, no feasible dual values"
557 <<
" can satisfy the constraints of the proportional columns"
559 <<
" the associated quantity must be in ["
569 for (
const ColIndex
col : proportional_columns) {
573 column_factors_[
col];
576 bool variable_can_be_fixed =
false;
584 variable_can_be_fixed =
true;
589 variable_can_be_fixed =
true;
593 if (variable_can_be_fixed) {
598 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED.";
611 std::vector<ColumnWithRepresentativeAndScaledCost> sorted_columns;
612 for (
const ColIndex
col : proportional_columns) {
617 sorted_columns.
push_back(ColumnWithRepresentativeAndScaledCost(
622 std::sort(sorted_columns.begin(), sorted_columns.end());
631 for (
int i = 0; i < sorted_columns.size();) {
632 const ColIndex target_col = sorted_columns[i].col;
633 const ColIndex target_representative = sorted_columns[i].representative;
634 const Fractional target_scaled_cost = sorted_columns[i].scaled_cost;
641 for (++i; i < sorted_columns.size(); ++i) {
642 if (sorted_columns[i].
representative != target_representative)
break;
643 if (std::abs(sorted_columns[i].
scaled_cost - target_scaled_cost) >=
648 const ColIndex
col = sorted_columns[i].col;
653 merged_columns_[
col] = target_col;
658 column_factors_[
col] / column_factors_[target_col];
672 if (bound_factor < 0.0) {
678 SubtractColumnMultipleFromConstraintBound(
col, target_value, lp);
687 if (num_merged > 0) {
688 merged_columns_[target_col] = target_col;
689 const Fractional target_value = MinInMagnitudeOrZeroIfInfinite(
690 lower_bounds_[target_col], upper_bounds_[target_col]);
694 SubtractColumnMultipleFromConstraintBound(target_col, target_value, lp);
701 return !column_deletion_helper_.
IsEmpty();
712 const ColIndex num_cols = merged_columns_.
size();
715 DenseRow distance_to_bound(num_cols, 0.0);
716 DenseRow wanted_value(num_cols, 0.0);
720 for (ColIndex
col(0);
col < num_cols; ++
col) {
721 if (merged_columns_[
col] ==
col) {
725 if (distance_to_upper_bound < distance_to_lower_bound) {
726 distance_to_bound[
col] = distance_to_upper_bound;
727 is_distance_to_upper_bound[
col] =
true;
729 distance_to_bound[
col] = distance_to_lower_bound;
730 is_distance_to_upper_bound[
col] =
false;
732 is_representative_basic[
col] =
739 lower_bounds_[
col], upper_bounds_[
col]);
746 for (ColIndex
col(0);
col < num_cols; ++
col) {
758 const bool to_upper_bound =
759 (bound_factor > 0.0) == is_distance_to_upper_bound[
representative];
760 if (width <= scaled_distance) {
762 to_upper_bound ? lower_bounds_[
col] : upper_bounds_[
col];
765 lower_bounds_[
col], upper_bounds_[
col]);
766 distance_to_bound[
representative] -= width * std::abs(bound_factor);
769 to_upper_bound ? upper_bounds_[
col] - scaled_distance
770 : lower_bounds_[
col] + scaled_distance;
793 const bool use_this_variable =
794 (error * bound_factor > 0.0) ? (upper_bounds_[
col] ==
kInfinity)
796 if (use_this_variable) {
829 row_factors_.
assign(num_rows, 0.0);
830 for (RowIndex
row(0);
row < num_rows; ++
row) {
832 if (!row_transpose.
IsEmpty()) {
851 transpose,
parameters_.preprocessor_zero_tolerance());
853 int num_proportional_rows = 0;
854 for (RowIndex
row(0);
row < num_rows; ++
row) {
857 mapping[representative_row_as_col] = representative_row_as_col;
858 is_a_representative[
ColToRowIndex(representative_row_as_col)] =
true;
859 ++num_proportional_rows;
865 for (RowIndex
row(0);
row < num_rows; ++
row) {
871 const RowIndex representative_row =
ColToRowIndex(mapping[row_as_col]);
874 row_factors_[representative_row] / row_factors_[
row];
884 lower_bound_sources_[representative_row] =
row;
888 upper_bound_sources_[representative_row] =
row;
896 for (RowIndex
row(0);
row < num_rows; ++
row) {
897 if (!is_a_representative[
row])
continue;
898 const RowIndex lower_source = lower_bound_sources_[
row];
899 const RowIndex upper_source = upper_bound_sources_[
row];
904 if (lower_source == upper_source) {
908 row_deletion_helper_.
UnmarkRow(lower_source);
921 row_deletion_helper_.
UnmarkRow(lower_source);
926 row_deletion_helper_.
UnmarkRow(upper_source);
934 RowIndex new_representative = lower_source;
935 RowIndex other = upper_source;
936 if (std::abs(row_factors_[new_representative]) <
937 std::abs(row_factors_[other])) {
943 row_factors_[new_representative] / row_factors_[other];
950 lower_bound_sources_[new_representative] = new_representative;
951 upper_bound_sources_[new_representative] = new_representative;
954 lower_bound_sources_[new_representative] = other;
958 if (new_ub < lp->constraint_upper_bounds()[new_representative]) {
959 upper_bound_sources_[new_representative] = other;
963 const RowIndex new_lower_source =
964 lower_bound_sources_[new_representative];
965 if (new_lower_source == upper_bound_sources_[new_representative]) {
966 row_deletion_helper_.
UnmarkRow(new_lower_source);
967 lower_bound_sources_[new_representative] =
kInvalidRow;
968 upper_bound_sources_[new_representative] =
kInvalidRow;
977 if (new_lb > new_ub) {
978 if (lower_bound_sources_[new_representative] == new_representative) {
984 row_deletion_helper_.
UnmarkRow(new_representative);
991 return !row_deletion_helper_.
IsEmpty();
1004 for (RowIndex
row(0);
row < num_rows; ++
row) {
1005 const RowIndex lower_source = lower_bound_sources_[
row];
1006 const RowIndex upper_source = upper_bound_sources_[
row];
1021 const Fractional corrected_dual_value = lp_is_maximization_problem_
1024 if (corrected_dual_value != 0.0) {
1035 const Fractional factor = row_factors_[
row] / row_factors_[lower_source];
1045 const Fractional factor = row_factors_[
row] / row_factors_[upper_source];
1072 for (ColIndex
col(0);
col < num_cols; ++
col) {
1080 SubtractColumnMultipleFromConstraintBound(
col, fixed_value, lp);
1087 return !column_deletion_helper_.
IsEmpty();
1111 for (ColIndex
col(0);
col < num_cols; ++
col) {
1115 const RowIndex
row = e.row();
1118 implied_lower_bounds[
row] += lower * coeff;
1119 implied_upper_bounds[
row] += upper * coeff;
1121 implied_lower_bounds[
row] += upper * coeff;
1122 implied_upper_bounds[
row] += lower * coeff;
1130 int num_implied_free_constraints = 0;
1131 int num_forcing_constraints = 0;
1132 is_forcing_up_.
assign(num_rows,
false);
1134 for (RowIndex
row(0);
row < num_rows; ++
row) {
1135 if (row_degree[
row] == 0)
continue;
1141 implied_upper_bounds[
row]) ||
1144 VLOG(1) <<
"implied bound " << implied_lower_bounds[
row] <<
" "
1145 << implied_upper_bounds[
row];
1146 VLOG(1) <<
"constraint bound " << lower <<
" " << upper;
1155 is_forcing_down[
row] =
true;
1156 ++num_forcing_constraints;
1160 implied_lower_bounds[
row])) {
1161 is_forcing_up_[
row] =
true;
1162 ++num_forcing_constraints;
1173 implied_lower_bounds[
row]) &&
1177 ++num_implied_free_constraints;
1181 if (num_implied_free_constraints > 0) {
1182 VLOG(1) << num_implied_free_constraints <<
" implied free constraints.";
1185 if (num_forcing_constraints > 0) {
1186 VLOG(1) << num_forcing_constraints <<
" forcing constraints.";
1188 costs_.
resize(num_cols, 0.0);
1189 for (ColIndex
col(0);
col < num_cols; ++
col) {
1193 bool is_forced =
false;
1196 if (is_forcing_down[e.row()]) {
1197 const Fractional candidate = e.coefficient() < 0.0 ? lower : upper;
1206 target_bound = std::abs(lower) < std::abs(upper) ? lower : upper;
1209 VLOG(1) <<
"A variable is forced in both directions! bounds: ["
1210 << std::fixed << std::setprecision(10) << lower <<
", "
1211 << upper <<
"]. coeff:" << e.coefficient();
1218 if (is_forcing_up_[e.row()]) {
1219 const Fractional candidate = e.coefficient() < 0.0 ? upper : lower;
1224 target_bound = std::abs(lower) < std::abs(upper) ? lower : upper;
1227 VLOG(1) <<
"A variable is forced in both directions! bounds: ["
1228 << std::fixed << std::setprecision(10) << lower <<
", "
1229 << upper <<
"]. coeff:" << e.coefficient();
1248 for (RowIndex
row(0);
row < num_rows; ++
row) {
1255 if (is_forcing_down[
row] || is_forcing_up_[
row]) {
1263 return !column_deletion_helper_.
IsEmpty();
1273 struct DeletionEntry {
1278 std::vector<DeletionEntry> entries;
1282 for (ColIndex
col(0);
col < size; ++
col) {
1288 const RowIndex
row = e.row();
1291 last_coefficient = e.coefficient();
1295 entries.push_back({last_row,
col, last_coefficient});
1300 std::sort(entries.begin(), entries.end(),
1301 [](
const DeletionEntry&
a,
const DeletionEntry&
b) {
1302 if (a.row == b.row) return a.col < b.col;
1303 return a.row < b.row;
1316 for (
int i = 0; i < entries.size();) {
1317 const RowIndex
row = entries[i].row;
1323 for (; i < entries.size(); ++i) {
1324 if (entries[i].
row !=
row)
break;
1325 const ColIndex
col = entries[i].col;
1329 const Fractional reduced_cost = costs_[
col] - scalar_product;
1331 if (is_forcing_up_[
row] == !lp_is_maximization_problem_) {
1332 if (
bound < new_dual_value) {
1333 new_dual_value =
bound;
1334 new_basic_column =
col;
1337 if (
bound > new_dual_value) {
1338 new_dual_value =
bound;
1339 new_basic_column =
col;
1358 struct ColWithDegree {
1361 ColWithDegree(ColIndex c, EntryIndex n) :
col(c),
num_entries(n) {}
1362 bool operator<(
const ColWithDegree& other)
const {
1364 return col < other.col;
1381 const int size = num_rows.value();
1390 for (ColIndex
col(0);
col < num_cols; ++
col) {
1396 if (e.coefficient() < 0.0)
std::swap(entry_lb, entry_ub);
1397 lb_sums[e.row()].Add(entry_lb);
1398 ub_sums[e.row()].Add(entry_ub);
1408 for (RowIndex
row(0);
row < num_rows; ++
row) {
1417 variable_offsets_.
assign(num_cols, 0.0);
1434 std::vector<ColWithDegree> col_by_degree;
1435 for (ColIndex
col(0);
col < num_cols; ++
col) {
1436 col_by_degree.push_back(
1439 std::sort(col_by_degree.begin(), col_by_degree.end());
1442 int num_already_free_variables = 0;
1443 int num_implied_free_variables = 0;
1444 int num_fixed_variables = 0;
1445 for (ColWithDegree col_with_degree : col_by_degree) {
1446 const ColIndex
col = col_with_degree.col;
1452 ++num_already_free_variables;
1463 if (used_rows[e.row()])
continue;
1469 if (coeff < 0.0)
std::swap(entry_lb, entry_ub);
1480 coeff > 0.0 ? -ub_sums[e.row()].SumWithoutUb(entry_ub) / coeff
1481 : -lb_sums[e.row()].SumWithoutLb(entry_lb) / coeff;
1483 coeff > 0.0 ? -lb_sums[e.row()].SumWithoutLb(entry_lb) / coeff
1484 : -ub_sums[e.row()].SumWithoutUb(entry_ub) / coeff;
1486 overall_implied_lb =
std::max(overall_implied_lb, implied_lb);
1487 overall_implied_ub =
std::min(overall_implied_ub, implied_ub);
1494 overall_implied_ub)) {
1502 overall_implied_lb) ||
1508 ++num_fixed_variables;
1511 overall_implied_lb)) {
1517 ++num_fixed_variables;
1524 overall_implied_lb) &&
1527 ++num_implied_free_variables;
1530 used_rows[e.row()] =
true;
1554 if (offset != 0.0) {
1555 variable_offsets_[
col] = offset;
1556 SubtractColumnMultipleFromConstraintBound(
col, offset, lp);
1558 postsolve_status_of_free_variables_[
col] =
1562 VLOG(1) << num_already_free_variables <<
" free variables in the problem.";
1563 VLOG(1) << num_implied_free_variables <<
" implied free columns.";
1564 VLOG(1) << num_fixed_variables <<
" variables can be fixed.";
1566 return num_implied_free_variables > 0;
1573 for (ColIndex
col(0);
col < num_cols; ++
col) {
1581 postsolve_status_of_free_variables_[
col];
1603 for (ColIndex doubleton_col(0); doubleton_col < num_cols; ++doubleton_col) {
1612 r.col = doubleton_col;
1616 if (row_deletion_helper_.
IsRowMarked(e.row()))
break;
1617 r.row[
index] = e.row();
1618 r.coeff[
index] = e.coefficient();
1622 if (
index != NUM_ROWS)
continue;
1634 if (std::abs(r.coeff[DELETED]) < std::abs(r.coeff[MODIFIED])) {
1635 std::swap(r.coeff[DELETED], r.coeff[MODIFIED]);
1636 std::swap(r.row[DELETED], r.row[MODIFIED]);
1643 r.deleted_row_as_column.Swap(
1652 new_variable_lb /= r.coeff[DELETED];
1653 new_variable_ub /= r.coeff[DELETED];
1654 if (r.coeff[DELETED] < 0.0)
std::swap(new_variable_lb, new_variable_ub);
1660 r.deleted_row_as_column.AddMultipleToSparseVectorAndIgnoreCommonIndex(
1661 -r.coeff[MODIFIED] / r.coeff[DELETED],
ColToRowIndex(r.col),
1667 if (r.objective_coefficient != 0.0) {
1670 if (
col == r.col)
continue;
1673 e.coefficient() * r.objective_coefficient / r.coeff[DELETED];
1679 if (std::abs(new_objective) >
parameters_.drop_tolerance()) {
1687 restore_stack_.push_back(r);
1690 if (!row_deletion_helper_.
IsEmpty()) {
1703 for (
const RestoreInfo& r :
Reverse(restore_stack_)) {
1735 if (
col == r.col)
continue;
1736 new_variable_value -= (e.coefficient() / r.coeff[DELETED]) *
1749 r.objective_coefficient -
1750 r.coeff[MODIFIED] * solution->
dual_values[r.row[MODIFIED]];
1753 current_reduced_cost / r.coeff[DELETED];
1788 const RowIndex
row = e.row();
1795 const bool is_constraint_upper_bound_relevant =
1796 e.coefficient() > 0.0 ? !is_unbounded_up : is_unbounded_up;
1797 activity_sign_correction_[
row] =
1798 is_constraint_upper_bound_relevant ? 1.0 : -1.0;
1799 rhs_[
row] = is_constraint_upper_bound_relevant
1807 is_unbounded_[
col] =
true;
1808 Fractional initial_feasible_value = MinInMagnitudeOrZeroIfInfinite(
1811 col, initial_feasible_value,
1812 ComputeVariableStatus(initial_feasible_value,
1835 for (RowIndex
row(0);
row < num_rows; ++
row) {
1837 dual_ub_[
row] = 0.0;
1840 dual_lb_[
row] = 0.0;
1845 may_have_participated_lb_.
assign(num_cols,
false);
1846 may_have_participated_ub_.
assign(num_cols,
false);
1849 std::deque<ColIndex> columns_to_process;
1851 std::vector<RowIndex> changed_rows;
1852 for (ColIndex
col(0);
col < num_cols; ++
col) {
1853 columns_to_process.push_back(
col);
1859 const int limit = 5 * num_cols.value();
1860 for (
int count = 0; !columns_to_process.empty() && count < limit; ++count) {
1861 const ColIndex
col = columns_to_process.front();
1862 columns_to_process.pop_front();
1863 in_columns_to_process[
col] =
false;
1875 rc_lb.
Add(col_cost);
1876 rc_ub.
Add(col_cost);
1878 if (row_deletion_helper_.
IsRowMarked(e.row()))
continue;
1881 rc_lb.
Add(-coeff * dual_ub_[e.row()]);
1882 rc_ub.
Add(-coeff * dual_lb_[e.row()]);
1884 rc_lb.
Add(-coeff * dual_lb_[e.row()]);
1885 rc_ub.
Add(-coeff * dual_ub_[e.row()]);
1893 bool can_be_removed =
false;
1895 bool rc_is_away_from_zero;
1896 if (rc_ub.
Sum() <= low_tolerance) {
1897 can_be_removed =
true;
1899 rc_is_away_from_zero = rc_ub.
Sum() <= -high_tolerance;
1900 can_be_removed = !may_have_participated_ub_[
col];
1902 if (rc_lb.
Sum() >= -low_tolerance) {
1906 can_be_removed =
true;
1908 rc_is_away_from_zero = rc_lb.
Sum() >= high_tolerance;
1909 can_be_removed = !may_have_participated_lb_[
col];
1913 if (can_be_removed) {
1925 if (rc_is_away_from_zero) {
1926 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, variable " <<
col
1928 <<
" and its reduced cost is in [" << rc_lb.
Sum() <<
", "
1929 << rc_ub.
Sum() <<
"]";
1941 if (col_cost != 0.0)
continue;
1946 if (IsConstraintBlockingVariable(*lp, e.coefficient(), e.row())) {
1971 changed_rows.clear();
1973 if (row_deletion_helper_.
IsRowMarked(e.row()))
continue;
1975 const RowIndex
row = e.row();
1980 if (candidate < dual_ub_[
row]) {
1981 dual_ub_[
row] = candidate;
1982 may_have_participated_lb_[
col] =
true;
1988 if (candidate > dual_lb_[
row]) {
1989 dual_lb_[
row] = candidate;
1990 may_have_participated_lb_[
col] =
true;
1999 if (candidate > dual_lb_[
row]) {
2000 dual_lb_[
row] = candidate;
2001 may_have_participated_ub_[
col] =
true;
2007 if (candidate < dual_ub_[
row]) {
2008 dual_ub_[
row] = candidate;
2009 may_have_participated_ub_[
col] =
true;
2016 if (!changed_rows.empty()) {
2018 for (
const RowIndex
row : changed_rows) {
2022 if (!in_columns_to_process[
col]) {
2023 columns_to_process.push_back(
col);
2024 in_columns_to_process[
col] =
true;
2036 for (ColIndex
col(0);
col < end; ++
col) {
2046 return !column_deletion_helper_.
IsEmpty() || !row_deletion_helper_.
IsEmpty();
2056 struct DeletionEntry {
2061 std::vector<DeletionEntry> entries;
2066 for (RowIndex
row(0);
row < num_rows; ++
row) {
2074 if (is_unbounded_[
col]) {
2076 last_coefficient = e.coefficient();
2080 entries.push_back({
row, last_col, last_coefficient});
2085 std::sort(entries.begin(), entries.end(),
2086 [](
const DeletionEntry&
a,
const DeletionEntry&
b) {
2087 if (a.col == b.col) return a.row < b.row;
2088 return a.col < b.col;
2092 for (
int i = 0; i < entries.size();) {
2093 const ColIndex
col = entries[i].col;
2098 for (; i < entries.size(); ++i) {
2099 if (entries[i].
col !=
col)
break;
2100 const RowIndex
row = entries[i].row;
2119 if (activity * activity_sign_correction_[
row] < 0.0) {
2121 if (std::abs(
bound) > std::abs(primal_value_shift)) {
2122 primal_value_shift =
bound;
2131 activity_sign_correction_[row_at_bound] == 1.0
2146 for (RowIndex
row(0);
row < num_rows; ++
row) {
2154 return !row_deletion_helper_.
IsEmpty();
2176 for (ColIndex
col(0);
col < num_cols; ++
col) {
2183 for (RowIndex
row(0);
row < num_rows; ++
row) {
2184 if (degree[
row] == 0) {
2191 VLOG(1) <<
"Problem PRIMAL_INFEASIBLE, constraint " <<
row
2192 <<
" is empty and its range ["
2202 return !row_deletion_helper_.
IsEmpty();
2219 is_maximization_(lp.IsMaximizationProblem()),
2221 cost_(lp.objective_coefficients()[e.
col]),
2222 variable_lower_bound_(lp.variable_lower_bounds()[e.
col]),
2223 variable_upper_bound_(lp.variable_upper_bounds()[e.
col]),
2224 constraint_lower_bound_(lp.constraint_lower_bounds()[e.
row]),
2225 constraint_upper_bound_(lp.constraint_upper_bounds()[e.
row]),
2226 constraint_status_(status) {}
2234 SingletonRowUndo(saved_column, solution);
2237 ZeroCostSingletonColumnUndo(
parameters, saved_row, solution);
2240 SingletonColumnInEqualityUndo(
parameters, saved_row, solution);
2243 MakeConstraintAnEqualityUndo(solution);
2248 void SingletonPreprocessor::DeleteSingletonRow(MatrixEntry e,
2254 if (e.coeff < 0.0) {
2255 std::swap(implied_lower_bound, implied_upper_bound);
2262 std::abs(
parameters_.preprocessor_zero_tolerance() / e.coeff);
2264 implied_lower_bound - potential_error > old_lower_bound
2265 ? implied_lower_bound
2268 implied_upper_bound + potential_error < old_upper_bound
2269 ? implied_upper_bound
2272 if (new_upper_bound < new_lower_bound) {
2275 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2276 "row causes the bound of the variable "
2277 << e.col <<
" to be infeasible by "
2278 << new_lower_bound - new_upper_bound;
2284 new_upper_bound = new_lower_bound;
2287 new_lower_bound = new_upper_bound;
2289 DCHECK_EQ(new_lower_bound, new_upper_bound);
2300 void SingletonUndo::SingletonRowUndo(
const SparseColumn& saved_column,
2301 ProblemSolution* solution)
const {
2302 DCHECK_EQ(0, solution->dual_values[e_.row]);
2307 const VariableStatus status = solution->variable_statuses[e_.col];
2311 Fractional implied_lower_bound = constraint_lower_bound_ / e_.coeff;
2312 Fractional implied_upper_bound = constraint_upper_bound_ / e_.coeff;
2313 if (e_.coeff < 0.0) {
2314 std::swap(implied_lower_bound, implied_upper_bound);
2316 const bool lower_bound_changed = implied_lower_bound > variable_lower_bound_;
2317 const bool upper_bound_changed = implied_upper_bound < variable_upper_bound_;
2319 if (!lower_bound_changed && !upper_bound_changed)
return;
2327 const Fractional reduced_cost_for_minimization =
2328 is_maximization_ ? -reduced_cost : reduced_cost;
2331 DCHECK(lower_bound_changed || upper_bound_changed);
2332 if (reduced_cost_for_minimization >= 0.0 && !lower_bound_changed) {
2336 if (reduced_cost_for_minimization <= 0.0 && !upper_bound_changed) {
2347 solution->dual_values[e_.row] = reduced_cost / e_.coeff;
2350 (!lower_bound_changed || !upper_bound_changed)) {
2351 new_constraint_status = lower_bound_changed
2355 if (e_.coeff < 0.0) {
2363 solution->constraint_statuses[e_.row] = new_constraint_status;
2366 void SingletonPreprocessor::UpdateConstraintBoundsWithVariableBounds(
2367 MatrixEntry e, LinearProgram* lp) {
2368 Fractional lower_delta = -e.coeff * lp->variable_upper_bounds()[e.col];
2369 Fractional upper_delta = -e.coeff * lp->variable_lower_bounds()[e.col];
2370 if (e.coeff < 0.0) {
2373 lp->SetConstraintBounds(e.row,
2374 lp->constraint_lower_bounds()[e.row] + lower_delta,
2375 lp->constraint_upper_bounds()[e.row] + upper_delta);
2378 bool SingletonPreprocessor::IntegerSingletonColumnIsRemovable(
2379 const MatrixEntry& matrix_entry,
const LinearProgram& lp)
const {
2381 DCHECK(lp.IsVariableInteger(matrix_entry.col));
2382 const SparseMatrix& transpose = lp.GetTransposeSparseMatrix();
2394 coefficient_ratio,
parameters_.solution_feasibility_tolerance())) {
2399 lp.constraint_lower_bounds()[matrix_entry.row];
2401 const Fractional lower_bound_ratio = constraint_lb / matrix_entry.coeff;
2403 lower_bound_ratio,
parameters_.solution_feasibility_tolerance())) {
2408 lp.constraint_upper_bounds()[matrix_entry.row];
2410 const Fractional upper_bound_ratio = constraint_ub / matrix_entry.coeff;
2412 upper_bound_ratio,
parameters_.solution_feasibility_tolerance())) {
2419 void SingletonPreprocessor::DeleteZeroCostSingletonColumn(
2420 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2424 const SparseColumn& row_as_col = transpose.column(transpose_col);
2426 UpdateConstraintBoundsWithVariableBounds(e, lp);
2431 void SingletonUndo::ZeroCostSingletonColumnUndo(
2432 const GlopParameters&
parameters,
const SparseColumn& saved_row,
2433 ProblemSolution* solution)
const {
2436 if (variable_upper_bound_ == variable_lower_bound_) {
2437 solution->primal_values[e_.col] = variable_lower_bound_;
2449 solution->primal_values[e_.col] = variable_lower_bound_;
2453 solution->primal_values[e_.col] = variable_upper_bound_;
2456 if (constraint_upper_bound_ == constraint_lower_bound_) {
2471 const auto is_smaller_with_tolerance = [tolerance](
Fractional a,
2475 if (variable_lower_bound_ != -
kInfinity) {
2477 activity + e_.coeff * variable_lower_bound_;
2478 if (is_smaller_with_tolerance(constraint_lower_bound_, activity_at_lb) &&
2479 is_smaller_with_tolerance(activity_at_lb, constraint_upper_bound_)) {
2480 solution->primal_values[e_.col] = variable_lower_bound_;
2485 if (variable_upper_bound_ !=
kInfinity) {
2487 activity + e_.coeff * variable_upper_bound_;
2488 if (is_smaller_with_tolerance(constraint_lower_bound_, actibity_at_ub) &&
2489 is_smaller_with_tolerance(actibity_at_ub, constraint_upper_bound_)) {
2490 solution->primal_values[e_.col] = variable_upper_bound_;
2499 if (constraint_lower_bound_ == -
kInfinity &&
2501 solution->primal_values[e_.col] = 0.0;
2509 if (constraint_lower_bound_ == constraint_upper_bound_) {
2510 solution->primal_values[e_.col] =
2511 (constraint_lower_bound_ - activity) / e_.coeff;
2516 bool set_constraint_to_lower_bound;
2517 if (constraint_lower_bound_ == -
kInfinity) {
2518 set_constraint_to_lower_bound =
false;
2519 }
else if (constraint_upper_bound_ ==
kInfinity) {
2520 set_constraint_to_lower_bound =
true;
2524 const Fractional to_lb = (constraint_lower_bound_ - activity) / e_.coeff;
2525 const Fractional to_ub = (constraint_upper_bound_ - activity) / e_.coeff;
2526 set_constraint_to_lower_bound =
2527 std::max(variable_lower_bound_ - to_lb, to_lb - variable_upper_bound_) <
2528 std::max(variable_lower_bound_ - to_ub, to_ub - variable_upper_bound_);
2531 if (set_constraint_to_lower_bound) {
2532 solution->primal_values[e_.col] =
2533 (constraint_lower_bound_ - activity) / e_.coeff;
2536 solution->primal_values[e_.col] =
2537 (constraint_upper_bound_ - activity) / e_.coeff;
2542 void SingletonPreprocessor::DeleteSingletonColumnInEquality(
2543 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2546 const SparseColumn& row_as_column = transpose.column(transpose_col);
2547 undo_stack_.push_back(
2557 const Fractional rhs = lp->constraint_upper_bounds()[e.row];
2560 lp->SetObjectiveOffset(lp->objective_offset() + rhs * multiplier);
2565 lp->objective_coefficients()[
col] - e.coefficient() * multiplier;
2572 if (std::abs(new_cost) <
parameters_.preprocessor_zero_tolerance()) {
2575 lp->SetObjectiveCoefficient(
col, new_cost);
2580 UpdateConstraintBoundsWithVariableBounds(e, lp);
2584 void SingletonUndo::SingletonColumnInEqualityUndo(
2585 const GlopParameters&
parameters,
const SparseColumn& saved_row,
2586 ProblemSolution* solution)
const {
2588 ZeroCostSingletonColumnUndo(
parameters, saved_row, solution);
2592 solution->dual_values[e_.row] += cost_ / e_.coeff;
2599 void SingletonUndo::MakeConstraintAnEqualityUndo(
2600 ProblemSolution* solution)
const {
2602 solution->constraint_statuses[e_.row] = constraint_status_;
2606 bool SingletonPreprocessor::MakeConstraintAnEqualityIfPossible(
2607 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2610 const Fractional cst_lower_bound = lp->constraint_lower_bounds()[e.row];
2611 const Fractional cst_upper_bound = lp->constraint_upper_bounds()[e.row];
2612 if (cst_lower_bound == cst_upper_bound)
return true;
2618 const DenseRow& variable_ubs = lp->variable_upper_bounds();
2619 const DenseRow& variable_lbs = lp->variable_lower_bounds();
2620 if (e.row >= row_sum_is_cached_.
size() || !row_sum_is_cached_[e.row]) {
2621 if (e.row >= row_sum_is_cached_.
size()) {
2622 const int new_size = e.row.value() + 1;
2623 row_sum_is_cached_.
resize(new_size);
2624 row_lb_sum_.resize(new_size);
2625 row_ub_sum_.resize(new_size);
2627 row_sum_is_cached_[e.row] =
true;
2628 row_lb_sum_[e.row].Add(cst_lower_bound);
2629 row_ub_sum_[e.row].Add(cst_upper_bound);
2640 if (column_deletion_helper_.
IsColumnMarked(row_as_col))
continue;
2641 if (entry.coefficient() > 0.0) {
2642 row_lb_sum_[e.row].Add(-entry.coefficient() * variable_ubs[row_as_col]);
2643 row_ub_sum_[e.row].Add(-entry.coefficient() * variable_lbs[row_as_col]);
2645 row_lb_sum_[e.row].Add(-entry.coefficient() * variable_lbs[row_as_col]);
2646 row_ub_sum_[e.row].Add(-entry.coefficient() * variable_ubs[row_as_col]);
2658 c > 0.0 ? row_lb_sum_[e.row].SumWithoutLb(-c * variable_ubs[e.col]) / c
2659 : row_ub_sum_[e.row].SumWithoutUb(-c * variable_ubs[e.col]) / c;
2661 c > 0.0 ? row_ub_sum_[e.row].SumWithoutUb(-c * variable_lbs[e.col]) / c
2662 : row_lb_sum_[e.row].SumWithoutLb(-c * variable_lbs[e.col]) / c;
2668 lp->GetObjectiveCoefficientForMinimizationVersion(e.col);
2675 ub, lp->variable_upper_bounds()[e.col])) {
2681 lp->SetConstraintBounds(e.row, cst_upper_bound, cst_upper_bound);
2688 lp->SetConstraintBounds(e.row, cst_lower_bound, cst_lower_bound);
2694 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2696 << e.col <<
" has a cost (for minimization) of " <<
cost
2697 <<
" and is unbounded towards kInfinity.";
2714 lp->SetVariableBounds(e.col, lp->variable_lower_bounds()[e.col],
kInfinity);
2717 lp->variable_lower_bounds()[e.col], lb)) {
2723 lp->SetConstraintBounds(e.row, cst_lower_bound, cst_lower_bound);
2730 lp->SetConstraintBounds(e.row, cst_upper_bound, cst_upper_bound);
2736 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2738 << e.col <<
" has a cost (for minimization) of " <<
cost
2739 <<
" and is unbounded towards -kInfinity.";
2744 lp->SetVariableBounds(e.col, -
kInfinity,
2745 lp->variable_upper_bounds()[e.col]);
2748 if (lp->constraint_lower_bounds()[e.row] ==
2749 lp->constraint_upper_bounds()[e.row]) {
2750 undo_stack_.push_back(SingletonUndo(
2764 ColIndex num_cols(matrix.
num_cols());
2765 RowIndex num_rows(matrix.
num_rows());
2767 std::vector<ColIndex> column_to_process;
2768 for (ColIndex
col(0);
col < num_cols; ++
col) {
2770 if (column_degree[
col] == 1) {
2771 column_to_process.push_back(
col);
2777 std::vector<RowIndex> row_to_process;
2778 for (RowIndex
row(0);
row < num_rows; ++
row) {
2780 if (row_degree[
row] == 1) {
2781 row_to_process.push_back(
row);
2787 (!column_to_process.empty() || !row_to_process.empty())) {
2789 const ColIndex
col = column_to_process.back();
2790 column_to_process.pop_back();
2791 if (column_degree[
col] <= 0)
continue;
2792 const MatrixEntry e = GetSingletonColumnMatrixEntry(
col, matrix);
2794 !IntegerSingletonColumnIsRemovable(e, *lp)) {
2801 DeleteZeroCostSingletonColumn(transpose, e, lp);
2802 }
else if (MakeConstraintAnEqualityIfPossible(transpose, e, lp)) {
2803 DeleteSingletonColumnInEquality(transpose, e, lp);
2807 --row_degree[e.row];
2808 if (row_degree[e.row] == 1) {
2813 const RowIndex
row = row_to_process.back();
2814 row_to_process.pop_back();
2815 if (row_degree[
row] <= 0)
continue;
2816 const MatrixEntry e = GetSingletonRowMatrixEntry(
row, transpose);
2818 DeleteSingletonRow(e, lp);
2819 --column_degree[e.col];
2820 if (column_degree[e.col] == 1) {
2829 return !column_deletion_helper_.
IsEmpty() || !row_deletion_helper_.
IsEmpty();
2847 for (
int i = undo_stack_.size() - 1; i >= 0; --i) {
2852 undo_stack_[i].Undo(
parameters_, saved_col, saved_row, solution);
2856 MatrixEntry SingletonPreprocessor::GetSingletonColumnMatrixEntry(
2861 return MatrixEntry(e.row(),
col, e.coefficient());
2865 LOG(DFATAL) <<
"No unmarked entry in a column that is supposed to have one.";
2867 return MatrixEntry(RowIndex(0), ColIndex(0), 0.0);
2870 MatrixEntry SingletonPreprocessor::GetSingletonRowMatrixEntry(
2871 RowIndex
row,
const SparseMatrix& transpose) {
2876 return MatrixEntry(
row,
col, e.coefficient());
2880 LOG(DFATAL) <<
"No unmarked entry in a row that is supposed to have one.";
2882 return MatrixEntry(RowIndex(0), ColIndex(0), 0.0);
2893 if (num_cols == 0)
return false;
2899 Fractional num_non_zero_objective_coefficients = 0.0;
2900 for (ColIndex
col(0);
col < num_cols; ++
col) {
2902 row_degree[e.row()] += 1.0;
2905 num_non_zero_objective_coefficients += 1.0;
2917 const EntryIndex initial_num_entries = lp->
num_entries();
2918 int num_zeroed_objective_coefficients = 0;
2919 for (ColIndex
col(0);
col < num_cols; ++
col) {
2928 if (max_magnitude ==
kInfinity || max_magnitude == 0)
continue;
2929 const Fractional threshold = allowed_impact / max_magnitude;
2931 threshold, row_degree);
2934 num_non_zero_objective_coefficients *
2938 ++num_zeroed_objective_coefficients;
2945 <<
" near-zero entries.";
2947 if (num_zeroed_objective_coefficients > 0) {
2948 VLOG(1) <<
"Removed " << num_zeroed_objective_coefficients
2949 <<
" near-zero objective coefficients.";
2967 if (num_cols == 0)
return false;
2969 changed_columns_.clear();
2970 int num_singletons = 0;
2971 for (ColIndex
col(0);
col < num_cols; ++
col) {
2983 changed_columns_.push_back(
col);
2986 VLOG(1) <<
"Changed the sign of " << changed_columns_.size() <<
" columns.";
2987 VLOG(1) << num_singletons <<
" singleton columns left.";
2988 return !changed_columns_.empty();
2995 for (
int i = 0; i < changed_columns_.size(); ++i) {
2996 const ColIndex
col = changed_columns_[i];
3033 std::vector<std::pair<int64_t, RowIndex>> sorted_rows;
3035 for (RowIndex
row(0);
row < num_rows; ++
row) {
3048 sorted_rows.push_back({score,
row});
3050 std::sort(sorted_rows.begin(), sorted_rows.end());
3056 for (
const auto p : sorted_rows) {
3057 const RowIndex
row = p.second;
3067 int entry_index = 0;
3071 r.col[entry_index] =
col;
3072 r.coeff[entry_index] = e.coefficient();
3080 if (entry_index < 2)
continue;
3086 for (
int col_choice = 0; col_choice < NUM_DOUBLETON_COLS; ++col_choice) {
3087 const ColIndex
col = r.col[col_choice];
3095 if (r.lb[DELETED] == r.ub[DELETED] || r.lb[MODIFIED] == r.ub[MODIFIED]) {
3112 const Fractional carry_over_offset = r.rhs / r.coeff[MODIFIED];
3114 -r.coeff[DELETED] / r.coeff[MODIFIED];
3116 carry_over_factor == 0.0) {
3124 r.lb[DELETED] * carry_over_factor + carry_over_offset;
3126 r.ub[DELETED] * carry_over_factor + carry_over_offset;
3127 if (carry_over_factor < 0) {
3128 std::swap(carried_over_lb, carried_over_ub);
3130 if (carried_over_lb <= lb) {
3135 lb = carried_over_lb;
3140 carry_over_factor > 0 ? r.lb[DELETED] : r.ub[DELETED]);
3142 if (carried_over_ub >= ub) {
3147 ub = carried_over_ub;
3152 carry_over_factor > 0 ? r.ub[DELETED] : r.lb[DELETED]);
3161 restore_stack_.push_back(r);
3170 -r.coeff[MODIFIED] / r.coeff[DELETED];
3171 const Fractional constant_offset_factor = r.rhs / r.coeff[DELETED];
3173 if (!
IsFinite(substitution_factor) || substitution_factor == 0.0 ||
3174 !
IsFinite(constant_offset_factor)) {
3182 for (
const int col_choice : {DELETED, MODIFIED}) {
3183 const ColIndex
col = r.col[col_choice];
3189 substitution_factor, r.row,
parameters_.drop_tolerance(),
3197 r.objective_coefficient[MODIFIED] +
3198 substitution_factor * r.objective_coefficient[DELETED];
3199 if (std::abs(new_objective) >
parameters_.drop_tolerance()) {
3209 SubtractColumnMultipleFromConstraintBound(r.col[DELETED],
3210 constant_offset_factor, lp);
3226 return !column_deletion_helper_.
IsEmpty();
3239 for (
const RestoreInfo& r :
Reverse(restore_stack_)) {
3242 LOG(DFATAL) <<
"FIXED variable produced by DoubletonPreprocessor!";
3249 ABSL_FALLTHROUGH_INTENDED;
3254 new_basic_columns[r.col[DELETED]] =
true;
3257 ABSL_FALLTHROUGH_INTENDED;
3265 ? r.bound_backtracking_at_lower_bound
3266 : r.bound_backtracking_at_upper_bound;
3267 const ColIndex bounded_var = r.col[bound_backtracking.
col_choice];
3268 const ColIndex basic_var =
3269 r.col[OtherColChoice(bound_backtracking.
col_choice)];
3273 new_basic_columns[basic_var] =
true;
3284 solution->
primal_values[r.col[MODIFIED]] * r.coeff[MODIFIED]) /
3313 for (
int i = 0; i < restore_stack_.size(); ++i) {
3314 const RestoreInfo& r = restore_stack_[i];
3315 col_to_index[r.col[MODIFIED]].
insert(i);
3316 col_to_index[r.col[DELETED]].
insert(i);
3318 std::vector<ColIndex> singleton_col;
3319 for (ColIndex
col(0);
col < num_cols; ++
col) {
3320 if (!new_basic_columns[
col])
continue;
3321 if (col_to_index[
col].size() == 1) singleton_col.push_back(
col);
3323 while (!singleton_col.empty()) {
3324 const ColIndex
col = singleton_col.back();
3325 singleton_col.pop_back();
3326 if (!new_basic_columns[
col])
continue;
3327 if (col_to_index[
col].empty())
continue;
3330 const RestoreInfo& r = restore_stack_[
index];
3332 const ColChoice col_choice = r.col[MODIFIED] ==
col ? MODIFIED : DELETED;
3340 saved_objective_[r.col[col_choice]] -
3342 solution->
dual_values[r.row] = current_reduced_cost / r.coeff[col_choice];
3347 if (col_to_index[r.col[DELETED]].
size() == 1) {
3348 singleton_col.push_back(r.col[DELETED]);
3350 if (col_to_index[r.col[MODIFIED]].
size() == 1) {
3351 singleton_col.push_back(r.col[MODIFIED]);
3357 saved_row_upper_bounds_, solution);
3366 for (RowIndex
row(0);
row < num_rows; ++
row) {
3370 if (row_lower_bounds[
row] == row_upper_bounds[
row])
continue;
3382 void DoubletonEqualityRowPreprocessor::
3383 SwapDeletedAndModifiedVariableRestoreInfo(RestoreInfo* r) {
3385 swap(r->col[DELETED], r->col[MODIFIED]);
3386 swap(r->coeff[DELETED], r->coeff[MODIFIED]);
3387 swap(r->lb[DELETED], r->lb[MODIFIED]);
3388 swap(r->ub[DELETED], r->ub[MODIFIED]);
3389 swap(r->objective_coefficient[DELETED], r->objective_coefficient[MODIFIED]);
3399 if (
parameters_.solve_dual_problem() == GlopParameters::NEVER_DO) {
3426 if (
parameters_.solve_dual_problem() == GlopParameters::LET_SOLVER_DECIDE) {
3427 if (1.0 * primal_num_rows_.value() <
3428 parameters_.dualizer_threshold() * primal_num_cols_.value()) {
3438 variable_lower_bounds_.
assign(num_cols, 0.0);
3439 variable_upper_bounds_.
assign(num_cols, 0.0);
3440 for (ColIndex
col(0);
col < num_cols; ++
col) {
3445 variable_lower_bounds_[
col] = lower;
3446 variable_upper_bounds_[
col] = upper;
3447 const Fractional value = MinInMagnitudeOrZeroIfInfinite(lower, upper);
3450 SubtractColumnMultipleFromConstraintBound(
col,
value, lp);
3458 dual_status_correspondence_.
clear();
3459 for (RowIndex
row(0);
row < primal_num_rows_; ++
row) {
3469 LOG(DFATAL) <<
"There should be no free constraint in this lp.";
3472 slack_or_surplus_mapping_.
clear();
3473 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3483 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3516 DenseRow new_primal_values(primal_num_cols_, 0.0);
3520 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3527 const Fractional shift = MinInMagnitudeOrZeroIfInfinite(lower, upper);
3538 new_variable_statuses[
col] = ComputeVariableStatus(shift, lower, upper);
3547 const ColIndex end = dual_status_correspondence_.
size();
3552 const ColIndex
col = slack_or_surplus_mapping_[
index - begin];
3560 new_primal_values[
col] = variable_upper_bounds_[
col];
3563 new_primal_values[
col] = variable_lower_bounds_[
col];
3571 DenseColumn new_dual_values(primal_num_rows_, 0.0);
3578 Fractional sign = primal_is_maximization_problem_ ? -1 : 1;
3579 for (RowIndex
row(0);
row < primal_num_rows_; ++
row) {
3597 new_constraint_statuses[
row] =
3604 new_dual_values[
row] +=
3650 bool all_variable_domains_contain_zero =
true;
3652 variable_initial_lbs_.
assign(num_cols, 0.0);
3653 variable_initial_ubs_.
assign(num_cols, 0.0);
3654 for (ColIndex
col(0);
col < num_cols; ++
col) {
3657 if (0.0 < variable_initial_lbs_[
col] || 0.0 > variable_initial_ubs_[
col]) {
3658 all_variable_domains_contain_zero =
false;
3661 VLOG(1) <<
"Maximum variable bounds magnitude (before shift): "
3662 << ComputeMaxVariableBoundsMagnitude(*lp);
3665 if (all_variable_domains_contain_zero)
return false;
3669 int num_bound_shifts = 0;
3673 offsets_.
assign(num_cols, 0.0);
3674 for (ColIndex
col(0);
col < num_cols; ++
col) {
3675 if (0.0 < variable_initial_lbs_[
col] || 0.0 > variable_initial_ubs_[
col]) {
3676 Fractional offset = MinInMagnitudeOrZeroIfInfinite(
3677 variable_initial_lbs_[
col], variable_initial_ubs_[
col]);
3685 offset = trunc(offset);
3689 offsets_[
col] = offset;
3691 variable_initial_ubs_[
col] - offset);
3694 row_offsets[e.row()].Add(e.coefficient() * offset);
3700 VLOG(1) <<
"Maximum variable bounds magnitude (after " << num_bound_shifts
3701 <<
" shifts): " << ComputeMaxVariableBoundsMagnitude(*lp);
3704 for (RowIndex
row(0);
row < num_rows; ++
row) {
3718 for (ColIndex
col(0);
col < num_cols; ++
col) {
3724 ABSL_FALLTHROUGH_INTENDED;
3752 variable_lower_bounds_.
assign(num_cols, 0.0);
3753 variable_upper_bounds_.
assign(num_cols, 0.0);
3754 for (ColIndex
col(0);
col < num_cols; ++
col) {
3786 for (ColIndex
col(0);
col < num_cols; ++
col) {
3789 ABSL_FALLTHROUGH_INTENDED;
3797 ABSL_FALLTHROUGH_INTENDED;
3848 for (RowIndex
row(0);
row < num_rows; ++
row) {
3855 switch (variable_status) {
#define DCHECK_LE(val1, val2)
#define DCHECK_NE(val1, val2)
#define CHECK_EQ(val1, val2)
#define DCHECK_GE(val1, val2)
#define DCHECK(condition)
#define DCHECK_EQ(val1, val2)
#define VLOG(verboselevel)
iterator erase(const_iterator pos)
iterator insert(const_iterator pos, const value_type &x)
void resize(size_type new_size)
void push_back(const value_type &x)
void swap(StrongVector &x)
void Add(const FpNumber &value)
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool IsColumnMarked(ColIndex col) const
void MarkColumnForDeletionWithState(ColIndex col, Fractional value, VariableStatus status)
void MarkColumnForDeletion(ColIndex col)
const DenseBooleanRow & GetMarkedColumns() const
void RestoreDeletedColumns(ProblemSolution *solution) const
const DenseRow & GetStoredValue() const
const SparseColumn & SavedOrEmptyColumn(ColIndex col) const
void SaveColumnIfNotAlreadyDone(ColIndex col, const SparseColumn &column)
void SaveColumn(ColIndex col, const SparseColumn &column)
const SparseColumn & SavedColumn(ColIndex col) const
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
ProblemStatus ChangeStatusToDualStatus(ProblemStatus status) const
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
SparseMatrix * GetMutableTransposeSparseMatrix()
void SetObjectiveScalingFactor(Fractional objective_scaling_factor)
DenseColumn * mutable_constraint_upper_bounds()
void SetVariableBounds(ColIndex col, Fractional lower_bound, Fractional upper_bound)
const SparseMatrix & GetTransposeSparseMatrix() const
void SetObjectiveOffset(Fractional objective_offset)
ColIndex GetFirstSlackVariable() const
const SparseMatrix & GetSparseMatrix() const
const DenseRow & variable_lower_bounds() const
const DenseColumn & constraint_lower_bounds() const
Fractional ScaleObjective(GlopParameters::CostScalingAlgorithm method)
const DenseRow & objective_coefficients() const
Fractional GetObjectiveCoefficientForMinimizationVersion(ColIndex col) const
void SetConstraintBounds(RowIndex row, Fractional lower_bound, Fractional upper_bound)
void Swap(LinearProgram *linear_program)
Fractional objective_offset() const
SparseColumn * GetMutableSparseColumn(ColIndex col)
void UseTransposeMatrixAsReference()
void AddSlackVariablesWhereNecessary(bool detect_integer_constraints)
const DenseColumn & constraint_upper_bounds() const
bool IsVariableInteger(ColIndex col) const
void SetObjectiveCoefficient(ColIndex col, Fractional value)
void DeleteRows(const DenseBooleanColumn &rows_to_delete)
void DeleteColumns(const DenseBooleanRow &columns_to_delete)
bool IsMaximizationProblem() const
const DenseRow & variable_upper_bounds() const
ColIndex num_variables() const
void PopulateFromDual(const LinearProgram &dual, RowToColMapping *duplicated_rows)
Fractional objective_scaling_factor() const
void SetMaximizationProblem(bool maximize)
const SparseColumn & GetSparseColumn(ColIndex col) const
EntryIndex num_entries() const
DenseColumn * mutable_constraint_lower_bounds()
RowIndex num_constraints() const
void RecoverSolution(ProblemSolution *solution) const override
bool Run(LinearProgram *lp) final
ProblemStatus status() const
bool IsSmallerWithinPreprocessorZeroTolerance(Fractional a, Fractional b) const
Preprocessor(const GlopParameters *parameters)
const GlopParameters & parameters_
bool IsSmallerWithinFeasibilityTolerance(Fractional a, Fractional b) const
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
void MarkRowForDeletion(RowIndex row)
void UnmarkRow(RowIndex row)
void RestoreDeletedRows(ProblemSolution *solution) const
const DenseBooleanColumn & GetMarkedRows() const
bool IsRowMarked(RowIndex row) const
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
void Undo(const GlopParameters ¶meters, const SparseColumn &saved_column, const SparseColumn &saved_row, ProblemSolution *solution) const
SingletonUndo(OperationType type, const LinearProgram &lp, MatrixEntry e, ConstraintStatus status)
@ ZERO_COST_SINGLETON_COLUMN
@ MAKE_CONSTRAINT_AN_EQUALITY
@ SINGLETON_COLUMN_IN_EQUALITY
SparseColumn * mutable_column(ColIndex col)
ColIndex num_cols() const
RowIndex num_rows() const
const SparseColumn & column(ColIndex col) const
void ScaleColumnVector(bool up, DenseColumn *column_vector) const
void ScaleRowVector(bool up, DenseRow *row_vector) const
Fractional LookUpCoefficient(Index index) const
void RemoveNearZeroEntriesWithWeights(Fractional threshold, const DenseVector &weights)
void AddMultipleToSparseVectorAndDeleteCommonIndex(Fractional multiplier, Index removed_common_index, Fractional drop_tolerance, SparseVector *accumulator_vector) const
void MultiplyByConstant(Fractional factor)
typename Iterator::Entry Entry
Fractional GetFirstCoefficient() const
EntryIndex num_entries() const
void resize(IntType size)
void assign(IntType size, const T &v)
Fractional SumWithoutUb(Fractional c) const
Fractional SumWithoutLb(Fractional c) const
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
void RemoveZeroCostUnconstrainedVariable(ColIndex col, Fractional target_bound, LinearProgram *lp)
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
SharedTimeLimit * time_limit
const RowIndex kInvalidRow(-1)
Fractional ScalarProduct(const DenseRowOrColumn1 &u, const DenseRowOrColumn2 &v)
Fractional PreciseScalarProduct(const DenseRowOrColumn &u, const DenseRowOrColumn2 &v)
StrictITIVector< ColIndex, Fractional > DenseRow
std::string GetProblemStatusString(ProblemStatus problem_status)
void FixConstraintWithFixedStatuses(const DenseColumn &row_lower_bounds, const DenseColumn &row_upper_bounds, ProblemSolution *solution)
@ INFEASIBLE_OR_UNBOUNDED
ColIndex RowToColIndex(RowIndex row)
bool IsFinite(Fractional value)
RowIndex ColToRowIndex(ColIndex col)
ConstraintStatus VariableToConstraintStatus(VariableStatus status)
ColMapping FindProportionalColumns(const SparseMatrix &matrix, Fractional tolerance)
void Scale(LinearProgram *lp, SparseMatrixScaler *scaler)
const ColIndex kInvalidCol(-1)
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
Collection of objects used to extend the Constraint Solver library.
bool IsSmallerWithinTolerance(FloatType x, FloatType y, FloatType tolerance)
bool IsIntegerWithinTolerance(FloatType x, FloatType tolerance)
BeginEndReverseIteratorWrapper< Container > Reverse(const Container &c)
#define RUN_PREPROCESSOR(name)
#define RETURN_IF_NULL(x)
#define RETURN_VALUE_IF_NULL(x, v)
std::vector< double > lower_bounds
std::vector< double > upper_bounds
#define SCOPED_INSTRUCTION_COUNT(time_limit)
VariableStatusRow variable_statuses
ConstraintStatusColumn constraint_statuses
#define VLOG_IS_ON(verboselevel)