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)
81 const int kMaxNumPasses = 20;
82 for (
int i = 0; i < kMaxNumPasses; ++i) {
83 const int old_stack_size = preprocessors_.size();
96 if (preprocessors_.size() == old_stack_size) {
98 SOLVER_LOG(logger_,
"Reached fixed point after presolve pass #", i);
112 const int old_stack_size = preprocessors_.size();
118 if (old_stack_size != preprocessors_.size()) {
132 return !preprocessors_.empty();
135#undef RUN_PREPROCESSOR
137void MainLpPreprocessor::RunAndPushIfRelevant(
138 std::unique_ptr<Preprocessor> preprocessor,
const std::string&
name,
144 const double start_time =
time_limit->GetElapsedTime();
154 if (preprocessor->Run(lp)) {
155 const EntryIndex new_num_entries = lp->
num_entries();
156 const double preprocess_time =
time_limit->GetElapsedTime() - start_time;
159 "%-45s: %d(%d) rows, %d(%d) columns, %d(%d) entries. (%fs)",
166 static_cast<int64_t
>(new_num_entries.value()),
167 static_cast<int64_t
>(new_num_entries.value() -
168 initial_num_entries_.value()),
170 status_ = preprocessor->status();
171 preprocessors_.push_back(std::move(preprocessor));
176 status_ = preprocessor->status();
187 p->RecoverSolution(solution);
193 while (!preprocessors_.empty()) {
194 preprocessors_.back()->RecoverSolution(solution);
195 preprocessors_.pop_back();
204 const int index = saved_columns_.size();
205 CHECK(saved_columns_index_.insert({col, index}).second);
206 saved_columns_.push_back(column);
211 const int index = saved_columns_.size();
212 const bool inserted = saved_columns_index_.insert({
col,
index}).second;
213 if (inserted) saved_columns_.push_back(column);
217 const auto it = saved_columns_index_.find(
col);
218 CHECK(it != saved_columns_index_.end());
219 return saved_columns_[it->second];
223 const auto it = saved_columns_index_.find(
col);
224 return it == saved_columns_index_.end() ? empty_column_
225 : saved_columns_[it->second];
229 is_column_deleted_.
clear();
230 stored_value_.
clear();
240 if (
col >= is_column_deleted_.
size()) {
241 is_column_deleted_.
resize(
col + 1,
false);
245 is_column_deleted_[
col] =
true;
246 stored_value_[
col] = fixed_value;
247 stored_status_[
col] = status;
254 ColIndex old_index(0);
255 for (ColIndex
col(0);
col < is_column_deleted_.
size(); ++
col) {
256 if (is_column_deleted_[
col]) {
269 for (; old_index < num_cols; ++old_index) {
285 if (
row >= is_row_deleted_.
size()) {
288 is_row_deleted_[
row] =
true;
292 if (
row >= is_row_deleted_.
size())
return;
293 is_row_deleted_[
row] =
false;
297 return is_row_deleted_;
303 RowIndex old_index(0);
304 const RowIndex end = is_row_deleted_.
size();
305 for (RowIndex
row(0);
row < end; ++
row) {
306 if (is_row_deleted_[
row]) {
320 for (; old_index < num_rows; ++old_index) {
371Fractional ComputeMaxVariableBoundsMagnitude(
const LinearProgram& lp) {
373 const ColIndex num_cols = lp.num_variables();
374 for (ColIndex
col(0);
col < num_cols; ++
col) {
376 max_bounds_magnitude,
377 std::max(MagnitudeOrZeroIfInfinite(lp.variable_lower_bounds()[
col]),
378 MagnitudeOrZeroIfInfinite(lp.variable_upper_bounds()[
col])));
380 return max_bounds_magnitude;
388 column_deletion_helper_.
Clear();
390 for (ColIndex
col(0);
col < num_cols; ++
col) {
397 if (objective_coefficient == 0) {
411 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, empty column " <<
col
412 <<
" has a minimization cost of " << objective_coefficient
426 return !column_deletion_helper_.
IsEmpty();
444void SubtractColumnMultipleFromConstraintBound(ColIndex
col,
450 const RowIndex
row = e.row();
464struct ColumnWithRepresentativeAndScaledCost {
465 ColumnWithRepresentativeAndScaledCost(ColIndex _col, ColIndex _representative,
472 bool operator<(
const ColumnWithRepresentativeAndScaledCost& other)
const {
475 return col < other.col;
496 int num_proportionality_classes = 0;
497 std::vector<ColIndex> proportional_columns;
503 ++num_proportionality_classes;
506 proportional_columns.push_back(
col);
509 if (proportional_columns.empty())
return false;
510 VLOG(1) <<
"The problem contains " << proportional_columns.size()
511 <<
" columns which belong to " << num_proportionality_classes
512 <<
" proportionality classes.";
516 column_factors_.
assign(num_cols, 0.0);
517 for (
const ColIndex
col : proportional_columns) {
531 for (
const ColIndex
col : proportional_columns) {
535 const bool is_rc_positive_or_zero =
537 const bool is_rc_negative_or_zero =
539 bool is_slope_upper_bounded = is_rc_positive_or_zero;
540 bool is_slope_lower_bounded = is_rc_negative_or_zero;
541 if (column_factors_[
col] < 0.0) {
542 std::swap(is_slope_lower_bounded, is_slope_upper_bounded);
546 column_factors_[
col];
547 if (is_slope_lower_bounded) {
551 if (is_slope_upper_bounded) {
558 for (
const ColIndex
col : proportional_columns) {
566 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, no feasible dual values"
567 <<
" can satisfy the constraints of the proportional columns"
569 <<
" the associated quantity must be in ["
579 for (
const ColIndex
col : proportional_columns) {
583 column_factors_[
col];
586 bool variable_can_be_fixed =
false;
594 variable_can_be_fixed =
true;
599 variable_can_be_fixed =
true;
603 if (variable_can_be_fixed) {
608 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED.";
621 std::vector<ColumnWithRepresentativeAndScaledCost> sorted_columns;
622 for (
const ColIndex
col : proportional_columns) {
627 sorted_columns.
push_back(ColumnWithRepresentativeAndScaledCost(
632 std::sort(sorted_columns.begin(), sorted_columns.end());
641 for (
int i = 0; i < sorted_columns.size();) {
642 const ColIndex target_col = sorted_columns[i].col;
643 const ColIndex target_representative = sorted_columns[i].representative;
644 const Fractional target_scaled_cost = sorted_columns[i].scaled_cost;
651 for (++i; i < sorted_columns.size(); ++i) {
652 if (sorted_columns[i].
representative != target_representative)
break;
653 if (std::abs(sorted_columns[i].
scaled_cost - target_scaled_cost) >=
658 const ColIndex
col = sorted_columns[i].col;
663 merged_columns_[
col] = target_col;
668 column_factors_[
col] / column_factors_[target_col];
682 if (bound_factor < 0.0) {
688 SubtractColumnMultipleFromConstraintBound(
col, target_value, lp);
697 if (num_merged > 0) {
698 merged_columns_[target_col] = target_col;
699 const Fractional target_value = MinInMagnitudeOrZeroIfInfinite(
700 lower_bounds_[target_col], upper_bounds_[target_col]);
704 SubtractColumnMultipleFromConstraintBound(target_col, target_value, lp);
711 return !column_deletion_helper_.
IsEmpty();
722 const ColIndex num_cols = merged_columns_.
size();
725 DenseRow distance_to_bound(num_cols, 0.0);
726 DenseRow wanted_value(num_cols, 0.0);
730 for (ColIndex
col(0);
col < num_cols; ++
col) {
731 if (merged_columns_[
col] ==
col) {
735 if (distance_to_upper_bound < distance_to_lower_bound) {
736 distance_to_bound[
col] = distance_to_upper_bound;
737 is_distance_to_upper_bound[
col] =
true;
739 distance_to_bound[
col] = distance_to_lower_bound;
740 is_distance_to_upper_bound[
col] =
false;
742 is_representative_basic[
col] =
749 lower_bounds_[
col], upper_bounds_[
col]);
756 for (ColIndex
col(0);
col < num_cols; ++
col) {
768 const bool to_upper_bound =
769 (bound_factor > 0.0) == is_distance_to_upper_bound[
representative];
770 if (width <= scaled_distance) {
772 to_upper_bound ? lower_bounds_[
col] : upper_bounds_[
col];
775 lower_bounds_[
col], upper_bounds_[
col]);
776 distance_to_bound[
representative] -= width * std::abs(bound_factor);
779 to_upper_bound ? upper_bounds_[
col] - scaled_distance
780 : lower_bounds_[
col] + scaled_distance;
803 const bool use_this_variable =
804 (error * bound_factor > 0.0) ? (upper_bounds_[
col] ==
kInfinity)
806 if (use_this_variable) {
839 row_factors_.
assign(num_rows, 0.0);
840 for (RowIndex
row(0);
row < num_rows; ++
row) {
842 if (!row_transpose.
IsEmpty()) {
863 int num_proportional_rows = 0;
864 for (RowIndex
row(0);
row < num_rows; ++
row) {
867 mapping[representative_row_as_col] = representative_row_as_col;
868 is_a_representative[
ColToRowIndex(representative_row_as_col)] =
true;
869 ++num_proportional_rows;
875 for (RowIndex
row(0);
row < num_rows; ++
row) {
881 const RowIndex representative_row =
ColToRowIndex(mapping[row_as_col]);
884 row_factors_[representative_row] / row_factors_[
row];
894 lower_bound_sources_[representative_row] =
row;
898 upper_bound_sources_[representative_row] =
row;
906 for (RowIndex
row(0);
row < num_rows; ++
row) {
907 if (!is_a_representative[
row])
continue;
908 const RowIndex lower_source = lower_bound_sources_[
row];
909 const RowIndex upper_source = upper_bound_sources_[
row];
914 if (lower_source == upper_source) {
918 row_deletion_helper_.
UnmarkRow(lower_source);
931 row_deletion_helper_.
UnmarkRow(lower_source);
936 row_deletion_helper_.
UnmarkRow(upper_source);
944 RowIndex new_representative = lower_source;
945 RowIndex other = upper_source;
946 if (std::abs(row_factors_[new_representative]) <
947 std::abs(row_factors_[other])) {
953 row_factors_[new_representative] / row_factors_[other];
960 lower_bound_sources_[new_representative] = new_representative;
961 upper_bound_sources_[new_representative] = new_representative;
964 lower_bound_sources_[new_representative] = other;
968 if (new_ub < lp->constraint_upper_bounds()[new_representative]) {
969 upper_bound_sources_[new_representative] = other;
973 const RowIndex new_lower_source =
974 lower_bound_sources_[new_representative];
975 if (new_lower_source == upper_bound_sources_[new_representative]) {
976 row_deletion_helper_.
UnmarkRow(new_lower_source);
977 lower_bound_sources_[new_representative] =
kInvalidRow;
978 upper_bound_sources_[new_representative] =
kInvalidRow;
987 if (new_lb > new_ub) {
988 if (lower_bound_sources_[new_representative] == new_representative) {
994 row_deletion_helper_.
UnmarkRow(new_representative);
1001 return !row_deletion_helper_.
IsEmpty();
1014 for (RowIndex
row(0);
row < num_rows; ++
row) {
1015 const RowIndex lower_source = lower_bound_sources_[
row];
1016 const RowIndex upper_source = upper_bound_sources_[
row];
1031 const Fractional corrected_dual_value = lp_is_maximization_problem_
1034 if (corrected_dual_value != 0.0) {
1045 const Fractional factor = row_factors_[
row] / row_factors_[lower_source];
1055 const Fractional factor = row_factors_[
row] / row_factors_[upper_source];
1082 for (ColIndex
col(0);
col < num_cols; ++
col) {
1090 SubtractColumnMultipleFromConstraintBound(
col, fixed_value, lp);
1097 return !column_deletion_helper_.
IsEmpty();
1121 for (ColIndex
col(0);
col < num_cols; ++
col) {
1125 const RowIndex
row = e.row();
1128 implied_lower_bounds[
row] += lower * coeff;
1129 implied_upper_bounds[
row] += upper * coeff;
1131 implied_lower_bounds[
row] += upper * coeff;
1132 implied_upper_bounds[
row] += lower * coeff;
1140 int num_implied_free_constraints = 0;
1141 int num_forcing_constraints = 0;
1142 is_forcing_up_.
assign(num_rows,
false);
1144 for (RowIndex
row(0);
row < num_rows; ++
row) {
1145 if (row_degree[
row] == 0)
continue;
1151 implied_upper_bounds[
row]) ||
1154 VLOG(1) <<
"implied bound " << implied_lower_bounds[
row] <<
" "
1155 << implied_upper_bounds[
row];
1156 VLOG(1) <<
"constraint bound " << lower <<
" " << upper;
1165 is_forcing_down[
row] =
true;
1166 ++num_forcing_constraints;
1170 implied_lower_bounds[
row])) {
1171 is_forcing_up_[
row] =
true;
1172 ++num_forcing_constraints;
1183 implied_lower_bounds[
row]) &&
1187 ++num_implied_free_constraints;
1191 if (num_implied_free_constraints > 0) {
1192 VLOG(1) << num_implied_free_constraints <<
" implied free constraints.";
1195 if (num_forcing_constraints > 0) {
1196 VLOG(1) << num_forcing_constraints <<
" forcing constraints.";
1198 costs_.
resize(num_cols, 0.0);
1199 for (ColIndex
col(0);
col < num_cols; ++
col) {
1203 bool is_forced =
false;
1206 if (is_forcing_down[e.row()]) {
1207 const Fractional candidate = e.coefficient() < 0.0 ? lower : upper;
1216 target_bound = std::abs(lower) < std::abs(upper) ? lower : upper;
1219 VLOG(1) <<
"A variable is forced in both directions! bounds: ["
1220 << std::fixed << std::setprecision(10) << lower <<
", "
1221 << upper <<
"]. coeff:" << e.coefficient();
1228 if (is_forcing_up_[e.row()]) {
1229 const Fractional candidate = e.coefficient() < 0.0 ? upper : lower;
1234 target_bound = std::abs(lower) < std::abs(upper) ? lower : upper;
1237 VLOG(1) <<
"A variable is forced in both directions! bounds: ["
1238 << std::fixed << std::setprecision(10) << lower <<
", "
1239 << upper <<
"]. coeff:" << e.coefficient();
1258 for (RowIndex
row(0);
row < num_rows; ++
row) {
1265 if (is_forcing_down[
row] || is_forcing_up_[
row]) {
1273 return !column_deletion_helper_.
IsEmpty();
1283 struct DeletionEntry {
1288 std::vector<DeletionEntry> entries;
1292 for (ColIndex
col(0);
col < size; ++
col) {
1298 const RowIndex
row = e.row();
1301 last_coefficient = e.coefficient();
1305 entries.push_back({last_row,
col, last_coefficient});
1310 std::sort(entries.begin(), entries.end(),
1311 [](
const DeletionEntry&
a,
const DeletionEntry&
b) {
1312 if (a.row == b.row) return a.col < b.col;
1313 return a.row < b.row;
1326 for (
int i = 0; i < entries.size();) {
1327 const RowIndex
row = entries[i].row;
1333 for (; i < entries.size(); ++i) {
1334 if (entries[i].
row !=
row)
break;
1335 const ColIndex
col = entries[i].col;
1339 const Fractional reduced_cost = costs_[
col] - scalar_product;
1341 if (is_forcing_up_[
row] == !lp_is_maximization_problem_) {
1342 if (
bound < new_dual_value) {
1343 new_dual_value =
bound;
1344 new_basic_column =
col;
1347 if (
bound > new_dual_value) {
1348 new_dual_value =
bound;
1349 new_basic_column =
col;
1368struct ColWithDegree {
1371 ColWithDegree(ColIndex c, EntryIndex n) :
col(c),
num_entries(n) {}
1372 bool operator<(
const ColWithDegree& other)
const {
1374 return col < other.col;
1392 const int size = num_rows.value();
1401 for (ColIndex
col(0);
col < num_cols; ++
col) {
1407 if (e.coefficient() < 0.0)
std::swap(entry_lb, entry_ub);
1408 lb_sums[e.row()].Add(entry_lb);
1409 ub_sums[e.row()].Add(entry_ub);
1419 for (RowIndex
row(0);
row < num_rows; ++
row) {
1428 variable_offsets_.
assign(num_cols, 0.0);
1445 std::vector<ColWithDegree> col_by_degree;
1446 for (ColIndex
col(0);
col < num_cols; ++
col) {
1447 col_by_degree.push_back(
1450 std::sort(col_by_degree.begin(), col_by_degree.end());
1453 int num_already_free_variables = 0;
1454 int num_implied_free_variables = 0;
1455 int num_fixed_variables = 0;
1456 for (ColWithDegree col_with_degree : col_by_degree) {
1457 const ColIndex
col = col_with_degree.col;
1463 ++num_already_free_variables;
1474 if (used_rows[e.row()])
continue;
1480 if (coeff < 0.0)
std::swap(entry_lb, entry_ub);
1491 coeff > 0.0 ? -ub_sums[e.row()].SumWithoutUb(entry_ub) / coeff
1492 : -lb_sums[e.row()].SumWithoutLb(entry_lb) / coeff;
1494 coeff > 0.0 ? -lb_sums[e.row()].SumWithoutLb(entry_lb) / coeff
1495 : -ub_sums[e.row()].SumWithoutUb(entry_ub) / coeff;
1497 overall_implied_lb =
std::max(overall_implied_lb, implied_lb);
1498 overall_implied_ub =
std::min(overall_implied_ub, implied_ub);
1505 overall_implied_ub)) {
1513 overall_implied_lb) ||
1519 ++num_fixed_variables;
1522 overall_implied_lb)) {
1528 ++num_fixed_variables;
1535 overall_implied_lb) &&
1538 ++num_implied_free_variables;
1541 used_rows[e.row()] =
true;
1565 if (offset != 0.0) {
1566 variable_offsets_[
col] = offset;
1567 SubtractColumnMultipleFromConstraintBound(
col, offset, lp);
1569 postsolve_status_of_free_variables_[
col] =
1573 VLOG(1) << num_already_free_variables <<
" free variables in the problem.";
1574 VLOG(1) << num_implied_free_variables <<
" implied free columns.";
1575 VLOG(1) << num_fixed_variables <<
" variables can be fixed.";
1577 return num_implied_free_variables > 0;
1584 for (ColIndex
col(0);
col < num_cols; ++
col) {
1592 postsolve_status_of_free_variables_[
col];
1614 for (ColIndex doubleton_col(0); doubleton_col < num_cols; ++doubleton_col) {
1623 r.col = doubleton_col;
1627 if (row_deletion_helper_.
IsRowMarked(e.row()))
break;
1628 r.row[
index] = e.row();
1629 r.coeff[
index] = e.coefficient();
1633 if (
index != NUM_ROWS)
continue;
1645 if (std::abs(r.coeff[DELETED]) < std::abs(r.coeff[MODIFIED])) {
1646 std::swap(r.coeff[DELETED], r.coeff[MODIFIED]);
1647 std::swap(r.row[DELETED], r.row[MODIFIED]);
1654 r.deleted_row_as_column.Swap(
1663 new_variable_lb /= r.coeff[DELETED];
1664 new_variable_ub /= r.coeff[DELETED];
1665 if (r.coeff[DELETED] < 0.0)
std::swap(new_variable_lb, new_variable_ub);
1671 r.deleted_row_as_column.AddMultipleToSparseVectorAndIgnoreCommonIndex(
1672 -r.coeff[MODIFIED] / r.coeff[DELETED],
ColToRowIndex(r.col),
1678 if (r.objective_coefficient != 0.0) {
1681 if (
col == r.col)
continue;
1684 e.coefficient() * r.objective_coefficient / r.coeff[DELETED];
1698 restore_stack_.push_back(r);
1701 if (!row_deletion_helper_.
IsEmpty()) {
1714 for (
const RestoreInfo& r :
Reverse(restore_stack_)) {
1746 if (
col == r.col)
continue;
1747 new_variable_value -= (e.coefficient() / r.coeff[DELETED]) *
1760 r.objective_coefficient -
1761 r.coeff[MODIFIED] * solution->
dual_values[r.row[MODIFIED]];
1764 current_reduced_cost / r.coeff[DELETED];
1799 const RowIndex
row = e.row();
1806 const bool is_constraint_upper_bound_relevant =
1807 e.coefficient() > 0.0 ? !is_unbounded_up : is_unbounded_up;
1808 activity_sign_correction_[
row] =
1809 is_constraint_upper_bound_relevant ? 1.0 : -1.0;
1810 rhs_[
row] = is_constraint_upper_bound_relevant
1819 is_unbounded_[
col] =
true;
1820 Fractional initial_feasible_value = MinInMagnitudeOrZeroIfInfinite(
1823 col, initial_feasible_value,
1824 ComputeVariableStatus(initial_feasible_value,
1847 for (RowIndex
row(0);
row < num_rows; ++
row) {
1849 dual_ub_[
row] = 0.0;
1852 dual_lb_[
row] = 0.0;
1857 may_have_participated_lb_.
assign(num_cols,
false);
1858 may_have_participated_ub_.
assign(num_cols,
false);
1861 std::deque<ColIndex> columns_to_process;
1863 std::vector<RowIndex> changed_rows;
1864 for (ColIndex
col(0);
col < num_cols; ++
col) {
1865 columns_to_process.push_back(
col);
1871 const int limit = 5 * num_cols.value();
1872 for (
int count = 0; !columns_to_process.empty() && count < limit; ++count) {
1873 const ColIndex
col = columns_to_process.front();
1874 columns_to_process.pop_front();
1875 in_columns_to_process[
col] =
false;
1887 rc_lb.
Add(col_cost);
1888 rc_ub.
Add(col_cost);
1890 if (row_deletion_helper_.
IsRowMarked(e.row()))
continue;
1893 rc_lb.
Add(-coeff * dual_ub_[e.row()]);
1894 rc_ub.
Add(-coeff * dual_lb_[e.row()]);
1896 rc_lb.
Add(-coeff * dual_lb_[e.row()]);
1897 rc_ub.
Add(-coeff * dual_ub_[e.row()]);
1905 bool can_be_removed =
false;
1907 bool rc_is_away_from_zero;
1908 if (rc_ub.
Sum() <= low_tolerance) {
1909 can_be_removed =
true;
1915 rc_is_away_from_zero = rc_ub.
Sum() <= -high_tolerance;
1916 can_be_removed = !may_have_participated_ub_[
col];
1918 if (rc_lb.
Sum() >= -low_tolerance) {
1922 can_be_removed =
true;
1928 rc_is_away_from_zero = rc_lb.
Sum() >= high_tolerance;
1929 can_be_removed = !may_have_participated_lb_[
col];
1933 if (can_be_removed) {
1945 if (rc_is_away_from_zero) {
1946 VLOG(1) <<
"Problem INFEASIBLE_OR_UNBOUNDED, variable " <<
col
1948 <<
" and its reduced cost is in [" << rc_lb.
Sum() <<
", "
1949 << rc_ub.
Sum() <<
"]";
1962 if (col_cost != 0.0)
continue;
1969 if (IsConstraintBlockingVariable(
1970 *lp, sign_correction * e.coefficient(), e.row())) {
1995 changed_rows.clear();
1997 if (row_deletion_helper_.
IsRowMarked(e.row()))
continue;
1999 const RowIndex
row = e.row();
2004 if (candidate < dual_ub_[
row]) {
2005 dual_ub_[
row] = candidate;
2006 may_have_participated_lb_[
col] =
true;
2012 if (candidate > dual_lb_[
row]) {
2013 dual_lb_[
row] = candidate;
2014 may_have_participated_lb_[
col] =
true;
2023 if (candidate > dual_lb_[
row]) {
2024 dual_lb_[
row] = candidate;
2025 may_have_participated_ub_[
col] =
true;
2031 if (candidate < dual_ub_[
row]) {
2032 dual_ub_[
row] = candidate;
2033 may_have_participated_ub_[
col] =
true;
2040 if (!changed_rows.empty()) {
2042 for (
const RowIndex
row : changed_rows) {
2046 if (!in_columns_to_process[
col]) {
2047 columns_to_process.push_back(
col);
2048 in_columns_to_process[
col] =
true;
2060 for (ColIndex
col(0);
col < end; ++
col) {
2070 return !column_deletion_helper_.
IsEmpty() || !row_deletion_helper_.
IsEmpty();
2080 struct DeletionEntry {
2085 std::vector<DeletionEntry> entries;
2090 for (RowIndex
row(0);
row < num_rows; ++
row) {
2098 if (is_unbounded_[
col]) {
2100 last_coefficient = e.coefficient();
2104 entries.push_back({
row, last_col, last_coefficient});
2109 std::sort(entries.begin(), entries.end(),
2110 [](
const DeletionEntry&
a,
const DeletionEntry&
b) {
2111 if (a.col == b.col) return a.row < b.row;
2112 return a.col < b.col;
2116 for (
int i = 0; i < entries.size();) {
2117 const ColIndex
col = entries[i].col;
2122 for (; i < entries.size(); ++i) {
2123 if (entries[i].
col !=
col)
break;
2124 const RowIndex
row = entries[i].row;
2143 if (activity * activity_sign_correction_[
row] < 0.0) {
2145 if (std::abs(
bound) > std::abs(primal_value_shift)) {
2146 primal_value_shift =
bound;
2155 activity_sign_correction_[row_at_bound] == 1.0
2170 for (RowIndex
row(0);
row < num_rows; ++
row) {
2178 return !row_deletion_helper_.
IsEmpty();
2200 for (ColIndex
col(0);
col < num_cols; ++
col) {
2207 for (RowIndex
row(0);
row < num_rows; ++
row) {
2208 if (degree[
row] == 0) {
2215 VLOG(1) <<
"Problem PRIMAL_INFEASIBLE, constraint " <<
row
2216 <<
" is empty and its range ["
2226 return !row_deletion_helper_.
IsEmpty();
2243 is_maximization_(lp.IsMaximizationProblem()),
2245 cost_(lp.objective_coefficients()[e.
col]),
2246 variable_lower_bound_(lp.variable_lower_bounds()[e.
col]),
2247 variable_upper_bound_(lp.variable_upper_bounds()[e.
col]),
2248 constraint_lower_bound_(lp.constraint_lower_bounds()[e.
row]),
2249 constraint_upper_bound_(lp.constraint_upper_bounds()[e.
row]),
2250 constraint_status_(status) {}
2258 SingletonRowUndo(saved_column, solution);
2261 ZeroCostSingletonColumnUndo(
parameters, saved_row, solution);
2264 SingletonColumnInEqualityUndo(
parameters, saved_row, solution);
2267 MakeConstraintAnEqualityUndo(solution);
2272void SingletonPreprocessor::DeleteSingletonRow(MatrixEntry e,
2278 if (e.coeff < 0.0) {
2279 std::swap(implied_lower_bound, implied_upper_bound);
2288 implied_lower_bound - potential_error > old_lower_bound
2289 ? implied_lower_bound
2292 implied_upper_bound + potential_error < old_upper_bound
2293 ? implied_upper_bound
2298 VLOG(1) <<
"Problem ProblemStatus::PRIMAL_INFEASIBLE, singleton "
2299 "row causes the bound of the variable "
2300 << e.col <<
" to go to infinity.";
2305 if (new_upper_bound < new_lower_bound) {
2308 VLOG(1) <<
"Problem ProblemStatus::PRIMAL_INFEASIBLE, singleton "
2309 "row causes the bound of the variable "
2310 << e.col <<
" to be infeasible by "
2311 << new_lower_bound - new_upper_bound;
2318 new_upper_bound = new_lower_bound;
2321 new_lower_bound = new_upper_bound;
2331 new_upper_bound = new_lower_bound;
2342void SingletonUndo::SingletonRowUndo(
const SparseColumn& saved_column,
2343 ProblemSolution* solution)
const {
2344 DCHECK_EQ(0, solution->dual_values[e_.row]);
2349 const VariableStatus status = solution->variable_statuses[e_.col];
2353 Fractional implied_lower_bound = constraint_lower_bound_ / e_.coeff;
2354 Fractional implied_upper_bound = constraint_upper_bound_ / e_.coeff;
2355 if (e_.coeff < 0.0) {
2356 std::swap(implied_lower_bound, implied_upper_bound);
2358 const bool lower_bound_changed = implied_lower_bound > variable_lower_bound_;
2359 const bool upper_bound_changed = implied_upper_bound < variable_upper_bound_;
2361 if (!lower_bound_changed && !upper_bound_changed)
return;
2369 const Fractional reduced_cost_for_minimization =
2370 is_maximization_ ? -reduced_cost : reduced_cost;
2373 DCHECK(lower_bound_changed || upper_bound_changed);
2374 if (reduced_cost_for_minimization >= 0.0 && !lower_bound_changed) {
2378 if (reduced_cost_for_minimization <= 0.0 && !upper_bound_changed) {
2389 solution->dual_values[e_.row] = reduced_cost / e_.coeff;
2392 (!lower_bound_changed || !upper_bound_changed)) {
2393 new_constraint_status = lower_bound_changed
2397 if (e_.coeff < 0.0) {
2405 solution->constraint_statuses[e_.row] = new_constraint_status;
2408void SingletonPreprocessor::UpdateConstraintBoundsWithVariableBounds(
2409 MatrixEntry e, LinearProgram* lp) {
2410 Fractional lower_delta = -e.coeff * lp->variable_upper_bounds()[e.col];
2411 Fractional upper_delta = -e.coeff * lp->variable_lower_bounds()[e.col];
2412 if (e.coeff < 0.0) {
2415 lp->SetConstraintBounds(e.row,
2416 lp->constraint_lower_bounds()[e.row] + lower_delta,
2417 lp->constraint_upper_bounds()[e.row] + upper_delta);
2420bool SingletonPreprocessor::IntegerSingletonColumnIsRemovable(
2421 const MatrixEntry& matrix_entry,
const LinearProgram& lp)
const {
2423 DCHECK(lp.IsVariableInteger(matrix_entry.col));
2424 const SparseMatrix& transpose = lp.GetTransposeSparseMatrix();
2441 lp.constraint_lower_bounds()[matrix_entry.row];
2443 const Fractional lower_bound_ratio = constraint_lb / matrix_entry.coeff;
2450 lp.constraint_upper_bounds()[matrix_entry.row];
2452 const Fractional upper_bound_ratio = constraint_ub / matrix_entry.coeff;
2461void SingletonPreprocessor::DeleteZeroCostSingletonColumn(
2462 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2466 const SparseColumn& row_as_col = transpose.column(transpose_col);
2468 UpdateConstraintBoundsWithVariableBounds(e, lp);
2473void SingletonUndo::ZeroCostSingletonColumnUndo(
2474 const GlopParameters&
parameters,
const SparseColumn& saved_row,
2475 ProblemSolution* solution)
const {
2480 if (variable_upper_bound_ == variable_lower_bound_) {
2481 solution->primal_values[e_.col] = variable_lower_bound_;
2488 const Fractional corrected_dual = is_maximization_
2489 ? -solution->dual_values[e_.row]
2490 : solution->dual_values[e_.row];
2491 if (corrected_dual > 0) {
2493 solution->primal_values[e_.col] = variable_lower_bound_;
2497 solution->primal_values[e_.col] = variable_upper_bound_;
2506 solution->primal_values[e_.col] = variable_lower_bound_;
2510 solution->primal_values[e_.col] = variable_upper_bound_;
2513 if (constraint_upper_bound_ == constraint_lower_bound_) {
2528 const auto is_smaller_with_tolerance = [tolerance](
Fractional a,
2532 if (variable_lower_bound_ != -
kInfinity) {
2534 activity + e_.coeff * variable_lower_bound_;
2535 if (is_smaller_with_tolerance(constraint_lower_bound_, activity_at_lb) &&
2536 is_smaller_with_tolerance(activity_at_lb, constraint_upper_bound_)) {
2537 solution->primal_values[e_.col] = variable_lower_bound_;
2542 if (variable_upper_bound_ !=
kInfinity) {
2544 activity + e_.coeff * variable_upper_bound_;
2545 if (is_smaller_with_tolerance(constraint_lower_bound_, activity_at_ub) &&
2546 is_smaller_with_tolerance(activity_at_ub, constraint_upper_bound_)) {
2547 solution->primal_values[e_.col] = variable_upper_bound_;
2556 if (constraint_lower_bound_ == -
kInfinity &&
2558 solution->primal_values[e_.col] = 0.0;
2566 if (constraint_lower_bound_ == constraint_upper_bound_) {
2567 solution->primal_values[e_.col] =
2568 (constraint_lower_bound_ - activity) / e_.coeff;
2573 bool set_constraint_to_lower_bound;
2574 if (constraint_lower_bound_ == -
kInfinity) {
2575 set_constraint_to_lower_bound =
false;
2576 }
else if (constraint_upper_bound_ ==
kInfinity) {
2577 set_constraint_to_lower_bound =
true;
2581 const Fractional to_lb = (constraint_lower_bound_ - activity) / e_.coeff;
2582 const Fractional to_ub = (constraint_upper_bound_ - activity) / e_.coeff;
2583 set_constraint_to_lower_bound =
2584 std::max(variable_lower_bound_ - to_lb, to_lb - variable_upper_bound_) <
2585 std::max(variable_lower_bound_ - to_ub, to_ub - variable_upper_bound_);
2588 if (set_constraint_to_lower_bound) {
2589 solution->primal_values[e_.col] =
2590 (constraint_lower_bound_ - activity) / e_.coeff;
2593 solution->primal_values[e_.col] =
2594 (constraint_upper_bound_ - activity) / e_.coeff;
2599void SingletonPreprocessor::DeleteSingletonColumnInEquality(
2600 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2603 const SparseColumn& row_as_column = transpose.column(transpose_col);
2604 undo_stack_.push_back(
2614 const Fractional rhs = lp->constraint_upper_bounds()[e.row];
2617 lp->SetObjectiveOffset(lp->objective_offset() + rhs * multiplier);
2622 lp->objective_coefficients()[
col] - e.coefficient() * multiplier;
2632 lp->SetObjectiveCoefficient(
col, new_cost);
2637 UpdateConstraintBoundsWithVariableBounds(e, lp);
2641void SingletonUndo::SingletonColumnInEqualityUndo(
2642 const GlopParameters&
parameters,
const SparseColumn& saved_row,
2643 ProblemSolution* solution)
const {
2645 ZeroCostSingletonColumnUndo(
parameters, saved_row, solution);
2649 solution->dual_values[e_.row] += cost_ / e_.coeff;
2656void SingletonUndo::MakeConstraintAnEqualityUndo(
2657 ProblemSolution* solution)
const {
2659 solution->constraint_statuses[e_.row] = constraint_status_;
2663bool SingletonPreprocessor::MakeConstraintAnEqualityIfPossible(
2664 const SparseMatrix& transpose, MatrixEntry e, LinearProgram* lp) {
2667 const Fractional cst_lower_bound = lp->constraint_lower_bounds()[e.row];
2668 const Fractional cst_upper_bound = lp->constraint_upper_bounds()[e.row];
2669 if (cst_lower_bound == cst_upper_bound)
return true;
2675 const DenseRow& variable_ubs = lp->variable_upper_bounds();
2676 const DenseRow& variable_lbs = lp->variable_lower_bounds();
2677 if (e.row >= row_sum_is_cached_.size() || !row_sum_is_cached_[e.row]) {
2678 if (e.row >= row_sum_is_cached_.size()) {
2679 const int new_size = e.row.value() + 1;
2680 row_sum_is_cached_.resize(new_size);
2681 row_lb_sum_.resize(new_size);
2682 row_ub_sum_.resize(new_size);
2684 row_sum_is_cached_[e.row] =
true;
2685 row_lb_sum_[e.row].Add(cst_lower_bound);
2686 row_ub_sum_[e.row].Add(cst_upper_bound);
2697 if (column_deletion_helper_.
IsColumnMarked(row_as_col))
continue;
2698 if (entry.coefficient() > 0.0) {
2699 row_lb_sum_[e.row].Add(-entry.coefficient() * variable_ubs[row_as_col]);
2700 row_ub_sum_[e.row].Add(-entry.coefficient() * variable_lbs[row_as_col]);
2702 row_lb_sum_[e.row].Add(-entry.coefficient() * variable_lbs[row_as_col]);
2703 row_ub_sum_[e.row].Add(-entry.coefficient() * variable_ubs[row_as_col]);
2715 c > 0.0 ? row_lb_sum_[e.row].SumWithoutLb(-c * variable_ubs[e.col]) / c
2716 : row_ub_sum_[e.row].SumWithoutUb(-c * variable_ubs[e.col]) / c;
2718 c > 0.0 ? row_ub_sum_[e.row].SumWithoutUb(-c * variable_lbs[e.col]) / c
2719 : row_lb_sum_[e.row].SumWithoutLb(-c * variable_lbs[e.col]) / c;
2725 lp->GetObjectiveCoefficientForMinimizationVersion(e.col);
2732 ub, lp->variable_upper_bounds()[e.col])) {
2738 lp->SetConstraintBounds(e.row, cst_upper_bound, cst_upper_bound);
2745 lp->SetConstraintBounds(e.row, cst_lower_bound, cst_lower_bound);
2751 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2753 << e.col <<
" has a cost (for minimization) of " <<
cost
2754 <<
" and is unbounded towards kInfinity.";
2771 lp->SetVariableBounds(e.col, lp->variable_lower_bounds()[e.col],
kInfinity);
2774 lp->variable_lower_bounds()[e.col], lb)) {
2780 lp->SetConstraintBounds(e.row, cst_lower_bound, cst_lower_bound);
2787 lp->SetConstraintBounds(e.row, cst_upper_bound, cst_upper_bound);
2793 VLOG(1) <<
"Problem ProblemStatus::INFEASIBLE_OR_UNBOUNDED, singleton "
2795 << e.col <<
" has a cost (for minimization) of " <<
cost
2796 <<
" and is unbounded towards -kInfinity.";
2801 lp->SetVariableBounds(e.col, -
kInfinity,
2802 lp->variable_upper_bounds()[e.col]);
2805 if (lp->constraint_lower_bounds()[e.row] ==
2806 lp->constraint_upper_bounds()[e.row]) {
2807 undo_stack_.push_back(SingletonUndo(
2821 ColIndex num_cols(matrix.
num_cols());
2822 RowIndex num_rows(matrix.
num_rows());
2824 std::vector<ColIndex> column_to_process;
2825 for (ColIndex
col(0);
col < num_cols; ++
col) {
2827 if (column_degree[
col] == 1) {
2828 column_to_process.push_back(
col);
2834 std::vector<RowIndex> row_to_process;
2835 for (RowIndex
row(0);
row < num_rows; ++
row) {
2837 if (row_degree[
row] == 1) {
2838 row_to_process.push_back(
row);
2844 (!column_to_process.empty() || !row_to_process.empty())) {
2846 const ColIndex
col = column_to_process.back();
2847 column_to_process.pop_back();
2848 if (column_degree[
col] <= 0)
continue;
2849 const MatrixEntry e = GetSingletonColumnMatrixEntry(
col, matrix);
2851 !IntegerSingletonColumnIsRemovable(e, *lp)) {
2858 DeleteZeroCostSingletonColumn(transpose, e, lp);
2859 }
else if (MakeConstraintAnEqualityIfPossible(transpose, e, lp)) {
2860 DeleteSingletonColumnInEquality(transpose, e, lp);
2864 --row_degree[e.row];
2865 if (row_degree[e.row] == 1) {
2870 const RowIndex
row = row_to_process.back();
2871 row_to_process.pop_back();
2872 if (row_degree[
row] <= 0)
continue;
2873 const MatrixEntry e = GetSingletonRowMatrixEntry(
row, transpose);
2875 DeleteSingletonRow(e, lp);
2876 --column_degree[e.col];
2877 if (column_degree[e.col] == 1) {
2886 return !column_deletion_helper_.
IsEmpty() || !row_deletion_helper_.
IsEmpty();
2904 for (
int i = undo_stack_.size() - 1; i >= 0; --i) {
2909 undo_stack_[i].Undo(
parameters_, saved_col, saved_row, solution);
2913MatrixEntry SingletonPreprocessor::GetSingletonColumnMatrixEntry(
2918 return MatrixEntry(e.row(),
col, e.coefficient());
2922 LOG(DFATAL) <<
"No unmarked entry in a column that is supposed to have one.";
2924 return MatrixEntry(RowIndex(0), ColIndex(0), 0.0);
2927MatrixEntry SingletonPreprocessor::GetSingletonRowMatrixEntry(
2928 RowIndex
row,
const SparseMatrix& transpose) {
2933 return MatrixEntry(
row,
col, e.coefficient());
2937 LOG(DFATAL) <<
"No unmarked entry in a row that is supposed to have one.";
2939 return MatrixEntry(RowIndex(0), ColIndex(0), 0.0);
2950 if (num_cols == 0)
return false;
2956 Fractional num_non_zero_objective_coefficients = 0.0;
2957 for (ColIndex
col(0);
col < num_cols; ++
col) {
2959 row_degree[e.row()] += 1.0;
2962 num_non_zero_objective_coefficients += 1.0;
2974 const EntryIndex initial_num_entries = lp->
num_entries();
2975 int num_zeroed_objective_coefficients = 0;
2976 for (ColIndex
col(0);
col < num_cols; ++
col) {
2985 if (max_magnitude ==
kInfinity || max_magnitude == 0)
continue;
2986 const Fractional threshold = allowed_impact / max_magnitude;
2988 threshold, row_degree);
2991 num_non_zero_objective_coefficients *
2995 ++num_zeroed_objective_coefficients;
3002 <<
" near-zero entries.";
3004 if (num_zeroed_objective_coefficients > 0) {
3005 VLOG(1) <<
"Removed " << num_zeroed_objective_coefficients
3006 <<
" near-zero objective coefficients.";
3024 if (num_cols == 0)
return false;
3026 changed_columns_.clear();
3027 int num_singletons = 0;
3028 for (ColIndex
col(0);
col < num_cols; ++
col) {
3040 changed_columns_.push_back(
col);
3043 VLOG(1) <<
"Changed the sign of " << changed_columns_.size() <<
" columns.";
3044 VLOG(1) << num_singletons <<
" singleton columns left.";
3045 return !changed_columns_.empty();
3052 for (
int i = 0; i < changed_columns_.size(); ++i) {
3053 const ColIndex
col = changed_columns_[i];
3090 std::vector<std::pair<int64_t, RowIndex>> sorted_rows;
3092 for (RowIndex
row(0);
row < num_rows; ++
row) {
3105 sorted_rows.push_back({score,
row});
3107 std::sort(sorted_rows.begin(), sorted_rows.end());
3113 for (
const auto p : sorted_rows) {
3114 const RowIndex
row = p.second;
3124 int entry_index = 0;
3128 r.col[entry_index] =
col;
3129 r.coeff[entry_index] = e.coefficient();
3137 if (entry_index < 2)
continue;
3143 for (
int col_choice = 0; col_choice < NUM_DOUBLETON_COLS; ++col_choice) {
3144 const ColIndex
col = r.col[col_choice];
3152 if (r.lb[DELETED] == r.ub[DELETED] || r.lb[MODIFIED] == r.ub[MODIFIED]) {
3169 const Fractional carry_over_offset = r.rhs / r.coeff[MODIFIED];
3171 -r.coeff[DELETED] / r.coeff[MODIFIED];
3173 carry_over_factor == 0.0) {
3181 r.lb[DELETED] * carry_over_factor + carry_over_offset;
3183 r.ub[DELETED] * carry_over_factor + carry_over_offset;
3184 if (carry_over_factor < 0) {
3185 std::swap(carried_over_lb, carried_over_ub);
3187 if (carried_over_lb <= lb) {
3192 lb = carried_over_lb;
3197 carry_over_factor > 0 ? r.lb[DELETED] : r.ub[DELETED]);
3199 if (carried_over_ub >= ub) {
3204 ub = carried_over_ub;
3209 carry_over_factor > 0 ? r.ub[DELETED] : r.lb[DELETED]);
3218 restore_stack_.push_back(r);
3227 -r.coeff[MODIFIED] / r.coeff[DELETED];
3228 const Fractional constant_offset_factor = r.rhs / r.coeff[DELETED];
3230 if (!
IsFinite(substitution_factor) || substitution_factor == 0.0 ||
3231 !
IsFinite(constant_offset_factor)) {
3239 for (
const int col_choice : {DELETED, MODIFIED}) {
3240 const ColIndex
col = r.col[col_choice];
3254 r.objective_coefficient[MODIFIED] +
3255 substitution_factor * r.objective_coefficient[DELETED];
3266 SubtractColumnMultipleFromConstraintBound(r.col[DELETED],
3267 constant_offset_factor, lp);
3283 return !column_deletion_helper_.
IsEmpty();
3296 for (
const RestoreInfo& r :
Reverse(restore_stack_)) {
3299 LOG(DFATAL) <<
"FIXED variable produced by DoubletonPreprocessor!";
3306 ABSL_FALLTHROUGH_INTENDED;
3311 new_basic_columns[r.col[DELETED]] =
true;
3314 ABSL_FALLTHROUGH_INTENDED;
3322 ? r.bound_backtracking_at_lower_bound
3323 : r.bound_backtracking_at_upper_bound;
3324 const ColIndex bounded_var = r.col[bound_backtracking.
col_choice];
3325 const ColIndex basic_var =
3326 r.col[OtherColChoice(bound_backtracking.
col_choice)];
3330 new_basic_columns[basic_var] =
true;
3341 solution->
primal_values[r.col[MODIFIED]] * r.coeff[MODIFIED]) /
3370 for (
int i = 0; i < restore_stack_.size(); ++i) {
3371 const RestoreInfo& r = restore_stack_[i];
3372 col_to_index[r.col[MODIFIED]].
insert(i);
3373 col_to_index[r.col[DELETED]].
insert(i);
3375 std::vector<ColIndex> singleton_col;
3376 for (ColIndex
col(0);
col < num_cols; ++
col) {
3377 if (!new_basic_columns[
col])
continue;
3378 if (col_to_index[
col].size() == 1) singleton_col.push_back(
col);
3380 while (!singleton_col.empty()) {
3381 const ColIndex
col = singleton_col.back();
3382 singleton_col.pop_back();
3383 if (!new_basic_columns[
col])
continue;
3384 if (col_to_index[
col].empty())
continue;
3387 const RestoreInfo& r = restore_stack_[
index];
3389 const ColChoice col_choice = r.col[MODIFIED] ==
col ? MODIFIED : DELETED;
3397 saved_objective_[r.col[col_choice]] -
3399 solution->
dual_values[r.row] = current_reduced_cost / r.coeff[col_choice];
3404 if (col_to_index[r.col[DELETED]].
size() == 1) {
3405 singleton_col.push_back(r.col[DELETED]);
3407 if (col_to_index[r.col[MODIFIED]].
size() == 1) {
3408 singleton_col.push_back(r.col[MODIFIED]);
3414 saved_row_upper_bounds_, solution);
3423 for (RowIndex
row(0);
row < num_rows; ++
row) {
3427 if (row_lower_bounds[
row] == row_upper_bounds[
row])
continue;
3439void DoubletonEqualityRowPreprocessor::
3440 SwapDeletedAndModifiedVariableRestoreInfo(RestoreInfo* r) {
3442 swap(r->col[DELETED], r->col[MODIFIED]);
3443 swap(r->coeff[DELETED], r->coeff[MODIFIED]);
3444 swap(r->lb[DELETED], r->lb[MODIFIED]);
3445 swap(r->ub[DELETED], r->ub[MODIFIED]);
3446 swap(r->objective_coefficient[DELETED], r->objective_coefficient[MODIFIED]);
3484 if (1.0 * primal_num_rows_.value() <
3495 variable_lower_bounds_.
assign(num_cols, 0.0);
3496 variable_upper_bounds_.
assign(num_cols, 0.0);
3497 for (ColIndex
col(0);
col < num_cols; ++
col) {
3502 variable_lower_bounds_[
col] = lower;
3503 variable_upper_bounds_[
col] = upper;
3504 const Fractional value = MinInMagnitudeOrZeroIfInfinite(lower, upper);
3507 SubtractColumnMultipleFromConstraintBound(
col,
value, lp);
3515 dual_status_correspondence_.
clear();
3516 for (RowIndex
row(0);
row < primal_num_rows_; ++
row) {
3526 LOG(DFATAL) <<
"There should be no free constraint in this lp.";
3529 slack_or_surplus_mapping_.
clear();
3530 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3540 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3573 DenseRow new_primal_values(primal_num_cols_, 0.0);
3577 for (ColIndex
col(0);
col < primal_num_cols_; ++
col) {
3584 const Fractional shift = MinInMagnitudeOrZeroIfInfinite(lower, upper);
3595 new_variable_statuses[
col] = ComputeVariableStatus(shift, lower, upper);
3604 const ColIndex end = dual_status_correspondence_.
size();
3609 const ColIndex
col = slack_or_surplus_mapping_[
index - begin];
3617 new_primal_values[
col] = variable_upper_bounds_[
col];
3620 new_primal_values[
col] = variable_lower_bounds_[
col];
3628 DenseColumn new_dual_values(primal_num_rows_, 0.0);
3635 Fractional sign = primal_is_maximization_problem_ ? -1 : 1;
3636 for (RowIndex
row(0);
row < primal_num_rows_; ++
row) {
3654 new_constraint_statuses[
row] =
3661 new_dual_values[
row] +=
3707 bool all_variable_domains_contain_zero =
true;
3709 variable_initial_lbs_.
assign(num_cols, 0.0);
3710 variable_initial_ubs_.
assign(num_cols, 0.0);
3711 for (ColIndex
col(0);
col < num_cols; ++
col) {
3714 if (0.0 < variable_initial_lbs_[
col] || 0.0 > variable_initial_ubs_[
col]) {
3715 all_variable_domains_contain_zero =
false;
3718 VLOG(1) <<
"Maximum variable bounds magnitude (before shift): "
3719 << ComputeMaxVariableBoundsMagnitude(*lp);
3722 if (all_variable_domains_contain_zero)
return false;
3726 int num_bound_shifts = 0;
3730 offsets_.
assign(num_cols, 0.0);
3731 for (ColIndex
col(0);
col < num_cols; ++
col) {
3732 if (0.0 < variable_initial_lbs_[
col] || 0.0 > variable_initial_ubs_[
col]) {
3733 Fractional offset = MinInMagnitudeOrZeroIfInfinite(
3734 variable_initial_lbs_[
col], variable_initial_ubs_[
col]);
3742 offset = trunc(offset);
3746 offsets_[
col] = offset;
3748 variable_initial_ubs_[
col] - offset);
3751 row_offsets[e.row()].Add(e.coefficient() * offset);
3757 VLOG(1) <<
"Maximum variable bounds magnitude (after " << num_bound_shifts
3758 <<
" shifts): " << ComputeMaxVariableBoundsMagnitude(*lp);
3761 for (RowIndex
row(0);
row < num_rows; ++
row) {
3762 if (!std::isfinite(row_offsets[
row].
Value())) {
3765 VLOG(1) <<
"Shifting variable bounds causes a floating point overflow "
3775 if (!std::isfinite(objective_offset.
Value())) {
3776 VLOG(1) <<
"Shifting variable bounds causes a floating point overflow "
3777 "for the objective.";
3790 for (ColIndex
col(0);
col < num_cols; ++
col) {
3796 ABSL_FALLTHROUGH_INTENDED;
3824 variable_lower_bounds_.
assign(num_cols, 0.0);
3825 variable_upper_bounds_.
assign(num_cols, 0.0);
3826 for (ColIndex
col(0);
col < num_cols; ++
col) {
3858 for (ColIndex
col(0);
col < num_cols; ++
col) {
3861 ABSL_FALLTHROUGH_INTENDED;
3869 ABSL_FALLTHROUGH_INTENDED;
3920 for (RowIndex
row(0);
row < num_rows; ++
row) {
3927 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)
void SetLogToStdOut(bool enable)
void EnableLogging(bool enable)
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
bool use_implied_free_preprocessor() 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 log_to_stdout() 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)
std::function< int64_t(const Model &)> Value(IntegerVariable v)
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 SOLVER_LOG(logger,...)