2022-06-16 15:21:00 +02:00
|
|
|
// Copyright 2010-2022 Google LLC
|
2017-03-28 16:11:06 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/sat/linear_programming_constraint.h"
|
2017-03-28 16:11:06 +02:00
|
|
|
|
2019-08-22 13:15:49 +02:00
|
|
|
#include <algorithm>
|
2017-07-27 11:28:55 -07:00
|
|
|
#include <cmath>
|
2021-03-04 18:26:01 +01:00
|
|
|
#include <cstdint>
|
2022-02-15 18:00:11 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <functional>
|
2017-07-27 11:28:55 -07:00
|
|
|
#include <limits>
|
2022-02-15 18:00:11 +01:00
|
|
|
#include <memory>
|
2023-03-22 19:37:00 +01:00
|
|
|
#include <numeric>
|
2022-02-15 18:00:11 +01:00
|
|
|
#include <random>
|
2017-07-27 11:28:55 -07:00
|
|
|
#include <string>
|
2019-08-22 13:15:49 +02:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2017-07-27 11:28:55 -07:00
|
|
|
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "absl/container/flat_hash_map.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "absl/container/inlined_vector.h"
|
|
|
|
|
#include "absl/meta/type_traits.h"
|
2019-11-20 14:28:11 -08:00
|
|
|
#include "absl/numeric/int128.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "absl/random/distributions.h"
|
|
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
|
#include "absl/types/span.h"
|
2023-03-22 19:37:00 +01:00
|
|
|
#include "ortools/algorithms/binary_search.h"
|
2017-07-27 11:28:55 -07:00
|
|
|
#include "ortools/base/logging.h"
|
2019-12-05 16:36:11 +01:00
|
|
|
#include "ortools/base/mathutil.h"
|
2020-11-19 00:17:26 +01:00
|
|
|
#include "ortools/base/strong_vector.h"
|
2017-07-27 11:28:55 -07:00
|
|
|
#include "ortools/glop/parameters.pb.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/glop/revised_simplex.h"
|
2017-07-27 11:28:55 -07:00
|
|
|
#include "ortools/glop/status.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/lp_data/lp_data.h"
|
|
|
|
|
#include "ortools/lp_data/lp_data_utils.h"
|
2019-05-03 16:30:50 +02:00
|
|
|
#include "ortools/lp_data/lp_types.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/lp_data/scattered_vector.h"
|
|
|
|
|
#include "ortools/lp_data/sparse_column.h"
|
|
|
|
|
#include "ortools/sat/cuts.h"
|
2019-09-18 17:31:11 +02:00
|
|
|
#include "ortools/sat/implied_bounds.h"
|
2019-08-22 13:15:49 +02:00
|
|
|
#include "ortools/sat/integer.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/sat/integer_expr.h"
|
|
|
|
|
#include "ortools/sat/linear_constraint.h"
|
|
|
|
|
#include "ortools/sat/linear_constraint_manager.h"
|
|
|
|
|
#include "ortools/sat/model.h"
|
|
|
|
|
#include "ortools/sat/sat_base.h"
|
|
|
|
|
#include "ortools/sat/sat_parameters.pb.h"
|
|
|
|
|
#include "ortools/sat/sat_solver.h"
|
|
|
|
|
#include "ortools/sat/util.h"
|
2020-09-03 09:08:10 +02:00
|
|
|
#include "ortools/sat/zero_half_cuts.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/util/bitset.h"
|
|
|
|
|
#include "ortools/util/rev.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "ortools/util/saturated_arithmetic.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/util/strong_integers.h"
|
|
|
|
|
#include "ortools/util/time_limit.h"
|
2017-03-28 16:11:06 +02:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
namespace sat {
|
|
|
|
|
|
2018-11-05 16:24:47 +01:00
|
|
|
using glop::ColIndex;
|
|
|
|
|
using glop::Fractional;
|
|
|
|
|
using glop::RowIndex;
|
|
|
|
|
|
2020-09-21 09:44:39 +02:00
|
|
|
void ScatteredIntegerVector::ClearAndResize(int size) {
|
|
|
|
|
if (is_sparse_) {
|
|
|
|
|
for (const glop::ColIndex col : non_zeros_) {
|
|
|
|
|
dense_vector_[col] = IntegerValue(0);
|
|
|
|
|
}
|
|
|
|
|
dense_vector_.resize(size, IntegerValue(0));
|
|
|
|
|
} else {
|
|
|
|
|
dense_vector_.assign(size, IntegerValue(0));
|
|
|
|
|
}
|
|
|
|
|
for (const glop::ColIndex col : non_zeros_) {
|
|
|
|
|
is_zeros_[col] = true;
|
|
|
|
|
}
|
|
|
|
|
is_zeros_.resize(size, true);
|
|
|
|
|
non_zeros_.clear();
|
|
|
|
|
is_sparse_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ScatteredIntegerVector::Add(glop::ColIndex col, IntegerValue value) {
|
2021-03-04 18:26:01 +01:00
|
|
|
const int64_t add = CapAdd(value.value(), dense_vector_[col].value());
|
|
|
|
|
if (add == std::numeric_limits<int64_t>::min() ||
|
|
|
|
|
add == std::numeric_limits<int64_t>::max())
|
|
|
|
|
return false;
|
2020-09-21 09:44:39 +02:00
|
|
|
dense_vector_[col] = IntegerValue(add);
|
|
|
|
|
if (is_sparse_ && is_zeros_[col]) {
|
|
|
|
|
is_zeros_[col] = false;
|
|
|
|
|
non_zeros_.push_back(col);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
template <bool check_overflow>
|
2020-09-21 09:44:39 +02:00
|
|
|
bool ScatteredIntegerVector::AddLinearExpressionMultiple(
|
2023-03-22 19:37:00 +01:00
|
|
|
const IntegerValue multiplier,
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::vector<std::pair<glop::ColIndex, IntegerValue>>& terms) {
|
2020-09-21 09:44:39 +02:00
|
|
|
const double threshold = 0.1 * static_cast<double>(dense_vector_.size());
|
|
|
|
|
if (is_sparse_ && static_cast<double>(terms.size()) < threshold) {
|
2022-02-24 14:17:43 +01:00
|
|
|
for (const std::pair<glop::ColIndex, IntegerValue>& term : terms) {
|
2020-09-21 09:44:39 +02:00
|
|
|
if (is_zeros_[term.first]) {
|
|
|
|
|
is_zeros_[term.first] = false;
|
|
|
|
|
non_zeros_.push_back(term.first);
|
|
|
|
|
}
|
2023-03-22 19:37:00 +01:00
|
|
|
if (check_overflow) {
|
|
|
|
|
if (!AddProductTo(multiplier, term.second,
|
|
|
|
|
&dense_vector_[term.first])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
dense_vector_[term.first] += multiplier * term.second;
|
2020-09-21 09:44:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-07-23 21:59:20 +02:00
|
|
|
if (static_cast<double>(non_zeros_.size()) > threshold) {
|
2020-09-21 09:44:39 +02:00
|
|
|
is_sparse_ = false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
is_sparse_ = false;
|
2022-02-24 14:17:43 +01:00
|
|
|
for (const std::pair<glop::ColIndex, IntegerValue>& term : terms) {
|
2023-03-22 19:37:00 +01:00
|
|
|
if (check_overflow) {
|
|
|
|
|
if (!AddProductTo(multiplier, term.second,
|
|
|
|
|
&dense_vector_[term.first])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
dense_vector_[term.first] += multiplier * term.second;
|
2020-09-21 09:44:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScatteredIntegerVector::ConvertToLinearConstraint(
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::vector<IntegerVariable>& integer_variables,
|
|
|
|
|
IntegerValue upper_bound, LinearConstraint* result) {
|
2020-09-21 09:44:39 +02:00
|
|
|
result->vars.clear();
|
|
|
|
|
result->coeffs.clear();
|
|
|
|
|
if (is_sparse_) {
|
|
|
|
|
std::sort(non_zeros_.begin(), non_zeros_.end());
|
|
|
|
|
for (const glop::ColIndex col : non_zeros_) {
|
|
|
|
|
const IntegerValue coeff = dense_vector_[col];
|
2020-10-22 23:36:58 +02:00
|
|
|
if (coeff == 0) continue;
|
2020-09-21 09:44:39 +02:00
|
|
|
result->vars.push_back(integer_variables[col.value()]);
|
|
|
|
|
result->coeffs.push_back(coeff);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const int size = dense_vector_.size();
|
|
|
|
|
for (glop::ColIndex col(0); col < size; ++col) {
|
|
|
|
|
const IntegerValue coeff = dense_vector_[col];
|
2020-10-22 23:36:58 +02:00
|
|
|
if (coeff == 0) continue;
|
2020-09-21 09:44:39 +02:00
|
|
|
result->vars.push_back(integer_variables[col.value()]);
|
|
|
|
|
result->coeffs.push_back(coeff);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result->lb = kMinIntegerValue;
|
|
|
|
|
result->ub = upper_bound;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::pair<glop::ColIndex, IntegerValue>>
|
2020-09-21 09:44:39 +02:00
|
|
|
ScatteredIntegerVector::GetTerms() {
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::pair<glop::ColIndex, IntegerValue>> result;
|
2020-09-21 09:44:39 +02:00
|
|
|
if (is_sparse_) {
|
|
|
|
|
std::sort(non_zeros_.begin(), non_zeros_.end());
|
|
|
|
|
for (const glop::ColIndex col : non_zeros_) {
|
|
|
|
|
const IntegerValue coeff = dense_vector_[col];
|
2020-10-22 23:36:58 +02:00
|
|
|
if (coeff != 0) result.push_back({col, coeff});
|
2020-09-21 09:44:39 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const int size = dense_vector_.size();
|
|
|
|
|
for (glop::ColIndex col(0); col < size; ++col) {
|
|
|
|
|
const IntegerValue coeff = dense_vector_[col];
|
2020-10-22 23:36:58 +02:00
|
|
|
if (coeff != 0) result.push_back({col, coeff});
|
2020-09-21 09:44:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 11:23:11 +01:00
|
|
|
// TODO(user): make SatParameters singleton too, otherwise changing them after
|
|
|
|
|
// a constraint was added will have no effect on this class.
|
2022-11-29 14:39:27 +01:00
|
|
|
LinearProgrammingConstraint::LinearProgrammingConstraint(
|
|
|
|
|
Model* model, absl::Span<const IntegerVariable> vars)
|
2019-04-05 17:00:40 +02:00
|
|
|
: constraint_manager_(model),
|
2021-11-19 17:57:56 +01:00
|
|
|
parameters_(*(model->GetOrCreate<SatParameters>())),
|
2020-10-22 23:36:58 +02:00
|
|
|
model_(model),
|
2017-12-06 11:23:11 +01:00
|
|
|
time_limit_(model->GetOrCreate<TimeLimit>()),
|
2017-10-18 15:19:19 +02:00
|
|
|
integer_trail_(model->GetOrCreate<IntegerTrail>()),
|
2023-01-16 13:26:55 +01:00
|
|
|
sat_solver_(model->GetOrCreate<SatSolver>()),
|
2017-10-18 15:19:19 +02:00
|
|
|
trail_(model->GetOrCreate<Trail>()),
|
2018-02-12 11:36:18 +01:00
|
|
|
integer_encoder_(model->GetOrCreate<IntegerEncoder>()),
|
2019-12-05 16:36:11 +01:00
|
|
|
random_(model->GetOrCreate<ModelRandomGenerator>()),
|
2020-10-22 23:36:58 +02:00
|
|
|
implied_bounds_processor_({}, integer_trail_,
|
2019-09-18 17:31:11 +02:00
|
|
|
model->GetOrCreate<ImpliedBounds>()),
|
2018-12-10 17:33:20 +01:00
|
|
|
dispatcher_(model->GetOrCreate<LinearProgrammingDispatcher>()),
|
|
|
|
|
expanded_lp_solution_(
|
|
|
|
|
*model->GetOrCreate<LinearProgrammingConstraintLpSolution>()) {
|
2017-03-28 16:11:06 +02:00
|
|
|
// Tweak the default parameters to make the solve incremental.
|
2023-01-27 16:53:53 +01:00
|
|
|
simplex_params_.set_use_dual_simplex(true);
|
|
|
|
|
simplex_params_.set_cost_scaling(glop::GlopParameters::MEAN_COST_SCALING);
|
|
|
|
|
if (parameters_.use_exact_lp_reason()) {
|
|
|
|
|
simplex_params_.set_change_status_to_imprecise(false);
|
|
|
|
|
simplex_params_.set_primal_feasibility_tolerance(1e-7);
|
|
|
|
|
simplex_params_.set_dual_feasibility_tolerance(1e-7);
|
|
|
|
|
}
|
|
|
|
|
simplex_.SetParameters(simplex_params_);
|
2021-11-19 17:57:56 +01:00
|
|
|
if (parameters_.use_branching_in_lp() ||
|
|
|
|
|
parameters_.search_branching() == SatParameters::LP_SEARCH) {
|
2019-08-22 13:15:49 +02:00
|
|
|
compute_reduced_cost_averages_ = true;
|
|
|
|
|
}
|
2020-03-05 17:24:49 +01:00
|
|
|
|
|
|
|
|
// Register our local rev int repository.
|
|
|
|
|
integer_trail_->RegisterReversibleClass(&rc_rev_int_repository_);
|
2022-10-25 20:31:33 +02:00
|
|
|
|
|
|
|
|
integer_rounding_cut_helper_.SetSharedStatistics(
|
|
|
|
|
model->GetOrCreate<SharedStatistics>());
|
2023-01-16 13:26:55 +01:00
|
|
|
cover_cut_helper_.SetSharedStatistics(model->GetOrCreate<SharedStatistics>());
|
2022-11-29 14:39:27 +01:00
|
|
|
|
|
|
|
|
// Initialize the IntegerVariable -> ColIndex mapping.
|
|
|
|
|
CHECK(std::is_sorted(vars.begin(), vars.end()));
|
|
|
|
|
|
|
|
|
|
integer_variables_.assign(vars.begin(), vars.end());
|
|
|
|
|
ColIndex col{0};
|
|
|
|
|
for (const IntegerVariable positive_variable : vars) {
|
|
|
|
|
CHECK(VariableIsPositive(positive_variable));
|
|
|
|
|
implied_bounds_processor_.AddLpVariable(positive_variable);
|
|
|
|
|
(*dispatcher_)[positive_variable] = this;
|
|
|
|
|
mirror_lp_variable_[positive_variable] = col;
|
|
|
|
|
|
|
|
|
|
++col;
|
|
|
|
|
}
|
|
|
|
|
lp_solution_.assign(vars.size(), std::numeric_limits<double>::infinity());
|
|
|
|
|
lp_reduced_cost_.assign(vars.size(), 0.0);
|
|
|
|
|
|
|
|
|
|
if (!vars.empty()) {
|
|
|
|
|
const int max_index = NegationOf(vars.back()).value();
|
|
|
|
|
if (max_index >= expanded_lp_solution_.size()) {
|
|
|
|
|
expanded_lp_solution_.assign(max_index + 1, 0.0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
void LinearProgrammingConstraint::AddLinearConstraint(
|
2020-10-28 13:42:36 +01:00
|
|
|
const LinearConstraint& ct) {
|
2017-03-28 16:11:06 +02:00
|
|
|
DCHECK(!lp_constraint_is_registered_);
|
2018-12-03 14:26:31 +01:00
|
|
|
constraint_manager_.Add(ct);
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 14:39:27 +01:00
|
|
|
glop::ColIndex LinearProgrammingConstraint::GetMirrorVariable(
|
2017-07-07 11:13:35 -07:00
|
|
|
IntegerVariable positive_variable) {
|
|
|
|
|
DCHECK(VariableIsPositive(positive_variable));
|
2022-11-29 14:39:27 +01:00
|
|
|
return mirror_lp_variable_.at(positive_variable);
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-28 14:33:56 +02:00
|
|
|
void LinearProgrammingConstraint::SetObjectiveCoefficient(IntegerVariable ivar,
|
2018-11-05 16:24:47 +01:00
|
|
|
IntegerValue coeff) {
|
2017-03-28 16:11:06 +02:00
|
|
|
CHECK(!lp_constraint_is_registered_);
|
|
|
|
|
objective_is_defined_ = true;
|
2017-07-07 11:13:35 -07:00
|
|
|
IntegerVariable pos_var = VariableIsPositive(ivar) ? ivar : NegationOf(ivar);
|
2020-10-22 23:36:58 +02:00
|
|
|
if (ivar != pos_var) coeff = -coeff;
|
2018-11-05 16:24:47 +01:00
|
|
|
|
2019-07-11 12:03:31 -07:00
|
|
|
constraint_manager_.SetObjectiveCoefficient(pos_var, coeff);
|
2022-11-29 14:39:27 +01:00
|
|
|
const glop::ColIndex col = GetMirrorVariable(pos_var);
|
2020-10-22 23:36:58 +02:00
|
|
|
integer_objective_.push_back({col, coeff});
|
2019-03-01 11:11:36 +01:00
|
|
|
objective_infinity_norm_ =
|
|
|
|
|
std::max(objective_infinity_norm_, IntTypeAbs(coeff));
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
// TODO(user): As the search progress, some variables might get fixed. Exploit
|
|
|
|
|
// this to reduce the number of variables in the LP and in the
|
|
|
|
|
// ConstraintManager? We might also detect during the search that two variable
|
|
|
|
|
// are equivalent.
|
2019-07-12 10:22:51 -07:00
|
|
|
//
|
|
|
|
|
// TODO(user): On TSP/VRP with a lot of cuts, this can take 20% of the overall
|
|
|
|
|
// running time. We should be able to almost remove most of this from the
|
|
|
|
|
// profile by being more incremental (modulo LP scaling).
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): A longer term idea for LP with a lot of variables is to not
|
|
|
|
|
// add all variables to each LP solve and do some "sifting". That can be useful
|
|
|
|
|
// for TSP for instance where the number of edges is large, but only a small
|
|
|
|
|
// fraction will be used in the optimal solution.
|
2019-11-20 14:28:11 -08:00
|
|
|
bool LinearProgrammingConstraint::CreateLpFromConstraintManager() {
|
2023-02-01 17:46:22 +01:00
|
|
|
simplex_.NotifyThatMatrixIsChangedForNextSolve();
|
|
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
// Fill integer_lp_.
|
|
|
|
|
integer_lp_.clear();
|
2019-03-01 11:11:36 +01:00
|
|
|
infinity_norms_.clear();
|
2023-04-21 12:48:03 +02:00
|
|
|
ct_bound_diff_.clear();
|
2020-10-28 13:42:36 +01:00
|
|
|
const auto& all_constraints = constraint_manager_.AllConstraints();
|
2018-12-04 14:36:46 +01:00
|
|
|
for (const auto index : constraint_manager_.LpConstraints()) {
|
2020-10-28 13:42:36 +01:00
|
|
|
const LinearConstraint& ct = all_constraints[index].constraint;
|
2019-11-18 10:06:11 -08:00
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
integer_lp_.push_back(LinearConstraintInternal());
|
2020-10-28 13:42:36 +01:00
|
|
|
LinearConstraintInternal& new_ct = integer_lp_.back();
|
2018-12-04 14:36:46 +01:00
|
|
|
new_ct.lb = ct.lb;
|
|
|
|
|
new_ct.ub = ct.ub;
|
|
|
|
|
const int size = ct.vars.size();
|
2019-03-01 11:11:36 +01:00
|
|
|
IntegerValue infinity_norm(0);
|
2019-11-20 14:28:11 -08:00
|
|
|
if (ct.lb > ct.ub) {
|
2020-09-12 17:48:48 +02:00
|
|
|
VLOG(1) << "Trivial infeasible bound in an LP constraint";
|
2019-11-20 14:28:11 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-03-01 11:11:36 +01:00
|
|
|
if (ct.lb > kMinIntegerValue) {
|
|
|
|
|
infinity_norm = std::max(infinity_norm, IntTypeAbs(ct.lb));
|
|
|
|
|
}
|
|
|
|
|
if (ct.ub < kMaxIntegerValue) {
|
|
|
|
|
infinity_norm = std::max(infinity_norm, IntTypeAbs(ct.ub));
|
|
|
|
|
}
|
2022-11-29 14:39:27 +01:00
|
|
|
new_ct.terms.reserve(size);
|
2018-12-03 14:26:31 +01:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
// We only use positive variable inside this class.
|
2022-11-29 14:39:27 +01:00
|
|
|
const IntegerVariable var = ct.vars[i];
|
|
|
|
|
const IntegerValue coeff = ct.coeffs[i];
|
2019-03-01 11:11:36 +01:00
|
|
|
infinity_norm = std::max(infinity_norm, IntTypeAbs(coeff));
|
2022-11-29 14:39:27 +01:00
|
|
|
new_ct.terms.push_back({GetMirrorVariable(var), coeff});
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
2019-03-01 11:11:36 +01:00
|
|
|
infinity_norms_.push_back(infinity_norm);
|
2023-04-21 12:48:03 +02:00
|
|
|
ct_bound_diff_.push_back(all_constraints[index].activity_range);
|
2018-12-03 14:26:31 +01:00
|
|
|
|
2023-04-21 12:48:03 +02:00
|
|
|
// It is important to keep lp_data_ "clean".
|
2022-11-29 14:39:27 +01:00
|
|
|
DCHECK(std::is_sorted(new_ct.terms.begin(), new_ct.terms.end()));
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
// Copy the integer_lp_ into lp_data_.
|
|
|
|
|
lp_data_.Clear();
|
|
|
|
|
for (int i = 0; i < integer_variables_.size(); ++i) {
|
|
|
|
|
CHECK_EQ(glop::ColIndex(i), lp_data_.CreateNewVariable());
|
|
|
|
|
}
|
2020-11-16 08:44:14 +01:00
|
|
|
|
|
|
|
|
// We remove fixed variables from the objective. This should help the LP
|
|
|
|
|
// scaling, but also our integer reason computation.
|
|
|
|
|
int new_size = 0;
|
|
|
|
|
objective_infinity_norm_ = 0;
|
2022-02-24 14:17:43 +01:00
|
|
|
for (const auto& entry : integer_objective_) {
|
2020-11-16 08:44:14 +01:00
|
|
|
const IntegerVariable var = integer_variables_[entry.first.value()];
|
|
|
|
|
if (integer_trail_->IsFixedAtLevelZero(var)) {
|
|
|
|
|
integer_objective_offset_ +=
|
|
|
|
|
entry.second * integer_trail_->LevelZeroLowerBound(var);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
objective_infinity_norm_ =
|
|
|
|
|
std::max(objective_infinity_norm_, IntTypeAbs(entry.second));
|
|
|
|
|
integer_objective_[new_size++] = entry;
|
2018-12-03 14:26:31 +01:00
|
|
|
lp_data_.SetObjectiveCoefficient(entry.first, ToDouble(entry.second));
|
|
|
|
|
}
|
2020-11-16 08:44:14 +01:00
|
|
|
objective_infinity_norm_ =
|
|
|
|
|
std::max(objective_infinity_norm_, IntTypeAbs(integer_objective_offset_));
|
|
|
|
|
integer_objective_.resize(new_size);
|
|
|
|
|
lp_data_.SetObjectiveOffset(ToDouble(integer_objective_offset_));
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
for (const LinearConstraintInternal& ct : integer_lp_) {
|
2018-11-05 16:24:47 +01:00
|
|
|
const ConstraintIndex row = lp_data_.CreateNewConstraint();
|
|
|
|
|
lp_data_.SetConstraintBounds(row, ToDouble(ct.lb), ToDouble(ct.ub));
|
2020-10-28 13:42:36 +01:00
|
|
|
for (const auto& term : ct.terms) {
|
2018-11-05 16:24:47 +01:00
|
|
|
lp_data_.SetCoefficient(row, term.first, ToDouble(term.second));
|
2017-06-28 14:33:56 +02:00
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
2019-07-12 10:22:51 -07:00
|
|
|
lp_data_.NotifyThatColumnsAreClean();
|
2018-11-05 16:24:47 +01:00
|
|
|
|
2019-10-04 08:51:53 -04:00
|
|
|
// We scale the LP using the level zero bounds that we later override
|
|
|
|
|
// with the current ones.
|
2018-02-12 11:36:18 +01:00
|
|
|
//
|
|
|
|
|
// TODO(user): As part of the scaling, we may also want to shift the initial
|
|
|
|
|
// variable bounds so that each variable contain the value zero in their
|
|
|
|
|
// domain. Maybe just once and for all at the beginning.
|
2019-10-04 08:51:53 -04:00
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
const IntegerVariable cp_var = integer_variables_[i];
|
|
|
|
|
const double lb = ToDouble(integer_trail_->LevelZeroLowerBound(cp_var));
|
|
|
|
|
const double ub = ToDouble(integer_trail_->LevelZeroUpperBound(cp_var));
|
|
|
|
|
lp_data_.SetVariableBounds(glop::ColIndex(i), lb, ub);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 08:44:14 +01:00
|
|
|
// TODO(user): As we have an idea of the LP optimal after the first solves,
|
|
|
|
|
// maybe we can adapt the scaling accordingly.
|
2023-01-27 16:53:53 +01:00
|
|
|
scaler_.Scale(simplex_params_, &lp_data_);
|
2018-02-12 11:36:18 +01:00
|
|
|
UpdateBoundsOfLpVariables();
|
2018-11-05 16:24:47 +01:00
|
|
|
|
2020-11-16 08:44:14 +01:00
|
|
|
// Set the information for the step to polish the LP basis. All our variables
|
|
|
|
|
// are integer, but for now, we just try to minimize the fractionality of the
|
|
|
|
|
// binary variables.
|
2021-11-19 17:57:56 +01:00
|
|
|
if (parameters_.polish_lp_solution()) {
|
2020-11-16 08:44:14 +01:00
|
|
|
simplex_.ClearIntegralityScales();
|
|
|
|
|
for (int i = 0; i < num_vars; ++i) {
|
|
|
|
|
const IntegerVariable cp_var = integer_variables_[i];
|
|
|
|
|
const IntegerValue lb = integer_trail_->LevelZeroLowerBound(cp_var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->LevelZeroUpperBound(cp_var);
|
|
|
|
|
if (lb != 0 || ub != 1) continue;
|
|
|
|
|
simplex_.SetIntegralityScale(
|
|
|
|
|
glop::ColIndex(i),
|
|
|
|
|
1.0 / scaler_.VariableScalingFactor(glop::ColIndex(i)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
lp_data_.NotifyThatColumnsAreClean();
|
2023-02-01 17:46:22 +01:00
|
|
|
VLOG(3) << "LP relaxation: " << lp_data_.GetDimensionString() << ". "
|
2018-12-04 14:36:46 +01:00
|
|
|
<< constraint_manager_.AllConstraints().size()
|
|
|
|
|
<< " Managed constraints.";
|
2019-11-20 14:28:11 -08:00
|
|
|
return true;
|
2018-12-03 14:26:31 +01:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 13:15:49 +02:00
|
|
|
LPSolveInfo LinearProgrammingConstraint::SolveLpForBranching() {
|
|
|
|
|
LPSolveInfo info;
|
|
|
|
|
glop::BasisState basis_state = simplex_.GetState();
|
|
|
|
|
|
2019-12-05 16:36:11 +01:00
|
|
|
const glop::Status status = simplex_.Solve(lp_data_, time_limit_);
|
2019-09-18 17:31:11 +02:00
|
|
|
total_num_simplex_iterations_ += simplex_.GetNumberOfIterations();
|
2019-08-22 13:15:49 +02:00
|
|
|
simplex_.LoadStateForNextSolve(basis_state);
|
|
|
|
|
if (!status.ok()) {
|
|
|
|
|
VLOG(1) << "The LP solver encountered an error: " << status.error_message();
|
|
|
|
|
info.status = glop::ProblemStatus::ABNORMAL;
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
info.status = simplex_.GetProblemStatus();
|
|
|
|
|
if (info.status == glop::ProblemStatus::OPTIMAL ||
|
|
|
|
|
info.status == glop::ProblemStatus::DUAL_FEASIBLE) {
|
|
|
|
|
// Record the objective bound.
|
|
|
|
|
info.lp_objective = simplex_.GetObjectiveValue();
|
|
|
|
|
info.new_obj_bound = IntegerValue(
|
2021-03-04 18:26:01 +01:00
|
|
|
static_cast<int64_t>(std::ceil(info.lp_objective - kCpEpsilon)));
|
2019-08-22 13:15:49 +02:00
|
|
|
}
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinearProgrammingConstraint::FillReducedCostReasonIn(
|
2020-10-28 13:42:36 +01:00
|
|
|
const glop::DenseRow& reduced_costs,
|
|
|
|
|
std::vector<IntegerLiteral>* integer_reason) {
|
2019-08-22 13:15:49 +02:00
|
|
|
integer_reason->clear();
|
|
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
const double rc = reduced_costs[glop::ColIndex(i)];
|
|
|
|
|
if (rc > kLpEpsilon) {
|
|
|
|
|
integer_reason->push_back(
|
|
|
|
|
integer_trail_->LowerBoundAsLiteral(integer_variables_[i]));
|
|
|
|
|
} else if (rc < -kLpEpsilon) {
|
|
|
|
|
integer_reason->push_back(
|
|
|
|
|
integer_trail_->UpperBoundAsLiteral(integer_variables_[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
integer_trail_->RemoveLevelZeroBounds(integer_reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LinearProgrammingConstraint::BranchOnVar(IntegerVariable positive_var) {
|
|
|
|
|
// From the current LP solution, branch on the given var if fractional.
|
|
|
|
|
DCHECK(lp_solution_is_set_);
|
|
|
|
|
const double current_value = GetSolutionValue(positive_var);
|
|
|
|
|
DCHECK_GT(std::abs(current_value - std::round(current_value)), kCpEpsilon);
|
|
|
|
|
|
|
|
|
|
// Used as empty reason in this method.
|
|
|
|
|
integer_reason_.clear();
|
|
|
|
|
|
|
|
|
|
bool deductions_were_made = false;
|
|
|
|
|
|
|
|
|
|
UpdateBoundsOfLpVariables();
|
|
|
|
|
|
|
|
|
|
const IntegerValue current_obj_lb = integer_trail_->LowerBound(objective_cp_);
|
|
|
|
|
// This will try to branch in both direction around the LP value of the
|
|
|
|
|
// given variable and push any deduction done this way.
|
|
|
|
|
|
2022-11-29 14:39:27 +01:00
|
|
|
const glop::ColIndex lp_var = GetMirrorVariable(positive_var);
|
2019-08-22 13:15:49 +02:00
|
|
|
const double current_lb = ToDouble(integer_trail_->LowerBound(positive_var));
|
|
|
|
|
const double current_ub = ToDouble(integer_trail_->UpperBound(positive_var));
|
2019-10-04 08:51:53 -04:00
|
|
|
const double factor = scaler_.VariableScalingFactor(lp_var);
|
2019-08-22 13:15:49 +02:00
|
|
|
if (current_value < current_lb || current_value > current_ub) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Form LP1 var <= floor(current_value)
|
|
|
|
|
const double new_ub = std::floor(current_value);
|
|
|
|
|
lp_data_.SetVariableBounds(lp_var, current_lb * factor, new_ub * factor);
|
|
|
|
|
|
|
|
|
|
LPSolveInfo lower_branch_info = SolveLpForBranching();
|
|
|
|
|
if (lower_branch_info.status != glop::ProblemStatus::OPTIMAL &&
|
|
|
|
|
lower_branch_info.status != glop::ProblemStatus::DUAL_FEASIBLE &&
|
|
|
|
|
lower_branch_info.status != glop::ProblemStatus::DUAL_UNBOUNDED) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lower_branch_info.status == glop::ProblemStatus::DUAL_UNBOUNDED) {
|
|
|
|
|
// Push the other branch.
|
|
|
|
|
const IntegerLiteral deduction = IntegerLiteral::GreaterOrEqual(
|
|
|
|
|
positive_var, IntegerValue(std::ceil(current_value)));
|
2020-10-22 23:36:58 +02:00
|
|
|
if (!integer_trail_->Enqueue(deduction, {}, integer_reason_)) {
|
2019-08-22 13:15:49 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
deductions_were_made = true;
|
|
|
|
|
} else if (lower_branch_info.new_obj_bound <= current_obj_lb) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Form LP2 var >= ceil(current_value)
|
|
|
|
|
const double new_lb = std::ceil(current_value);
|
|
|
|
|
lp_data_.SetVariableBounds(lp_var, new_lb * factor, current_ub * factor);
|
|
|
|
|
|
|
|
|
|
LPSolveInfo upper_branch_info = SolveLpForBranching();
|
|
|
|
|
if (upper_branch_info.status != glop::ProblemStatus::OPTIMAL &&
|
|
|
|
|
upper_branch_info.status != glop::ProblemStatus::DUAL_FEASIBLE &&
|
|
|
|
|
upper_branch_info.status != glop::ProblemStatus::DUAL_UNBOUNDED) {
|
|
|
|
|
return deductions_were_made;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (upper_branch_info.status == glop::ProblemStatus::DUAL_UNBOUNDED) {
|
|
|
|
|
// Push the other branch if not infeasible.
|
|
|
|
|
if (lower_branch_info.status != glop::ProblemStatus::DUAL_UNBOUNDED) {
|
|
|
|
|
const IntegerLiteral deduction = IntegerLiteral::LowerOrEqual(
|
|
|
|
|
positive_var, IntegerValue(std::floor(current_value)));
|
2020-10-22 23:36:58 +02:00
|
|
|
if (!integer_trail_->Enqueue(deduction, {}, integer_reason_)) {
|
2019-08-22 13:15:49 +02:00
|
|
|
return deductions_were_made;
|
|
|
|
|
}
|
|
|
|
|
deductions_were_made = true;
|
|
|
|
|
}
|
|
|
|
|
} else if (upper_branch_info.new_obj_bound <= current_obj_lb) {
|
|
|
|
|
return deductions_were_made;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntegerValue approximate_obj_lb = kMinIntegerValue;
|
|
|
|
|
|
|
|
|
|
if (lower_branch_info.status == glop::ProblemStatus::DUAL_UNBOUNDED &&
|
|
|
|
|
upper_branch_info.status == glop::ProblemStatus::DUAL_UNBOUNDED) {
|
|
|
|
|
return integer_trail_->ReportConflict(integer_reason_);
|
|
|
|
|
} else if (lower_branch_info.status == glop::ProblemStatus::DUAL_UNBOUNDED) {
|
|
|
|
|
approximate_obj_lb = upper_branch_info.new_obj_bound;
|
|
|
|
|
} else if (upper_branch_info.status == glop::ProblemStatus::DUAL_UNBOUNDED) {
|
|
|
|
|
approximate_obj_lb = lower_branch_info.new_obj_bound;
|
|
|
|
|
} else {
|
|
|
|
|
approximate_obj_lb = std::min(lower_branch_info.new_obj_bound,
|
|
|
|
|
upper_branch_info.new_obj_bound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: On some problems, the approximate_obj_lb could be inexact which add
|
|
|
|
|
// some tolerance to CP-SAT where currently there is none.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (approximate_obj_lb <= current_obj_lb) return deductions_were_made;
|
2019-08-22 13:15:49 +02:00
|
|
|
|
|
|
|
|
// Push the bound to the trail.
|
|
|
|
|
const IntegerLiteral deduction =
|
|
|
|
|
IntegerLiteral::GreaterOrEqual(objective_cp_, approximate_obj_lb);
|
2020-10-22 23:36:58 +02:00
|
|
|
if (!integer_trail_->Enqueue(deduction, {}, integer_reason_)) {
|
2019-08-22 13:15:49 +02:00
|
|
|
return deductions_were_made;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
void LinearProgrammingConstraint::RegisterWith(Model* model) {
|
2018-12-03 14:26:31 +01:00
|
|
|
DCHECK(!lp_constraint_is_registered_);
|
|
|
|
|
lp_constraint_is_registered_ = true;
|
|
|
|
|
model->GetOrCreate<LinearProgrammingConstraintCollection>()->push_back(this);
|
|
|
|
|
|
2018-12-04 14:36:46 +01:00
|
|
|
// Note fdid, this is not really needed by should lead to better cache
|
|
|
|
|
// locality.
|
2018-12-03 14:26:31 +01:00
|
|
|
std::sort(integer_objective_.begin(), integer_objective_.end());
|
2018-12-04 14:36:46 +01:00
|
|
|
|
|
|
|
|
// Set the LP to its initial content.
|
2021-11-19 17:57:56 +01:00
|
|
|
if (!parameters_.add_lp_constraints_lazily()) {
|
2018-12-04 14:36:46 +01:00
|
|
|
constraint_manager_.AddAllConstraintsToLp();
|
|
|
|
|
}
|
2019-11-20 14:28:11 -08:00
|
|
|
if (!CreateLpFromConstraintManager()) {
|
|
|
|
|
model->GetOrCreate<SatSolver>()->NotifyThatModelIsUnsat();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
GenericLiteralWatcher* watcher = model->GetOrCreate<GenericLiteralWatcher>();
|
2017-03-28 16:11:06 +02:00
|
|
|
const int watcher_id = watcher->Register(this);
|
|
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
watcher->WatchIntegerVariable(integer_variables_[i], watcher_id, i);
|
|
|
|
|
}
|
2017-07-06 04:57:49 -07:00
|
|
|
if (objective_is_defined_) {
|
|
|
|
|
watcher->WatchUpperBound(objective_cp_, watcher_id);
|
|
|
|
|
}
|
2022-05-09 14:44:50 +02:00
|
|
|
watcher->SetPropagatorPriority(watcher_id, 2);
|
2019-03-25 11:26:14 +01:00
|
|
|
watcher->AlwaysCallAtLevelZero(watcher_id);
|
2017-12-08 14:52:49 +01:00
|
|
|
|
2018-01-10 13:21:06 +01:00
|
|
|
// Registering it with the trail make sure this class is always in sync when
|
|
|
|
|
// it is used in the decision heuristics.
|
|
|
|
|
integer_trail_->RegisterReversibleClass(this);
|
2018-12-21 13:59:58 +01:00
|
|
|
watcher->RegisterReversibleInt(watcher_id, &rev_optimal_constraints_size_);
|
2018-01-10 13:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinearProgrammingConstraint::SetLevel(int level) {
|
2018-12-21 13:59:58 +01:00
|
|
|
optimal_constraints_.resize(rev_optimal_constraints_size_);
|
2018-01-10 13:21:06 +01:00
|
|
|
if (lp_solution_is_set_ && level < lp_solution_level_) {
|
|
|
|
|
lp_solution_is_set_ = false;
|
|
|
|
|
}
|
2018-12-04 14:36:46 +01:00
|
|
|
|
|
|
|
|
// Special case for level zero, we "reload" any previously known optimal
|
|
|
|
|
// solution from that level.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): Keep all optimal solution in the current branch?
|
2019-01-09 11:50:34 +01:00
|
|
|
// TODO(user): Still try to add cuts/constraints though!
|
2018-12-04 14:36:46 +01:00
|
|
|
if (level == 0 && !level_zero_lp_solution_.empty()) {
|
|
|
|
|
lp_solution_is_set_ = true;
|
|
|
|
|
lp_solution_ = level_zero_lp_solution_;
|
|
|
|
|
lp_solution_level_ = 0;
|
|
|
|
|
for (int i = 0; i < lp_solution_.size(); i++) {
|
|
|
|
|
expanded_lp_solution_[integer_variables_[i]] = lp_solution_[i];
|
|
|
|
|
expanded_lp_solution_[NegationOf(integer_variables_[i])] =
|
|
|
|
|
-lp_solution_[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-18 11:09:13 +02:00
|
|
|
void LinearProgrammingConstraint::AddCutGenerator(CutGenerator generator) {
|
|
|
|
|
cut_generators_.push_back(std::move(generator));
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 16:11:06 +02:00
|
|
|
bool LinearProgrammingConstraint::IncrementalPropagate(
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::vector<int>& watch_indices) {
|
2023-02-01 17:46:22 +01:00
|
|
|
if (!lp_solution_is_set_) {
|
|
|
|
|
return Propagate();
|
|
|
|
|
}
|
2018-12-04 14:36:46 +01:00
|
|
|
|
2019-01-10 15:56:33 +01:00
|
|
|
// At level zero, if there is still a chance to add cuts or lazy constraints,
|
|
|
|
|
// we re-run the LP.
|
|
|
|
|
if (trail_->CurrentDecisionLevel() == 0 && !lp_at_level_zero_is_final_) {
|
|
|
|
|
return Propagate();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 14:36:46 +01:00
|
|
|
// Check whether the change breaks the current LP solution. If it does, call
|
|
|
|
|
// Propagate() on the current LP.
|
2017-03-28 16:11:06 +02:00
|
|
|
for (const int index : watch_indices) {
|
2018-11-05 16:24:47 +01:00
|
|
|
const double lb =
|
|
|
|
|
ToDouble(integer_trail_->LowerBound(integer_variables_[index]));
|
|
|
|
|
const double ub =
|
|
|
|
|
ToDouble(integer_trail_->UpperBound(integer_variables_[index]));
|
2017-03-28 16:11:06 +02:00
|
|
|
const double value = lp_solution_[index];
|
2020-10-22 23:36:58 +02:00
|
|
|
if (value < lb - kCpEpsilon || value > ub + kCpEpsilon) return Propagate();
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
2018-12-04 14:36:46 +01:00
|
|
|
|
|
|
|
|
// TODO(user): The saved lp solution is still valid given the current variable
|
|
|
|
|
// bounds, so the LP optimal didn't change. However we might still want to add
|
|
|
|
|
// new cuts or new lazy constraints?
|
2018-12-21 13:59:58 +01:00
|
|
|
//
|
|
|
|
|
// TODO(user): Propagate the last optimal_constraint? Note that we need
|
|
|
|
|
// to be careful since the reversible int in IntegerSumLE are not registered.
|
|
|
|
|
// However, because we delete "optimalconstraints" on backtrack, we might not
|
|
|
|
|
// care.
|
2017-03-28 16:11:06 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
glop::Fractional LinearProgrammingConstraint::GetVariableValueAtCpScale(
|
|
|
|
|
glop::ColIndex var) {
|
2019-10-04 08:51:53 -04:00
|
|
|
return scaler_.UnscaleVariableValue(var, simplex_.GetVariableValue(var));
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
double LinearProgrammingConstraint::GetSolutionValue(
|
|
|
|
|
IntegerVariable variable) const {
|
2022-03-07 11:31:58 +01:00
|
|
|
return lp_solution_[mirror_lp_variable_.at(variable).value()];
|
2017-08-03 10:20:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double LinearProgrammingConstraint::GetSolutionReducedCost(
|
|
|
|
|
IntegerVariable variable) const {
|
2022-03-07 11:31:58 +01:00
|
|
|
return lp_reduced_cost_[mirror_lp_variable_.at(variable).value()];
|
2017-08-03 10:20:59 -07:00
|
|
|
}
|
|
|
|
|
|
2018-02-12 11:36:18 +01:00
|
|
|
void LinearProgrammingConstraint::UpdateBoundsOfLpVariables() {
|
2017-03-28 16:11:06 +02:00
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
const IntegerVariable cp_var = integer_variables_[i];
|
2018-11-05 16:24:47 +01:00
|
|
|
const double lb = ToDouble(integer_trail_->LowerBound(cp_var));
|
|
|
|
|
const double ub = ToDouble(integer_trail_->UpperBound(cp_var));
|
2019-10-04 08:51:53 -04:00
|
|
|
const double factor = scaler_.VariableScalingFactor(glop::ColIndex(i));
|
2018-01-10 13:21:06 +01:00
|
|
|
lp_data_.SetVariableBounds(glop::ColIndex(i), lb * factor, ub * factor);
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
2018-02-12 11:36:18 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
bool LinearProgrammingConstraint::SolveLp() {
|
2019-01-10 15:56:33 +01:00
|
|
|
if (trail_->CurrentDecisionLevel() == 0) {
|
|
|
|
|
lp_at_level_zero_is_final_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
const auto status = simplex_.Solve(lp_data_, time_limit_);
|
2019-09-18 17:31:11 +02:00
|
|
|
total_num_simplex_iterations_ += simplex_.GetNumberOfIterations();
|
2018-12-03 14:26:31 +01:00
|
|
|
if (!status.ok()) {
|
2019-05-16 16:50:11 +02:00
|
|
|
VLOG(1) << "The LP solver encountered an error: " << status.error_message();
|
2018-12-03 14:26:31 +01:00
|
|
|
simplex_.ClearStateForNextSolve();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-05 18:02:49 +02:00
|
|
|
average_degeneracy_.AddData(CalculateDegeneracy());
|
2019-05-03 16:30:50 +02:00
|
|
|
if (average_degeneracy_.CurrentAverage() >= 1000.0) {
|
2020-10-22 23:36:58 +02:00
|
|
|
VLOG(2) << "High average degeneracy: "
|
|
|
|
|
<< average_degeneracy_.CurrentAverage();
|
2019-05-03 16:30:50 +02:00
|
|
|
}
|
2018-12-03 14:26:31 +01:00
|
|
|
|
2023-02-01 17:46:22 +01:00
|
|
|
// By default we assume the matrix is unchanged.
|
|
|
|
|
// This will be reset by CreateLpFromConstraintManager().
|
|
|
|
|
simplex_.NotifyThatMatrixIsUnchangedForNextSolve();
|
|
|
|
|
|
2020-11-16 08:44:14 +01:00
|
|
|
const int status_as_int = static_cast<int>(simplex_.GetProblemStatus());
|
|
|
|
|
if (status_as_int >= num_solves_by_status_.size()) {
|
|
|
|
|
num_solves_by_status_.resize(status_as_int + 1);
|
|
|
|
|
}
|
2021-04-30 14:20:46 +02:00
|
|
|
num_solves_++;
|
2020-11-16 08:44:14 +01:00
|
|
|
num_solves_by_status_[status_as_int]++;
|
|
|
|
|
VLOG(2) << "lvl:" << trail_->CurrentDecisionLevel() << " "
|
|
|
|
|
<< simplex_.GetProblemStatus()
|
|
|
|
|
<< " iter:" << simplex_.GetNumberOfIterations()
|
|
|
|
|
<< " obj:" << simplex_.GetObjectiveValue();
|
|
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
if (simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL) {
|
2018-12-04 14:36:46 +01:00
|
|
|
lp_solution_is_set_ = true;
|
|
|
|
|
lp_solution_level_ = trail_->CurrentDecisionLevel();
|
2018-12-03 14:26:31 +01:00
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
const glop::Fractional value =
|
|
|
|
|
GetVariableValueAtCpScale(glop::ColIndex(i));
|
2018-12-04 14:36:46 +01:00
|
|
|
lp_solution_[i] = value;
|
|
|
|
|
expanded_lp_solution_[integer_variables_[i]] = value;
|
|
|
|
|
expanded_lp_solution_[NegationOf(integer_variables_[i])] = -value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lp_solution_level_ == 0) {
|
|
|
|
|
level_zero_lp_solution_ = lp_solution_;
|
2018-12-03 14:26:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:46:22 +01:00
|
|
|
bool LinearProgrammingConstraint::AnalyzeLp() {
|
|
|
|
|
// A dual-unbounded problem is infeasible. We use the dual ray reason.
|
|
|
|
|
if (simplex_.GetProblemStatus() == glop::ProblemStatus::DUAL_UNBOUNDED) {
|
|
|
|
|
if (parameters_.use_exact_lp_reason()) {
|
|
|
|
|
if (!FillExactDualRayReason()) return true;
|
|
|
|
|
} else {
|
|
|
|
|
FillReducedCostReasonIn(simplex_.GetDualRayRowCombination(),
|
|
|
|
|
&integer_reason_);
|
|
|
|
|
}
|
|
|
|
|
return integer_trail_->ReportConflict(integer_reason_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(user): Update limits for DUAL_UNBOUNDED status as well.
|
|
|
|
|
UpdateSimplexIterationLimit(/*min_iter=*/10, /*max_iter=*/1000);
|
|
|
|
|
|
|
|
|
|
// Optimality deductions if problem has an objective.
|
|
|
|
|
if (objective_is_defined_ &&
|
|
|
|
|
(simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL ||
|
|
|
|
|
simplex_.GetProblemStatus() == glop::ProblemStatus::DUAL_FEASIBLE)) {
|
|
|
|
|
// TODO(user): Maybe do a bit less computation when we cannot propagate
|
|
|
|
|
// anything.
|
|
|
|
|
if (parameters_.use_exact_lp_reason()) {
|
|
|
|
|
if (!ExactLpReasonning()) return false;
|
|
|
|
|
|
|
|
|
|
// Display when the inexact bound would have propagated more.
|
|
|
|
|
if (VLOG_IS_ON(2)) {
|
|
|
|
|
const double relaxed_optimal_objective = simplex_.GetObjectiveValue();
|
|
|
|
|
const IntegerValue approximate_new_lb(static_cast<int64_t>(
|
|
|
|
|
std::ceil(relaxed_optimal_objective - kCpEpsilon)));
|
|
|
|
|
const IntegerValue propagated_lb =
|
|
|
|
|
integer_trail_->LowerBound(objective_cp_);
|
|
|
|
|
if (approximate_new_lb > propagated_lb) {
|
|
|
|
|
VLOG(2) << "LP objective [ " << ToDouble(propagated_lb) << ", "
|
|
|
|
|
<< ToDouble(integer_trail_->UpperBound(objective_cp_))
|
|
|
|
|
<< " ] approx_lb += "
|
|
|
|
|
<< ToDouble(approximate_new_lb - propagated_lb) << " gap: "
|
|
|
|
|
<< integer_trail_->UpperBound(objective_cp_) - propagated_lb;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Try to filter optimal objective value. Note that GetObjectiveValue()
|
|
|
|
|
// already take care of the scaling so that it returns an objective in the
|
|
|
|
|
// CP world.
|
|
|
|
|
FillReducedCostReasonIn(simplex_.GetReducedCosts(), &integer_reason_);
|
|
|
|
|
const double objective_cp_ub =
|
|
|
|
|
ToDouble(integer_trail_->UpperBound(objective_cp_));
|
|
|
|
|
const double relaxed_optimal_objective = simplex_.GetObjectiveValue();
|
|
|
|
|
ReducedCostStrengtheningDeductions(objective_cp_ub -
|
|
|
|
|
relaxed_optimal_objective);
|
|
|
|
|
if (!deductions_.empty()) {
|
|
|
|
|
deductions_reason_ = integer_reason_;
|
|
|
|
|
deductions_reason_.push_back(
|
|
|
|
|
integer_trail_->UpperBoundAsLiteral(objective_cp_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Push new objective lb.
|
|
|
|
|
const IntegerValue approximate_new_lb(static_cast<int64_t>(
|
|
|
|
|
std::ceil(relaxed_optimal_objective - kCpEpsilon)));
|
|
|
|
|
if (approximate_new_lb > integer_trail_->LowerBound(objective_cp_)) {
|
|
|
|
|
const IntegerLiteral deduction =
|
|
|
|
|
IntegerLiteral::GreaterOrEqual(objective_cp_, approximate_new_lb);
|
|
|
|
|
if (!integer_trail_->Enqueue(deduction, {}, integer_reason_)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Push reduced cost strengthening bounds.
|
|
|
|
|
if (!deductions_.empty()) {
|
|
|
|
|
const int trail_index_with_same_reason = integer_trail_->Index();
|
|
|
|
|
for (const IntegerLiteral deduction : deductions_) {
|
|
|
|
|
if (!integer_trail_->Enqueue(deduction, {}, deductions_reason_,
|
|
|
|
|
trail_index_with_same_reason)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy more info about the current solution.
|
|
|
|
|
if (simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL) {
|
|
|
|
|
CHECK(lp_solution_is_set_);
|
|
|
|
|
|
|
|
|
|
lp_objective_ = simplex_.GetObjectiveValue();
|
|
|
|
|
lp_solution_is_integer_ = true;
|
|
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
lp_reduced_cost_[i] = scaler_.UnscaleReducedCost(
|
|
|
|
|
glop::ColIndex(i), simplex_.GetReducedCost(glop::ColIndex(i)));
|
|
|
|
|
if (std::abs(lp_solution_[i] - std::round(lp_solution_[i])) >
|
|
|
|
|
kCpEpsilon) {
|
|
|
|
|
lp_solution_is_integer_ = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (compute_reduced_cost_averages_) {
|
|
|
|
|
UpdateAverageReducedCosts();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:26:55 +01:00
|
|
|
// If we return false, we don't process this cut. So it is okay to leave it
|
|
|
|
|
// in a bad state.
|
|
|
|
|
bool LinearProgrammingConstraint::RemoveFixedTerms(LinearConstraint* cut) {
|
|
|
|
|
int new_size = 0;
|
|
|
|
|
const int num_terms = static_cast<int>(cut->vars.size());
|
|
|
|
|
for (int i = 0; i < num_terms; ++i) {
|
|
|
|
|
const IntegerVariable var = cut->vars[i];
|
|
|
|
|
const IntegerValue coeff = cut->coeffs[i];
|
|
|
|
|
const IntegerValue lb = integer_trail_->LevelZeroLowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->LevelZeroUpperBound(var);
|
|
|
|
|
if (lb == ub) {
|
|
|
|
|
if (!AddProductTo(lb, -coeff, &cut->ub)) return false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
cut->vars[new_size] = var;
|
|
|
|
|
cut->coeffs[new_size] = coeff;
|
|
|
|
|
++new_size;
|
|
|
|
|
}
|
|
|
|
|
cut->vars.resize(new_size);
|
|
|
|
|
cut->coeffs.resize(new_size);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This assume an <= constraint.
|
|
|
|
|
//
|
|
|
|
|
// For now we just remove fixed variable and do propagation. Because these
|
|
|
|
|
// constraint can be derived from a sum of other, we might have non-trivial
|
|
|
|
|
// propagation. It is like reduced cost fixing for different dual values.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): We could also strengthen the coefficients, but we do need the
|
|
|
|
|
// constraint slack to be added first though.
|
|
|
|
|
bool LinearProgrammingConstraint::PreprocessCut(LinearConstraint* cut) {
|
|
|
|
|
CHECK_EQ(cut->lb, kMinIntegerValue);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
bool min_sum_overflow = false;
|
|
|
|
|
IntegerValue min_sum(0);
|
|
|
|
|
IntegerValue max_range(0);
|
|
|
|
|
bool has_fixed_term = false;
|
|
|
|
|
const int num_terms = static_cast<int>(cut->vars.size());
|
|
|
|
|
if (num_terms == 0) return false;
|
|
|
|
|
for (int i = 0; i < num_terms; ++i) {
|
|
|
|
|
const IntegerVariable var = cut->vars[i];
|
|
|
|
|
const IntegerValue magnitude = cut->coeffs[i];
|
|
|
|
|
CHECK_GT(magnitude, 0);
|
|
|
|
|
const IntegerValue lb = integer_trail_->LevelZeroLowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->LevelZeroUpperBound(var);
|
|
|
|
|
if (lb == ub) {
|
|
|
|
|
has_fixed_term = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
max_range =
|
|
|
|
|
std::max(max_range,
|
|
|
|
|
IntegerValue(CapProd(magnitude.value(), (ub - lb).value())));
|
|
|
|
|
if (!AddProductTo(magnitude, lb, &min_sum)) min_sum_overflow = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (has_fixed_term) {
|
|
|
|
|
if (!RemoveFixedTerms(cut)) return false;
|
|
|
|
|
continue; // restart function.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const IntegerValue slack{CapSub(cut->ub.value(), min_sum.value())};
|
|
|
|
|
if (!min_sum_overflow && !AtMinOrMaxInt64(slack.value())) {
|
2023-04-21 12:48:03 +02:00
|
|
|
if (slack < 0) {
|
|
|
|
|
problem_proven_infeasible_by_cuts_ = true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-01-16 13:26:55 +01:00
|
|
|
|
|
|
|
|
if (trail_->CurrentDecisionLevel() == 0 && max_range > slack) {
|
|
|
|
|
bool newly_fixed = false;
|
|
|
|
|
for (int i = 0; i < num_terms; ++i) {
|
|
|
|
|
const IntegerVariable var = cut_.vars[i];
|
|
|
|
|
const IntegerValue magnitude = cut_.coeffs[i];
|
|
|
|
|
const IntegerValue lb = integer_trail_->LevelZeroLowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->LevelZeroUpperBound(var);
|
|
|
|
|
if (CapProd(magnitude.value(), (ub - lb).value()) > slack) {
|
|
|
|
|
newly_fixed = true;
|
|
|
|
|
++total_num_cut_propagations_;
|
|
|
|
|
const IntegerValue new_diff = slack / magnitude; // All positive.
|
|
|
|
|
if (!integer_trail_->Enqueue(
|
|
|
|
|
IntegerLiteral::LowerOrEqual(var, lb + new_diff), {}, {})) {
|
|
|
|
|
sat_solver_->NotifyThatModelIsUnsat();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (newly_fixed) {
|
|
|
|
|
if (!RemoveFixedTerms(cut)) return false;
|
|
|
|
|
if (cut->vars.empty()) return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 16:51:46 +01:00
|
|
|
bool LinearProgrammingConstraint::AddCutFromConstraints(
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::string& name,
|
|
|
|
|
const std::vector<std::pair<RowIndex, IntegerValue>>& integer_multipliers) {
|
2021-03-09 16:02:34 +01:00
|
|
|
// This is initialized to a valid linear constraint (by taking linear
|
2019-01-17 16:12:52 +01:00
|
|
|
// combination of the LP rows) and will be transformed into a cut if
|
|
|
|
|
// possible.
|
|
|
|
|
//
|
2019-11-18 10:06:11 -08:00
|
|
|
// TODO(user): For CG cuts, Ideally this linear combination should have only
|
2023-01-16 13:26:55 +01:00
|
|
|
// one fractional variable (basis_col). But because of imprecision, we can get
|
|
|
|
|
// a bunch of fractional entry with small coefficient (relative to the one of
|
2019-11-18 10:06:11 -08:00
|
|
|
// basis_col). We try to handle that in IntegerRoundingCut(), but it might be
|
|
|
|
|
// better to add small multiple of the involved rows to get rid of them.
|
2020-02-03 16:21:57 +01:00
|
|
|
IntegerValue cut_ub;
|
2020-09-21 09:44:39 +02:00
|
|
|
if (!ComputeNewLinearConstraint(integer_multipliers, &tmp_scattered_vector_,
|
2020-02-03 16:21:57 +01:00
|
|
|
&cut_ub)) {
|
2023-04-21 12:48:03 +02:00
|
|
|
++num_cut_overflows_;
|
2020-02-03 16:21:57 +01:00
|
|
|
VLOG(1) << "Issue, overflow!";
|
|
|
|
|
return false;
|
2019-01-17 16:12:52 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-03 16:21:57 +01:00
|
|
|
// Important: because we use integer_multipliers below, we cannot just
|
|
|
|
|
// divide by GCD or call PreventOverflow() here.
|
2020-09-21 09:44:39 +02:00
|
|
|
//
|
|
|
|
|
// TODO(user): the conversion col_index -> IntegerVariable is slow and could
|
|
|
|
|
// in principle be removed. Easy for cuts, but not so much for
|
|
|
|
|
// implied_bounds_processor_. Note that in theory this could allow us to
|
|
|
|
|
// use Literal directly without the need to have an IntegerVariable for them.
|
|
|
|
|
tmp_scattered_vector_.ConvertToLinearConstraint(integer_variables_, cut_ub,
|
|
|
|
|
&cut_);
|
2020-02-03 16:21:57 +01:00
|
|
|
|
2020-09-03 09:08:10 +02:00
|
|
|
// Note that the base constraint we use are currently always tight.
|
|
|
|
|
// It is not a requirement though.
|
2020-09-21 09:44:39 +02:00
|
|
|
if (DEBUG_MODE) {
|
|
|
|
|
const double norm = ToDouble(ComputeInfinityNorm(cut_));
|
|
|
|
|
const double activity = ComputeActivity(cut_, expanded_lp_solution_);
|
|
|
|
|
if (std::abs(activity - ToDouble(cut_.ub)) / norm > 1e-4) {
|
|
|
|
|
VLOG(1) << "Cut not tight " << activity << " <= " << ToDouble(cut_.ub);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-01-17 16:12:52 +01:00
|
|
|
}
|
2020-03-02 15:15:37 +01:00
|
|
|
|
2023-01-16 13:26:55 +01:00
|
|
|
// TODO(user): Do coeff strengthening before cuting ?
|
|
|
|
|
// The issue is that we need to add slack variable first, otherwise the
|
|
|
|
|
// coefficient min and the max activity of that constraint will change.
|
|
|
|
|
MakeAllCoefficientsPositive(&cut_);
|
|
|
|
|
if (!PreprocessCut(&cut_)) return false;
|
|
|
|
|
CHECK(!cut_.vars.empty());
|
2020-03-02 15:15:37 +01:00
|
|
|
|
2022-05-09 14:44:50 +02:00
|
|
|
bool at_least_one_added = false;
|
2022-11-29 14:39:27 +01:00
|
|
|
|
2022-05-09 14:44:50 +02:00
|
|
|
// Try single node flow cover cut.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): We should probably deal with slack here too. We can always
|
|
|
|
|
// add slack and integrate them if they lead to better cuts.
|
2023-02-03 16:13:06 +01:00
|
|
|
if (flow_cover_cut_helper_.ComputeFlowCoverRelaxationAndGenerateCut(
|
|
|
|
|
cut_, expanded_lp_solution_, integer_trail_,
|
|
|
|
|
&implied_bounds_processor_)) {
|
2022-05-09 14:44:50 +02:00
|
|
|
at_least_one_added |= constraint_manager_.AddCut(
|
|
|
|
|
flow_cover_cut_helper_.cut(), absl::StrCat(name, "_F"),
|
|
|
|
|
expanded_lp_solution_, flow_cover_cut_helper_.Info());
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 17:03:43 +01:00
|
|
|
// Note that this always complement the terms to have an lp value closer to
|
|
|
|
|
// zero.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): Keep track of the potential overflow here.
|
|
|
|
|
if (!base_ct_.FillFromLinearConstraint(cut_, expanded_lp_solution_,
|
|
|
|
|
integer_trail_)) {
|
2023-04-21 12:48:03 +02:00
|
|
|
++num_cut_overflows_;
|
|
|
|
|
VLOG(1) << "Issue, overflow!";
|
2023-01-24 17:03:43 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2022-10-25 20:31:33 +02:00
|
|
|
|
2023-01-24 17:03:43 +01:00
|
|
|
// If there are no integer (all Booleans), no need to try implied bounds
|
|
|
|
|
// heurititics. By setting this to nullptr, we are a bit faster.
|
|
|
|
|
bool some_ints = false;
|
|
|
|
|
bool some_relevant_positions = false;
|
|
|
|
|
for (const CutTerm& term : base_ct_.terms) {
|
|
|
|
|
if (term.bound_diff > 1) some_ints = true;
|
|
|
|
|
if (term.HasRelevantLpValue()) some_relevant_positions = true;
|
|
|
|
|
}
|
2019-01-17 16:12:52 +01:00
|
|
|
|
2023-01-24 17:03:43 +01:00
|
|
|
// If all value are integer, we will not be able to cut anything.
|
|
|
|
|
if (!some_relevant_positions) return false;
|
|
|
|
|
|
|
|
|
|
ImpliedBoundsProcessor* ib_processor =
|
|
|
|
|
some_ints ? &implied_bounds_processor_ : nullptr;
|
|
|
|
|
|
|
|
|
|
// Add constraint slack.
|
|
|
|
|
const IntegerVariable first_slack(expanded_lp_solution_.size());
|
|
|
|
|
CHECK_EQ(first_slack.value() % 2, 0);
|
|
|
|
|
tmp_slack_rows_.clear();
|
|
|
|
|
for (const auto& pair : integer_multipliers) {
|
|
|
|
|
const RowIndex row = pair.first;
|
|
|
|
|
const IntegerValue coeff = pair.second;
|
|
|
|
|
const auto status = simplex_.GetConstraintStatus(row);
|
|
|
|
|
if (status == glop::ConstraintStatus::FIXED_VALUE) continue;
|
|
|
|
|
|
|
|
|
|
CutTerm entry;
|
|
|
|
|
entry.coeff = IntTypeAbs(coeff);
|
|
|
|
|
entry.lp_value = 0.0;
|
2023-04-21 12:48:03 +02:00
|
|
|
entry.bound_diff = ct_bound_diff_[row];
|
2023-01-24 17:03:43 +01:00
|
|
|
entry.expr_vars[0] =
|
|
|
|
|
first_slack + 2 * IntegerVariable(tmp_slack_rows_.size());
|
|
|
|
|
entry.expr_coeffs[1] = 0;
|
|
|
|
|
if (coeff > 0) {
|
|
|
|
|
// Slack = ub - constraint;
|
|
|
|
|
entry.expr_coeffs[0] = IntegerValue(-1);
|
|
|
|
|
entry.expr_offset = integer_lp_[row].ub;
|
|
|
|
|
} else {
|
|
|
|
|
// Slack = constraint - lb;
|
|
|
|
|
entry.expr_coeffs[0] = IntegerValue(1);
|
|
|
|
|
entry.expr_offset = -integer_lp_[row].lb;
|
2023-01-16 13:26:55 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-24 17:03:43 +01:00
|
|
|
base_ct_.terms.push_back(entry);
|
|
|
|
|
tmp_slack_rows_.push_back(row);
|
|
|
|
|
}
|
2023-01-16 13:26:55 +01:00
|
|
|
|
2023-01-24 17:03:43 +01:00
|
|
|
// Try integer rounding heuristic to find cut.
|
|
|
|
|
RoundingOptions options;
|
|
|
|
|
options.max_scaling = parameters_.max_integer_rounding_scaling();
|
2019-01-17 16:12:52 +01:00
|
|
|
|
2023-01-24 17:03:43 +01:00
|
|
|
options.use_ib_before_heuristic = false;
|
|
|
|
|
if (integer_rounding_cut_helper_.ComputeCut(options, base_ct_,
|
|
|
|
|
ib_processor)) {
|
|
|
|
|
at_least_one_added |= PostprocessAndAddCut(
|
|
|
|
|
absl::StrCat(name, "_R"), integer_rounding_cut_helper_.Info(),
|
|
|
|
|
first_slack, integer_rounding_cut_helper_.cut());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options.use_ib_before_heuristic = true;
|
|
|
|
|
options.prefer_positive_ib = false;
|
|
|
|
|
if (ib_processor != nullptr && integer_rounding_cut_helper_.ComputeCut(
|
|
|
|
|
options, base_ct_, ib_processor)) {
|
|
|
|
|
at_least_one_added |= PostprocessAndAddCut(
|
|
|
|
|
absl::StrCat(name, "_RB"), integer_rounding_cut_helper_.Info(),
|
|
|
|
|
first_slack, integer_rounding_cut_helper_.cut());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options.use_ib_before_heuristic = true;
|
|
|
|
|
options.prefer_positive_ib = true;
|
|
|
|
|
if (ib_processor != nullptr && integer_rounding_cut_helper_.ComputeCut(
|
|
|
|
|
options, base_ct_, ib_processor)) {
|
|
|
|
|
at_least_one_added |= PostprocessAndAddCut(
|
|
|
|
|
absl::StrCat(name, "_RBP"), integer_rounding_cut_helper_.Info(),
|
|
|
|
|
first_slack, integer_rounding_cut_helper_.cut());
|
2020-09-03 17:50:04 +02:00
|
|
|
}
|
2023-01-24 17:03:43 +01:00
|
|
|
|
2023-01-25 18:36:12 +01:00
|
|
|
// Try cover approach to find cut.
|
|
|
|
|
if (cover_cut_helper_.MakeAllTermsPositive(&base_ct_)) {
|
|
|
|
|
if (cover_cut_helper_.TrySimpleKnapsack(base_ct_, ib_processor)) {
|
|
|
|
|
at_least_one_added |= PostprocessAndAddCut(
|
|
|
|
|
absl::StrCat(name, "_KB"), cover_cut_helper_.Info(), first_slack,
|
|
|
|
|
cover_cut_helper_.cut());
|
|
|
|
|
}
|
|
|
|
|
if (cover_cut_helper_.TryWithLetchfordSouliLifting(base_ct_,
|
|
|
|
|
ib_processor)) {
|
|
|
|
|
at_least_one_added |= PostprocessAndAddCut(
|
|
|
|
|
absl::StrCat(name, "_KL"), cover_cut_helper_.Info(), first_slack,
|
|
|
|
|
cover_cut_helper_.cut());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 17:50:04 +02:00
|
|
|
return at_least_one_added;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LinearProgrammingConstraint::PostprocessAndAddCut(
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::string& name, const std::string& info,
|
2023-01-24 17:03:43 +01:00
|
|
|
IntegerVariable first_slack, const LinearConstraint& cut) {
|
2019-01-17 16:12:52 +01:00
|
|
|
// Substitute any slack left.
|
2023-02-01 17:46:22 +01:00
|
|
|
tmp_scattered_vector_.ClearAndResize(integer_variables_.size());
|
|
|
|
|
IntegerValue cut_ub = cut.ub;
|
|
|
|
|
bool overflow = false;
|
|
|
|
|
for (int i = 0; i < cut.vars.size(); ++i) {
|
|
|
|
|
const IntegerVariable var = cut.vars[i];
|
2019-11-18 10:06:11 -08:00
|
|
|
|
2023-02-01 17:46:22 +01:00
|
|
|
// Simple copy for non-slack variables.
|
|
|
|
|
if (var < first_slack) {
|
|
|
|
|
const glop::ColIndex col = mirror_lp_variable_.at(PositiveVariable(var));
|
|
|
|
|
if (VariableIsPositive(var)) {
|
|
|
|
|
tmp_scattered_vector_.Add(col, cut.coeffs[i]);
|
|
|
|
|
} else {
|
|
|
|
|
tmp_scattered_vector_.Add(col, -cut.coeffs[i]);
|
2019-11-18 10:06:11 -08:00
|
|
|
}
|
2023-02-01 17:46:22 +01:00
|
|
|
continue;
|
2019-01-17 16:12:52 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:46:22 +01:00
|
|
|
// Replace slack from LP constraints.
|
|
|
|
|
const int slack_index = (var.value() - first_slack.value()) / 2;
|
|
|
|
|
const glop::RowIndex row = tmp_slack_rows_[slack_index];
|
|
|
|
|
const IntegerValue multiplier = cut.coeffs[i];
|
|
|
|
|
if (!tmp_scattered_vector_.AddLinearExpressionMultiple(
|
|
|
|
|
multiplier, integer_lp_[row].terms)) {
|
|
|
|
|
overflow = true;
|
|
|
|
|
break;
|
2019-01-17 16:12:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:46:22 +01:00
|
|
|
if (overflow) {
|
|
|
|
|
VLOG(1) << "Overflow in slack removal.";
|
|
|
|
|
return false;
|
2019-01-17 16:12:52 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:46:22 +01:00
|
|
|
tmp_scattered_vector_.ConvertToLinearConstraint(integer_variables_, cut_ub,
|
|
|
|
|
&cut_);
|
2023-01-24 17:03:43 +01:00
|
|
|
DivideByGCD(&cut_);
|
|
|
|
|
return constraint_manager_.AddCut(cut_, name, expanded_lp_solution_, info);
|
2019-01-17 16:12:52 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-14 10:32:45 +01:00
|
|
|
// TODO(user): This can be still too slow on some problems like
|
|
|
|
|
// 30_70_45_05_100.mps.gz. Not this actual function, but the set of computation
|
|
|
|
|
// it triggers. We should add heuristics to abort earlier if a cut is not
|
|
|
|
|
// promising. Or only test a few positions and not all rows.
|
2019-01-09 11:50:34 +01:00
|
|
|
void LinearProgrammingConstraint::AddCGCuts() {
|
|
|
|
|
const RowIndex num_rows = lp_data_.num_constraints();
|
|
|
|
|
for (RowIndex row(0); row < num_rows; ++row) {
|
|
|
|
|
ColIndex basis_col = simplex_.GetBasis(row);
|
|
|
|
|
const Fractional lp_value = GetVariableValueAtCpScale(basis_col);
|
|
|
|
|
|
2021-01-14 10:32:45 +01:00
|
|
|
// Only consider fractional basis element. We ignore element that are close
|
|
|
|
|
// to an integer to reduce the amount of positions we try.
|
|
|
|
|
//
|
2019-01-09 11:50:34 +01:00
|
|
|
// TODO(user): We could just look at the diff with std::floor() in the hope
|
|
|
|
|
// that when we are just under an integer, the exact computation below will
|
|
|
|
|
// also be just under it.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (std::abs(lp_value - std::round(lp_value)) < 0.01) continue;
|
2019-01-09 11:50:34 +01:00
|
|
|
|
|
|
|
|
// If this variable is a slack, we ignore it. This is because the
|
|
|
|
|
// corresponding row is not tight under the given lp values.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (basis_col >= integer_variables_.size()) continue;
|
2019-01-09 11:50:34 +01:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
if (time_limit_->LimitReached()) break;
|
2020-10-18 16:38:25 +02:00
|
|
|
|
2021-01-14 10:32:45 +01:00
|
|
|
// TODO(user): Avoid code duplication between the sparse/dense path.
|
2019-01-09 11:50:34 +01:00
|
|
|
double magnitude = 0.0;
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_lp_multipliers_.clear();
|
2021-01-14 10:32:45 +01:00
|
|
|
const glop::ScatteredRow& lambda = simplex_.GetUnitRowLeftInverse(row);
|
|
|
|
|
if (lambda.non_zeros.empty()) {
|
|
|
|
|
for (RowIndex row(0); row < num_rows; ++row) {
|
|
|
|
|
const double value = lambda.values[glop::RowToColIndex(row)];
|
|
|
|
|
if (std::abs(value) < kZeroTolerance) continue;
|
2019-01-09 11:50:34 +01:00
|
|
|
|
2021-01-14 10:32:45 +01:00
|
|
|
// There should be no BASIC status, but they could be imprecision
|
|
|
|
|
// in the GetUnitRowLeftInverse() code? not sure, so better be safe.
|
|
|
|
|
const auto status = simplex_.GetConstraintStatus(row);
|
|
|
|
|
if (status == glop::ConstraintStatus::BASIC) {
|
|
|
|
|
VLOG(1) << "BASIC row not expected! " << value;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
magnitude = std::max(magnitude, std::abs(value));
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_lp_multipliers_.push_back({row, value});
|
2019-01-09 11:50:34 +01:00
|
|
|
}
|
2021-01-14 10:32:45 +01:00
|
|
|
} else {
|
|
|
|
|
for (const ColIndex col : lambda.non_zeros) {
|
|
|
|
|
const RowIndex row = glop::ColToRowIndex(col);
|
|
|
|
|
const double value = lambda.values[col];
|
|
|
|
|
if (std::abs(value) < kZeroTolerance) continue;
|
2019-01-09 11:50:34 +01:00
|
|
|
|
2021-01-14 10:32:45 +01:00
|
|
|
const auto status = simplex_.GetConstraintStatus(row);
|
|
|
|
|
if (status == glop::ConstraintStatus::BASIC) {
|
|
|
|
|
VLOG(1) << "BASIC row not expected! " << value;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
magnitude = std::max(magnitude, std::abs(value));
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_lp_multipliers_.push_back({row, value});
|
2021-01-14 10:32:45 +01:00
|
|
|
}
|
2019-01-09 11:50:34 +01:00
|
|
|
}
|
2022-01-17 16:31:41 +01:00
|
|
|
if (tmp_lp_multipliers_.empty()) continue;
|
2019-01-09 11:50:34 +01:00
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
IntegerValue scaling;
|
2019-11-18 10:06:11 -08:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
|
if (i == 1) {
|
|
|
|
|
// Try other sign.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): Maybe add an heuristic to know beforehand which sign to
|
|
|
|
|
// use?
|
2022-01-17 16:31:41 +01:00
|
|
|
for (std::pair<RowIndex, double>& p : tmp_lp_multipliers_) {
|
2021-01-14 10:32:45 +01:00
|
|
|
p.second = -p.second;
|
2019-11-18 10:06:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-03-01 11:11:36 +01:00
|
|
|
|
2019-11-18 10:06:11 -08:00
|
|
|
// TODO(user): We use a lower value here otherwise we might run into
|
|
|
|
|
// overflow while computing the cut. This should be fixable.
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_integer_multipliers_ =
|
2020-10-28 13:42:36 +01:00
|
|
|
ScaleLpMultiplier(/*take_objective_into_account=*/false,
|
2023-03-22 19:37:00 +01:00
|
|
|
tmp_lp_multipliers_, &scaling, int64_t{1} << 52);
|
|
|
|
|
if (scaling != 0) {
|
|
|
|
|
AddCutFromConstraints("CG", tmp_integer_multipliers_);
|
|
|
|
|
}
|
2019-11-18 10:06:11 -08:00
|
|
|
}
|
2019-01-17 16:12:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-09 11:50:34 +01:00
|
|
|
|
2019-12-05 16:36:11 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
template <class ListOfTerms>
|
2020-10-28 13:42:36 +01:00
|
|
|
IntegerValue GetCoeff(ColIndex col, const ListOfTerms& terms) {
|
|
|
|
|
for (const auto& term : terms) {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (term.first == col) return term.second;
|
2019-12-05 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
return IntegerValue(0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace
|
2019-12-05 16:36:11 +01:00
|
|
|
|
2021-11-19 17:57:56 +01:00
|
|
|
// Because we know the objective is integer, the constraint objective >= lb can
|
|
|
|
|
// sometime cut the current lp optimal, and it can make a big difference to add
|
|
|
|
|
// it. Or at least use it when constructing more advanced cuts. See
|
|
|
|
|
// 'multisetcover_batch_0_case_115_instance_0_small_subset_elements_3_sumreqs
|
|
|
|
|
// _1295_candidates_41.fzn'
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): It might be better to just integrate this with the MIR code so
|
|
|
|
|
// that we not only consider MIR1 involving the objective but we also consider
|
|
|
|
|
// combining it with other constraints.
|
|
|
|
|
void LinearProgrammingConstraint::AddObjectiveCut() {
|
|
|
|
|
if (integer_objective_.size() <= 1) return;
|
|
|
|
|
|
2022-01-17 16:31:41 +01:00
|
|
|
// We only try to add such cut if the LB objective is "far" from the current
|
|
|
|
|
// objective lower bound. Note that this is in term of the "internal" integer
|
|
|
|
|
// objective.
|
|
|
|
|
const double obj_lp_value = simplex_.GetObjectiveValue();
|
|
|
|
|
const IntegerValue obj_lower_bound =
|
|
|
|
|
integer_trail_->LevelZeroLowerBound(objective_cp_);
|
|
|
|
|
if (obj_lp_value + 1.0 >= ToDouble(obj_lower_bound)) return;
|
|
|
|
|
|
2021-11-19 17:57:56 +01:00
|
|
|
// We negate everything to have a <= base constraint.
|
2022-01-17 16:31:41 +01:00
|
|
|
LinearConstraint objective_ct;
|
|
|
|
|
objective_ct.lb = kMinIntegerValue;
|
|
|
|
|
objective_ct.ub = integer_objective_offset_ -
|
|
|
|
|
integer_trail_->LevelZeroLowerBound(objective_cp_);
|
|
|
|
|
IntegerValue obj_coeff_magnitude(0);
|
2021-11-19 17:57:56 +01:00
|
|
|
for (const auto& [col, coeff] : integer_objective_) {
|
|
|
|
|
const IntegerVariable var = integer_variables_[col.value()];
|
2022-01-17 16:31:41 +01:00
|
|
|
objective_ct.vars.push_back(var);
|
|
|
|
|
objective_ct.coeffs.push_back(-coeff);
|
|
|
|
|
obj_coeff_magnitude = std::max(obj_coeff_magnitude, IntTypeAbs(coeff));
|
2021-11-19 17:57:56 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-17 16:31:41 +01:00
|
|
|
// If the magnitude is small enough, just try to add the full objective. Other
|
|
|
|
|
// cuts will be derived in subsequent passes. Otherwise, try normal cut
|
|
|
|
|
// heuristic that should result in a cut with reasonable coefficients.
|
2023-01-16 13:26:55 +01:00
|
|
|
if (obj_coeff_magnitude < 1e9 &&
|
|
|
|
|
constraint_manager_.AddCut(objective_ct, "Objective",
|
|
|
|
|
expanded_lp_solution_)) {
|
|
|
|
|
return;
|
2022-01-17 16:31:41 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:26:55 +01:00
|
|
|
if (!base_ct_.FillFromLinearConstraint(objective_ct, expanded_lp_solution_,
|
|
|
|
|
integer_trail_)) {
|
|
|
|
|
return;
|
2022-01-17 16:31:41 +01:00
|
|
|
}
|
2021-11-19 17:57:56 +01:00
|
|
|
|
2023-01-16 13:26:55 +01:00
|
|
|
// Try knapsack.
|
|
|
|
|
if (cover_cut_helper_.TrySimpleKnapsack(base_ct_)) {
|
|
|
|
|
constraint_manager_.AddCut(cover_cut_helper_.cut(), "Objective_K",
|
|
|
|
|
expanded_lp_solution_);
|
|
|
|
|
}
|
2022-01-17 16:31:41 +01:00
|
|
|
|
2023-01-16 13:26:55 +01:00
|
|
|
// Try rounding.
|
|
|
|
|
RoundingOptions options;
|
|
|
|
|
options.max_scaling = parameters_.max_integer_rounding_scaling();
|
|
|
|
|
if (integer_rounding_cut_helper_.ComputeCut(options, base_ct_,
|
|
|
|
|
&implied_bounds_processor_)) {
|
|
|
|
|
constraint_manager_.AddCut(integer_rounding_cut_helper_.cut(),
|
|
|
|
|
"Objective_R", expanded_lp_solution_);
|
2022-01-17 16:31:41 +01:00
|
|
|
}
|
2021-11-19 17:57:56 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-17 16:12:52 +01:00
|
|
|
void LinearProgrammingConstraint::AddMirCuts() {
|
2020-01-07 16:51:46 +01:00
|
|
|
// Heuristic to generate MIR_n cuts by combining a small number of rows. This
|
|
|
|
|
// works greedily and follow more or less the MIR cut description in the
|
|
|
|
|
// literature. We have a current cut, and we add one more row to it while
|
|
|
|
|
// eliminating a variable of the current cut whose LP value is far from its
|
|
|
|
|
// bound.
|
|
|
|
|
//
|
|
|
|
|
// A notable difference is that we randomize the variable we eliminate and
|
|
|
|
|
// the row we use to do so. We still have weights to indicate our preferred
|
|
|
|
|
// choices. This allows to generate different cuts when called again and
|
|
|
|
|
// again.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): We could combine n rows to make sure we eliminate n variables
|
|
|
|
|
// far away from their bounds by solving exactly in integer small linear
|
|
|
|
|
// system.
|
2020-11-19 00:17:26 +01:00
|
|
|
absl::StrongVector<ColIndex, IntegerValue> dense_cut(
|
|
|
|
|
integer_variables_.size(), IntegerValue(0));
|
2020-09-21 09:44:39 +02:00
|
|
|
SparseBitset<ColIndex> non_zeros_(ColIndex(integer_variables_.size()));
|
2020-01-07 16:51:46 +01:00
|
|
|
|
|
|
|
|
// We compute all the rows that are tight, these will be used as the base row
|
|
|
|
|
// for the MIR_n procedure below.
|
2023-01-27 16:53:53 +01:00
|
|
|
const int num_rows = lp_data_.num_constraints().value();
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::pair<RowIndex, IntegerValue>> base_rows;
|
2023-01-27 16:53:53 +01:00
|
|
|
absl::StrongVector<RowIndex, double> row_weights(num_rows, 0.0);
|
|
|
|
|
absl::StrongVector<RowIndex, bool> at_ub(num_rows, false);
|
|
|
|
|
absl::StrongVector<RowIndex, bool> at_lb(num_rows, false);
|
2019-01-17 16:12:52 +01:00
|
|
|
for (RowIndex row(0); row < num_rows; ++row) {
|
2023-01-27 16:53:53 +01:00
|
|
|
// We only consider tight rows.
|
|
|
|
|
// We use both the status and activity to have as much options as possible.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): shall we consider rows that are not tight?
|
2019-01-17 16:12:52 +01:00
|
|
|
const auto status = simplex_.GetConstraintStatus(row);
|
2023-01-27 16:53:53 +01:00
|
|
|
const double activity = simplex_.GetConstraintActivity(row);
|
|
|
|
|
if (activity > lp_data_.constraint_upper_bounds()[row] - 1e-4 ||
|
|
|
|
|
status == glop::ConstraintStatus::AT_UPPER_BOUND ||
|
2019-11-18 10:06:11 -08:00
|
|
|
status == glop::ConstraintStatus::FIXED_VALUE) {
|
2023-01-27 16:53:53 +01:00
|
|
|
at_ub[row] = true;
|
2020-10-22 23:36:58 +02:00
|
|
|
base_rows.push_back({row, IntegerValue(1)});
|
2019-11-18 10:06:11 -08:00
|
|
|
}
|
2023-01-27 16:53:53 +01:00
|
|
|
if (activity < lp_data_.constraint_lower_bounds()[row] + 1e-4 ||
|
|
|
|
|
status == glop::ConstraintStatus::AT_LOWER_BOUND ||
|
2019-11-18 10:06:11 -08:00
|
|
|
status == glop::ConstraintStatus::FIXED_VALUE) {
|
2023-01-27 16:53:53 +01:00
|
|
|
at_lb[row] = true;
|
2020-10-22 23:36:58 +02:00
|
|
|
base_rows.push_back({row, IntegerValue(-1)});
|
2019-11-18 10:06:11 -08:00
|
|
|
}
|
2020-01-07 16:51:46 +01:00
|
|
|
|
|
|
|
|
// For now, we use the dual values for the row "weights".
|
|
|
|
|
//
|
|
|
|
|
// Note that we use the dual at LP scale so that it make more sense when we
|
|
|
|
|
// compare different rows since the LP has been scaled.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): In Kati Wolter PhD "Implementation of Cutting Plane
|
|
|
|
|
// Separators for Mixed Integer Programs" which describe SCIP's MIR cuts
|
|
|
|
|
// implementation (or at least an early version of it), a more complex score
|
|
|
|
|
// is used.
|
2020-06-03 12:07:07 +02:00
|
|
|
//
|
|
|
|
|
// Note(user): Because we only consider tight rows under the current lp
|
|
|
|
|
// solution (i.e. non-basic rows), most should have a non-zero dual values.
|
|
|
|
|
// But there is some degenerate problem where these rows have a really low
|
|
|
|
|
// weight (or even zero), and having only weight of exactly zero in
|
|
|
|
|
// std::discrete_distribution will result in a crash.
|
|
|
|
|
row_weights[row] = std::max(1e-8, std::abs(simplex_.GetDualValue(row)));
|
2019-01-09 11:50:34 +01:00
|
|
|
}
|
2019-12-05 16:36:11 +01:00
|
|
|
|
2020-01-07 16:51:46 +01:00
|
|
|
std::vector<double> weights;
|
2020-11-19 00:17:26 +01:00
|
|
|
absl::StrongVector<RowIndex, bool> used_rows;
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::pair<RowIndex, IntegerValue>> integer_multipliers;
|
|
|
|
|
for (const std::pair<RowIndex, IntegerValue>& entry : base_rows) {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (time_limit_->LimitReached()) break;
|
2020-10-18 16:38:25 +02:00
|
|
|
|
2020-01-07 16:51:46 +01:00
|
|
|
// First try to generate a cut directly from this base row (MIR1).
|
|
|
|
|
//
|
|
|
|
|
// Note(user): We abort on success like it seems to be done in the
|
|
|
|
|
// literature. Note that we don't succeed that often in generating an
|
|
|
|
|
// efficient cut, so I am not sure aborting will make a big difference
|
|
|
|
|
// speedwise. We might generate similar cuts though, but hopefully the cut
|
|
|
|
|
// management can deal with that.
|
2020-10-22 23:36:58 +02:00
|
|
|
integer_multipliers = {entry};
|
2020-01-07 16:51:46 +01:00
|
|
|
if (AddCutFromConstraints("MIR_1", integer_multipliers)) {
|
2019-12-05 16:36:11 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 16:51:46 +01:00
|
|
|
// Cleanup.
|
2020-09-21 09:44:39 +02:00
|
|
|
for (const ColIndex col : non_zeros_.PositionsSetAtLeastOnce()) {
|
2020-01-07 16:51:46 +01:00
|
|
|
dense_cut[col] = IntegerValue(0);
|
|
|
|
|
}
|
2020-09-21 09:44:39 +02:00
|
|
|
non_zeros_.SparseClearAll();
|
2020-01-07 16:51:46 +01:00
|
|
|
|
|
|
|
|
// Copy cut.
|
|
|
|
|
const IntegerValue multiplier = entry.second;
|
2022-02-24 14:17:43 +01:00
|
|
|
for (const std::pair<ColIndex, IntegerValue>& term :
|
2020-01-07 16:51:46 +01:00
|
|
|
integer_lp_[entry.first].terms) {
|
|
|
|
|
const ColIndex col = term.first;
|
|
|
|
|
const IntegerValue coeff = term.second;
|
2020-09-21 09:44:39 +02:00
|
|
|
non_zeros_.Set(col);
|
2020-01-07 16:51:46 +01:00
|
|
|
dense_cut[col] += coeff * multiplier;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 16:53:53 +01:00
|
|
|
used_rows.assign(num_rows, false);
|
2020-01-07 16:51:46 +01:00
|
|
|
used_rows[entry.first] = true;
|
|
|
|
|
|
|
|
|
|
// We will aggregate at most kMaxAggregation more rows.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): optim + tune.
|
|
|
|
|
const int kMaxAggregation = 5;
|
|
|
|
|
for (int i = 0; i < kMaxAggregation; ++i) {
|
|
|
|
|
// First pick a variable to eliminate. We currently pick a random one with
|
|
|
|
|
// a weight that depend on how far it is from its closest bound.
|
|
|
|
|
IntegerValue max_magnitude(0);
|
|
|
|
|
weights.clear();
|
|
|
|
|
std::vector<ColIndex> col_candidates;
|
2020-09-21 09:44:39 +02:00
|
|
|
for (const ColIndex col : non_zeros_.PositionsSetAtLeastOnce()) {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (dense_cut[col] == 0) continue;
|
2020-01-07 16:51:46 +01:00
|
|
|
|
|
|
|
|
max_magnitude = std::max(max_magnitude, IntTypeAbs(dense_cut[col]));
|
|
|
|
|
const int col_degree =
|
|
|
|
|
lp_data_.GetSparseColumn(col).num_entries().value();
|
2020-10-22 23:36:58 +02:00
|
|
|
if (col_degree <= 1) continue;
|
2020-01-07 16:51:46 +01:00
|
|
|
if (simplex_.GetVariableStatus(col) != glop::VariableStatus::BASIC) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const IntegerVariable var = integer_variables_[col.value()];
|
|
|
|
|
const double lp_value = expanded_lp_solution_[var];
|
2020-11-16 17:01:21 +01:00
|
|
|
const double lb = ToDouble(integer_trail_->LevelZeroLowerBound(var));
|
|
|
|
|
const double ub = ToDouble(integer_trail_->LevelZeroUpperBound(var));
|
2020-01-07 16:51:46 +01:00
|
|
|
const double bound_distance = std::min(ub - lp_value, lp_value - lb);
|
|
|
|
|
if (bound_distance > 1e-2) {
|
|
|
|
|
weights.push_back(bound_distance);
|
|
|
|
|
col_candidates.push_back(col);
|
2019-12-05 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-22 23:36:58 +02:00
|
|
|
if (col_candidates.empty()) break;
|
2020-01-07 16:51:46 +01:00
|
|
|
|
|
|
|
|
const ColIndex var_to_eliminate =
|
|
|
|
|
col_candidates[std::discrete_distribution<>(weights.begin(),
|
|
|
|
|
weights.end())(*random_)];
|
|
|
|
|
|
|
|
|
|
// What rows can we add to eliminate var_to_eliminate?
|
|
|
|
|
std::vector<RowIndex> possible_rows;
|
|
|
|
|
weights.clear();
|
|
|
|
|
for (const auto entry : lp_data_.GetSparseColumn(var_to_eliminate)) {
|
|
|
|
|
const RowIndex row = entry.row();
|
|
|
|
|
|
2020-10-06 17:57:20 +02:00
|
|
|
// We disallow all the rows that contain a variable that we already
|
2020-01-07 16:51:46 +01:00
|
|
|
// eliminated (or are about to). This mean that we choose rows that
|
|
|
|
|
// form a "triangular" matrix on the position we choose to eliminate.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (used_rows[row]) continue;
|
2020-01-07 16:51:46 +01:00
|
|
|
used_rows[row] = true;
|
|
|
|
|
|
2023-01-27 16:53:53 +01:00
|
|
|
// We only consider "tight" rows, as defined above.
|
2020-01-07 16:51:46 +01:00
|
|
|
bool add_row = false;
|
2023-01-27 16:53:53 +01:00
|
|
|
if (at_ub[row]) {
|
2020-01-07 16:51:46 +01:00
|
|
|
if (entry.coefficient() > 0.0) {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (dense_cut[var_to_eliminate] < 0) add_row = true;
|
2020-01-07 16:51:46 +01:00
|
|
|
} else {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (dense_cut[var_to_eliminate] > 0) add_row = true;
|
2020-01-07 16:51:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-27 16:53:53 +01:00
|
|
|
if (at_lb[row]) {
|
2020-01-07 16:51:46 +01:00
|
|
|
if (entry.coefficient() > 0.0) {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (dense_cut[var_to_eliminate] > 0) add_row = true;
|
2020-01-07 16:51:46 +01:00
|
|
|
} else {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (dense_cut[var_to_eliminate] < 0) add_row = true;
|
2020-01-07 16:51:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (add_row) {
|
|
|
|
|
possible_rows.push_back(row);
|
|
|
|
|
weights.push_back(row_weights[row]);
|
2019-12-05 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-22 23:36:58 +02:00
|
|
|
if (possible_rows.empty()) break;
|
2020-01-07 16:51:46 +01:00
|
|
|
|
|
|
|
|
const RowIndex row_to_combine =
|
|
|
|
|
possible_rows[std::discrete_distribution<>(weights.begin(),
|
|
|
|
|
weights.end())(*random_)];
|
|
|
|
|
const IntegerValue to_combine_coeff =
|
|
|
|
|
GetCoeff(var_to_eliminate, integer_lp_[row_to_combine].terms);
|
|
|
|
|
CHECK_NE(to_combine_coeff, 0);
|
|
|
|
|
|
|
|
|
|
IntegerValue mult1 = -to_combine_coeff;
|
|
|
|
|
IntegerValue mult2 = dense_cut[var_to_eliminate];
|
|
|
|
|
CHECK_NE(mult2, 0);
|
|
|
|
|
if (mult1 < 0) {
|
|
|
|
|
mult1 = -mult1;
|
|
|
|
|
mult2 = -mult2;
|
|
|
|
|
}
|
2019-12-05 16:36:11 +01:00
|
|
|
|
|
|
|
|
const IntegerValue gcd = IntegerValue(
|
2020-01-07 16:51:46 +01:00
|
|
|
MathUtil::GCD64(std::abs(mult1.value()), std::abs(mult2.value())));
|
|
|
|
|
CHECK_NE(gcd, 0);
|
|
|
|
|
mult1 /= gcd;
|
|
|
|
|
mult2 /= gcd;
|
|
|
|
|
|
|
|
|
|
// Overflow detection.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): do that in the possible_rows selection? only problem is
|
|
|
|
|
// that we do not have the integer coefficient there...
|
2020-10-28 13:42:36 +01:00
|
|
|
for (std::pair<RowIndex, IntegerValue>& entry : integer_multipliers) {
|
2021-09-14 17:32:15 +02:00
|
|
|
max_magnitude = std::max(max_magnitude, IntTypeAbs(entry.second));
|
2020-10-06 17:57:20 +02:00
|
|
|
}
|
2020-01-07 16:51:46 +01:00
|
|
|
if (CapAdd(CapProd(max_magnitude.value(), std::abs(mult1.value())),
|
|
|
|
|
CapProd(infinity_norms_[row_to_combine].value(),
|
2021-03-04 18:26:01 +01:00
|
|
|
std::abs(mult2.value()))) ==
|
|
|
|
|
std::numeric_limits<int64_t>::max()) {
|
2020-01-07 16:51:46 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
for (std::pair<RowIndex, IntegerValue>& entry : integer_multipliers) {
|
2020-01-07 16:51:46 +01:00
|
|
|
entry.second *= mult1;
|
|
|
|
|
}
|
2020-10-22 23:36:58 +02:00
|
|
|
integer_multipliers.push_back({row_to_combine, mult2});
|
2020-01-07 16:51:46 +01:00
|
|
|
|
|
|
|
|
// TODO(user): Not supper efficient to recombine the rows.
|
|
|
|
|
if (AddCutFromConstraints(absl::StrCat("MIR_", i + 2),
|
|
|
|
|
integer_multipliers)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Minor optim: the computation below is only needed if we do one more
|
|
|
|
|
// iteration.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (i + 1 == kMaxAggregation) break;
|
2020-01-07 16:51:46 +01:00
|
|
|
|
2020-09-21 09:44:39 +02:00
|
|
|
for (ColIndex col : non_zeros_.PositionsSetAtLeastOnce()) {
|
2020-01-07 16:51:46 +01:00
|
|
|
dense_cut[col] *= mult1;
|
|
|
|
|
}
|
2022-02-24 14:17:43 +01:00
|
|
|
for (const std::pair<ColIndex, IntegerValue>& term :
|
2020-01-07 16:51:46 +01:00
|
|
|
integer_lp_[row_to_combine].terms) {
|
|
|
|
|
const ColIndex col = term.first;
|
|
|
|
|
const IntegerValue coeff = term.second;
|
2020-09-21 09:44:39 +02:00
|
|
|
non_zeros_.Set(col);
|
2020-01-07 16:51:46 +01:00
|
|
|
dense_cut[col] += coeff * mult2;
|
|
|
|
|
}
|
2019-12-05 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-09 11:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-03 09:08:10 +02:00
|
|
|
void LinearProgrammingConstraint::AddZeroHalfCuts() {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (time_limit_->LimitReached()) return;
|
2020-09-03 09:08:10 +02:00
|
|
|
|
|
|
|
|
tmp_lp_values_.clear();
|
|
|
|
|
tmp_var_lbs_.clear();
|
|
|
|
|
tmp_var_ubs_.clear();
|
|
|
|
|
for (const IntegerVariable var : integer_variables_) {
|
|
|
|
|
tmp_lp_values_.push_back(expanded_lp_solution_[var]);
|
2020-11-16 17:01:21 +01:00
|
|
|
tmp_var_lbs_.push_back(integer_trail_->LevelZeroLowerBound(var));
|
|
|
|
|
tmp_var_ubs_.push_back(integer_trail_->LevelZeroUpperBound(var));
|
2020-09-03 09:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(user): See if it make sense to try to use implied bounds there.
|
|
|
|
|
zero_half_cut_helper_.ProcessVariables(tmp_lp_values_, tmp_var_lbs_,
|
|
|
|
|
tmp_var_ubs_);
|
|
|
|
|
for (glop::RowIndex row(0); row < integer_lp_.size(); ++row) {
|
|
|
|
|
// Even though we could use non-tight row, for now we prefer to use tight
|
|
|
|
|
// ones.
|
|
|
|
|
const auto status = simplex_.GetConstraintStatus(row);
|
2020-10-22 23:36:58 +02:00
|
|
|
if (status == glop::ConstraintStatus::BASIC) continue;
|
|
|
|
|
if (status == glop::ConstraintStatus::FREE) continue;
|
2020-09-03 09:08:10 +02:00
|
|
|
|
|
|
|
|
zero_half_cut_helper_.AddOneConstraint(
|
|
|
|
|
row, integer_lp_[row].terms, integer_lp_[row].lb, integer_lp_[row].ub);
|
|
|
|
|
}
|
2020-10-28 13:42:36 +01:00
|
|
|
for (const std::vector<std::pair<RowIndex, IntegerValue>>& multipliers :
|
2020-09-03 09:08:10 +02:00
|
|
|
zero_half_cut_helper_.InterestingCandidates(random_)) {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (time_limit_->LimitReached()) break;
|
2020-10-18 16:38:25 +02:00
|
|
|
|
2020-09-03 09:08:10 +02:00
|
|
|
// TODO(user): Make sure that if the resulting linear coefficients are not
|
|
|
|
|
// too high, we do try a "divisor" of two and thus try a true zero-half cut
|
|
|
|
|
// instead of just using our best MIR heuristic (which might still be better
|
|
|
|
|
// though).
|
|
|
|
|
AddCutFromConstraints("ZERO_HALF", multipliers);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
void LinearProgrammingConstraint::UpdateSimplexIterationLimit(
|
2021-03-04 18:26:01 +01:00
|
|
|
const int64_t min_iter, const int64_t max_iter) {
|
2021-11-19 17:57:56 +01:00
|
|
|
if (parameters_.linearization_level() < 2) return;
|
2021-03-04 18:26:01 +01:00
|
|
|
const int64_t num_degenerate_columns = CalculateDegeneracy();
|
|
|
|
|
const int64_t num_cols = simplex_.GetProblemNumCols().value();
|
2019-05-29 22:39:15 +02:00
|
|
|
if (num_cols <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CHECK_GT(num_cols, 0);
|
2021-03-04 18:26:01 +01:00
|
|
|
const int64_t decrease_factor = (10 * num_degenerate_columns) / num_cols;
|
2019-05-23 13:53:26 +02:00
|
|
|
if (simplex_.GetProblemStatus() == glop::ProblemStatus::DUAL_FEASIBLE) {
|
|
|
|
|
// We reached here probably because we predicted wrong. We use this as a
|
|
|
|
|
// signal to increase the iterations or punish less for degeneracy compare
|
|
|
|
|
// to the other part.
|
2019-08-22 13:15:49 +02:00
|
|
|
if (is_degenerate_) {
|
2021-03-04 18:26:01 +01:00
|
|
|
next_simplex_iter_ /= std::max(int64_t{1}, decrease_factor);
|
2019-05-23 13:53:26 +02:00
|
|
|
} else {
|
|
|
|
|
next_simplex_iter_ *= 2;
|
|
|
|
|
}
|
|
|
|
|
} else if (simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL) {
|
2019-08-22 13:15:49 +02:00
|
|
|
if (is_degenerate_) {
|
2021-03-04 18:26:01 +01:00
|
|
|
next_simplex_iter_ /= std::max(int64_t{1}, 2 * decrease_factor);
|
2019-05-23 13:53:26 +02:00
|
|
|
} else {
|
|
|
|
|
// This is the most common case. We use the size of the problem to
|
|
|
|
|
// determine the limit and ignore the previous limit.
|
|
|
|
|
next_simplex_iter_ = num_cols / 40;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
next_simplex_iter_ =
|
|
|
|
|
std::max(min_iter, std::min(max_iter, next_simplex_iter_));
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 11:36:18 +01:00
|
|
|
bool LinearProgrammingConstraint::Propagate() {
|
|
|
|
|
UpdateBoundsOfLpVariables();
|
2017-03-28 16:11:06 +02:00
|
|
|
|
2018-02-12 11:36:18 +01:00
|
|
|
// TODO(user): It seems the time we loose by not stopping early might be worth
|
|
|
|
|
// it because we end up with a better explanation at optimality.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (/* DISABLES CODE */ (false) && objective_is_defined_) {
|
2017-07-12 11:38:46 -07:00
|
|
|
// We put a limit on the dual objective since there is no point increasing
|
|
|
|
|
// it past our current objective upper-bound (we will already fail as soon
|
|
|
|
|
// as we pass it). Note that this limit is properly transformed using the
|
|
|
|
|
// objective scaling factor and offset stored in lp_data_.
|
2017-10-11 03:05:13 -07:00
|
|
|
//
|
|
|
|
|
// Note that we use a bigger epsilon here to be sure that if we abort
|
|
|
|
|
// because of this, we will report a conflict.
|
2023-01-27 16:53:53 +01:00
|
|
|
simplex_params_.set_objective_upper_limit(
|
2018-02-12 11:36:18 +01:00
|
|
|
static_cast<double>(integer_trail_->UpperBound(objective_cp_).value() +
|
|
|
|
|
100.0 * kCpEpsilon));
|
2017-07-12 11:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put an iteration limit on the work we do in the simplex for this call. Note
|
|
|
|
|
// that because we are "incremental", even if we don't solve it this time we
|
|
|
|
|
// will make progress towards a solve in the lower node of the tree search.
|
2019-05-03 16:30:50 +02:00
|
|
|
if (trail_->CurrentDecisionLevel() == 0) {
|
2023-01-27 16:53:53 +01:00
|
|
|
simplex_params_.set_max_number_of_iterations(
|
|
|
|
|
parameters_.root_lp_iterations());
|
2019-05-03 16:30:50 +02:00
|
|
|
} else {
|
2023-01-27 16:53:53 +01:00
|
|
|
simplex_params_.set_max_number_of_iterations(next_simplex_iter_);
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
2017-07-12 11:38:46 -07:00
|
|
|
|
2023-01-27 16:53:53 +01:00
|
|
|
simplex_.SetParameters(simplex_params_);
|
2020-10-22 23:36:58 +02:00
|
|
|
if (!SolveLp()) return true;
|
2023-02-01 17:46:22 +01:00
|
|
|
if (!AnalyzeLp()) return false;
|
2017-03-28 16:11:06 +02:00
|
|
|
|
2018-12-04 14:36:46 +01:00
|
|
|
// Add new constraints to the LP and resolve?
|
2022-06-29 17:04:58 +02:00
|
|
|
const int max_cuts_rounds = trail_->CurrentDecisionLevel() == 0
|
|
|
|
|
? parameters_.max_cut_rounds_at_level_zero()
|
|
|
|
|
: 1;
|
2020-03-02 17:26:45 +01:00
|
|
|
int cuts_round = 0;
|
|
|
|
|
while (simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL &&
|
|
|
|
|
cuts_round < max_cuts_rounds) {
|
2019-11-14 12:42:35 -08:00
|
|
|
// We wait for the first batch of problem constraints to be added before we
|
2021-04-30 14:20:46 +02:00
|
|
|
// begin to generate cuts. Note that we rely on num_solves_ since on some
|
2022-04-06 17:33:00 +02:00
|
|
|
// problems there is no other constraints than the cuts.
|
2020-03-02 17:26:45 +01:00
|
|
|
cuts_round++;
|
2022-06-29 17:04:58 +02:00
|
|
|
if (parameters_.cut_level() > 0 && num_solves_ > 1) {
|
2021-05-03 12:11:39 +02:00
|
|
|
// This must be called first.
|
|
|
|
|
implied_bounds_processor_.RecomputeCacheAndSeparateSomeImpliedBoundCuts(
|
2020-10-01 18:08:34 +02:00
|
|
|
expanded_lp_solution_);
|
|
|
|
|
|
2019-01-17 16:12:52 +01:00
|
|
|
// The "generic" cuts are currently part of this class as they are using
|
|
|
|
|
// data from the current LP.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): Refactor so that they are just normal cut generators?
|
2022-06-23 16:16:17 +02:00
|
|
|
const int level = trail_->CurrentDecisionLevel();
|
2019-01-17 16:12:52 +01:00
|
|
|
if (trail_->CurrentDecisionLevel() == 0) {
|
2023-04-21 12:48:03 +02:00
|
|
|
problem_proven_infeasible_by_cuts_ = false;
|
2021-11-19 17:57:56 +01:00
|
|
|
if (parameters_.add_objective_cut()) AddObjectiveCut();
|
|
|
|
|
if (parameters_.add_mir_cuts()) AddMirCuts();
|
|
|
|
|
if (parameters_.add_cg_cuts()) AddCGCuts();
|
|
|
|
|
if (parameters_.add_zero_half_cuts()) AddZeroHalfCuts();
|
2023-04-21 12:48:03 +02:00
|
|
|
if (problem_proven_infeasible_by_cuts_) {
|
|
|
|
|
return integer_trail_->ReportConflict({});
|
|
|
|
|
}
|
2019-01-09 11:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-04 14:36:46 +01:00
|
|
|
// Try to add cuts.
|
2022-06-23 16:16:17 +02:00
|
|
|
if (level == 0 || !parameters_.only_add_cuts_at_level_zero()) {
|
2020-10-28 13:42:36 +01:00
|
|
|
for (const CutGenerator& generator : cut_generators_) {
|
2022-06-23 16:16:17 +02:00
|
|
|
if (level > 0 && generator.only_run_at_level_zero) continue;
|
2021-06-04 08:35:20 +02:00
|
|
|
if (!generator.generate_cuts(expanded_lp_solution_,
|
|
|
|
|
&constraint_manager_)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-12-04 14:36:46 +01:00
|
|
|
}
|
2019-01-09 11:50:34 +01:00
|
|
|
}
|
2020-10-01 18:08:34 +02:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
implied_bounds_processor_.IbCutPool().TransferToManager(
|
|
|
|
|
expanded_lp_solution_, &constraint_manager_);
|
2019-11-14 12:42:35 -08:00
|
|
|
}
|
2018-12-04 14:36:46 +01:00
|
|
|
|
2023-02-01 17:46:22 +01:00
|
|
|
int num_added = 0;
|
2023-01-27 16:53:53 +01:00
|
|
|
state_ = simplex_.GetState();
|
2023-02-01 17:46:22 +01:00
|
|
|
if (constraint_manager_.ChangeLp(expanded_lp_solution_, &state_,
|
|
|
|
|
&num_added)) {
|
2023-01-27 16:53:53 +01:00
|
|
|
simplex_.LoadStateForNextSolve(state_);
|
2019-11-20 14:28:11 -08:00
|
|
|
if (!CreateLpFromConstraintManager()) {
|
2020-10-22 23:36:58 +02:00
|
|
|
return integer_trail_->ReportConflict({});
|
2019-11-20 14:28:11 -08:00
|
|
|
}
|
2023-02-01 17:46:22 +01:00
|
|
|
|
|
|
|
|
// If we didn't add any new constraint, we delay the next Solve() since
|
|
|
|
|
// likely the optimal didn't change.
|
|
|
|
|
if (num_added == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 12:42:35 -08:00
|
|
|
const double old_obj = simplex_.GetObjectiveValue();
|
2020-10-22 23:36:58 +02:00
|
|
|
if (!SolveLp()) return true;
|
2023-02-01 17:46:22 +01:00
|
|
|
if (!AnalyzeLp()) return false;
|
2019-11-14 12:42:35 -08:00
|
|
|
if (simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL) {
|
2023-02-01 17:46:22 +01:00
|
|
|
VLOG(3) << "Relaxation improvement " << old_obj << " -> "
|
2019-11-14 12:42:35 -08:00
|
|
|
<< simplex_.GetObjectiveValue()
|
|
|
|
|
<< " diff: " << simplex_.GetObjectiveValue() - old_obj
|
|
|
|
|
<< " level: " << trail_->CurrentDecisionLevel();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (trail_->CurrentDecisionLevel() == 0) {
|
|
|
|
|
lp_at_level_zero_is_final_ = true;
|
2018-12-04 14:36:46 +01:00
|
|
|
}
|
2020-03-02 17:26:45 +01:00
|
|
|
break;
|
2017-10-18 11:09:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 17:46:22 +01:00
|
|
|
// TODO(user): Is this the best place for this ?
|
2021-11-19 17:57:56 +01:00
|
|
|
if (parameters_.use_branching_in_lp() && objective_is_defined_ &&
|
2019-08-22 13:15:49 +02:00
|
|
|
trail_->CurrentDecisionLevel() == 0 && !is_degenerate_ &&
|
|
|
|
|
lp_solution_is_set_ && !lp_solution_is_integer_ &&
|
2021-11-19 17:57:56 +01:00
|
|
|
parameters_.linearization_level() >= 2 &&
|
2019-08-22 13:15:49 +02:00
|
|
|
compute_reduced_cost_averages_ &&
|
|
|
|
|
simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL) {
|
|
|
|
|
count_since_last_branching_++;
|
|
|
|
|
if (count_since_last_branching_ < branching_frequency_) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
count_since_last_branching_ = 0;
|
|
|
|
|
bool branching_successful = false;
|
|
|
|
|
|
|
|
|
|
// Strong branching on top max_num_branches variable.
|
|
|
|
|
const int max_num_branches = 3;
|
|
|
|
|
const int num_vars = integer_variables_.size();
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::pair<double, IntegerVariable>> branching_vars;
|
2019-08-22 13:15:49 +02:00
|
|
|
for (int i = 0; i < num_vars; ++i) {
|
|
|
|
|
const IntegerVariable var = integer_variables_[i];
|
|
|
|
|
const IntegerVariable positive_var = PositiveVariable(var);
|
|
|
|
|
|
|
|
|
|
// Skip non fractional variables.
|
|
|
|
|
const double current_value = GetSolutionValue(positive_var);
|
|
|
|
|
if (std::abs(current_value - std::round(current_value)) <= kCpEpsilon) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip ignored variables.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
2019-08-22 13:15:49 +02:00
|
|
|
|
|
|
|
|
// We can use any metric to select a variable to branch on. Reduced cost
|
|
|
|
|
// average is one of the most promissing metric. It captures the history
|
|
|
|
|
// of the objective bound improvement in LP due to changes in the given
|
|
|
|
|
// variable bounds.
|
|
|
|
|
//
|
2019-09-09 15:10:26 +02:00
|
|
|
// NOTE: We also experimented using PseudoCosts and most recent reduced
|
|
|
|
|
// cost as metrics but it doesn't give better results on benchmarks.
|
2020-03-05 17:24:49 +01:00
|
|
|
const double cost_i = rc_scores_[i];
|
2019-08-22 13:15:49 +02:00
|
|
|
std::pair<double, IntegerVariable> branching_var =
|
|
|
|
|
std::make_pair(-cost_i, positive_var);
|
|
|
|
|
auto iterator = std::lower_bound(branching_vars.begin(),
|
|
|
|
|
branching_vars.end(), branching_var);
|
|
|
|
|
|
|
|
|
|
branching_vars.insert(iterator, branching_var);
|
|
|
|
|
if (branching_vars.size() > max_num_branches) {
|
|
|
|
|
branching_vars.resize(max_num_branches);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
for (const std::pair<double, IntegerVariable>& branching_var :
|
2019-08-22 13:15:49 +02:00
|
|
|
branching_vars) {
|
|
|
|
|
const IntegerVariable positive_var = branching_var.second;
|
|
|
|
|
VLOG(2) << "Branching on: " << positive_var;
|
|
|
|
|
if (BranchOnVar(positive_var)) {
|
|
|
|
|
VLOG(2) << "Branching successful.";
|
|
|
|
|
branching_successful = true;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!branching_successful) {
|
|
|
|
|
branching_frequency_ *= 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-01 17:46:22 +01:00
|
|
|
|
2017-03-28 16:11:06 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
absl::int128 LinearProgrammingConstraint::GetImpliedLowerBound(
|
2020-10-28 13:42:36 +01:00
|
|
|
const LinearConstraint& terms) const {
|
2023-03-22 19:37:00 +01:00
|
|
|
absl::int128 lower_bound(0);
|
2019-01-09 11:50:34 +01:00
|
|
|
const int size = terms.vars.size();
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
const IntegerVariable var = terms.vars[i];
|
|
|
|
|
const IntegerValue coeff = terms.coeffs[i];
|
2018-11-05 16:24:47 +01:00
|
|
|
const IntegerValue bound = coeff > 0 ? integer_trail_->LowerBound(var)
|
|
|
|
|
: integer_trail_->UpperBound(var);
|
2023-03-22 19:37:00 +01:00
|
|
|
lower_bound += absl::int128(bound.value()) * absl::int128(coeff.value());
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
return lower_bound;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 12:28:55 +02:00
|
|
|
bool PossibleOverflow(const IntegerTrail& integer_trail,
|
|
|
|
|
const LinearConstraint& constraint) {
|
2018-12-21 13:59:58 +01:00
|
|
|
IntegerValue lower_bound(0);
|
2019-01-09 11:50:34 +01:00
|
|
|
const int size = constraint.vars.size();
|
2018-12-21 13:59:58 +01:00
|
|
|
for (int i = 0; i < size; ++i) {
|
2019-01-09 11:50:34 +01:00
|
|
|
const IntegerVariable var = constraint.vars[i];
|
|
|
|
|
const IntegerValue coeff = constraint.coeffs[i];
|
2018-12-21 13:59:58 +01:00
|
|
|
CHECK_NE(coeff, 0);
|
2020-11-16 17:01:21 +01:00
|
|
|
const IntegerValue bound = coeff > 0
|
2022-07-04 12:28:55 +02:00
|
|
|
? integer_trail.LevelZeroLowerBound(var)
|
|
|
|
|
: integer_trail.LevelZeroUpperBound(var);
|
2019-03-01 11:11:36 +01:00
|
|
|
if (!AddProductTo(bound, coeff, &lower_bound)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-12-21 13:59:58 +01:00
|
|
|
}
|
2022-07-04 12:28:55 +02:00
|
|
|
const int64_t slack = CapSub(constraint.ub.value(), lower_bound.value());
|
|
|
|
|
return slack == std::numeric_limits<int64_t>::min() ||
|
|
|
|
|
slack == std::numeric_limits<int64_t>::max();
|
2018-12-21 13:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-18 13:29:21 +02:00
|
|
|
namespace {
|
2019-04-18 14:17:35 +02:00
|
|
|
|
2019-04-18 13:29:21 +02:00
|
|
|
absl::int128 FloorRatio128(absl::int128 x, IntegerValue positive_div) {
|
|
|
|
|
absl::int128 div128(positive_div.value());
|
|
|
|
|
absl::int128 result = x / div128;
|
2020-10-22 23:36:58 +02:00
|
|
|
if (result * div128 > x) return result - 1;
|
2019-04-18 13:29:21 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 12:28:55 +02:00
|
|
|
absl::int128 CeilRatio128(absl::int128 x, absl::int128 div128) {
|
|
|
|
|
absl::int128 result = x / div128;
|
|
|
|
|
if (result * div128 < x) return result + 1;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-04-18 13:29:21 +02:00
|
|
|
|
2022-07-04 12:28:55 +02:00
|
|
|
// TODO(user): This code is tricky and similar to the one to generate cuts.
|
|
|
|
|
// Maybe reduce the duplication? note however that here we use int128 to deal
|
|
|
|
|
// with potential overflow.
|
|
|
|
|
void DivideConstraint(const IntegerTrail& integer_trail, IntegerValue divisor,
|
|
|
|
|
LinearConstraint* constraint) {
|
2019-04-18 13:29:21 +02:00
|
|
|
// To be correct, we need to shift all variable so that they are positive.
|
|
|
|
|
//
|
2019-11-25 12:02:25 +01:00
|
|
|
// Important: One might be tempted to think that using the current variable
|
|
|
|
|
// bounds is okay here since we only use this to derive cut/constraint that
|
|
|
|
|
// only needs to be locally valid. However, in some corner cases (like when
|
|
|
|
|
// one term become zero), we might loose the fact that we used one of the
|
|
|
|
|
// variable bound to derive the new constraint, so we will miss it in the
|
|
|
|
|
// explanation !!
|
2019-04-18 13:29:21 +02:00
|
|
|
int new_size = 0;
|
|
|
|
|
absl::int128 adjust = 0;
|
2022-07-04 12:28:55 +02:00
|
|
|
const int size = constraint->vars.size();
|
2019-04-18 13:29:21 +02:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
const IntegerValue old_coeff = constraint->coeffs[i];
|
|
|
|
|
const IntegerValue new_coeff = FloorRatio(old_coeff, divisor);
|
|
|
|
|
|
|
|
|
|
// Compute the rhs adjustement.
|
|
|
|
|
const absl::int128 remainder =
|
|
|
|
|
absl::int128(old_coeff.value()) -
|
|
|
|
|
absl::int128(new_coeff.value()) * absl::int128(divisor.value());
|
2020-10-22 23:36:58 +02:00
|
|
|
adjust +=
|
|
|
|
|
remainder *
|
|
|
|
|
absl::int128(
|
2022-07-04 12:28:55 +02:00
|
|
|
integer_trail.LevelZeroLowerBound(constraint->vars[i]).value());
|
2019-04-18 13:29:21 +02:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
if (new_coeff == 0) continue;
|
2019-04-18 13:29:21 +02:00
|
|
|
constraint->vars[new_size] = constraint->vars[i];
|
|
|
|
|
constraint->coeffs[new_size] = new_coeff;
|
|
|
|
|
++new_size;
|
|
|
|
|
}
|
|
|
|
|
constraint->vars.resize(new_size);
|
|
|
|
|
constraint->coeffs.resize(new_size);
|
|
|
|
|
|
2022-07-04 12:28:55 +02:00
|
|
|
// TODO(user): I am not 100% sure this cannot overflow. If it does it means
|
|
|
|
|
// our reduced constraint is trivial though, and we can cap it.
|
2021-03-04 18:26:01 +01:00
|
|
|
constraint->ub = IntegerValue(static_cast<int64_t>(
|
2019-04-18 13:29:21 +02:00
|
|
|
FloorRatio128(absl::int128(constraint->ub.value()) - adjust, divisor)));
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 12:28:55 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
// The goal here is to prevent overflow in the IntegerSumLE propagation code.
|
|
|
|
|
// We want to be as tight as possible. We do it in two steps, which is not ideal
|
|
|
|
|
// but easier.
|
2023-03-22 19:37:00 +01:00
|
|
|
bool PreventOverflow(const IntegerTrail& integer_trail,
|
2022-07-04 12:28:55 +02:00
|
|
|
LinearConstraint* constraint) {
|
2023-03-22 19:37:00 +01:00
|
|
|
bool prevented = false;
|
|
|
|
|
|
2022-07-04 12:28:55 +02:00
|
|
|
// We use kint64max - 1 so that PossibleOverflow() can distinguish overflow
|
|
|
|
|
// for a sum exactly equal to kint64max.
|
|
|
|
|
const absl::int128 threshold(std::numeric_limits<int64_t>::max() - 1);
|
|
|
|
|
|
|
|
|
|
// First, make all coefficient positive.
|
|
|
|
|
MakeAllCoefficientsPositive(constraint);
|
|
|
|
|
|
|
|
|
|
// First step is to make sure coeff * (ub - lb) and coeff * lb will not
|
|
|
|
|
// overflow. Note that we already know (ub - lb) cannot overflow.
|
|
|
|
|
{
|
|
|
|
|
absl::int128 max_delta = 0;
|
|
|
|
|
const int size = constraint->vars.size();
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
const IntegerVariable var = constraint->vars[i];
|
|
|
|
|
const IntegerValue lb = integer_trail.LevelZeroLowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail.LevelZeroUpperBound(var);
|
|
|
|
|
const absl::int128 coeff(constraint->coeffs[i].value());
|
|
|
|
|
const absl::int128 diff(
|
|
|
|
|
std::max({IntTypeAbs(lb), IntTypeAbs(ub), ub - lb}).value());
|
|
|
|
|
max_delta = std::max(max_delta, coeff * diff);
|
|
|
|
|
}
|
|
|
|
|
if (max_delta > threshold) {
|
2023-03-22 19:37:00 +01:00
|
|
|
prevented = true;
|
2022-07-04 12:28:55 +02:00
|
|
|
const IntegerValue divisor(
|
|
|
|
|
static_cast<int64_t>(CeilRatio128(max_delta, threshold)));
|
|
|
|
|
DivideConstraint(integer_trail, divisor, constraint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Second step is to make sure computing the lower bound will not overflow
|
|
|
|
|
// whatever the order and at whatever level. And also that computing the slack
|
|
|
|
|
// will not overflow.
|
|
|
|
|
//
|
|
|
|
|
// Note that because each term fit on an int64_t per first step, we will not
|
|
|
|
|
// have int128 overflow.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): We could change the propag to detect a conflict without
|
|
|
|
|
// computing the full activity, and thus avoid some overflow. Like precompute
|
|
|
|
|
// a base lb and then compute the activity from there? Or we could have
|
|
|
|
|
// a custom code here, actually we only propagate this with the current lb
|
|
|
|
|
// instead of the LevelZeroUpperBound(). Or we could just propagate using
|
|
|
|
|
// int128 arithmetic.
|
|
|
|
|
{
|
|
|
|
|
absl::int128 sum_min_neg = 0;
|
|
|
|
|
absl::int128 sum_min_pos = 0;
|
|
|
|
|
absl::int128 sum_max_neg = 0;
|
|
|
|
|
absl::int128 sum_max_pos = 0;
|
|
|
|
|
const int size = constraint->vars.size();
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
const IntegerVariable var = constraint->vars[i];
|
|
|
|
|
const absl::int128 coeff(constraint->coeffs[i].value());
|
|
|
|
|
const absl::int128 lb(integer_trail.LevelZeroLowerBound(var).value());
|
|
|
|
|
if (lb > 0) {
|
|
|
|
|
sum_min_pos += coeff * lb;
|
|
|
|
|
} else {
|
|
|
|
|
sum_min_neg += coeff * lb;
|
|
|
|
|
}
|
|
|
|
|
const absl::int128 ub(integer_trail.LevelZeroUpperBound(var).value());
|
|
|
|
|
if (ub > 0) {
|
|
|
|
|
sum_max_pos += coeff * ub;
|
|
|
|
|
} else {
|
|
|
|
|
sum_max_neg += coeff * ub;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const absl::int128 min_slack =
|
|
|
|
|
static_cast<absl::int128>(constraint->ub.value()) -
|
|
|
|
|
(sum_min_pos + sum_min_neg);
|
|
|
|
|
const absl::int128 max_slack =
|
|
|
|
|
static_cast<absl::int128>(constraint->ub.value()) -
|
|
|
|
|
(sum_max_pos + sum_max_neg);
|
|
|
|
|
const absl::int128 max_value =
|
|
|
|
|
std::max({-sum_min_neg, sum_min_pos, sum_min_pos + sum_min_neg,
|
|
|
|
|
-sum_max_neg, sum_max_pos, sum_max_pos + sum_max_neg,
|
|
|
|
|
min_slack, -min_slack, max_slack, -max_slack});
|
|
|
|
|
if (max_value > threshold) {
|
2023-03-22 19:37:00 +01:00
|
|
|
prevented = true;
|
2022-07-04 12:28:55 +02:00
|
|
|
const IntegerValue divisor(
|
|
|
|
|
static_cast<int64_t>(CeilRatio128(max_value, threshold)));
|
|
|
|
|
DivideConstraint(integer_trail, divisor, constraint);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-22 19:37:00 +01:00
|
|
|
|
|
|
|
|
return prevented;
|
2022-07-04 12:28:55 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-05 16:24:47 +01:00
|
|
|
// TODO(user): combine this with RelaxLinearReason() to avoid the extra
|
|
|
|
|
// magnitude vector and the weird precondition of RelaxLinearReason().
|
|
|
|
|
void LinearProgrammingConstraint::SetImpliedLowerBoundReason(
|
2020-10-28 13:42:36 +01:00
|
|
|
const LinearConstraint& terms, IntegerValue slack) {
|
2018-11-05 16:24:47 +01:00
|
|
|
integer_reason_.clear();
|
|
|
|
|
std::vector<IntegerValue> magnitudes;
|
2019-01-09 11:50:34 +01:00
|
|
|
const int size = terms.vars.size();
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
const IntegerVariable var = terms.vars[i];
|
|
|
|
|
const IntegerValue coeff = terms.coeffs[i];
|
2018-11-05 16:24:47 +01:00
|
|
|
CHECK_NE(coeff, 0);
|
|
|
|
|
if (coeff > 0) {
|
|
|
|
|
magnitudes.push_back(coeff);
|
|
|
|
|
integer_reason_.push_back(integer_trail_->LowerBoundAsLiteral(var));
|
|
|
|
|
} else {
|
|
|
|
|
magnitudes.push_back(-coeff);
|
|
|
|
|
integer_reason_.push_back(integer_trail_->UpperBoundAsLiteral(var));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CHECK_GE(slack, 0);
|
|
|
|
|
if (slack > 0) {
|
|
|
|
|
integer_trail_->RelaxLinearReason(slack, magnitudes, &integer_reason_);
|
|
|
|
|
}
|
|
|
|
|
integer_trail_->RemoveLevelZeroBounds(&integer_reason_);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
bool LinearProgrammingConstraint::ScalingCanOverflow(
|
|
|
|
|
int power, bool take_objective_into_account,
|
|
|
|
|
const std::vector<std::pair<glop::RowIndex, double>>& multipliers,
|
|
|
|
|
int64_t overflow_cap) const {
|
|
|
|
|
int64_t bound = 0;
|
|
|
|
|
for (const auto [row, double_coeff] : multipliers) {
|
|
|
|
|
const double magnitude =
|
|
|
|
|
std::abs(std::round(std::ldexp(double_coeff, power)));
|
|
|
|
|
if (std::isnan(magnitude)) return true;
|
|
|
|
|
if (magnitude >= static_cast<double>(std::numeric_limits<int64_t>::max())) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
bound = CapAdd(bound, CapProd(static_cast<int64_t>(magnitude),
|
|
|
|
|
infinity_norms_[row].value()));
|
|
|
|
|
if (bound >= overflow_cap) return true;
|
|
|
|
|
}
|
|
|
|
|
if (take_objective_into_account) {
|
|
|
|
|
bound = CapAdd(
|
|
|
|
|
bound, CapProd(int64_t{1} << power, objective_infinity_norm_.value()));
|
|
|
|
|
if (bound >= overflow_cap) return true;
|
|
|
|
|
}
|
|
|
|
|
return bound >= overflow_cap;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::pair<RowIndex, IntegerValue>>
|
2019-01-17 16:12:52 +01:00
|
|
|
LinearProgrammingConstraint::ScaleLpMultiplier(
|
2019-11-18 10:06:11 -08:00
|
|
|
bool take_objective_into_account,
|
2021-01-14 10:32:45 +01:00
|
|
|
const std::vector<std::pair<RowIndex, double>>& lp_multipliers,
|
2023-03-22 19:37:00 +01:00
|
|
|
IntegerValue* scaling, int64_t overflow_cap) const {
|
|
|
|
|
*scaling = 0;
|
|
|
|
|
|
|
|
|
|
// First unscale the values with the LP scaling and remove bad cases.
|
2021-01-14 10:32:45 +01:00
|
|
|
tmp_cp_multipliers_.clear();
|
|
|
|
|
for (const std::pair<RowIndex, double>& p : lp_multipliers) {
|
|
|
|
|
const RowIndex row = p.first;
|
|
|
|
|
const Fractional lp_multi = p.second;
|
2019-11-18 10:06:11 -08:00
|
|
|
|
|
|
|
|
// We ignore small values since these are likely errors and will not
|
|
|
|
|
// contribute much to the new lp constraint anyway.
|
2021-01-14 10:32:45 +01:00
|
|
|
if (std::abs(lp_multi) < kZeroTolerance) continue;
|
2018-11-05 16:24:47 +01:00
|
|
|
|
|
|
|
|
// Remove trivial bad cases.
|
2019-11-18 10:06:11 -08:00
|
|
|
//
|
|
|
|
|
// TODO(user): It might be better (when possible) to use the OPTIMAL row
|
|
|
|
|
// status since in most situation we do want the constraint we add to be
|
|
|
|
|
// tight under the current LP solution. Only for infeasible problem we might
|
|
|
|
|
// not have access to the status.
|
|
|
|
|
if (lp_multi > 0.0 && integer_lp_[row].ub >= kMaxIntegerValue) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (lp_multi < 0.0 && integer_lp_[row].lb <= kMinIntegerValue) {
|
|
|
|
|
continue;
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
tmp_cp_multipliers_.push_back(
|
|
|
|
|
{row, scaler_.UnscaleDualValue(row, lp_multi)});
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::pair<RowIndex, IntegerValue>> integer_multipliers;
|
2023-03-22 19:37:00 +01:00
|
|
|
if (tmp_cp_multipliers_.empty()) {
|
2019-11-20 14:28:11 -08:00
|
|
|
// Empty linear combinaison.
|
|
|
|
|
return integer_multipliers;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
// TODO(user): we currently do not support scaling down, so we just abort
|
|
|
|
|
// if with a scaling of 1, we reach the overflow_cap.
|
|
|
|
|
if (ScalingCanOverflow(/*power=*/0, take_objective_into_account,
|
|
|
|
|
tmp_cp_multipliers_, overflow_cap)) {
|
|
|
|
|
++num_scaling_issues_;
|
2020-10-06 17:57:20 +02:00
|
|
|
return integer_multipliers;
|
|
|
|
|
}
|
2018-12-11 17:03:03 +01:00
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
// Note that we don't try to scale by more than 63 since in practice the
|
|
|
|
|
// constraint multipliers should not be all super small.
|
2018-11-05 16:24:47 +01:00
|
|
|
//
|
2023-03-22 19:37:00 +01:00
|
|
|
// TODO(user): We could be faster here, but trying to compute the exact power
|
|
|
|
|
// in one go with floating points seems tricky. So we just do around 6 passes
|
|
|
|
|
// plus the one above for zero.
|
|
|
|
|
const int power = BinarySearch<int>(0, 63, [&](int candidate) {
|
|
|
|
|
// Because BinarySearch() wants f(upper_bound) to fail, we bypass the test
|
|
|
|
|
// here as when the set of floating points are really small, we could pass
|
|
|
|
|
// with a large power.
|
|
|
|
|
if (candidate >= 63) return false;
|
|
|
|
|
|
|
|
|
|
return !ScalingCanOverflow(candidate, take_objective_into_account,
|
|
|
|
|
tmp_cp_multipliers_, overflow_cap);
|
|
|
|
|
});
|
|
|
|
|
*scaling = int64_t{1} << power;
|
|
|
|
|
|
|
|
|
|
// Scale the multipliers by *scaling.
|
|
|
|
|
// Note that we use the exact same formula as in ScalingCanOverflow().
|
|
|
|
|
int64_t gcd = scaling->value();
|
|
|
|
|
for (const auto [row, double_coeff] : tmp_cp_multipliers_) {
|
|
|
|
|
const IntegerValue coeff(std::round(std::ldexp(double_coeff, power)));
|
|
|
|
|
if (coeff != 0) {
|
|
|
|
|
gcd = std::gcd(gcd, std::abs(coeff.value()));
|
|
|
|
|
integer_multipliers.push_back({row, coeff});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (gcd > 1) {
|
|
|
|
|
*scaling /= gcd;
|
|
|
|
|
for (auto& entry : integer_multipliers) {
|
|
|
|
|
entry.second /= gcd;
|
|
|
|
|
}
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
2019-01-17 16:12:52 +01:00
|
|
|
return integer_multipliers;
|
|
|
|
|
}
|
2018-11-05 16:24:47 +01:00
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
template <bool check_overflow>
|
2019-01-17 16:12:52 +01:00
|
|
|
bool LinearProgrammingConstraint::ComputeNewLinearConstraint(
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::vector<std::pair<RowIndex, IntegerValue>>& integer_multipliers,
|
|
|
|
|
ScatteredIntegerVector* scattered_vector, IntegerValue* upper_bound) const {
|
2018-11-05 16:24:47 +01:00
|
|
|
// Initialize the new constraint.
|
|
|
|
|
*upper_bound = 0;
|
2020-09-21 09:44:39 +02:00
|
|
|
scattered_vector->ClearAndResize(integer_variables_.size());
|
2018-11-05 16:24:47 +01:00
|
|
|
|
|
|
|
|
// Compute the new constraint by taking the linear combination given by
|
|
|
|
|
// integer_multipliers of the integer constraints in integer_lp_.
|
2022-02-24 14:17:43 +01:00
|
|
|
for (const std::pair<RowIndex, IntegerValue>& term : integer_multipliers) {
|
2018-11-05 16:24:47 +01:00
|
|
|
const RowIndex row = term.first;
|
|
|
|
|
const IntegerValue multiplier = term.second;
|
|
|
|
|
CHECK_LT(row, integer_lp_.size());
|
|
|
|
|
|
|
|
|
|
// Update the constraint.
|
2023-03-22 19:37:00 +01:00
|
|
|
if (!scattered_vector->AddLinearExpressionMultiple<check_overflow>(
|
2020-09-21 09:44:39 +02:00
|
|
|
multiplier, integer_lp_[row].terms)) {
|
2018-11-05 16:24:47 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the upper bound.
|
2019-11-18 10:06:11 -08:00
|
|
|
const IntegerValue bound =
|
|
|
|
|
multiplier > 0 ? integer_lp_[row].ub : integer_lp_[row].lb;
|
2020-10-22 23:36:58 +02:00
|
|
|
if (!AddProductTo(multiplier, bound, upper_bound)) return false;
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 11:11:36 +01:00
|
|
|
// TODO(user): no need to update the multipliers.
|
|
|
|
|
void LinearProgrammingConstraint::AdjustNewLinearConstraint(
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::pair<glop::RowIndex, IntegerValue>>* integer_multipliers,
|
|
|
|
|
ScatteredIntegerVector* scattered_vector, IntegerValue* upper_bound) const {
|
2019-03-01 11:11:36 +01:00
|
|
|
const IntegerValue kMaxWantedCoeff(1e18);
|
2023-03-22 19:37:00 +01:00
|
|
|
bool adjusted = false;
|
2020-10-28 13:42:36 +01:00
|
|
|
for (std::pair<RowIndex, IntegerValue>& term : *integer_multipliers) {
|
2019-03-01 11:11:36 +01:00
|
|
|
const RowIndex row = term.first;
|
|
|
|
|
const IntegerValue multiplier = term.second;
|
2020-10-22 23:36:58 +02:00
|
|
|
if (multiplier == 0) continue;
|
2019-03-01 11:11:36 +01:00
|
|
|
|
|
|
|
|
// We will only allow change of the form "multiplier += to_add" with to_add
|
|
|
|
|
// in [-negative_limit, positive_limit].
|
2023-03-22 19:37:00 +01:00
|
|
|
//
|
|
|
|
|
// We do not want to_add * row to overflow.
|
|
|
|
|
IntegerValue negative_limit =
|
|
|
|
|
FloorRatio(kMaxWantedCoeff, infinity_norms_[row]);
|
|
|
|
|
IntegerValue positive_limit = negative_limit;
|
2019-03-01 11:11:36 +01:00
|
|
|
|
|
|
|
|
// Make sure we never change the sign of the multiplier, except if the
|
|
|
|
|
// row is an equality in which case we don't care.
|
2019-09-11 12:50:25 +02:00
|
|
|
if (integer_lp_[row].ub != integer_lp_[row].lb) {
|
2019-03-01 11:11:36 +01:00
|
|
|
if (multiplier > 0) {
|
|
|
|
|
negative_limit = std::min(negative_limit, multiplier);
|
|
|
|
|
} else {
|
|
|
|
|
positive_limit = std::min(positive_limit, -multiplier);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure upper_bound + to_add * row_bound never overflow.
|
2019-09-11 12:50:25 +02:00
|
|
|
const IntegerValue row_bound =
|
|
|
|
|
multiplier > 0 ? integer_lp_[row].ub : integer_lp_[row].lb;
|
2019-03-01 11:11:36 +01:00
|
|
|
if (row_bound != 0) {
|
|
|
|
|
const IntegerValue limit1 = FloorRatio(
|
|
|
|
|
std::max(IntegerValue(0), kMaxWantedCoeff - IntTypeAbs(*upper_bound)),
|
|
|
|
|
IntTypeAbs(row_bound));
|
|
|
|
|
const IntegerValue limit2 =
|
|
|
|
|
FloorRatio(kMaxWantedCoeff, IntTypeAbs(row_bound));
|
2020-12-20 12:24:11 +01:00
|
|
|
if ((*upper_bound > 0) == (row_bound > 0)) { // Same sign.
|
2019-03-01 11:11:36 +01:00
|
|
|
positive_limit = std::min(positive_limit, limit1);
|
|
|
|
|
negative_limit = std::min(negative_limit, limit2);
|
|
|
|
|
} else {
|
|
|
|
|
negative_limit = std::min(negative_limit, limit1);
|
|
|
|
|
positive_limit = std::min(positive_limit, limit2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 09:44:39 +02:00
|
|
|
// If we add the row to the scattered_vector, diff will indicate by how much
|
|
|
|
|
// |upper_bound - ImpliedLB(scattered_vector)| will change. That correspond
|
|
|
|
|
// to increasing the multiplier by 1.
|
2019-12-16 12:34:56 +01:00
|
|
|
//
|
|
|
|
|
// At this stage, we are not sure computing sum coeff * bound will not
|
|
|
|
|
// overflow, so we use floating point numbers. It is fine to do so since
|
|
|
|
|
// this is not directly involved in the actual exact constraint generation:
|
|
|
|
|
// these variables are just used in an heuristic.
|
2023-03-22 19:37:00 +01:00
|
|
|
double common_diff = ToDouble(row_bound);
|
|
|
|
|
double positive_diff = 0.0;
|
|
|
|
|
double negative_diff = 0.0;
|
2019-03-01 11:11:36 +01:00
|
|
|
|
|
|
|
|
// TODO(user): we could relax a bit some of the condition and allow a sign
|
|
|
|
|
// change. It is just trickier to compute the diff when we allow such
|
|
|
|
|
// changes.
|
2022-02-24 14:17:43 +01:00
|
|
|
for (const auto& entry : integer_lp_[row].terms) {
|
2019-03-01 11:11:36 +01:00
|
|
|
const ColIndex col = entry.first;
|
|
|
|
|
const IntegerValue coeff = entry.second;
|
2023-03-22 19:37:00 +01:00
|
|
|
DCHECK_NE(coeff, 0);
|
2019-11-14 12:42:35 -08:00
|
|
|
|
2019-03-01 11:11:36 +01:00
|
|
|
// Moving a variable away from zero seems to improve the bound even
|
|
|
|
|
// if it reduces the number of non-zero. Note that this is because of
|
|
|
|
|
// this that positive_diff and negative_diff are not the same.
|
2020-09-21 09:44:39 +02:00
|
|
|
const IntegerValue current = (*scattered_vector)[col];
|
2019-03-01 11:11:36 +01:00
|
|
|
if (current == 0) {
|
2023-03-22 19:37:00 +01:00
|
|
|
const IntegerVariable var = integer_variables_[col.value()];
|
|
|
|
|
const IntegerValue lb = integer_trail_->LowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->UpperBound(var);
|
2019-03-01 11:11:36 +01:00
|
|
|
if (coeff > 0) {
|
2019-12-16 12:34:56 +01:00
|
|
|
positive_diff -= ToDouble(coeff) * ToDouble(lb);
|
|
|
|
|
negative_diff -= ToDouble(coeff) * ToDouble(ub);
|
2019-03-01 11:11:36 +01:00
|
|
|
} else {
|
2019-12-16 12:34:56 +01:00
|
|
|
positive_diff -= ToDouble(coeff) * ToDouble(ub);
|
|
|
|
|
negative_diff -= ToDouble(coeff) * ToDouble(lb);
|
2019-03-01 11:11:36 +01:00
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 12:42:35 -08:00
|
|
|
// We don't want to change the sign of current (except if the variable is
|
2023-03-22 19:37:00 +01:00
|
|
|
// fixed, but in practice we should have removed any fixed variable, so we
|
|
|
|
|
// don't care here) or to have an overflow.
|
2020-01-21 10:59:37 +01:00
|
|
|
//
|
|
|
|
|
// Corner case:
|
|
|
|
|
// - IntTypeAbs(current) can be larger than kMaxWantedCoeff!
|
|
|
|
|
// - The code assumes that 2 * kMaxWantedCoeff do not overflow.
|
2023-03-22 19:37:00 +01:00
|
|
|
//
|
|
|
|
|
// Note that because we now that limit * abs_coeff will never overflow
|
|
|
|
|
// because we used infinity_norms_[row] above.
|
|
|
|
|
const IntegerValue abs_coeff = IntTypeAbs(coeff);
|
2020-01-21 10:59:37 +01:00
|
|
|
const IntegerValue current_magnitude = IntTypeAbs(current);
|
2023-03-22 19:37:00 +01:00
|
|
|
const IntegerValue overflow_threshold =
|
|
|
|
|
std::max(IntegerValue(0), kMaxWantedCoeff - current_magnitude);
|
2020-12-20 12:24:11 +01:00
|
|
|
if ((current > 0) == (coeff > 0)) { // Same sign.
|
2023-03-22 19:37:00 +01:00
|
|
|
if (negative_limit * abs_coeff > current_magnitude) {
|
|
|
|
|
negative_limit = current_magnitude / abs_coeff;
|
|
|
|
|
if (positive_limit == 0 && negative_limit == 0) break;
|
|
|
|
|
}
|
|
|
|
|
if (positive_limit * abs_coeff > overflow_threshold) {
|
|
|
|
|
positive_limit = overflow_threshold / abs_coeff;
|
|
|
|
|
if (positive_limit == 0 && negative_limit == 0) break;
|
|
|
|
|
}
|
2019-03-01 11:11:36 +01:00
|
|
|
} else {
|
2023-03-22 19:37:00 +01:00
|
|
|
if (negative_limit * abs_coeff > overflow_threshold) {
|
|
|
|
|
negative_limit = overflow_threshold / abs_coeff;
|
|
|
|
|
if (positive_limit == 0 && negative_limit == 0) break;
|
|
|
|
|
}
|
|
|
|
|
if (positive_limit * abs_coeff > current_magnitude) {
|
|
|
|
|
positive_limit = current_magnitude / abs_coeff;
|
|
|
|
|
if (positive_limit == 0 && negative_limit == 0) break;
|
|
|
|
|
}
|
2019-03-01 11:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is how diff change.
|
2023-03-22 19:37:00 +01:00
|
|
|
const IntegerVariable var = integer_variables_[col.value()];
|
|
|
|
|
const IntegerValue implied = current > 0
|
|
|
|
|
? integer_trail_->LowerBound(var)
|
|
|
|
|
: integer_trail_->UpperBound(var);
|
2019-11-14 12:42:35 -08:00
|
|
|
if (implied != 0) {
|
2023-03-22 19:37:00 +01:00
|
|
|
common_diff -= ToDouble(coeff) * ToDouble(implied);
|
2019-11-14 12:42:35 -08:00
|
|
|
}
|
2019-03-01 11:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
positive_diff += common_diff;
|
|
|
|
|
negative_diff += common_diff;
|
|
|
|
|
|
2019-03-01 11:11:36 +01:00
|
|
|
// Only add a multiple of this row if it tighten the final constraint.
|
2019-12-16 12:34:56 +01:00
|
|
|
// The positive_diff/negative_diff are supposed to be integer modulo the
|
|
|
|
|
// double precision, so we only add a multiple if they seems far away from
|
|
|
|
|
// zero.
|
2019-03-01 11:11:36 +01:00
|
|
|
IntegerValue to_add(0);
|
2019-12-16 12:34:56 +01:00
|
|
|
if (positive_diff <= -1.0 && positive_limit > 0) {
|
2019-03-01 11:11:36 +01:00
|
|
|
to_add = positive_limit;
|
|
|
|
|
}
|
2019-12-16 12:34:56 +01:00
|
|
|
if (negative_diff >= 1.0 && negative_limit > 0) {
|
2019-03-01 11:11:36 +01:00
|
|
|
// Pick this if it is better than the positive sign.
|
2019-12-16 12:34:56 +01:00
|
|
|
if (to_add == 0 ||
|
|
|
|
|
std::abs(ToDouble(negative_limit) * negative_diff) >
|
|
|
|
|
std::abs(ToDouble(positive_limit) * positive_diff)) {
|
2019-03-01 11:11:36 +01:00
|
|
|
to_add = -negative_limit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (to_add != 0) {
|
|
|
|
|
term.second += to_add;
|
|
|
|
|
*upper_bound += to_add * row_bound;
|
2020-09-21 09:44:39 +02:00
|
|
|
|
|
|
|
|
// TODO(user): we could avoid checking overflow here, but this is likely
|
|
|
|
|
// not in the hot loop.
|
2023-03-22 19:37:00 +01:00
|
|
|
adjusted = true;
|
|
|
|
|
CHECK(scattered_vector
|
|
|
|
|
->AddLinearExpressionMultiple</*check_overflow=*/false>(
|
|
|
|
|
to_add, integer_lp_[row].terms));
|
2019-03-01 11:11:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-22 19:37:00 +01:00
|
|
|
if (adjusted) ++num_adjusts_;
|
2019-03-01 11:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-05 16:24:47 +01:00
|
|
|
// The "exact" computation go as follow:
|
|
|
|
|
//
|
|
|
|
|
// Given any INTEGER linear combination of the LP constraints, we can create a
|
|
|
|
|
// new integer constraint that is valid (its computation must not overflow
|
2018-12-21 13:59:58 +01:00
|
|
|
// though). Lets call this "linear_combination <= ub". We can then always add to
|
|
|
|
|
// it the inequality "objective_terms <= objective_var", so we get:
|
|
|
|
|
// ImpliedLB(objective_terms + linear_combination) - ub <= objective_var.
|
2018-11-05 16:24:47 +01:00
|
|
|
// where ImpliedLB() is computed from the variable current bounds.
|
|
|
|
|
//
|
|
|
|
|
// Now, if we use for the linear combination and approximation of the optimal
|
2018-12-21 13:59:58 +01:00
|
|
|
// negated dual LP values (by scaling them and rounding them to integer), we
|
|
|
|
|
// will get an EXACT objective lower bound that is more or less the same as the
|
|
|
|
|
// inexact bound given by the LP relaxation. This allows to derive exact reasons
|
|
|
|
|
// for any propagation done by this constraint.
|
|
|
|
|
bool LinearProgrammingConstraint::ExactLpReasonning() {
|
2018-11-05 16:24:47 +01:00
|
|
|
// Clear old reason and deductions.
|
|
|
|
|
integer_reason_.clear();
|
|
|
|
|
deductions_.clear();
|
|
|
|
|
deductions_reason_.clear();
|
|
|
|
|
|
|
|
|
|
// The row multipliers will be the negation of the LP duals.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): Provide and use a sparse API in Glop to get the duals.
|
|
|
|
|
const RowIndex num_rows = simplex_.GetProblemNumRows();
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_lp_multipliers_.clear();
|
2018-11-05 16:24:47 +01:00
|
|
|
for (RowIndex row(0); row < num_rows; ++row) {
|
2021-01-14 10:32:45 +01:00
|
|
|
const double value = -simplex_.GetDualValue(row);
|
|
|
|
|
if (std::abs(value) < kZeroTolerance) continue;
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_lp_multipliers_.push_back({row, value});
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-21 12:48:03 +02:00
|
|
|
// In this case, the LP lower bound match the basic objective "constraint"
|
|
|
|
|
// propagation. That is there is an LP solution with all objective variable at
|
|
|
|
|
// their current best bound. There is no need to do more work here.
|
|
|
|
|
if (tmp_lp_multipliers_.empty()) return true;
|
|
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
IntegerValue scaling = 0;
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_integer_multipliers_ = ScaleLpMultiplier(
|
|
|
|
|
/*take_objective_into_account=*/true, tmp_lp_multipliers_, &scaling);
|
2023-03-22 19:37:00 +01:00
|
|
|
if (scaling == 0) {
|
2019-04-05 17:00:40 +02:00
|
|
|
VLOG(1) << "Issue while computing the exact LP reason. Aborting.";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-11-05 16:24:47 +01:00
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
IntegerValue rc_ub;
|
|
|
|
|
CHECK(ComputeNewLinearConstraint</*check_overflow=*/false>(
|
|
|
|
|
tmp_integer_multipliers_, &tmp_scattered_vector_, &rc_ub));
|
|
|
|
|
|
2018-11-05 16:24:47 +01:00
|
|
|
// The "objective constraint" behave like if the unscaled cp multiplier was
|
|
|
|
|
// 1.0, so we will multiply it by this number and add it to reduced_costs.
|
2023-03-22 19:37:00 +01:00
|
|
|
const IntegerValue obj_scale = scaling;
|
|
|
|
|
CHECK(tmp_scattered_vector_
|
|
|
|
|
.AddLinearExpressionMultiple</*check_overflow=*/false>(
|
|
|
|
|
obj_scale, integer_objective_));
|
2020-11-16 08:44:14 +01:00
|
|
|
CHECK(AddProductTo(-obj_scale, integer_objective_offset_, &rc_ub));
|
2022-01-17 16:31:41 +01:00
|
|
|
AdjustNewLinearConstraint(&tmp_integer_multipliers_, &tmp_scattered_vector_,
|
2020-09-21 09:44:39 +02:00
|
|
|
&rc_ub);
|
2018-11-05 16:24:47 +01:00
|
|
|
|
2018-12-21 13:59:58 +01:00
|
|
|
// Create the IntegerSumLE that will allow to propagate the objective and more
|
|
|
|
|
// generally do the reduced cost fixing.
|
2020-09-21 09:44:39 +02:00
|
|
|
tmp_scattered_vector_.ConvertToLinearConstraint(integer_variables_, rc_ub,
|
2022-01-17 16:31:41 +01:00
|
|
|
&tmp_constraint_);
|
|
|
|
|
tmp_constraint_.vars.push_back(objective_cp_);
|
|
|
|
|
tmp_constraint_.coeffs.push_back(-obj_scale);
|
|
|
|
|
DivideByGCD(&tmp_constraint_);
|
|
|
|
|
DCHECK(constraint_manager_.DebugCheckConstraint(tmp_constraint_));
|
2018-11-05 16:24:47 +01:00
|
|
|
|
2021-09-03 16:11:39 +02:00
|
|
|
// Corner case where prevent overflow removed all terms.
|
2022-01-17 16:31:41 +01:00
|
|
|
if (tmp_constraint_.vars.empty()) {
|
2021-09-03 16:11:39 +02:00
|
|
|
trail_->MutableConflict()->clear();
|
2022-01-17 16:31:41 +01:00
|
|
|
return tmp_constraint_.ub >= 0;
|
2021-09-03 16:11:39 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
IntegerSumLE128* cp_constraint =
|
|
|
|
|
new IntegerSumLE128({}, tmp_constraint_.vars, tmp_constraint_.coeffs,
|
|
|
|
|
tmp_constraint_.ub, model_);
|
2019-11-14 12:42:35 -08:00
|
|
|
if (trail_->CurrentDecisionLevel() == 0) {
|
|
|
|
|
// Since we will never ask the reason for a constraint at level 0, we just
|
|
|
|
|
// keep the last one.
|
|
|
|
|
optimal_constraints_.clear();
|
|
|
|
|
}
|
2018-12-21 13:59:58 +01:00
|
|
|
optimal_constraints_.emplace_back(cp_constraint);
|
|
|
|
|
rev_optimal_constraints_size_ = optimal_constraints_.size();
|
2020-11-16 17:01:21 +01:00
|
|
|
if (!cp_constraint->PropagateAtLevelZero()) return false;
|
2018-12-21 13:59:58 +01:00
|
|
|
return cp_constraint->Propagate();
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LinearProgrammingConstraint::FillExactDualRayReason() {
|
2023-03-22 19:37:00 +01:00
|
|
|
IntegerValue scaling;
|
2021-01-14 10:32:45 +01:00
|
|
|
const glop::DenseColumn ray = simplex_.GetDualRay();
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_lp_multipliers_.clear();
|
2021-01-14 10:32:45 +01:00
|
|
|
for (RowIndex row(0); row < ray.size(); ++row) {
|
|
|
|
|
const double value = ray[row];
|
|
|
|
|
if (std::abs(value) < kZeroTolerance) continue;
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_lp_multipliers_.push_back({row, value});
|
2021-01-14 10:32:45 +01:00
|
|
|
}
|
2022-01-17 16:31:41 +01:00
|
|
|
tmp_integer_multipliers_ = ScaleLpMultiplier(
|
|
|
|
|
/*take_objective_into_account=*/false, tmp_lp_multipliers_, &scaling);
|
2023-03-22 19:37:00 +01:00
|
|
|
if (scaling == 0) {
|
2019-04-05 17:00:40 +02:00
|
|
|
VLOG(1) << "Isse while computing the exact dual ray reason. Aborting.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-03-01 11:11:36 +01:00
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
IntegerValue new_constraint_ub;
|
|
|
|
|
CHECK(ComputeNewLinearConstraint</*check_overflow=*/false>(
|
|
|
|
|
tmp_integer_multipliers_, &tmp_scattered_vector_, &new_constraint_ub))
|
|
|
|
|
<< scaling;
|
|
|
|
|
|
2022-01-17 16:31:41 +01:00
|
|
|
AdjustNewLinearConstraint(&tmp_integer_multipliers_, &tmp_scattered_vector_,
|
2019-03-01 11:11:36 +01:00
|
|
|
&new_constraint_ub);
|
|
|
|
|
|
2020-09-21 09:44:39 +02:00
|
|
|
tmp_scattered_vector_.ConvertToLinearConstraint(
|
2022-01-17 16:31:41 +01:00
|
|
|
integer_variables_, new_constraint_ub, &tmp_constraint_);
|
|
|
|
|
DivideByGCD(&tmp_constraint_);
|
|
|
|
|
DCHECK(constraint_manager_.DebugCheckConstraint(tmp_constraint_));
|
|
|
|
|
|
2023-03-22 19:37:00 +01:00
|
|
|
const absl::int128 implied_lb = GetImpliedLowerBound(tmp_constraint_);
|
|
|
|
|
if (implied_lb <= absl::int128(tmp_constraint_.ub.value())) {
|
2018-12-17 16:50:15 +01:00
|
|
|
VLOG(1) << "LP exact dual ray not infeasible,"
|
2023-03-22 19:37:00 +01:00
|
|
|
<< " implied_lb: " << implied_lb
|
|
|
|
|
<< " ub: " << tmp_constraint_.ub.value();
|
2018-11-05 16:24:47 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-03-22 19:37:00 +01:00
|
|
|
const IntegerValue slack = static_cast<int64_t>(
|
|
|
|
|
std::min(absl::int128(std::numeric_limits<int64_t>::max() - 1),
|
|
|
|
|
(implied_lb - absl::int128(tmp_constraint_.ub.value())) - 1));
|
2022-01-17 16:31:41 +01:00
|
|
|
SetImpliedLowerBoundReason(tmp_constraint_, slack);
|
2018-11-05 16:24:47 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 18:26:01 +01:00
|
|
|
int64_t LinearProgrammingConstraint::CalculateDegeneracy() {
|
2019-05-05 18:02:49 +02:00
|
|
|
const glop::ColIndex num_vars = simplex_.GetProblemNumCols();
|
2019-05-03 16:30:50 +02:00
|
|
|
int num_non_basic_with_zero_rc = 0;
|
2019-05-05 18:02:49 +02:00
|
|
|
for (glop::ColIndex i(0); i < num_vars; ++i) {
|
|
|
|
|
const double rc = simplex_.GetReducedCost(i);
|
2020-10-22 23:36:58 +02:00
|
|
|
if (rc != 0.0) continue;
|
2019-05-05 18:02:49 +02:00
|
|
|
if (simplex_.GetVariableStatus(i) == glop::VariableStatus::BASIC) {
|
2019-05-03 16:30:50 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
num_non_basic_with_zero_rc++;
|
|
|
|
|
}
|
2021-03-04 18:26:01 +01:00
|
|
|
const int64_t num_cols = simplex_.GetProblemNumCols().value();
|
2019-08-22 13:15:49 +02:00
|
|
|
is_degenerate_ = num_non_basic_with_zero_rc >= 0.3 * num_cols;
|
2019-05-05 18:02:49 +02:00
|
|
|
return num_non_basic_with_zero_rc;
|
2019-05-03 16:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-03-28 16:11:06 +02:00
|
|
|
void LinearProgrammingConstraint::ReducedCostStrengtheningDeductions(
|
2017-06-28 14:33:56 +02:00
|
|
|
double cp_objective_delta) {
|
2017-03-28 16:11:06 +02:00
|
|
|
deductions_.clear();
|
|
|
|
|
|
2017-06-28 14:33:56 +02:00
|
|
|
// TRICKY: while simplex_.GetObjectiveValue() use the objective scaling factor
|
|
|
|
|
// stored in the lp_data_, all the other functions like GetReducedCost() or
|
|
|
|
|
// GetVariableValue() do not.
|
|
|
|
|
const double lp_objective_delta =
|
2017-07-12 11:38:46 -07:00
|
|
|
cp_objective_delta / lp_data_.objective_scaling_factor();
|
2017-03-28 16:11:06 +02:00
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
const IntegerVariable cp_var = integer_variables_[i];
|
2018-01-10 13:21:06 +01:00
|
|
|
const glop::ColIndex lp_var = glop::ColIndex(i);
|
2017-06-28 14:33:56 +02:00
|
|
|
const double rc = simplex_.GetReducedCost(lp_var);
|
2017-03-28 16:11:06 +02:00
|
|
|
const double value = simplex_.GetVariableValue(lp_var);
|
2017-11-07 15:45:52 +01:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
if (rc == 0.0) continue;
|
2017-03-28 16:11:06 +02:00
|
|
|
const double lp_other_bound = value + lp_objective_delta / rc;
|
2019-10-04 08:51:53 -04:00
|
|
|
const double cp_other_bound =
|
|
|
|
|
scaler_.UnscaleVariableValue(lp_var, lp_other_bound);
|
2017-03-28 16:11:06 +02:00
|
|
|
|
2018-02-12 11:36:18 +01:00
|
|
|
if (rc > kLpEpsilon) {
|
2018-11-05 16:24:47 +01:00
|
|
|
const double ub = ToDouble(integer_trail_->UpperBound(cp_var));
|
2018-02-12 11:36:18 +01:00
|
|
|
const double new_ub = std::floor(cp_other_bound + kCpEpsilon);
|
2017-03-28 16:11:06 +02:00
|
|
|
if (new_ub < ub) {
|
2018-02-12 11:36:18 +01:00
|
|
|
// TODO(user): Because rc > kLpEpsilon, the lower_bound of cp_var
|
|
|
|
|
// will be part of the reason returned by FillReducedCostsReason(), but
|
|
|
|
|
// we actually do not need it here. Same below.
|
2017-03-28 16:11:06 +02:00
|
|
|
const IntegerValue new_ub_int(static_cast<IntegerValue>(new_ub));
|
|
|
|
|
deductions_.push_back(IntegerLiteral::LowerOrEqual(cp_var, new_ub_int));
|
|
|
|
|
}
|
2018-02-12 11:36:18 +01:00
|
|
|
} else if (rc < -kLpEpsilon) {
|
2018-11-05 16:24:47 +01:00
|
|
|
const double lb = ToDouble(integer_trail_->LowerBound(cp_var));
|
2018-02-12 11:36:18 +01:00
|
|
|
const double new_lb = std::ceil(cp_other_bound - kCpEpsilon);
|
2017-03-28 16:11:06 +02:00
|
|
|
if (new_lb > lb) {
|
|
|
|
|
const IntegerValue new_lb_int(static_cast<IntegerValue>(new_lb));
|
|
|
|
|
deductions_.push_back(
|
|
|
|
|
IntegerLiteral::GreaterOrEqual(cp_var, new_lb_int));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
void LinearProgrammingConstraint::UpdateAverageReducedCosts() {
|
|
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
if (sum_cost_down_.size() < num_vars) {
|
|
|
|
|
sum_cost_down_.resize(num_vars, 0.0);
|
|
|
|
|
num_cost_down_.resize(num_vars, 0);
|
|
|
|
|
sum_cost_up_.resize(num_vars, 0.0);
|
|
|
|
|
num_cost_up_.resize(num_vars, 0);
|
|
|
|
|
rc_scores_.resize(num_vars, 0.0);
|
2020-07-05 00:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// Decay averages.
|
|
|
|
|
num_calls_since_reduced_cost_averages_reset_++;
|
|
|
|
|
if (num_calls_since_reduced_cost_averages_reset_ == 10000) {
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
sum_cost_up_[i] /= 2;
|
|
|
|
|
num_cost_up_[i] /= 2;
|
|
|
|
|
sum_cost_down_[i] /= 2;
|
|
|
|
|
num_cost_down_[i] /= 2;
|
2020-07-05 00:29:47 +02:00
|
|
|
}
|
2022-10-11 14:28:56 +02:00
|
|
|
num_calls_since_reduced_cost_averages_reset_ = 0;
|
2020-07-05 00:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// Accumulate reduced costs of all unassigned variables.
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
const IntegerVariable var = integer_variables_[i];
|
2018-01-10 13:21:06 +01:00
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// Skip ignored and fixed variables.
|
|
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
|
|
|
|
if (integer_trail_->IsFixed(var)) continue;
|
2017-11-07 15:45:52 +01:00
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// Skip reduced costs that are zero or close.
|
|
|
|
|
const double rc = lp_reduced_cost_[i];
|
|
|
|
|
if (std::abs(rc) < kCpEpsilon) continue;
|
2017-11-07 15:45:52 +01:00
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
if (rc < 0.0) {
|
|
|
|
|
sum_cost_down_[i] -= rc;
|
|
|
|
|
num_cost_down_[i]++;
|
|
|
|
|
} else {
|
|
|
|
|
sum_cost_up_[i] += rc;
|
|
|
|
|
num_cost_up_[i]++;
|
2019-07-12 10:22:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// Tricky, we artificially reset the rc_rev_int_repository_ to level zero
|
|
|
|
|
// so that the rev_rc_start_ is zero.
|
|
|
|
|
rc_rev_int_repository_.SetLevel(0);
|
|
|
|
|
rc_rev_int_repository_.SetLevel(trail_->CurrentDecisionLevel());
|
|
|
|
|
rev_rc_start_ = 0;
|
2022-04-07 17:59:22 +02:00
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// Cache the new score (higher is better) using the average reduced costs
|
|
|
|
|
// as a signal.
|
|
|
|
|
positions_by_decreasing_rc_score_.clear();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
// If only one direction exist, we takes its value divided by 2, so that
|
|
|
|
|
// such variable should have a smaller cost than the min of the two side
|
|
|
|
|
// except if one direction have a really high reduced costs.
|
|
|
|
|
const double a_up =
|
|
|
|
|
num_cost_up_[i] > 0 ? sum_cost_up_[i] / num_cost_up_[i] : 0.0;
|
|
|
|
|
const double a_down =
|
|
|
|
|
num_cost_down_[i] > 0 ? sum_cost_down_[i] / num_cost_down_[i] : 0.0;
|
|
|
|
|
if (num_cost_down_[i] > 0 && num_cost_up_[i] > 0) {
|
|
|
|
|
rc_scores_[i] = std::min(a_up, a_down);
|
2022-04-07 17:59:22 +02:00
|
|
|
} else {
|
2022-10-11 14:28:56 +02:00
|
|
|
rc_scores_[i] = 0.5 * (a_down + a_up);
|
2019-07-12 10:22:51 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// We ignore scores of zero (i.e. no data) and will follow the default
|
|
|
|
|
// search heuristic if all variables are like this.
|
|
|
|
|
if (rc_scores_[i] > 0.0) {
|
|
|
|
|
positions_by_decreasing_rc_score_.push_back({-rc_scores_[i], i});
|
2019-07-12 10:22:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-11 14:28:56 +02:00
|
|
|
std::sort(positions_by_decreasing_rc_score_.begin(),
|
|
|
|
|
positions_by_decreasing_rc_score_.end());
|
2019-07-12 10:22:51 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// TODO(user): Remove duplication with HeuristicLpReducedCostBinary().
|
|
|
|
|
std::function<IntegerLiteral()>
|
|
|
|
|
LinearProgrammingConstraint::HeuristicLpReducedCostAverageBranching() {
|
|
|
|
|
return [this]() { return this->LPReducedCostAverageDecision(); };
|
2019-07-18 11:36:47 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
IntegerLiteral LinearProgrammingConstraint::LPReducedCostAverageDecision() {
|
|
|
|
|
// Select noninstantiated variable with highest positive average reduced cost.
|
|
|
|
|
int selected_index = -1;
|
|
|
|
|
const int size = positions_by_decreasing_rc_score_.size();
|
|
|
|
|
rc_rev_int_repository_.SaveState(&rev_rc_start_);
|
|
|
|
|
for (int i = rev_rc_start_; i < size; ++i) {
|
|
|
|
|
const int index = positions_by_decreasing_rc_score_[i].second;
|
|
|
|
|
const IntegerVariable var = integer_variables_[index];
|
|
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
|
|
|
|
if (integer_trail_->IsFixed(var)) continue;
|
|
|
|
|
selected_index = index;
|
|
|
|
|
rev_rc_start_ = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-11-07 15:45:52 +01:00
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
if (selected_index == -1) return IntegerLiteral();
|
|
|
|
|
const IntegerVariable var = integer_variables_[selected_index];
|
2017-10-18 11:09:13 +02:00
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// If ceil(value) is current upper bound, try var == upper bound first.
|
|
|
|
|
// Guarding with >= prevents numerical problems.
|
|
|
|
|
// With 0/1 variables, this will tend to try setting to 1 first,
|
|
|
|
|
// which produces more shallow trees.
|
|
|
|
|
const IntegerValue ub = integer_trail_->UpperBound(var);
|
|
|
|
|
const IntegerValue value_ceil(
|
|
|
|
|
std::ceil(this->GetSolutionValue(var) - kCpEpsilon));
|
|
|
|
|
if (value_ceil >= ub) {
|
|
|
|
|
return IntegerLiteral::GreaterOrEqual(var, ub);
|
|
|
|
|
}
|
2022-04-07 17:59:22 +02:00
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// If floor(value) is current lower bound, try var == lower bound first.
|
|
|
|
|
// Guarding with <= prevents numerical problems.
|
|
|
|
|
const IntegerValue lb = integer_trail_->LowerBound(var);
|
|
|
|
|
const IntegerValue value_floor(
|
|
|
|
|
std::floor(this->GetSolutionValue(var) + kCpEpsilon));
|
|
|
|
|
if (value_floor <= lb) {
|
|
|
|
|
return IntegerLiteral::LowerOrEqual(var, lb);
|
|
|
|
|
}
|
2022-04-07 17:59:22 +02:00
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
// Here lb < value_floor <= value_ceil < ub.
|
|
|
|
|
// Try the most promising split between var <= floor or var >= ceil.
|
|
|
|
|
const double a_up =
|
|
|
|
|
num_cost_up_[selected_index] > 0
|
|
|
|
|
? sum_cost_up_[selected_index] / num_cost_up_[selected_index]
|
|
|
|
|
: 0.0;
|
|
|
|
|
const double a_down =
|
|
|
|
|
num_cost_down_[selected_index] > 0
|
|
|
|
|
? sum_cost_down_[selected_index] / num_cost_down_[selected_index]
|
|
|
|
|
: 0.0;
|
|
|
|
|
if (a_down < a_up) {
|
|
|
|
|
return IntegerLiteral::LowerOrEqual(var, value_floor);
|
|
|
|
|
} else {
|
|
|
|
|
return IntegerLiteral::GreaterOrEqual(var, value_ceil);
|
2022-04-07 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:28:56 +02:00
|
|
|
std::string LinearProgrammingConstraint::Statistics() const {
|
|
|
|
|
std::string result = "LP statistics:\n";
|
|
|
|
|
absl::StrAppend(&result, " final dimension: ", DimensionString(), "\n");
|
|
|
|
|
absl::StrAppend(&result, " total number of simplex iterations: ",
|
2022-10-25 20:31:33 +02:00
|
|
|
FormatCounter(total_num_simplex_iterations_), "\n");
|
2023-01-16 13:26:55 +01:00
|
|
|
absl::StrAppend(&result, " total num cut propagation: ",
|
|
|
|
|
FormatCounter(total_num_cut_propagations_), "\n");
|
2023-04-21 12:48:03 +02:00
|
|
|
absl::StrAppend(&result, " total num cut overflow: ",
|
|
|
|
|
FormatCounter(num_cut_overflows_), "\n");
|
2023-03-22 19:37:00 +01:00
|
|
|
absl::StrAppend(&result, " total num adjust: ", FormatCounter(num_adjusts_),
|
|
|
|
|
"\n");
|
|
|
|
|
absl::StrAppend(&result, " total num scaling issues: ",
|
|
|
|
|
FormatCounter(num_scaling_issues_), "\n");
|
2022-10-11 14:28:56 +02:00
|
|
|
absl::StrAppend(&result, " num solves: \n");
|
|
|
|
|
for (int i = 0; i < num_solves_by_status_.size(); ++i) {
|
|
|
|
|
if (num_solves_by_status_[i] == 0) continue;
|
|
|
|
|
absl::StrAppend(&result, " - #",
|
|
|
|
|
glop::GetProblemStatusString(glop::ProblemStatus(i)), ": ",
|
2022-10-25 20:31:33 +02:00
|
|
|
FormatCounter(num_solves_by_status_[i]), "\n");
|
2022-10-11 14:28:56 +02:00
|
|
|
}
|
|
|
|
|
absl::StrAppend(&result, constraint_manager_.Statistics());
|
2022-04-07 17:59:22 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 16:38:25 +02:00
|
|
|
std::function<IntegerLiteral()>
|
2020-10-28 13:42:36 +01:00
|
|
|
LinearProgrammingConstraint::HeuristicLpMostInfeasibleBinary(Model* model) {
|
2020-10-18 16:38:25 +02:00
|
|
|
// Gather all 0-1 variables that appear in this LP.
|
2017-08-03 10:20:59 -07:00
|
|
|
std::vector<IntegerVariable> variables;
|
2017-12-08 14:52:49 +01:00
|
|
|
for (IntegerVariable var : integer_variables_) {
|
|
|
|
|
if (integer_trail_->LowerBound(var) == 0 &&
|
|
|
|
|
integer_trail_->UpperBound(var) == 1) {
|
2017-08-03 10:20:59 -07:00
|
|
|
variables.push_back(var);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-17 13:11:14 +01:00
|
|
|
VLOG(1) << "HeuristicLPMostInfeasibleBinary has " << variables.size()
|
|
|
|
|
<< " variables.";
|
2017-08-03 10:20:59 -07:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
return [this, variables]() {
|
|
|
|
|
const double kEpsilon = 1e-6;
|
2017-08-03 10:20:59 -07:00
|
|
|
// Find most fractional value.
|
|
|
|
|
IntegerVariable fractional_var = kNoIntegerVariable;
|
|
|
|
|
double fractional_distance_best = -1.0;
|
|
|
|
|
for (const IntegerVariable var : variables) {
|
2017-12-08 14:52:49 +01:00
|
|
|
// Skip ignored and fixed variables.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
2017-12-08 14:52:49 +01:00
|
|
|
const IntegerValue lb = integer_trail_->LowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->UpperBound(var);
|
2020-10-22 23:36:58 +02:00
|
|
|
if (lb == ub) continue;
|
2017-08-03 10:20:59 -07:00
|
|
|
|
|
|
|
|
// Check variable's support is fractional.
|
2017-12-08 14:52:49 +01:00
|
|
|
const double lp_value = this->GetSolutionValue(var);
|
2017-08-03 10:20:59 -07:00
|
|
|
const double fractional_distance =
|
|
|
|
|
std::min(std::ceil(lp_value - kEpsilon) - lp_value,
|
|
|
|
|
lp_value - std::floor(lp_value + kEpsilon));
|
2020-10-22 23:36:58 +02:00
|
|
|
if (fractional_distance < kEpsilon) continue;
|
2017-08-03 10:20:59 -07:00
|
|
|
|
|
|
|
|
// Keep variable if it is farther from integrality than the previous.
|
|
|
|
|
if (fractional_distance > fractional_distance_best) {
|
|
|
|
|
fractional_var = var;
|
|
|
|
|
fractional_distance_best = fractional_distance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fractional_var != kNoIntegerVariable) {
|
2020-10-18 16:38:25 +02:00
|
|
|
IntegerLiteral::GreaterOrEqual(fractional_var, IntegerValue(1));
|
2017-08-03 10:20:59 -07:00
|
|
|
}
|
2020-10-18 16:38:25 +02:00
|
|
|
return IntegerLiteral();
|
2020-10-22 23:36:58 +02:00
|
|
|
};
|
2017-08-03 10:20:59 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 16:38:25 +02:00
|
|
|
std::function<IntegerLiteral()>
|
2020-10-28 13:42:36 +01:00
|
|
|
LinearProgrammingConstraint::HeuristicLpReducedCostBinary(Model* model) {
|
2018-02-12 11:36:18 +01:00
|
|
|
// Gather all 0-1 variables that appear in this LP.
|
2017-08-03 10:20:59 -07:00
|
|
|
std::vector<IntegerVariable> variables;
|
2017-12-08 14:52:49 +01:00
|
|
|
for (IntegerVariable var : integer_variables_) {
|
|
|
|
|
if (integer_trail_->LowerBound(var) == 0 &&
|
|
|
|
|
integer_trail_->UpperBound(var) == 1) {
|
2017-08-03 10:20:59 -07:00
|
|
|
variables.push_back(var);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-18 16:38:25 +02:00
|
|
|
VLOG(1) << "HeuristicLpReducedCostBinary has " << variables.size()
|
2018-01-17 13:11:14 +01:00
|
|
|
<< " variables.";
|
2017-08-03 10:20:59 -07:00
|
|
|
|
|
|
|
|
// Store average of reduced cost from 1 to 0. The best heuristic only sets
|
|
|
|
|
// variables to one and cares about cost to zero, even though classic
|
2018-01-10 13:21:06 +01:00
|
|
|
// pseudocost will use max_var min(cost_to_one[var], cost_to_zero[var]).
|
2017-08-03 10:20:59 -07:00
|
|
|
const int num_vars = variables.size();
|
|
|
|
|
std::vector<double> cost_to_zero(num_vars, 0.0);
|
|
|
|
|
std::vector<int> num_cost_to_zero(num_vars);
|
|
|
|
|
int num_calls = 0;
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
return [=]() mutable {
|
|
|
|
|
const double kEpsilon = 1e-6;
|
2017-08-03 10:20:59 -07:00
|
|
|
|
|
|
|
|
// Every 10000 calls, decay pseudocosts.
|
|
|
|
|
num_calls++;
|
|
|
|
|
if (num_calls == 10000) {
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
cost_to_zero[i] /= 2;
|
|
|
|
|
num_cost_to_zero[i] /= 2;
|
|
|
|
|
}
|
|
|
|
|
num_calls = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accumulate pseudo-costs of all unassigned variables.
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
const IntegerVariable var = variables[i];
|
2017-12-08 14:52:49 +01:00
|
|
|
// Skip ignored and fixed variables.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
2017-12-08 14:52:49 +01:00
|
|
|
const IntegerValue lb = integer_trail_->LowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->UpperBound(var);
|
2020-10-22 23:36:58 +02:00
|
|
|
if (lb == ub) continue;
|
2017-08-03 10:20:59 -07:00
|
|
|
|
2017-12-08 14:52:49 +01:00
|
|
|
const double rc = this->GetSolutionReducedCost(var);
|
2017-08-03 10:20:59 -07:00
|
|
|
// Skip reduced costs that are nonzero because of numerical issues.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (std::abs(rc) < kEpsilon) continue;
|
2017-08-03 10:20:59 -07:00
|
|
|
|
2017-12-08 14:52:49 +01:00
|
|
|
const double value = std::round(this->GetSolutionValue(var));
|
2017-08-03 10:20:59 -07:00
|
|
|
if (value == 1.0 && rc < 0.0) {
|
|
|
|
|
cost_to_zero[i] -= rc;
|
|
|
|
|
num_cost_to_zero[i]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Select noninstantiated variable with highest pseudo-cost.
|
|
|
|
|
int selected_index = -1;
|
|
|
|
|
double best_cost = 0.0;
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
const IntegerVariable var = variables[i];
|
2017-12-08 14:52:49 +01:00
|
|
|
// Skip ignored and fixed variables.
|
2020-10-22 23:36:58 +02:00
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
|
|
|
|
if (integer_trail_->IsFixed(var)) continue;
|
2017-08-03 10:20:59 -07:00
|
|
|
|
|
|
|
|
if (num_cost_to_zero[i] > 0 &&
|
|
|
|
|
best_cost < cost_to_zero[i] / num_cost_to_zero[i]) {
|
|
|
|
|
best_cost = cost_to_zero[i] / num_cost_to_zero[i];
|
|
|
|
|
selected_index = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selected_index >= 0) {
|
2020-10-18 16:38:25 +02:00
|
|
|
return IntegerLiteral::GreaterOrEqual(variables[selected_index],
|
|
|
|
|
IntegerValue(1));
|
2017-08-03 10:20:59 -07:00
|
|
|
}
|
2020-10-18 16:38:25 +02:00
|
|
|
return IntegerLiteral();
|
2020-10-22 23:36:58 +02:00
|
|
|
};
|
2017-08-03 10:20:59 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace sat
|
|
|
|
|
} // namespace operations_research
|