19#include "absl/strings/str_format.h"
37 return absl::StrFormat(
"[%g, %g]", lb, ub);
41double trunc(
double d) {
return d > 0 ? floor(d) : ceil(d); }
51 in_mip_context_(false),
52 infinite_time_limit_(
TimeLimit::Infinite()),
53 time_limit_(infinite_time_limit_.get()) {}
60#define RUN_PREPROCESSOR(name) \
61 RunAndPushIfRelevant(std::unique_ptr<Preprocessor>(new name(¶meters_)), \
62 #name, time_limit_, lp)
74 const int kMaxNumPasses = 20;
75 for (
int i = 0; i < kMaxNumPasses; ++i) {
76 const int old_stack_size = preprocessors_.size();
89 if (preprocessors_.size() == old_stack_size) {
92 LOG(
INFO) <<
"Reached fixed point after presolve pass #" << i;
107 const int old_stack_size = preprocessors_.size();
113 if (old_stack_size != preprocessors_.size()) {
127 return !preprocessors_.empty();
130#undef RUN_PREPROCESSOR
132void MainLpPreprocessor::RunAndPushIfRelevant(
133 std::unique_ptr<Preprocessor> preprocessor,
const std::string&
name,
139 const double start_time =
time_limit->GetElapsedTime();
150 if (preprocessor->Run(lp)) {
151 const EntryIndex new_num_entries = lp->
num_entries();
152 const double preprocess_time =
time_limit->GetElapsedTime() - start_time;
155 "%s(%fs): %d(%d) rows, %d(%d) columns, %d(%d) entries.",
name,
162 static_cast<int64_t
>(new_num_entries.value()),
163 static_cast<int64_t
>(new_num_entries.value() -
164 initial_num_entries_.value()));
166 status_ = preprocessor->status();
167 preprocessors_.push_back(std::move(preprocessor));
172 status_ = preprocessor->status();
174 LOG(
INFO) <<
name <<
" detected that the problem is "
183 p->RecoverSolution(solution);
189 while (!preprocessors_.empty()) {
190 preprocessors_.back()->RecoverSolution(solution);
191 preprocessors_.pop_back();
200 const int index = saved_columns_.size();
201 CHECK(saved_columns_index_.insert({col, index}).second);
202 saved_columns_.push_back(column);
207 const int index = saved_columns_.size();
208 const bool inserted = saved_columns_index_.insert({
col,
index}).second;
209 if (inserted) saved_columns_.push_back(column);
213 const auto it = saved_columns_index_.find(
col);
214 CHECK(it != saved_columns_index_.end());
215 return saved_columns_[it->second];
219 const auto it = saved_columns_index_.find(
col);
220 return it == saved_columns_index_.end() ? empty_column_
221 : saved_columns_[it->second];
225 is_column_deleted_.
clear();
226 stored_value_.
clear();
236 if (
col >= is_column_deleted_.
size()) {
237 is_column_deleted_.
resize(
col + 1,
false);
241 is_column_deleted_[
col] =
true;
242 stored_value_[
col] = fixed_value;
243 stored_status_[
col] = status;
250 ColIndex old_index(0);
251 for (ColIndex
col(0);
col < is_column_deleted_.
size(); ++
col) {
252 if (is_column_deleted_[
col]) {
265 for (; old_index < num_cols; ++old_index) {
281 if (
row >= is_row_deleted_.
size()) {
284 is_row_deleted_[
row] =
true;
288 if (
row >= is_row_deleted_.
size())
return;
289 is_row_deleted_[
row] =
false;
293 return is_row_deleted_;
299 RowIndex old_index(0);
300 const RowIndex end = is_row_deleted_.
size();
301 for (RowIndex
row(0);
row < end; ++
row) {
302 if (is_row_deleted_[
row]) {
316 for (; old_index < num_rows; ++old_index) {
367Fractional ComputeMaxVariableBoundsMagnitude(
const LinearProgram& lp) {
369 const ColIndex num_cols = lp.num_variables();
370 for (ColIndex
col(0);
col < num_cols; ++
col) {
372 max_bounds_magnitude,
373 std::max(MagnitudeOrZeroIfInfinite(lp.variable_lower_bounds()[
col]),
374 MagnitudeOrZeroIfInfinite(lp.variable_upper_bounds()[
col])));
376 return max_bounds_magnitude;
384 column_deletion_helper_.
Clear();
386 for (ColIndex
col(0);
col < num_cols; ++
col) {
393 if (objective_coefficient == 0) {
407 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, empty column " <<
col
408 <<
" has a minimization cost of " << objective_coefficient
422 return !column_deletion_helper_.
IsEmpty();
440void SubtractColumnMultipleFromConstraintBound(ColIndex
col,
446 const RowIndex
row = e.row();
460struct ColumnWithRepresentativeAndScaledCost {
461 ColumnWithRepresentativeAndScaledCost(ColIndex _col, ColIndex _representative,
468 bool operator<(
const ColumnWithRepresentativeAndScaledCost& other)
const {
471 return col < other.col;
492 int num_proportionality_classes = 0;
493 std::vector<ColIndex> proportional_columns;
499 ++num_proportionality_classes;
502 proportional_columns.push_back(
col);
505 if (proportional_columns.empty())
return false;
506 VLOG(1) <<
"The problem contains " << proportional_columns.size()
507 <<
" columns which belong to " << num_proportionality_classes
508 <<
" proportionality classes.";
512 column_factors_.
assign(num_cols, 0.0);
513 for (
const ColIndex
col : proportional_columns) {
527 for (
const ColIndex
col : proportional_columns) {
531 const bool is_rc_positive_or_zero =
533 const bool is_rc_negative_or_zero =
535 bool is_slope_upper_bounded = is_rc_positive_or_zero;
536 bool is_slope_lower_bounded = is_rc_negative_or_zero;
537 if (column_factors_[
col] < 0.0) {
538 std::swap(is_slope_lower_bounded, is_slope_upper_bounded);
542 column_factors_[
col];
543 if (is_slope_lower_bounded) {
547 if (is_slope_upper_bounded) {
554 for (
const ColIndex
col : proportional_columns) {
562 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, no feasible dual values"
563 <<
" can satisfy the constraints of the proportional columns"
565 <<
" the associated quantity must be in ["
575 for (
const ColIndex
col : proportional_columns) {
579 column_factors_[
col];
582 bool variable_can_be_fixed =
false;
590 variable_can_be_fixed =
true;
595 variable_can_be_fixed =
true;
599 if (variable_can_be_fixed) {
604 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED.";
617 std::vector<ColumnWithRepresentativeAndScaledCost> sorted_columns;
618 for (
const ColIndex
col : proportional_columns) {
623 sorted_columns.
push_back(ColumnWithRepresentativeAndScaledCost(
628 std::sort(sorted_columns.begin(), sorted_columns.end());
637 for (
int i = 0; i < sorted_columns.size();) {
638 const ColIndex target_col = sorted_columns[i].col;
639 const ColIndex target_representative = sorted_columns[i].representative;
640 const Fractional target_scaled_cost = sorted_columns[i].scaled_cost;
647 for (++i; i < sorted_columns.size(); ++i) {
648 if (sorted_columns[i].
representative != target_representative)
break;
649 if (std::abs(sorted_columns[i].
scaled_cost - target_scaled_cost) >=
654 const ColIndex
col = sorted_columns[i].col;
659 merged_columns_[
col] = target_col;
664 column_factors_[
col] / column_factors_[target_col];
678 if (bound_factor < 0.0) {
684 SubtractColumnMultipleFromConstraintBound(
col, target_value, lp);
693 if (num_merged > 0) {
694 merged_columns_[target_col] = target_col;
695 const Fractional target_value = MinInMagnitudeOrZeroIfInfinite(
696 lower_bounds_[target_col], upper_bounds_[target_col]);
700 SubtractColumnMultipleFromConstraintBound(target_col, target_value, lp);
707 return !column_deletion_helper_.
IsEmpty();
718 const ColIndex num_cols = merged_columns_.
size();
721 DenseRow distance_to_bound(num_cols, 0.0);
722 DenseRow wanted_value(num_cols, 0.0);
726 for (ColIndex
col(0);
col < num_cols; ++
col) {
727 if (merged_columns_[
col] ==
col) {
731 if (distance_to_upper_bound < distance_to_lower_bound) {
732 distance_to_bound[
col] = distance_to_upper_bound;
733 is_distance_to_upper_bound[
col] =
true;
735 distance_to_bound[
col] = distance_to_lower_bound;
736 is_distance_to_upper_bound[
col] =
false;
738 is_representative_basic[
col] =
745 lower_bounds_[
col], upper_bounds_[
col]);
752 for (ColIndex
col(0);
col < num_cols; ++
col) {
764 const bool to_upper_bound =
765 (bound_factor > 0.0) == is_distance_to_upper_bound[
representative];
766 if (width <= scaled_distance) {
768 to_upper_bound ? lower_bounds_[
col] : upper_bounds_[
col];
771 lower_bounds_[
col], upper_bounds_[
col]);
772 distance_to_bound[
representative] -= width * std::abs(bound_factor);
775 to_upper_bound ? upper_bounds_[
col] - scaled_distance
776 : lower_bounds_[
col] + scaled_distance;
799 const bool use_this_variable =
800 (error * bound_factor > 0.0) ? (upper_bounds_[
col] ==
kInfinity)
802 if (use_this_variable) {
835 row_factors_.
assign(num_rows, 0.0);
836 for (RowIndex
row(0);
row < num_rows; ++
row) {
838 if (!row_transpose.
IsEmpty()) {
859 int num_proportional_rows = 0;
860 for (RowIndex
row(0);
row < num_rows; ++
row) {
863 mapping[representative_row_as_col] = representative_row_as_col;
864 is_a_representative[
ColToRowIndex(representative_row_as_col)] =
true;
865 ++num_proportional_rows;
871 for (RowIndex
row(0);
row < num_rows; ++
row) {
877 const RowIndex representative_row =
ColToRowIndex(mapping[row_as_col]);
880 row_factors_[representative_row] / row_factors_[
row];
890 lower_bound_sources_[representative_row] =
row;
894 upper_bound_sources_[representative_row] =
row;
902 for (RowIndex
row(0);
row < num_rows; ++
row) {
903 if (!is_a_representative[
row])
continue;
904 const RowIndex lower_source = lower_bound_sources_[
row];
905 const RowIndex upper_source = upper_bound_sources_[
row];
910 if (lower_source == upper_source) {
914 row_deletion_helper_.
UnmarkRow(lower_source);
927 row_deletion_helper_.
UnmarkRow(lower_source);
932 row_deletion_helper_.
UnmarkRow(upper_source);
940 RowIndex new_representative = lower_source;
941 RowIndex other = upper_source;
942 if (std::abs(row_factors_[new_representative]) <
943 std::abs(row_factors_[other])) {
949 row_factors_[new_representative] / row_factors_[other];
956 lower_bound_sources_[new_representative] = new_representative;
957 upper_bound_sources_[new_representative] = new_representative;
960 lower_bound_sources_[new_representative] = other;
964 if (new_ub < lp->constraint_upper_bounds()[new_representative]) {
965 upper_bound_sources_[new_representative] = other;
969 const RowIndex new_lower_source =
970 lower_bound_sources_[new_representative];
971 if (new_lower_source == upper_bound_sources_[new_representative]) {
972 row_deletion_helper_.
UnmarkRow(new_lower_source);
973 lower_bound_sources_[new_representative] =
kInvalidRow;
974 upper_bound_sources_[new_representative] =
kInvalidRow;
983 if (new_lb > new_ub) {
984 if (lower_bound_sources_[new_representative] == new_representative) {
990 row_deletion_helper_.
UnmarkRow(new_representative);
997 return !row_deletion_helper_.
IsEmpty();
1010 for (RowIndex
row(0);
row < num_rows; ++
row) {
1011 const RowIndex lower_source = lower_bound_sources_[
row];
1012 const RowIndex upper_source = upper_bound_sources_[
row];
1027 const Fractional corrected_dual_value = lp_is_maximization_problem_
1030 if (corrected_dual_value != 0.0) {
1041 const Fractional factor = row_factors_[
row] / row_factors_[lower_source];
1051 const Fractional factor = row_factors_[
row] / row_factors_[upper_source];
1078 for (ColIndex
col(0);
col < num_cols; ++
col) {
1086 SubtractColumnMultipleFromConstraintBound(
col, fixed_value, lp);
1093 return !column_deletion_helper_.
IsEmpty();
1117 for (ColIndex
col(0);
col < num_cols; ++
col) {
1121 const RowIndex
row = e.row();
1124 implied_lower_bounds[
row] += lower * coeff;
1125 implied_upper_bounds[
row] += upper * coeff;
1127 implied_lower_bounds[
row] += upper * coeff;
1128 implied_upper_bounds[
row] += lower * coeff;
1136 int num_implied_free_constraints = 0;
1137 int num_forcing_constraints = 0;
1138 is_forcing_up_.
assign(num_rows,
false);
1140 for (RowIndex
row(0);
row < num_rows; ++
row) {
1141 if (row_degree[
row] == 0)
continue;
1147 implied_upper_bounds[
row]) ||
1150 VLOG(1) <<
"implied bound " << implied_lower_bounds[
row] <<
" "
1151 << implied_upper_bounds[
row];
1152 VLOG(1) <<
"constraint bound " << lower <<
" " << upper;
1161 is_forcing_down[
row] =
true;
1162 ++num_forcing_constraints;
1166 implied_lower_bounds[
row])) {
1167 is_forcing_up_[
row] =
true;
1168 ++num_forcing_constraints;
1179 implied_lower_bounds[
row]) &&
1183 ++num_implied_free_constraints;
1187 if (num_implied_free_constraints > 0) {
1188 VLOG(1) << num_implied_free_constraints <<
" implied free constraints.";
1191 if (num_forcing_constraints > 0) {
1192 VLOG(1) << num_forcing_constraints <<
" forcing constraints.";
1194 costs_.
resize(num_cols, 0.0);
1195 for (ColIndex
col(0);
col < num_cols; ++
col) {
1199 bool is_forced =
false;
1202 if (is_forcing_down[e.row()]) {
1203 const Fractional candidate = e.coefficient() < 0.0 ? lower : upper;
1212 target_bound = std::abs(lower) < std::abs(upper) ? lower : upper;
1215 VLOG(1) <<
"A variable is forced in both directions! bounds: ["
1216 << std::fixed << std::setprecision(10) << lower <<
", "
1217 << upper <<
"]. coeff:" << e.coefficient();
1224 if (is_forcing_up_[e.row()]) {
1225 const Fractional candidate = e.coefficient() < 0.0 ? upper : lower;
1230 target_bound = std::abs(lower) < std::abs(upper) ? lower : upper;
1233 VLOG(1) <<
"A variable is forced in both directions! bounds: ["
1234 << std::fixed << std::setprecision(10) << lower <<
", "
1235 << upper <<
"]. coeff:" << e.coefficient();
1254 for (RowIndex
row(0);
row < num_rows; ++
row) {
1261 if (is_forcing_down[
row] || is_forcing_up_[
row]) {
1269 return !column_deletion_helper_.
IsEmpty();
1279 struct DeletionEntry {
1284 std::vector<DeletionEntry> entries;
1288 for (ColIndex
col(0);
col < size; ++
col) {
1294 const RowIndex
row = e.row();
1297 last_coefficient = e.coefficient();
1301 entries.push_back({last_row,
col, last_coefficient});
1306 std::sort(entries.begin(), entries.end(),
1307 [](
const DeletionEntry&
a,
const DeletionEntry&
b) {
1308 if (a.row == b.row) return a.col < b.col;
1309 return a.row < b.row;
1322 for (
int i = 0; i < entries.size();) {
1323 const RowIndex
row = entries[i].row;
1329 for (; i < entries.size(); ++i) {
1330 if (entries[i].
row !=
row)
break;
1331 const ColIndex
col = entries[i].col;
1335 const Fractional reduced_cost = costs_[
col] - scalar_product;
1337 if (is_forcing_up_[
row] == !lp_is_maximization_problem_) {
1338 if (
bound < new_dual_value) {
1339 new_dual_value =
bound;
1340 new_basic_column =
col;
1343 if (
bound > new_dual_value) {
1344 new_dual_value =
bound;
1345 new_basic_column =
col;
1364struct ColWithDegree {
1367 ColWithDegree(ColIndex c, EntryIndex n) :
col(c),
num_entries(n) {}
1368 bool operator<(
const ColWithDegree& other)
const {
1370 return col < other.col;
1387 const int size = num_rows.value();
1396 for (ColIndex
col(0);
col < num_cols; ++
col) {
1402 if (e.coefficient() < 0.0)
std::swap(entry_lb, entry_ub);
1403 lb_sums[e.row()].Add(entry_lb);
1404 ub_sums[e.row()].Add(entry_ub);
1414 for (RowIndex
row(0);
row < num_rows; ++
row) {
1423 variable_offsets_.
assign(num_cols, 0.0);
1440 std::vector<ColWithDegree> col_by_degree;
1441 for (ColIndex
col(0);
col < num_cols; ++
col) {
1442 col_by_degree.push_back(
1445 std::sort(col_by_degree.begin(), col_by_degree.end());
1448 int num_already_free_variables = 0;
1449 int num_implied_free_variables = 0;
1450 int num_fixed_variables = 0;
1451 for (ColWithDegree col_with_degree : col_by_degree) {
1452 const ColIndex
col = col_with_degree.col;
1458 ++num_already_free_variables;
1469 if (used_rows[e.row()])
continue;
1475 if (coeff < 0.0)
std::swap(entry_lb, entry_ub);
1486 coeff > 0.0 ? -ub_sums[e.row()].SumWithoutUb(entry_ub) / coeff
1487 : -lb_sums[e.row()].SumWithoutLb(entry_lb) / coeff;
1489 coeff > 0.0 ? -lb_sums[e.row()].SumWithoutLb(entry_lb) / coeff
1490 : -ub_sums[e.row()].SumWithoutUb(entry_ub) / coeff;
1492 overall_implied_lb =
std::max(overall_implied_lb, implied_lb);
1493 overall_implied_ub =
std::min(overall_implied_ub, implied_ub);
1500 overall_implied_ub)) {
1508 overall_implied_lb) ||
1514 ++num_fixed_variables;
1517 overall_implied_lb)) {
1523 ++num_fixed_variables;
1530 overall_implied_lb) &&
1533 ++num_implied_free_variables;
1536 used_rows[e.row()] =
true;
1560 if (offset != 0.0) {
1561 variable_offsets_[
col] = offset;
1562 SubtractColumnMultipleFromConstraintBound(
col, offset, lp);
1564 postsolve_status_of_free_variables_[
col] =
1568 VLOG(1) << num_already_free_variables <<
" free variables in the problem.";
1569 VLOG(1) << num_implied_free_variables <<
" implied free columns.";
1570 VLOG(1) << num_fixed_variables <<
" variables can be fixed.";
1572 return num_implied_free_variables > 0;
1579 for (ColIndex
col(0);
col < num_cols; ++
col) {
1587 postsolve_status_of_free_variables_[
col];
1609 for (ColIndex doubleton_col(0); doubleton_col < num_cols; ++doubleton_col) {
1618 r.col = doubleton_col;
1622 if (row_deletion_helper_.
IsRowMarked(e.row()))
break;
1623 r.row[
index] = e.row();
1624 r.coeff[
index] = e.coefficient();
1628 if (
index != NUM_ROWS)
continue;
1640 if (std::abs(r.coeff[DELETED]) < std::abs(r.coeff[MODIFIED])) {
1641 std::swap(r.coeff[DELETED], r.coeff[MODIFIED]);
1642 std::swap(r.row[DELETED], r.row[MODIFIED]);
1649 r.deleted_row_as_column.Swap(
1658 new_variable_lb /= r.coeff[DELETED];
1659 new_variable_ub /= r.coeff[DELETED];
1660 if (r.coeff[DELETED] < 0.0)
std::swap(new_variable_lb, new_variable_ub);
1666 r.deleted_row_as_column.AddMultipleToSparseVectorAndIgnoreCommonIndex(
1667 -r.coeff[MODIFIED] / r.coeff[DELETED],
ColToRowIndex(r.col),
1673 if (r.objective_coefficient != 0.0) {
1676 if (
col == r.col)
continue;
1679 e.coefficient() * r.objective_coefficient / r.coeff[DELETED];
1693 restore_stack_.push_back(r);
1696 if (!row_deletion_helper_.
IsEmpty()) {
1709 for (
const RestoreInfo& r :
Reverse(restore_stack_)) {
1741 if (
col == r.col)
continue;
1742 new_variable_value -= (e.coefficient() / r.coeff[DELETED]) *
1755 r.objective_coefficient -
1756 r.coeff[MODIFIED] * solution->
dual_values[r.row[MODIFIED]];
1759 current_reduced_cost / r.coeff[DELETED];
1794 const RowIndex
row = e.row();
1801 const bool is_constraint_upper_bound_relevant =
1802 e.coefficient() > 0.0 ? !is_unbounded_up : is_unbounded_up;
1803 activity_sign_correction_[
row] =
1804 is_constraint_upper_bound_relevant ? 1.0 : -1.0;
1805 rhs_[
row] = is_constraint_upper_bound_relevant
1813 is_unbounded_[
col] =
true;
1814 Fractional initial_feasible_value = MinInMagnitudeOrZeroIfInfinite(
1817 col, initial_feasible_value,
1818 ComputeVariableStatus(initial_feasible_value,
1841 for (RowIndex
row(0);
row < num_rows; ++
row) {
1843 dual_ub_[
row] = 0.0;
1846 dual_lb_[
row] = 0.0;
1851 may_have_participated_lb_.
assign(num_cols,
false);
1852 may_have_participated_ub_.
assign(num_cols,
false);
1855 std::deque<ColIndex> columns_to_process;
1857 std::vector<RowIndex> changed_rows;
1858 for (ColIndex
col(0);
col < num_cols; ++
col) {
1859 columns_to_process.push_back(
col);
1865 const int limit = 5 * num_cols.value();
1866 for (
int count = 0; !columns_to_process.empty() && count < limit; ++count) {
1867 const ColIndex
col = columns_to_process.front();
1868 columns_to_process.pop_front();
1869 in_columns_to_process[
col] =
false;
1881 rc_lb.
Add(col_cost);
1882 rc_ub.
Add(col_cost);
1884 if (row_deletion_helper_.
IsRowMarked(e.row()))
continue;
1887 rc_lb.
Add(-coeff * dual_ub_[e.row()]);
1888 rc_ub.
Add(-coeff * dual_lb_[e.row()]);
1890 rc_lb.
Add(-coeff * dual_lb_[e.row()]);
1891 rc_ub.
Add(-coeff * dual_ub_[e.row()]);
1899 bool can_be_removed =
false;
1901 bool rc_is_away_from_zero;
1902 if (rc_ub.
Sum() <= low_tolerance) {
1903 can_be_removed =
true;
1905 rc_is_away_from_zero = rc_ub.
Sum() <= -high_tolerance;
1906 can_be_removed = !may_have_participated_ub_[
col];
1908 if (rc_lb.
Sum() >= -low_tolerance) {
1912 can_be_removed =
true;
1914 rc_is_away_from_zero = rc_lb.
Sum() >= high_tolerance;
1915 can_be_removed = !may_have_participated_lb_[
col];
1919 if (can_be_removed) {
1931 if (rc_is_away_from_zero) {
1932 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, variable " <<
col
1934 <<
" and its reduced cost is in [" << rc_lb.
Sum() <<
", "
1935 << rc_ub.
Sum() <<
"]";
1947 if (col_cost != 0.0)
continue;
1952 if (IsConstraintBlockingVariable(*lp, e.coefficient(), e.row())) {
1977 changed_rows.clear();
1979 if (row_deletion_helper_.
IsRowMarked(e.row()))
continue;
1981 const RowIndex
row = e.row();
1986 if (candidate < dual_ub_[
row]) {
1987 dual_ub_[
row] = candidate;
1988 may_have_participated_lb_[
col] =
true;
1994 if (candidate > dual_lb_[
row]) {
1995 dual_lb_[
row] = candidate;
1996 may_have_participated_lb_[
col] =
true;
2005 if (candidate > dual_lb_[
row]) {
2006 dual_lb_[
row] = candidate;
2007 may_have_participated_ub_[
col] =
true;
2013 if (candidate < dual_ub_[
row]) {
2014 dual_ub_[
row] = candidate;
2015 may_have_participated_ub_[
col] =
true;
2022 if (!changed_rows.empty()) {
2024 for (
const RowIndex
row : changed_rows) {
2028 if (!in_columns_to_process[
col]) {
2029 columns_to_process.push_back(
col);
2030 in_columns_to_process[
col] =
true;
2042 for (ColIndex
col(0);
col < end; ++
col) {
2052 return !column_deletion_helper_.
IsEmpty() || !row_deletion_helper_.
IsEmpty();
2062 struct DeletionEntry {
2067 std::vector<DeletionEntry> entries;
2072 for (RowIndex
row(0);
row < num_rows; ++
row) {
2080 if (is_unbounded_[
col]) {
2082 last_coefficient = e.coefficient();
2086 entries.push_back({
row, last_col, last_coefficient});
2091 std::sort(entries.begin(), entries.end(),
2092 [](
const DeletionEntry&
a,
const DeletionEntry&
b) {
2093 if (a.col == b.col) return a.row < b.row;
2094 return a.col < b.col;
2098 for (
int i = 0; i < entries.size();) {
2099 const ColIndex
col = entries[i].col;
2104 for (; i < entries.size(); ++i) {
2105 if (entries[i].
col !=
col)
break;
2106 const RowIndex
row = entries[i].row;
2125 if (activity * activity_sign_correction_[
row] < 0.0) {
2127 if (std::abs(
bound) > std::abs(primal_value_shift)) {
2128 primal_value_shift =
bound;
2137 activity_sign_correction_[row_at_bound] == 1.0
2152 for (RowIndex
row(0);
row < num_rows; ++
row) {
2160 return !row_deletion_helper_.
IsEmpty();
2182 for (ColIndex
col(0);
col < num_cols; ++
col) {
2189 for (RowIndex
row(0);
row < num_rows; ++
row) {
2190 if (degree[
row] == 0) {
2197 VLOG(1) <<
"Problem PRIMAL_INFEASIBLE, constraint " <<
row
2198 <<
" is empty and its range ["
2208 return !row_deletion_helper_.
IsEmpty();
2225 is_maximization_(lp.IsMaximizationProblem()),
2227 cost_(lp.objective_coefficients()[e.
col]),
2228 variable_lower_bound_(lp.variable_lower_bounds()[e.
col]),
2229 variable_upper_bound_(lp.variable_upper_bounds()[e.
col]),
2230 constraint_lower_bound_(lp.constraint_lower_bounds()[e.
row]),
2231 constraint_upper_bound_(lp.constraint_upper_bounds()[e.
row]),
2232 constraint_status_(status) {}
2240 SingletonRowUndo(saved_column, solution);
2243 ZeroCostSingletonColumnUndo(
parameters, saved_row, solution);
2246 SingletonColumnInEqualityUndo(
parameters, saved_row, solution);
2249 MakeConstraintAnEqualityUndo(solution);
2254void SingletonPreprocessor::DeleteSingletonRow(MatrixEntry e,
2260 if (e.coeff < 0.0) {
2261 std::swap(implied_lower_bound, implied_upper_bound);
2270 implied_lower_bound - potential_error > old_lower_bound
2271 ? implied_lower_bound
2274 implied_upper_bound + potential_error < old_upper_bound
2275 ? implied_upper_bound
2278 if (new_upper_bound < new_lower_bound) {
2281 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2282 "row causes the bound of the variable "
2283 << e.col <<
" to be infeasible by "
2284 << new_lower_bound - new_upper_bound;
2290 new_upper_bound = new_lower_bound;
2293 new_lower_bound = new_upper_bound;
2295 DCHECK_EQ(new_lower_bound, new_upper_bound);
2306void SingletonUndo::SingletonRowUndo(
const SparseColumn& saved_column,
2307 ProblemSolution* solution)
const {
2308 DCHECK_EQ(0, solution->dual_values[e_.row]);
2313 const VariableStatus status = solution->variable_statuses[e_.col];
2317 Fractional implied_lower_bound = constraint_lower_bound_ / e_.coeff;
2318 Fractional implied_upper_bound = constraint_upper_bound_ / e_.coeff;
2319 if (e_.coeff < 0.0) {
2320 std::swap(implied_lower_bound, implied_upper_bound);
2322 const bool lower_bound_changed = implied_lower_bound > variable_lower_bound_;
2323 const bool upper_bound_changed = implied_upper_bound < variable_upper_bound_;
2325 if (!lower_bound_changed && !upper_bound_changed)
return;
2333 const Fractional reduced_cost_for_minimization =
2334 is_maximization_ ? -reduced_cost : reduced_cost;
2337 DCHECK(lower_bound_changed || upper_bound_changed);
2338 if (reduced_cost_for_minimization >= 0.0 && !lower_bound_changed) {
2342 if (reduced_cost_for_minimization <= 0.0 && !upper_bound_changed) {
2353 solution->dual_values[e_.row] = reduced_cost / e_.coeff;
2356 (!lower_bound_changed || !upper_bound_changed)) {
2357 new_constraint_status = lower_bound_changed
2361 if (e_.coeff < 0.0) {
2369 solution->constraint_statuses[e_.row] = new_constraint_status;
2372void SingletonPreprocessor::UpdateConstraintBoundsWithVariableBounds(
2373 MatrixEntry e, LinearProgram* lp) {
2374 Fractional lower_delta = -e.coeff * lp->variable_upper_bounds()[e.col];
2375 Fractional upper_delta = -e.coeff * lp->variable_lower_bounds()[e.col];
2376 if (e.coeff < 0.0) {
2379 lp->SetConstraintBounds(e.row,
2380 lp->constraint_lower_bounds()[e.row] + lower_delta,
2381 lp->constraint_upper_bounds()[e.row] + upper_delta);
2384bool SingletonPreprocessor::IntegerSingletonColumnIsRemovable(
2385 const MatrixEntry& matrix_entry,
const LinearProgram& lp)
const {
2387 DCHECK(lp.IsVariableInteger(matrix_entry.col));
2388 const SparseMatrix& transpose = lp.GetTransposeSparseMatrix();
2405 lp.constraint_lower_bounds()[matrix_entry.row];
2407 const Fractional lower_bound_ratio = constraint_lb / matrix_entry.coeff;
2414 lp.constraint_upper_bounds()[matrix_entry.row];
2416 const Fractional upper_bound_ratio = constraint_ub / matrix_entry.coeff;
2425void SingletonPreprocessor::DeleteZeroCostSingletonColumn(
2426 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2430 const SparseColumn& row_as_col = transpose.column(transpose_col);
2432 UpdateConstraintBoundsWithVariableBounds(e, lp);
2437void SingletonUndo::ZeroCostSingletonColumnUndo(
2438 const GlopParameters&
parameters,
const SparseColumn& saved_row,
2439 ProblemSolution* solution)
const {
2442 if (variable_upper_bound_ == variable_lower_bound_) {
2443 solution->primal_values[e_.col] = variable_lower_bound_;
2455 solution->primal_values[e_.col] = variable_lower_bound_;
2459 solution->primal_values[e_.col] = variable_upper_bound_;
2462 if (constraint_upper_bound_ == constraint_lower_bound_) {
2477 const auto is_smaller_with_tolerance = [tolerance](
Fractional a,
2481 if (variable_lower_bound_ != -
kInfinity) {
2483 activity + e_.coeff * variable_lower_bound_;
2484 if (is_smaller_with_tolerance(constraint_lower_bound_, activity_at_lb) &&
2485 is_smaller_with_tolerance(activity_at_lb, constraint_upper_bound_)) {
2486 solution->primal_values[e_.col] = variable_lower_bound_;
2491 if (variable_upper_bound_ !=
kInfinity) {
2493 activity + e_.coeff * variable_upper_bound_;
2494 if (is_smaller_with_tolerance(constraint_lower_bound_, activity_at_ub) &&
2495 is_smaller_with_tolerance(activity_at_ub, constraint_upper_bound_)) {
2496 solution->primal_values[e_.col] = variable_upper_bound_;
2505 if (constraint_lower_bound_ == -
kInfinity &&
2507 solution->primal_values[e_.col] = 0.0;
2515 if (constraint_lower_bound_ == constraint_upper_bound_) {
2516 solution->primal_values[e_.col] =
2517 (constraint_lower_bound_ - activity) / e_.coeff;
2522 bool set_constraint_to_lower_bound;
2523 if (constraint_lower_bound_ == -
kInfinity) {
2524 set_constraint_to_lower_bound =
false;
2525 }
else if (constraint_upper_bound_ ==
kInfinity) {
2526 set_constraint_to_lower_bound =
true;
2530 const Fractional to_lb = (constraint_lower_bound_ - activity) / e_.coeff;
2531 const Fractional to_ub = (constraint_upper_bound_ - activity) / e_.coeff;
2532 set_constraint_to_lower_bound =
2533 std::max(variable_lower_bound_ - to_lb, to_lb - variable_upper_bound_) <
2534 std::max(variable_lower_bound_ - to_ub, to_ub - variable_upper_bound_);
2537 if (set_constraint_to_lower_bound) {
2538 solution->primal_values[e_.col] =
2539 (constraint_lower_bound_ - activity) / e_.coeff;
2542 solution->primal_values[e_.col] =
2543 (constraint_upper_bound_ - activity) / e_.coeff;
2548void SingletonPreprocessor::DeleteSingletonColumnInEquality(
2549 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2552 const SparseColumn& row_as_column = transpose.column(transpose_col);
2553 undo_stack_.push_back(
2563 const Fractional rhs = lp->constraint_upper_bounds()[e.row];
2566 lp->SetObjectiveOffset(lp->objective_offset() + rhs * multiplier);
2571 lp->objective_coefficients()[
col] - e.coefficient() * multiplier;
2581 lp->SetObjectiveCoefficient(
col, new_cost);
2586 UpdateConstraintBoundsWithVariableBounds(e, lp);
2590void SingletonUndo::SingletonColumnInEqualityUndo(
2591 const GlopParameters&
parameters,
const SparseColumn& saved_row,
2592 ProblemSolution* solution)
const {
2594 ZeroCostSingletonColumnUndo(
parameters, saved_row, solution);
2598 solution->dual_values[e_.row] += cost_ / e_.coeff;
2605void SingletonUndo::MakeConstraintAnEqualityUndo(
2606 ProblemSolution* solution)
const {
2608 solution->constraint_statuses[e_.row] = constraint_status_;
2612bool SingletonPreprocessor::MakeConstraintAnEqualityIfPossible(
2613 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2616 const Fractional cst_lower_bound = lp->constraint_lower_bounds()[e.row];
2617 const Fractional cst_upper_bound = lp->constraint_upper_bounds()[e.row];
2618 if (cst_lower_bound == cst_upper_bound)
return true;
2624 const DenseRow& variable_ubs = lp->variable_upper_bounds();
2625 const DenseRow& variable_lbs = lp->variable_lower_bounds();
2626 if (e.row >= row_sum_is_cached_.size() || !row_sum_is_cached_[e.row]) {
2627 if (e.row >= row_sum_is_cached_.size()) {
2628 const int new_size = e.row.value() + 1;
2629 row_sum_is_cached_.resize(new_size);
2630 row_lb_sum_.resize(new_size);
2631 row_ub_sum_.resize(new_size);
2633 row_sum_is_cached_[e.row] =
true;
2634 row_lb_sum_[e.row].Add(cst_lower_bound);
2635 row_ub_sum_[e.row].Add(cst_upper_bound);
2646 if (column_deletion_helper_.
IsColumnMarked(row_as_col))
continue;
2647 if (entry.coefficient() > 0.0) {
2648 row_lb_sum_[e.row].Add(-entry.coefficient() * variable_ubs[row_as_col]);
2649 row_ub_sum_[e.row].Add(-entry.coefficient() * variable_lbs[row_as_col]);
2651 row_lb_sum_[e.row].Add(-entry.coefficient() * variable_lbs[row_as_col]);
2652 row_ub_sum_[e.row].Add(-entry.coefficient() * variable_ubs[row_as_col]);
2664 c > 0.0 ? row_lb_sum_[e.row].SumWithoutLb(-c * variable_ubs[e.col]) / c
2665 : row_ub_sum_[e.row].SumWithoutUb(-c * variable_ubs[e.col]) / c;
2667 c > 0.0 ? row_ub_sum_[e.row].SumWithoutUb(-c * variable_lbs[e.col]) / c
2668 : row_lb_sum_[e.row].SumWithoutLb(-c * variable_lbs[e.col]) / c;
2674 lp->GetObjectiveCoefficientForMinimizationVersion(e.col);
2681 ub, lp->variable_upper_bounds()[e.col])) {
2687 lp->SetConstraintBounds(e.row, cst_upper_bound, cst_upper_bound);
2694 lp->SetConstraintBounds(e.row, cst_lower_bound, cst_lower_bound);
2700 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2702 << e.col <<
" has a cost (for minimization) of " <<
cost
2703 <<
" and is unbounded towards kInfinity.";
2720 lp->SetVariableBounds(e.col, lp->variable_lower_bounds()[e.col],
kInfinity);
2723 lp->variable_lower_bounds()[e.col], lb)) {
2729 lp->SetConstraintBounds(e.row, cst_lower_bound, cst_lower_bound);
2736 lp->SetConstraintBounds(e.row, cst_upper_bound, cst_upper_bound);
2742 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2744 << e.col <<
" has a cost (for minimization) of " <<
cost
2745 <<
" and is unbounded towards -kInfinity.";
2750 lp->SetVariableBounds(e.col, -
kInfinity,
2751 lp->variable_upper_bounds()[e.col]);
2754 if (lp->constraint_lower_bounds()[e.row] ==
2755 lp->constraint_upper_bounds()[e.row]) {
2756 undo_stack_.push_back(SingletonUndo(
2770 ColIndex num_cols(matrix.
num_cols());
2771 RowIndex num_rows(matrix.
num_rows());
2773 std::vector<ColIndex> column_to_process;
2774 for (ColIndex
col(0);
col < num_cols; ++
col) {
2776 if (column_degree[
col] == 1) {
2777 column_to_process.push_back(
col);
2783 std::vector<RowIndex> row_to_process;
2784 for (RowIndex
row(0);
row < num_rows; ++
row) {
2786 if (row_degree[
row] == 1) {
2787 row_to_process.push_back(
row);
2793 (!column_to_process.empty() || !row_to_process.empty())) {
2795 const ColIndex
col = column_to_process.back();
2796 column_to_process.pop_back();
2797 if (column_degree[
col] <= 0)
continue;
2798 const MatrixEntry e = GetSingletonColumnMatrixEntry(
col, matrix);
2800 !IntegerSingletonColumnIsRemovable(e, *lp)) {
2807 DeleteZeroCostSingletonColumn(transpose, e, lp);
2808 }
else if (MakeConstraintAnEqualityIfPossible(transpose, e, lp)) {
2809 DeleteSingletonColumnInEquality(transpose, e, lp);
2813 --row_degree[e.row];
2814 if (row_degree[e.row] == 1) {
2819 const RowIndex
row = row_to_process.back();
2820 row_to_process.pop_back();
2821 if (row_degree[
row] <= 0)
continue;
2822 const MatrixEntry e = GetSingletonRowMatrixEntry(
row, transpose);
2824 DeleteSingletonRow(e, lp);
2825 --column_degree[e.col];
2826 if (column_degree[e.col] == 1) {
2835 return !column_deletion_helper_.
IsEmpty() || !row_deletion_helper_.
IsEmpty();
2853 for (
int i = undo_stack_.size() - 1; i >= 0; --i) {
2858 undo_stack_[i].Undo(
parameters_, saved_col, saved_row, solution);
2862MatrixEntry SingletonPreprocessor::GetSingletonColumnMatrixEntry(
2867 return MatrixEntry(e.row(),
col, e.coefficient());
2871 LOG(DFATAL) <<
"No unmarked entry in a column that is supposed to have one.";
2873 return MatrixEntry(RowIndex(0), ColIndex(0), 0.0);
2876MatrixEntry SingletonPreprocessor::GetSingletonRowMatrixEntry(
2877 RowIndex
row,
const SparseMatrix& transpose) {
2882 return MatrixEntry(
row,
col, e.coefficient());
2886 LOG(DFATAL) <<
"No unmarked entry in a row that is supposed to have one.";
2888 return MatrixEntry(RowIndex(0), ColIndex(0), 0.0);
2899 if (num_cols == 0)
return false;
2905 Fractional num_non_zero_objective_coefficients = 0.0;
2906 for (ColIndex
col(0);
col < num_cols; ++
col) {
2908 row_degree[e.row()] += 1.0;
2911 num_non_zero_objective_coefficients += 1.0;
2923 const EntryIndex initial_num_entries = lp->
num_entries();
2924 int num_zeroed_objective_coefficients = 0;
2925 for (ColIndex
col(0);
col < num_cols; ++
col) {
2934 if (max_magnitude ==
kInfinity || max_magnitude == 0)
continue;
2935 const Fractional threshold = allowed_impact / max_magnitude;
2937 threshold, row_degree);
2940 num_non_zero_objective_coefficients *
2944 ++num_zeroed_objective_coefficients;
2951 <<
" near-zero entries.";
2953 if (num_zeroed_objective_coefficients > 0) {
2954 VLOG(1) <<
"Removed " << num_zeroed_objective_coefficients
2955 <<
" near-zero objective coefficients.";
2973 if (num_cols == 0)
return false;
2975 changed_columns_.clear();
2976 int num_singletons = 0;
2977 for (ColIndex
col(0);
col < num_cols; ++
col) {
2989 changed_columns_.push_back(
col);
2992 VLOG(1) <<
"Changed the sign of " << changed_columns_.size() <<
" columns.";
2993 VLOG(1) << num_singletons <<
" singleton columns left.";
2994 return !changed_columns_.empty();
3001 for (
int i = 0; i < changed_columns_.size(); ++i) {
3002 const ColIndex
col = changed_columns_[i];
3039 std::vector<std::pair<int64_t, RowIndex>> sorted_rows;
3041 for (RowIndex
row(0);
row < num_rows; ++
row) {
3054 sorted_rows.push_back({score,
row});
3056 std::sort(sorted_rows.begin(), sorted_rows.end());
3062 for (
const auto p : sorted_rows) {
3063 const RowIndex
row = p.second;
3073 int entry_index = 0;
3077 r.col[entry_index] =
col;
3078 r.coeff[entry_index] = e.coefficient();
3086 if (entry_index < 2)
continue;
3092 for (
int col_choice = 0; col_choice < NUM_DOUBLETON_COLS; ++col_choice) {
3093 const ColIndex
col = r.col[col_choice];
3101 if (r.lb[DELETED] == r.ub[DELETED] || r.lb[MODIFIED] == r.ub[MODIFIED]) {
3118 const Fractional carry_over_offset = r.rhs / r.coeff[MODIFIED];
3120 -r.coeff[DELETED] / r.coeff[MODIFIED];
3122 carry_over_factor == 0.0) {
3130 r.lb[DELETED] * carry_over_factor + carry_over_offset;
3132 r.ub[DELETED] * carry_over_factor + carry_over_offset;
3133 if (carry_over_factor < 0) {
3134 std::swap(carried_over_lb, carried_over_ub);
3136 if (carried_over_lb <= lb) {
3141 lb = carried_over_lb;
3146 carry_over_factor > 0 ? r.lb[DELETED] : r.ub[DELETED]);
3148 if (carried_over_ub >= ub) {
3153 ub = carried_over_ub;
3158 carry_over_factor > 0 ? r.ub[DELETED] : r.lb[DELETED]);
3167 restore_stack_.push_back(r);
3176 -r.coeff[MODIFIED] / r.coeff[DELETED];
3177 const Fractional constant_offset_factor = r.rhs / r.coeff[DELETED];
3179 if (!
IsFinite(substitution_factor) || substitution_factor == 0.0 ||
3180 !
IsFinite(constant_offset_factor)) {
3188 for (
const int col_choice : {DELETED, MODIFIED}) {
3189 const ColIndex
col = r.col[col_choice];
3203 r.objective_coefficient[MODIFIED] +
3204 substitution_factor * r.objective_coefficient[DELETED];
3215 SubtractColumnMultipleFromConstraintBound(r.col[DELETED],
3216 constant_offset_factor, lp);
3232 return !column_deletion_helper_.
IsEmpty();
3245 for (
const RestoreInfo& r :
Reverse(restore_stack_)) {
3248 LOG(DFATAL) <<
"FIXED variable produced by DoubletonPreprocessor!";
3255 ABSL_FALLTHROUGH_INTENDED;
3260 new_basic_columns[r.col[DELETED]] =
true;
3263 ABSL_FALLTHROUGH_INTENDED;
3271 ? r.bound_backtracking_at_lower_bound
3272 : r.bound_backtracking_at_upper_bound;
3273 const ColIndex bounded_var = r.col[bound_backtracking.
col_choice];
3274 const ColIndex basic_var =
3275 r.col[OtherColChoice(bound_backtracking.
col_choice)];
3279 new_basic_columns[basic_var] =
true;
3290 solution->
primal_values[r.col[MODIFIED]] * r.coeff[MODIFIED]) /
3319 for (
int i = 0; i < restore_stack_.size(); ++i) {
3320 const RestoreInfo& r = restore_stack_[i];
3321 col_to_index[r.col[MODIFIED]].
insert(i);
3322 col_to_index[r.col[DELETED]].
insert(i);
3324 std::vector<ColIndex> singleton_col;
3325 for (ColIndex
col(0);
col < num_cols; ++
col) {
3326 if (!new_basic_columns[
col])
continue;
3327 if (col_to_index[
col].size() == 1) singleton_col.push_back(
col);
3329 while (!singleton_col.empty()) {
3330 const ColIndex
col = singleton_col.back();
3331 singleton_col.pop_back();
3332 if (!new_basic_columns[
col])
continue;
3333 if (col_to_index[
col].empty())
continue;
3336 const RestoreInfo& r = restore_stack_[
index];
3338 const ColChoice col_choice = r.col[MODIFIED] ==
col ? MODIFIED : DELETED;
3346 saved_objective_[r.col[col_choice]] -
3348 solution->
dual_values[r.row] = current_reduced_cost / r.coeff[col_choice];
3353 if (col_to_index[r.col[DELETED]].
size() == 1) {
3354 singleton_col.push_back(r.col[DELETED]);
3356 if (col_to_index[r.col[MODIFIED]].
size() == 1) {
3357 singleton_col.push_back(r.col[MODIFIED]);
3363 saved_row_upper_bounds_, solution);
3372 for (RowIndex
row(0);
row < num_rows; ++
row) {
3376 if (row_lower_bounds[
row] == row_upper_bounds[
row])
continue;
3388void DoubletonEqualityRowPreprocessor::
3389 SwapDeletedAndModifiedVariableRestoreInfo(RestoreInfo* r) {
3391 swap(r->col[DELETED], r->col[MODIFIED]);
3392 swap(r->coeff[DELETED], r->coeff[MODIFIED]);
3393 swap(r->lb[DELETED], r->lb[MODIFIED]);
3394 swap(r->ub[DELETED], r->ub[MODIFIED]);
3395 swap(r->objective_coefficient[DELETED], r->objective_coefficient[MODIFIED]);
3433 if (1.0 * primal_num_rows_.value() <
3444 variable_lower_bounds_.
assign(num_cols, 0.0);
3445 variable_upper_bounds_.
assign(num_cols, 0.0);
3446 for (ColIndex
col(0);
col < num_cols; ++
col) {
3451 variable_lower_bounds_[
col] = lower;
3452 variable_upper_bounds_[
col] = upper;
3453 const Fractional value = MinInMagnitudeOrZeroIfInfinite(lower, upper);
3456 SubtractColumnMultipleFromConstraintBound(
col,
value, lp);
3464 dual_status_correspondence_.
clear();
3465 for (RowIndex
row(0);
row < primal_num_rows_; ++
row) {
3475 LOG(DFATAL) <<
"There should be no free constraint in this lp.";
3478 slack_or_surplus_mapping_.
clear();
3479 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3489 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3522 DenseRow new_primal_values(primal_num_cols_, 0.0);
3526 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3533 const Fractional shift = MinInMagnitudeOrZeroIfInfinite(lower, upper);
3544 new_variable_statuses[
col] = ComputeVariableStatus(shift, lower, upper);
3553 const ColIndex end = dual_status_correspondence_.
size();
3558 const ColIndex
col = slack_or_surplus_mapping_[
index - begin];
3566 new_primal_values[
col] = variable_upper_bounds_[
col];
3569 new_primal_values[
col] = variable_lower_bounds_[
col];
3577 DenseColumn new_dual_values(primal_num_rows_, 0.0);
3584 Fractional sign = primal_is_maximization_problem_ ? -1 : 1;
3585 for (RowIndex
row(0);
row < primal_num_rows_; ++
row) {
3603 new_constraint_statuses[
row] =
3610 new_dual_values[
row] +=
3656 bool all_variable_domains_contain_zero =
true;
3658 variable_initial_lbs_.
assign(num_cols, 0.0);
3659 variable_initial_ubs_.
assign(num_cols, 0.0);
3660 for (ColIndex
col(0);
col < num_cols; ++
col) {
3663 if (0.0 < variable_initial_lbs_[
col] || 0.0 > variable_initial_ubs_[
col]) {
3664 all_variable_domains_contain_zero =
false;
3667 VLOG(1) <<
"Maximum variable bounds magnitude (before shift): "
3668 << ComputeMaxVariableBoundsMagnitude(*lp);
3671 if (all_variable_domains_contain_zero)
return false;
3675 int num_bound_shifts = 0;
3679 offsets_.
assign(num_cols, 0.0);
3680 for (ColIndex
col(0);
col < num_cols; ++
col) {
3681 if (0.0 < variable_initial_lbs_[
col] || 0.0 > variable_initial_ubs_[
col]) {
3682 Fractional offset = MinInMagnitudeOrZeroIfInfinite(
3683 variable_initial_lbs_[
col], variable_initial_ubs_[
col]);
3691 offset = trunc(offset);
3695 offsets_[
col] = offset;
3697 variable_initial_ubs_[
col] - offset);
3700 row_offsets[e.row()].Add(e.coefficient() * offset);
3706 VLOG(1) <<
"Maximum variable bounds magnitude (after " << num_bound_shifts
3707 <<
" shifts): " << ComputeMaxVariableBoundsMagnitude(*lp);
3710 for (RowIndex
row(0);
row < num_rows; ++
row) {
3724 for (ColIndex
col(0);
col < num_cols; ++
col) {
3730 ABSL_FALLTHROUGH_INTENDED;
3758 variable_lower_bounds_.
assign(num_cols, 0.0);
3759 variable_upper_bounds_.
assign(num_cols, 0.0);
3760 for (ColIndex
col(0);
col < num_cols; ++
col) {
3792 for (ColIndex
col(0);
col < num_cols; ++
col) {
3795 ABSL_FALLTHROUGH_INTENDED;
3803 ABSL_FALLTHROUGH_INTENDED;
3854 for (RowIndex
row(0);
row < num_rows; ++
row) {
3861 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 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
const DenseRow & GetStoredValue() const
void RestoreDeletedColumns(ProblemSolution *solution) 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
::operations_research::glop::GlopParameters_SolverBehavior solve_dual_problem() const
bool log_search_progress() const
double drop_tolerance() const
double preprocessor_zero_tolerance() const
static constexpr SolverBehavior LET_SOLVER_DECIDE
::operations_research::glop::GlopParameters_ScalingAlgorithm scaling_method() const
static constexpr SolverBehavior NEVER_DO
double solution_feasibility_tolerance() const
double dualizer_threshold() const
bool use_preprocessing() const
::operations_research::glop::GlopParameters_CostScalingAlgorithm cost_scaling() const
bool Run(LinearProgram *lp) final
void RecoverSolution(ProblemSolution *solution) const final
SparseMatrix * GetMutableTransposeSparseMatrix()
void SetObjectiveScalingFactor(Fractional objective_scaling_factor)
void SetVariableBounds(ColIndex col, Fractional lower_bound, Fractional upper_bound)
DenseColumn * mutable_constraint_upper_bounds()
const SparseMatrix & GetTransposeSparseMatrix() const
void SetObjectiveOffset(Fractional objective_offset)
ColIndex GetFirstSlackVariable() const
Fractional ScaleObjective(GlopParameters::CostScalingAlgorithm method)
const DenseColumn & constraint_lower_bounds() const
Fractional GetObjectiveCoefficientForMinimizationVersion(ColIndex col) const
void SetConstraintBounds(RowIndex row, Fractional lower_bound, Fractional upper_bound)
void Swap(LinearProgram *linear_program)
const DenseRow & variable_upper_bounds() const
DenseColumn * mutable_constraint_lower_bounds()
Fractional objective_offset() const
SparseColumn * GetMutableSparseColumn(ColIndex col)
const DenseColumn & constraint_upper_bounds() const
const DenseRow & objective_coefficients() const
void UseTransposeMatrixAsReference()
void AddSlackVariablesWhereNecessary(bool detect_integer_constraints)
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
ColIndex num_variables() const
void PopulateFromDual(const LinearProgram &dual, RowToColMapping *duplicated_rows)
const SparseMatrix & GetSparseMatrix() const
const DenseRow & variable_lower_bounds() const
Fractional objective_scaling_factor() const
void SetMaximizationProblem(bool maximize)
const SparseColumn & GetSparseColumn(ColIndex col) const
EntryIndex num_entries() const
RowIndex num_constraints() const
void RecoverSolution(ProblemSolution *solution) const override
bool Run(LinearProgram *lp) final
void DestructiveRecoverSolution(ProblemSolution *solution)
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
ColIndex num_cols() const
SparseColumn * mutable_column(ColIndex col)
const SparseColumn & column(ColIndex col) const
RowIndex num_rows() 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
ModelSharedTimeLimit * time_limit
ReverseView< Container > reversed_view(const Container &c)
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)