2018-11-10 18:00:53 +01:00
|
|
|
// Copyright 2010-2018 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
|
|
|
|
2017-07-27 11:28:55 -07:00
|
|
|
#include <cmath>
|
|
|
|
|
#include <limits>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "absl/container/flat_hash_map.h"
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/base/commandlineflags.h"
|
2018-06-08 16:40:43 +02:00
|
|
|
#include "ortools/base/int_type_indexed_vector.h"
|
2017-07-27 11:28:55 -07:00
|
|
|
#include "ortools/base/integral_types.h"
|
|
|
|
|
#include "ortools/base/logging.h"
|
|
|
|
|
#include "ortools/base/map_util.h"
|
|
|
|
|
#include "ortools/glop/parameters.pb.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "ortools/glop/preprocessor.h"
|
2017-07-27 11:28:55 -07:00
|
|
|
#include "ortools/glop/status.h"
|
2018-06-08 16:40:43 +02:00
|
|
|
#include "ortools/graph/strongly_connected_components.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "ortools/util/saturated_arithmetic.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;
|
|
|
|
|
|
2018-02-12 11:36:18 +01:00
|
|
|
const double LinearProgrammingConstraint::kCpEpsilon = 1e-4;
|
|
|
|
|
const double LinearProgrammingConstraint::kLpEpsilon = 1e-6;
|
2017-04-18 00:08:19 +02:00
|
|
|
|
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.
|
2017-06-28 14:33:56 +02:00
|
|
|
LinearProgrammingConstraint::LinearProgrammingConstraint(Model* model)
|
2017-12-06 11:23:11 +01:00
|
|
|
: sat_parameters_(*(model->GetOrCreate<SatParameters>())),
|
2018-12-21 13:59:58 +01: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>()),
|
|
|
|
|
trail_(model->GetOrCreate<Trail>()),
|
2017-12-08 14:52:49 +01:00
|
|
|
model_heuristics_(model->GetOrCreate<SearchHeuristicsVector>()),
|
2018-02-12 11:36:18 +01:00
|
|
|
integer_encoder_(model->GetOrCreate<IntegerEncoder>()),
|
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.
|
|
|
|
|
glop::GlopParameters parameters;
|
|
|
|
|
parameters.set_use_dual_simplex(true);
|
|
|
|
|
simplex_.SetParameters(parameters);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
void LinearProgrammingConstraint::AddLinearConstraint(
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// We still create the mirror variable right away though.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): clean this up? Note that it is important that the variable
|
|
|
|
|
// in lp_data_ never changes though, so we can restart from the current
|
|
|
|
|
// lp solution and be incremental (even if the constraints changed).
|
|
|
|
|
for (const IntegerVariable var : ct.vars) {
|
|
|
|
|
GetOrCreateMirrorVariable(PositiveVariable(var));
|
|
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glop::ColIndex LinearProgrammingConstraint::GetOrCreateMirrorVariable(
|
2017-07-07 11:13:35 -07:00
|
|
|
IntegerVariable positive_variable) {
|
|
|
|
|
DCHECK(VariableIsPositive(positive_variable));
|
2018-04-11 13:00:30 +02:00
|
|
|
if (!gtl::ContainsKey(mirror_lp_variable_, positive_variable)) {
|
2018-12-03 14:26:31 +01:00
|
|
|
const glop::ColIndex col(integer_variables_.size());
|
2018-01-10 13:21:06 +01:00
|
|
|
mirror_lp_variable_[positive_variable] = col;
|
2017-07-07 11:13:35 -07:00
|
|
|
integer_variables_.push_back(positive_variable);
|
2017-03-28 16:11:06 +02:00
|
|
|
lp_solution_.push_back(std::numeric_limits<double>::infinity());
|
2017-08-03 10:20:59 -07:00
|
|
|
lp_reduced_cost_.push_back(0.0);
|
|
|
|
|
(*dispatcher_)[positive_variable] = this;
|
2018-12-03 14:26:31 +01:00
|
|
|
|
|
|
|
|
const int index = std::max(positive_variable.value(),
|
|
|
|
|
NegationOf(positive_variable).value());
|
2018-12-04 14:36:46 +01:00
|
|
|
if (index >= expanded_lp_solution_.size()) {
|
|
|
|
|
expanded_lp_solution_.resize(index + 1, 0.0);
|
2018-12-03 14:26:31 +01:00
|
|
|
}
|
2018-01-10 13:21:06 +01:00
|
|
|
return col;
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
2018-01-10 13:21:06 +01:00
|
|
|
return mirror_lp_variable_[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);
|
2018-11-05 16:24:47 +01:00
|
|
|
if (ivar != pos_var) coeff = -coeff;
|
|
|
|
|
|
|
|
|
|
const glop::ColIndex col = GetOrCreateMirrorVariable(pos_var);
|
|
|
|
|
integer_objective_.push_back({col, 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.
|
|
|
|
|
void LinearProgrammingConstraint::CreateLpFromConstraintManager() {
|
|
|
|
|
// Fill integer_lp_.
|
|
|
|
|
integer_lp_.clear();
|
2018-12-04 14:36:46 +01:00
|
|
|
const auto& all_constraints = constraint_manager_.AllConstraints();
|
|
|
|
|
for (const auto index : constraint_manager_.LpConstraints()) {
|
|
|
|
|
const LinearConstraint& ct = all_constraints[index];
|
2018-12-03 14:26:31 +01:00
|
|
|
integer_lp_.push_back(LinearConstraintInternal());
|
|
|
|
|
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();
|
2018-12-03 14:26:31 +01:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
// We only use positive variable inside this class.
|
2018-12-04 14:36:46 +01:00
|
|
|
IntegerVariable var = ct.vars[i];
|
|
|
|
|
IntegerValue coeff = ct.coeffs[i];
|
2018-12-03 14:26:31 +01:00
|
|
|
if (!VariableIsPositive(var)) {
|
|
|
|
|
var = NegationOf(var);
|
|
|
|
|
coeff = -coeff;
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
2018-12-03 14:26:31 +01:00
|
|
|
new_ct.terms.push_back({GetOrCreateMirrorVariable(var), coeff});
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
2018-12-03 14:26:31 +01:00
|
|
|
|
|
|
|
|
// Important to keep lp_data_ "clean".
|
|
|
|
|
std::sort(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());
|
|
|
|
|
}
|
|
|
|
|
for (const auto entry : integer_objective_) {
|
|
|
|
|
lp_data_.SetObjectiveCoefficient(entry.first, ToDouble(entry.second));
|
|
|
|
|
}
|
2018-11-05 16:24:47 +01:00
|
|
|
for (const LinearConstraintInternal& ct : integer_lp_) {
|
|
|
|
|
const ConstraintIndex row = lp_data_.CreateNewConstraint();
|
|
|
|
|
lp_data_.SetConstraintBounds(row, ToDouble(ct.lb), ToDouble(ct.ub));
|
|
|
|
|
for (const auto& term : ct.terms) {
|
|
|
|
|
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
|
|
|
}
|
2018-11-05 16:24:47 +01:00
|
|
|
|
|
|
|
|
// Scale lp_data_.
|
2018-12-03 14:26:31 +01:00
|
|
|
scaler_.Clear();
|
2018-02-16 17:08:01 +01:00
|
|
|
Scale(&lp_data_, &scaler_, glop::GlopParameters::DEFAULT);
|
2017-07-12 11:38:46 -07:00
|
|
|
lp_data_.ScaleObjective();
|
2018-02-12 11:36:18 +01:00
|
|
|
|
|
|
|
|
// ScaleBounds() looks at both the constraints and variable bounds, so we
|
|
|
|
|
// initialize the LP variable bounds before scaling them.
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
bound_scaling_factor_ = 1.0;
|
|
|
|
|
UpdateBoundsOfLpVariables();
|
|
|
|
|
bound_scaling_factor_ = lp_data_.ScaleBounds();
|
2018-11-05 16:24:47 +01:00
|
|
|
|
2018-12-03 14:26:31 +01:00
|
|
|
lp_data_.NotifyThatColumnsAreClean();
|
2017-03-28 16:11:06 +02:00
|
|
|
lp_data_.AddSlackVariablesWhereNecessary(false);
|
2018-12-04 14:36:46 +01:00
|
|
|
VLOG(1) << "LP relaxation: " << lp_data_.GetDimensionString() << ". "
|
|
|
|
|
<< constraint_manager_.AllConstraints().size()
|
|
|
|
|
<< " Managed constraints.";
|
2018-12-03 14:26:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinearProgrammingConstraint::RegisterWith(Model* model) {
|
|
|
|
|
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.
|
|
|
|
|
if (!sat_parameters_.add_lp_constraints_lazily()) {
|
|
|
|
|
constraint_manager_.AddAllConstraintsToLp();
|
|
|
|
|
}
|
2018-12-03 14:26:31 +01:00
|
|
|
CreateLpFromConstraintManager();
|
2017-03-28 16:11:06 +02:00
|
|
|
|
2017-12-08 14:52:49 +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);
|
|
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
watcher->SetPropagatorPriority(watcher_id, 2);
|
2017-12-08 14:52:49 +01:00
|
|
|
|
|
|
|
|
if (integer_variables_.size() >= 20) { // Do not use on small subparts.
|
|
|
|
|
auto* container = model->GetOrCreate<SearchHeuristicsVector>();
|
|
|
|
|
container->push_back(HeuristicLPPseudoCostBinary(model));
|
|
|
|
|
container->push_back(HeuristicLPMostInfeasibleBinary(model));
|
|
|
|
|
}
|
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?
|
|
|
|
|
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) {
|
|
|
|
|
for (const IntegerVariable var : generator.vars) {
|
|
|
|
|
GetOrCreateMirrorVariable(VariableIsPositive(var) ? var : NegationOf(var));
|
|
|
|
|
}
|
|
|
|
|
cut_generators_.push_back(std::move(generator));
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 16:11:06 +02:00
|
|
|
bool LinearProgrammingConstraint::IncrementalPropagate(
|
|
|
|
|
const std::vector<int>& watch_indices) {
|
2018-01-10 13:21:06 +01:00
|
|
|
if (!lp_solution_is_set_) 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];
|
2018-02-12 11:36:18 +01: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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 11:36:18 +01:00
|
|
|
glop::Fractional LinearProgrammingConstraint::CpToLpScalingFactor(
|
|
|
|
|
glop::ColIndex col) const {
|
|
|
|
|
return scaler_.col_scale(col) / bound_scaling_factor_;
|
|
|
|
|
}
|
|
|
|
|
glop::Fractional LinearProgrammingConstraint::LpToCpScalingFactor(
|
|
|
|
|
glop::ColIndex col) const {
|
|
|
|
|
return bound_scaling_factor_ / scaler_.col_scale(col);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 16:11:06 +02:00
|
|
|
glop::Fractional LinearProgrammingConstraint::GetVariableValueAtCpScale(
|
|
|
|
|
glop::ColIndex var) {
|
2018-02-12 11:36:18 +01:00
|
|
|
return simplex_.GetVariableValue(var) * LpToCpScalingFactor(var);
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-03 10:20:59 -07:00
|
|
|
double LinearProgrammingConstraint::GetSolutionValue(
|
|
|
|
|
IntegerVariable variable) const {
|
2018-04-11 13:00:30 +02:00
|
|
|
return lp_solution_[gtl::FindOrDie(mirror_lp_variable_, variable).value()];
|
2017-08-03 10:20:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double LinearProgrammingConstraint::GetSolutionReducedCost(
|
|
|
|
|
IntegerVariable variable) const {
|
2018-04-11 13:00:30 +02:00
|
|
|
return lp_reduced_cost_[gtl::FindOrDie(mirror_lp_variable_, 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));
|
2018-02-12 11:36:18 +01:00
|
|
|
const double factor = CpToLpScalingFactor(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() {
|
|
|
|
|
const auto status = simplex_.Solve(lp_data_, time_limit_);
|
|
|
|
|
if (!status.ok()) {
|
|
|
|
|
LOG(WARNING) << "The LP solver encountered an error: "
|
|
|
|
|
<< status.error_message();
|
|
|
|
|
simplex_.ClearStateForNextSolve();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2017-07-12 11:38:46 -07:00
|
|
|
glop::GlopParameters parameters = simplex_.GetParameters();
|
2018-02-12 11:36:18 +01: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.
|
2018-02-12 11:36:18 +01:00
|
|
|
parameters.set_objective_upper_limit(
|
|
|
|
|
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.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): Put more at the root, and less afterwards?
|
|
|
|
|
parameters.set_max_number_of_iterations(500);
|
2018-11-05 16:24:47 +01:00
|
|
|
if (sat_parameters_.use_exact_lp_reason()) {
|
|
|
|
|
parameters.set_change_status_to_imprecise(false);
|
|
|
|
|
parameters.set_primal_feasibility_tolerance(1e-7);
|
|
|
|
|
parameters.set_dual_feasibility_tolerance(1e-7);
|
|
|
|
|
}
|
2017-07-12 11:38:46 -07:00
|
|
|
|
|
|
|
|
simplex_.SetParameters(parameters);
|
2017-10-18 11:09:13 +02:00
|
|
|
simplex_.NotifyThatMatrixIsUnchangedForNextSolve();
|
2018-12-03 14:26:31 +01:00
|
|
|
if (!SolveLp()) return true;
|
2017-03-28 16:11:06 +02:00
|
|
|
|
2018-12-04 14:36:46 +01:00
|
|
|
// Add new constraints to the LP and resolve?
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): We might want to do that more than once. Currently we rely on
|
|
|
|
|
// this beeing called again on the next IncrementalPropagate() call, but that
|
|
|
|
|
// might not always happen at level zero.
|
2018-12-03 14:26:31 +01:00
|
|
|
if (simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL) {
|
2018-12-04 14:36:46 +01:00
|
|
|
// First add any new lazy constraints or cuts that where previsouly
|
|
|
|
|
// generated and are now cutting the current solution.
|
|
|
|
|
if (constraint_manager_.ChangeLp(expanded_lp_solution_)) {
|
2018-12-03 14:26:31 +01:00
|
|
|
CreateLpFromConstraintManager();
|
|
|
|
|
if (!SolveLp()) return true;
|
2018-12-04 14:36:46 +01:00
|
|
|
} else {
|
|
|
|
|
// Try to add cuts.
|
|
|
|
|
if (!cut_generators_.empty() &&
|
|
|
|
|
num_cuts_ < sat_parameters_.max_num_cuts() &&
|
|
|
|
|
(trail_->CurrentDecisionLevel() == 0 ||
|
|
|
|
|
!sat_parameters_.only_add_cuts_at_level_zero())) {
|
|
|
|
|
int num_new_cuts = 0;
|
|
|
|
|
for (const CutGenerator& generator : cut_generators_) {
|
|
|
|
|
// TODO(user): Change api so cuts can directly be added to the manager
|
|
|
|
|
// and we don't need this intermediate vector.
|
|
|
|
|
std::vector<LinearConstraint> cuts =
|
|
|
|
|
generator.generate_cuts(expanded_lp_solution_);
|
|
|
|
|
|
|
|
|
|
// Add the cuts to the manager.
|
|
|
|
|
for (const LinearConstraint& cut : cuts) {
|
|
|
|
|
++num_new_cuts;
|
|
|
|
|
constraint_manager_.Add(cut);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (num_new_cuts > 0) {
|
|
|
|
|
num_cuts_ += num_new_cuts;
|
2018-12-06 17:16:54 +01:00
|
|
|
VLOG(1) << "#cuts " << num_cuts_;
|
2018-12-04 14:36:46 +01:00
|
|
|
|
|
|
|
|
if (constraint_manager_.ChangeLp(expanded_lp_solution_)) {
|
|
|
|
|
CreateLpFromConstraintManager();
|
|
|
|
|
if (!SolveLp()) return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-18 11:09:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 16:11:06 +02:00
|
|
|
// A dual-unbounded problem is infeasible. We use the dual ray reason.
|
|
|
|
|
if (simplex_.GetProblemStatus() == glop::ProblemStatus::DUAL_UNBOUNDED) {
|
2018-11-05 16:24:47 +01:00
|
|
|
if (sat_parameters_.use_exact_lp_reason()) {
|
|
|
|
|
if (!FillExactDualRayReason()) return true;
|
|
|
|
|
} else {
|
|
|
|
|
FillDualRayReason();
|
|
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
return integer_trail_->ReportConflict(integer_reason_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Optimality deductions if problem has an objective.
|
|
|
|
|
if (objective_is_defined_ &&
|
2017-07-12 11:38:46 -07:00
|
|
|
(simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL ||
|
|
|
|
|
simplex_.GetProblemStatus() == glop::ProblemStatus::DUAL_FEASIBLE)) {
|
2017-06-28 14:33:56 +02:00
|
|
|
// 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.
|
|
|
|
|
const double relaxed_optimal_objective = simplex_.GetObjectiveValue();
|
2018-11-05 16:24:47 +01:00
|
|
|
const IntegerValue approximate_new_lb(
|
|
|
|
|
static_cast<int64>(std::ceil(relaxed_optimal_objective - kCpEpsilon)));
|
|
|
|
|
|
|
|
|
|
// TODO(user): Maybe do a bit less computation when we cannot propagate
|
|
|
|
|
// anything.
|
|
|
|
|
if (sat_parameters_.use_exact_lp_reason()) {
|
2018-12-21 13:59:58 +01:00
|
|
|
if (!ExactLpReasonning()) return false;
|
2018-02-12 11:36:18 +01:00
|
|
|
|
2018-11-05 16:24:47 +01:00
|
|
|
// A difference of 1 happens relatively often, so we just display when
|
2018-12-21 13:59:58 +01:00
|
|
|
// there is more.
|
|
|
|
|
const IntegerValue propagated_lb =
|
|
|
|
|
integer_trail_->LowerBound(objective_cp_);
|
|
|
|
|
if (std::abs((approximate_new_lb - propagated_lb).value()) > 1) {
|
2018-12-06 17:16:54 +01:00
|
|
|
VLOG(2) << "LP objective lower bound approx = " << approximate_new_lb;
|
2018-12-21 13:59:58 +01:00
|
|
|
VLOG(2) << " exact = " << propagated_lb;
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2017-06-28 14:33:56 +02:00
|
|
|
FillReducedCostsReason();
|
2018-11-05 16:24:47 +01:00
|
|
|
const double objective_cp_ub =
|
|
|
|
|
ToDouble(integer_trail_->UpperBound(objective_cp_));
|
|
|
|
|
ReducedCostStrengtheningDeductions(objective_cp_ub -
|
|
|
|
|
relaxed_optimal_objective);
|
|
|
|
|
if (!deductions_.empty()) {
|
|
|
|
|
deductions_reason_ = integer_reason_;
|
|
|
|
|
deductions_reason_.push_back(
|
|
|
|
|
integer_trail_->UpperBoundAsLiteral(objective_cp_));
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 13:59:58 +01:00
|
|
|
// Push new objective lb.
|
|
|
|
|
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;
|
|
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
2018-12-21 13:59:58 +01:00
|
|
|
// 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;
|
|
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 14:36:46 +01:00
|
|
|
// Copy more info about the current solution.
|
2017-07-12 11:38:46 -07:00
|
|
|
if (simplex_.GetProblemStatus() == glop::ProblemStatus::OPTIMAL) {
|
2018-12-04 14:36:46 +01:00
|
|
|
CHECK(lp_solution_is_set_);
|
|
|
|
|
|
2018-01-10 13:21:06 +01:00
|
|
|
lp_objective_ = simplex_.GetObjectiveValue();
|
|
|
|
|
lp_solution_is_integer_ = true;
|
2018-02-12 11:36:18 +01:00
|
|
|
const int num_vars = integer_variables_.size();
|
2018-12-04 14:36:46 +01:00
|
|
|
const double objective_scale = lp_data_.objective_scaling_factor();
|
2017-07-12 11:38:46 -07:00
|
|
|
for (int i = 0; i < num_vars; i++) {
|
2018-02-12 11:36:18 +01:00
|
|
|
// The reduced cost need to be divided by LpToCpScalingFactor().
|
2018-01-10 13:21:06 +01:00
|
|
|
lp_reduced_cost_[i] = simplex_.GetReducedCost(glop::ColIndex(i)) *
|
2018-02-12 11:36:18 +01:00
|
|
|
CpToLpScalingFactor(glop::ColIndex(i)) *
|
2017-08-03 10:20:59 -07:00
|
|
|
objective_scale;
|
2018-02-12 11:36:18 +01:00
|
|
|
if (std::abs(lp_solution_[i] - std::round(lp_solution_[i])) >
|
|
|
|
|
kCpEpsilon) {
|
2018-01-10 13:21:06 +01:00
|
|
|
lp_solution_is_integer_ = false;
|
|
|
|
|
}
|
2017-07-12 11:38:46 -07:00
|
|
|
}
|
2018-02-12 11:36:18 +01:00
|
|
|
|
|
|
|
|
if (compute_reduced_cost_averages_) {
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
num_calls_since_reduced_cost_averages_reset_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accumulate pseudo-costs of all unassigned variables.
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
|
|
|
|
const IntegerVariable var = this->integer_variables_[i];
|
|
|
|
|
|
|
|
|
|
// Skip ignored and fixed variables.
|
|
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
|
|
|
|
const IntegerValue lb = integer_trail_->LowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->UpperBound(var);
|
|
|
|
|
if (lb == ub) continue;
|
|
|
|
|
|
|
|
|
|
// Skip reduced costs that are zero or close.
|
|
|
|
|
const double rc = this->GetSolutionReducedCost(var);
|
|
|
|
|
if (std::abs(rc) < kCpEpsilon) continue;
|
|
|
|
|
|
|
|
|
|
if (rc < 0.0) {
|
|
|
|
|
sum_cost_down_[i] -= rc;
|
|
|
|
|
num_cost_down_[i]++;
|
|
|
|
|
} else {
|
|
|
|
|
sum_cost_up_[i] += rc;
|
|
|
|
|
num_cost_up_[i]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 16:24:47 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
std::vector<std::pair<ColIndex, IntegerValue>> GetSparseRepresentation(
|
|
|
|
|
const gtl::ITIVector<ColIndex, IntegerValue>& dense_vector) {
|
|
|
|
|
std::vector<std::pair<ColIndex, IntegerValue>> result;
|
|
|
|
|
for (ColIndex col(0); col < dense_vector.size(); ++col) {
|
|
|
|
|
if (dense_vector[col] != 0) {
|
|
|
|
|
result.push_back({col, dense_vector[col]});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns false in case of overflow
|
|
|
|
|
bool AddLinearExpressionMultiple(
|
|
|
|
|
IntegerValue multiplier,
|
|
|
|
|
const std::vector<std::pair<ColIndex, IntegerValue>>& terms,
|
|
|
|
|
gtl::ITIVector<ColIndex, IntegerValue>* dense_vector) {
|
|
|
|
|
for (const std::pair<ColIndex, IntegerValue> term : terms) {
|
|
|
|
|
const int64 prod = CapProd(multiplier.value(), term.second.value());
|
|
|
|
|
if (prod == kint64min || prod == kint64max) return false;
|
|
|
|
|
const int64 result = CapAdd((*dense_vector)[term.first].value(), prod);
|
|
|
|
|
if (result == kint64min || result == kint64max) return false;
|
|
|
|
|
(*dense_vector)[term.first] = IntegerValue(result);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
// Returns kMinIntegerValue in case of overflow.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): To avoid overflow, we could relax the constraint Sum term <= ub
|
|
|
|
|
// with Sum floor(term/divisor) <= floor(ub/divisor). It will be less precise,
|
|
|
|
|
// but we should be able to avoid overlow.
|
|
|
|
|
IntegerValue LinearProgrammingConstraint::GetImpliedLowerBound(
|
|
|
|
|
const LinearExpression& terms) const {
|
|
|
|
|
IntegerValue lower_bound(0);
|
|
|
|
|
for (const auto term : terms) {
|
|
|
|
|
const IntegerVariable var = integer_variables_[term.first.value()];
|
|
|
|
|
const IntegerValue coeff = term.second;
|
|
|
|
|
CHECK_NE(coeff, 0);
|
|
|
|
|
const IntegerValue bound = coeff > 0 ? integer_trail_->LowerBound(var)
|
|
|
|
|
: integer_trail_->UpperBound(var);
|
|
|
|
|
const int64 prod = CapProd(bound.value(), coeff.value());
|
|
|
|
|
if (prod == kint64min || prod == kint64max) return kMinIntegerValue;
|
|
|
|
|
const int64 new_lb = CapAdd(lower_bound.value(), prod);
|
|
|
|
|
if (new_lb == kint64min || new_lb == kint64max) return kMinIntegerValue;
|
|
|
|
|
lower_bound = new_lb;
|
|
|
|
|
}
|
|
|
|
|
return lower_bound;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 13:59:58 +01:00
|
|
|
bool LinearProgrammingConstraint::PossibleOverflow(
|
|
|
|
|
const std::vector<IntegerVariable>& vars,
|
|
|
|
|
const std::vector<IntegerValue>& coeffs, IntegerValue ub) {
|
|
|
|
|
IntegerValue lower_bound(0);
|
|
|
|
|
const int size = vars.size();
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
const IntegerVariable var = vars[i];
|
|
|
|
|
const IntegerValue coeff = coeffs[i];
|
|
|
|
|
CHECK_NE(coeff, 0);
|
|
|
|
|
const IntegerValue bound = coeff > 0 ? integer_trail_->LowerBound(var)
|
|
|
|
|
: integer_trail_->UpperBound(var);
|
|
|
|
|
const int64 prod = CapProd(bound.value(), coeff.value());
|
|
|
|
|
if (prod == kint64min || prod == kint64max) return true;
|
|
|
|
|
const int64 new_lb = CapAdd(lower_bound.value(), prod);
|
|
|
|
|
if (new_lb == kint64min || new_lb == kint64max) return true;
|
|
|
|
|
lower_bound = new_lb;
|
|
|
|
|
}
|
|
|
|
|
const int64 slack = CapAdd(lower_bound.value(), -ub.value());
|
|
|
|
|
if (slack == kint64min || slack == kint64max) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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(
|
|
|
|
|
const LinearExpression& terms, IntegerValue slack) {
|
|
|
|
|
integer_reason_.clear();
|
|
|
|
|
std::vector<IntegerValue> magnitudes;
|
|
|
|
|
for (const auto term : terms) {
|
|
|
|
|
const IntegerVariable var = integer_variables_[term.first.value()];
|
|
|
|
|
const IntegerValue coeff = term.second;
|
|
|
|
|
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_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(user): Provide a sparse interface.
|
|
|
|
|
bool LinearProgrammingConstraint::ComputeNewLinearConstraint(
|
|
|
|
|
bool take_objective_into_account,
|
|
|
|
|
const glop::DenseColumn& dense_lp_multipliers, Fractional* scaling,
|
|
|
|
|
gtl::ITIVector<ColIndex, IntegerValue>* dense_terms,
|
|
|
|
|
IntegerValue* upper_bound) const {
|
|
|
|
|
// Process the dense_lp_multipliers and compute their infinity norm.
|
|
|
|
|
std::vector<std::pair<RowIndex, Fractional>> lp_multipliers;
|
|
|
|
|
Fractional lp_multipliers_norm = take_objective_into_account ? 1.0 : 0.0;
|
|
|
|
|
for (RowIndex row(0); row < dense_lp_multipliers.size(); ++row) {
|
|
|
|
|
const Fractional lp_multi = dense_lp_multipliers[row];
|
|
|
|
|
if (lp_multi == 0.0) continue;
|
|
|
|
|
|
|
|
|
|
// Remove trivial bad cases.
|
|
|
|
|
if (lp_multi > 0.0 && integer_lp_[row.value()].ub >= kMaxIntegerValue) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (lp_multi < 0.0 && integer_lp_[row.value()].lb <= kMinIntegerValue) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
lp_multipliers_norm = std::max(lp_multipliers_norm, std::abs(lp_multi));
|
|
|
|
|
lp_multipliers.push_back({row, lp_multi});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This scaling will be responsible to keep the wanted number of precision
|
|
|
|
|
// digit when we round a floating point value to integer. We will want
|
|
|
|
|
// around 6 digits for the larger lp_multi and less for the smaller ones.
|
|
|
|
|
*scaling = 1.0;
|
|
|
|
|
|
|
|
|
|
// Scale the lp_multipliers to the CP world (still Fractional though).
|
|
|
|
|
std::vector<std::pair<RowIndex, Fractional>> cp_multipliers;
|
2018-12-11 17:03:03 +01:00
|
|
|
Fractional max_cp_multi = 0.0;
|
2018-11-05 16:24:47 +01:00
|
|
|
Fractional min_cp_multi = glop::kInfinity;
|
|
|
|
|
const Fractional global_scaling =
|
|
|
|
|
bound_scaling_factor_ / lp_data_.objective_scaling_factor();
|
|
|
|
|
for (const auto entry : lp_multipliers) {
|
|
|
|
|
const RowIndex row = entry.first;
|
|
|
|
|
const Fractional lp_multi = entry.second;
|
|
|
|
|
|
|
|
|
|
// The LP guarantee about 6 digits of precision, so we ignore anything
|
|
|
|
|
// smaller that lp_multipliers_norm * 1e-6.
|
|
|
|
|
const Fractional magnitude_diff = lp_multipliers_norm / std::abs(lp_multi);
|
|
|
|
|
if (magnitude_diff > 1e6) continue;
|
|
|
|
|
|
|
|
|
|
// Scale back in the cp world.
|
|
|
|
|
const Fractional cp_multi =
|
|
|
|
|
lp_multi / scaler_.row_scale(row) / global_scaling;
|
|
|
|
|
|
|
|
|
|
// We want std::round(cp_multi * scaling) to have the same number of
|
|
|
|
|
// digits of relative precision as lp_multi.
|
|
|
|
|
const Fractional wanted_scaling =
|
|
|
|
|
(1e6 / magnitude_diff) / std::abs(cp_multi);
|
|
|
|
|
*scaling = std::max(*scaling, wanted_scaling);
|
|
|
|
|
|
2018-12-11 17:03:03 +01:00
|
|
|
max_cp_multi = std::max(std::abs(cp_multi), max_cp_multi);
|
2018-11-05 16:24:47 +01:00
|
|
|
min_cp_multi = std::min(std::abs(cp_multi), min_cp_multi);
|
|
|
|
|
cp_multipliers.push_back({row, cp_multi});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This behave exactly like if we had another "objective" constraint with
|
|
|
|
|
// an lp_multi of 1.0 and a cp_multi of 1.0.
|
|
|
|
|
if (take_objective_into_account) {
|
|
|
|
|
*scaling = std::max(*scaling, 1e6 / lp_multipliers_norm);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 14:25:52 +01:00
|
|
|
// Make sure the scaled coeff are not too big so that they can fit on
|
|
|
|
|
// an IntegerValue. Since 1<<63 is around 9.2e18, we use 1e18 here.
|
2018-12-11 17:03:03 +01:00
|
|
|
if (max_cp_multi > 0.0) {
|
2018-12-14 14:25:52 +01:00
|
|
|
*scaling = std::min(*scaling, 1e18 / max_cp_multi);
|
2018-12-11 17:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-05 16:24:47 +01:00
|
|
|
// Scale the multipliers by *scaling.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): Maybe use int128 to avoid overflow?
|
|
|
|
|
// TODO(user): Divide dual by gcd to limit overflow?
|
|
|
|
|
// TODO(user): To avoid overflow, we could lower scaling at the cost of
|
|
|
|
|
// loosing precision.
|
|
|
|
|
std::vector<std::pair<RowIndex, IntegerValue>> integer_multipliers;
|
|
|
|
|
for (const auto entry : cp_multipliers) {
|
|
|
|
|
const IntegerValue coeff(std::round(entry.second * (*scaling)));
|
|
|
|
|
if (coeff != 0) integer_multipliers.push_back({entry.first, coeff});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize the new constraint.
|
|
|
|
|
*upper_bound = 0;
|
|
|
|
|
dense_terms->assign(integer_variables_.size(), IntegerValue(0));
|
|
|
|
|
|
|
|
|
|
// Compute the new constraint by taking the linear combination given by
|
|
|
|
|
// integer_multipliers of the integer constraints in integer_lp_.
|
|
|
|
|
const ColIndex num_cols(integer_variables_.size());
|
|
|
|
|
for (const std::pair<RowIndex, IntegerValue> term : integer_multipliers) {
|
|
|
|
|
const RowIndex row = term.first;
|
|
|
|
|
const IntegerValue multiplier = term.second;
|
|
|
|
|
CHECK_LT(row, integer_lp_.size());
|
|
|
|
|
|
|
|
|
|
// Update the constraint.
|
|
|
|
|
if (!AddLinearExpressionMultiple(multiplier, integer_lp_[row.value()].terms,
|
|
|
|
|
dense_terms)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the upper bound.
|
|
|
|
|
const int64 bound = multiplier > 0 ? integer_lp_[row.value()].ub.value()
|
|
|
|
|
: integer_lp_[row.value()].lb.value();
|
|
|
|
|
const int64 prod = CapProd(multiplier.value(), bound);
|
|
|
|
|
if (prod == kint64min || prod == kint64max) return false;
|
|
|
|
|
const int64 result = CapAdd((*upper_bound).value(), prod);
|
|
|
|
|
if (result == kint64min || result == kint64max) return false;
|
|
|
|
|
(*upper_bound) = IntegerValue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
glop::DenseColumn lp_multipliers(num_rows);
|
|
|
|
|
for (RowIndex row(0); row < num_rows; ++row) {
|
|
|
|
|
lp_multipliers[row] = -simplex_.GetDualValue(row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Fractional scaling;
|
|
|
|
|
gtl::ITIVector<ColIndex, IntegerValue> reduced_costs;
|
2018-12-21 13:59:58 +01:00
|
|
|
IntegerValue rc_ub;
|
2018-11-05 16:24:47 +01:00
|
|
|
if (!ComputeNewLinearConstraint(/*take_objective_into_account=*/true,
|
|
|
|
|
lp_multipliers, &scaling, &reduced_costs,
|
2018-12-21 13:59:58 +01:00
|
|
|
&rc_ub)) {
|
|
|
|
|
VLOG(2) << "Overflow during exact LP reasoning.";
|
|
|
|
|
return true;
|
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.
|
|
|
|
|
const IntegerValue obj_scale(std::round(scaling));
|
2018-12-11 17:03:03 +01:00
|
|
|
if (obj_scale == 0) {
|
2018-12-21 13:59:58 +01:00
|
|
|
VLOG(2) << "Overflow during exact LP reasoning.";
|
|
|
|
|
return true;
|
2018-12-11 17:03:03 +01:00
|
|
|
}
|
2018-11-05 16:24:47 +01:00
|
|
|
if (!AddLinearExpressionMultiple(obj_scale, integer_objective_,
|
|
|
|
|
&reduced_costs)) {
|
2018-12-21 13:59:58 +01:00
|
|
|
VLOG(2) << "Overflow during exact LP reasoning.";
|
|
|
|
|
return true;
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(user): We could correct little imprecision by heuristically computing
|
|
|
|
|
// for each row the best multiple to improve the scaled_objective_lb below
|
2018-12-21 13:59:58 +01:00
|
|
|
// while keeping the coefficient of the same sign. This should improve the
|
2018-11-05 16:24:47 +01:00
|
|
|
// objective lower bound.
|
|
|
|
|
|
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.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): Make sure there cannot be any overflow if we want to reuse the
|
|
|
|
|
// constraint for different lower-bounds of the variables later.
|
|
|
|
|
std::vector<IntegerVariable> vars;
|
|
|
|
|
std::vector<IntegerValue> coeffs;
|
|
|
|
|
for (ColIndex col(0); col < reduced_costs.size(); ++col) {
|
|
|
|
|
if (reduced_costs[col] != 0) {
|
|
|
|
|
vars.push_back(integer_variables_[col.value()]);
|
|
|
|
|
coeffs.push_back(reduced_costs[col]);
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
2018-12-21 13:59:58 +01:00
|
|
|
}
|
|
|
|
|
vars.push_back(objective_cp_);
|
|
|
|
|
coeffs.push_back(-obj_scale);
|
2018-11-05 16:24:47 +01:00
|
|
|
|
2018-12-21 13:59:58 +01:00
|
|
|
// Check for possible overflow in IntegerSumLE::Propagate().
|
|
|
|
|
if (PossibleOverflow(vars, coeffs, rc_ub)) {
|
|
|
|
|
VLOG(2) << "Overflow during exact LP reasoning.";
|
|
|
|
|
return true;
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-21 13:59:58 +01:00
|
|
|
IntegerSumLE* cp_constraint =
|
|
|
|
|
new IntegerSumLE({}, vars, coeffs, rc_ub, model_);
|
|
|
|
|
optimal_constraints_.emplace_back(cp_constraint);
|
|
|
|
|
rev_optimal_constraints_size_ = optimal_constraints_.size();
|
|
|
|
|
return cp_constraint->Propagate();
|
2018-11-05 16:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LinearProgrammingConstraint::FillExactDualRayReason() {
|
|
|
|
|
Fractional scaling;
|
|
|
|
|
gtl::ITIVector<ColIndex, IntegerValue> dense_new_constraint;
|
|
|
|
|
IntegerValue new_constraint_ub;
|
|
|
|
|
if (!ComputeNewLinearConstraint(/*take_objective_into_account=*/false,
|
|
|
|
|
simplex_.GetDualRay(), &scaling,
|
|
|
|
|
&dense_new_constraint, &new_constraint_ub)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const LinearExpression new_constraint =
|
|
|
|
|
GetSparseRepresentation(dense_new_constraint);
|
|
|
|
|
const IntegerValue implied_lb = GetImpliedLowerBound(new_constraint);
|
|
|
|
|
if (implied_lb <= new_constraint_ub) {
|
2018-12-17 16:50:15 +01:00
|
|
|
VLOG(1) << "LP exact dual ray not infeasible,"
|
|
|
|
|
<< " implied_lb: " << implied_lb.value() / scaling
|
|
|
|
|
<< " ub: " << new_constraint_ub.value() / scaling;
|
2018-11-05 16:24:47 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const IntegerValue slack = (implied_lb - new_constraint_ub) - 1;
|
|
|
|
|
SetImpliedLowerBoundReason(new_constraint, slack);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 14:33:56 +02:00
|
|
|
void LinearProgrammingConstraint::FillReducedCostsReason() {
|
2017-03-28 16:11:06 +02:00
|
|
|
integer_reason_.clear();
|
|
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
2018-01-10 13:21:06 +01:00
|
|
|
const double rc = simplex_.GetReducedCost(glop::ColIndex(i));
|
2018-02-12 11:36:18 +01:00
|
|
|
if (rc > kLpEpsilon) {
|
2017-03-28 16:11:06 +02:00
|
|
|
integer_reason_.push_back(
|
|
|
|
|
integer_trail_->LowerBoundAsLiteral(integer_variables_[i]));
|
2018-02-12 11:36:18 +01:00
|
|
|
} else if (rc < -kLpEpsilon) {
|
2017-03-28 16:11:06 +02:00
|
|
|
integer_reason_.push_back(
|
|
|
|
|
integer_trail_->UpperBoundAsLiteral(integer_variables_[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-05 16:24:47 +01:00
|
|
|
|
|
|
|
|
integer_trail_->RemoveLevelZeroBounds(&integer_reason_);
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LinearProgrammingConstraint::FillDualRayReason() {
|
|
|
|
|
integer_reason_.clear();
|
|
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
for (int i = 0; i < num_vars; i++) {
|
2017-06-28 14:33:56 +02:00
|
|
|
// TODO(user): Like for FillReducedCostsReason(), the bounds could be
|
2017-03-28 16:11:06 +02:00
|
|
|
// extended here. Actually, the "dual ray cost updates" is the reduced cost
|
|
|
|
|
// of an optimal solution if we were optimizing one direction of one basic
|
|
|
|
|
// variable. The simplex_ interface would need to be slightly extended to
|
|
|
|
|
// retrieve the basis column in question and the variable values though.
|
2018-01-10 13:21:06 +01:00
|
|
|
const double rc = simplex_.GetDualRayRowCombination()[glop::ColIndex(i)];
|
2018-02-12 11:36:18 +01:00
|
|
|
if (rc > kLpEpsilon) {
|
2017-03-28 16:11:06 +02:00
|
|
|
integer_reason_.push_back(
|
|
|
|
|
integer_trail_->LowerBoundAsLiteral(integer_variables_[i]));
|
2018-02-12 11:36:18 +01:00
|
|
|
} else if (rc < -kLpEpsilon) {
|
2017-03-28 16:11:06 +02:00
|
|
|
integer_reason_.push_back(
|
|
|
|
|
integer_trail_->UpperBoundAsLiteral(integer_variables_[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-05 16:24:47 +01:00
|
|
|
integer_trail_->RemoveLevelZeroBounds(&integer_reason_);
|
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
|
|
|
|
|
|
|
|
if (rc == 0.0) continue;
|
2017-03-28 16:11:06 +02:00
|
|
|
const double lp_other_bound = value + lp_objective_delta / rc;
|
2018-02-12 11:36:18 +01:00
|
|
|
const double cp_other_bound = lp_other_bound * LpToCpScalingFactor(lp_var);
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 15:45:52 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// TODO(user): we could use a sparser algorithm, even if this do not seems to
|
|
|
|
|
// matter for now.
|
|
|
|
|
void AddIncomingAndOutgoingCutsIfNeeded(
|
2018-01-10 13:21:06 +01:00
|
|
|
int num_nodes, const std::vector<int>& s, const std::vector<int>& tails,
|
2017-11-07 15:45:52 +01:00
|
|
|
const std::vector<int>& heads, const std::vector<IntegerVariable>& vars,
|
2018-12-03 14:26:31 +01:00
|
|
|
const std::vector<double>& var_lp_values, int64 rhs_lower_bound,
|
2017-11-07 15:45:52 +01:00
|
|
|
std::vector<LinearConstraint>* cuts) {
|
|
|
|
|
LinearConstraint incoming;
|
|
|
|
|
LinearConstraint outgoing;
|
|
|
|
|
double sum_incoming = 0.0;
|
|
|
|
|
double sum_outgoing = 0.0;
|
2018-11-05 16:24:47 +01:00
|
|
|
incoming.lb = outgoing.lb = IntegerValue(rhs_lower_bound);
|
|
|
|
|
incoming.ub = outgoing.ub = kMaxIntegerValue;
|
2017-11-07 15:45:52 +01:00
|
|
|
const std::set<int> subset(s.begin(), s.end());
|
2018-01-10 13:21:06 +01:00
|
|
|
|
|
|
|
|
// Add incoming/outgoing cut arcs, compute flow through cuts.
|
2017-11-07 15:45:52 +01:00
|
|
|
for (int i = 0; i < tails.size(); ++i) {
|
2018-04-11 13:00:30 +02:00
|
|
|
const bool out = gtl::ContainsKey(subset, tails[i]);
|
|
|
|
|
const bool in = gtl::ContainsKey(subset, heads[i]);
|
2017-11-07 15:45:52 +01:00
|
|
|
if (out && in) continue;
|
|
|
|
|
if (out) {
|
2018-12-03 14:26:31 +01:00
|
|
|
sum_outgoing += var_lp_values[i];
|
2017-11-07 15:45:52 +01:00
|
|
|
outgoing.vars.push_back(vars[i]);
|
2018-11-05 16:24:47 +01:00
|
|
|
outgoing.coeffs.push_back(IntegerValue(1));
|
2017-11-07 15:45:52 +01:00
|
|
|
}
|
|
|
|
|
if (in) {
|
2018-12-03 14:26:31 +01:00
|
|
|
sum_incoming += var_lp_values[i];
|
2017-11-07 15:45:52 +01:00
|
|
|
incoming.vars.push_back(vars[i]);
|
2018-11-05 16:24:47 +01:00
|
|
|
incoming.coeffs.push_back(IntegerValue(1));
|
2017-11-07 15:45:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-01-10 13:21:06 +01:00
|
|
|
|
|
|
|
|
// A node is said to be optional if it can be excluded from the subcircuit,
|
|
|
|
|
// in which case there is a self-loop on that node.
|
|
|
|
|
// If there are optional nodes, use extended formula:
|
|
|
|
|
// sum(cut) >= 1 - optional_loop_in - optional_loop_out
|
|
|
|
|
// where optional_loop_in's node is in subset, optional_loop_out's is out.
|
|
|
|
|
// TODO(user): Favor optional loops fixed to zero at root.
|
|
|
|
|
int num_optional_nodes_in = 0;
|
|
|
|
|
int num_optional_nodes_out = 0;
|
|
|
|
|
int optional_loop_in = -1;
|
|
|
|
|
int optional_loop_out = -1;
|
|
|
|
|
for (int i = 0; i < tails.size(); ++i) {
|
|
|
|
|
if (tails[i] != heads[i]) continue;
|
2018-04-11 13:00:30 +02:00
|
|
|
if (gtl::ContainsKey(subset, tails[i])) {
|
2018-01-10 13:21:06 +01:00
|
|
|
num_optional_nodes_in++;
|
|
|
|
|
if (optional_loop_in == -1 ||
|
2018-12-03 14:26:31 +01:00
|
|
|
var_lp_values[i] < var_lp_values[optional_loop_in]) {
|
2018-01-10 13:21:06 +01:00
|
|
|
optional_loop_in = i;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
num_optional_nodes_out++;
|
|
|
|
|
if (optional_loop_out == -1 ||
|
2018-12-03 14:26:31 +01:00
|
|
|
var_lp_values[i] < var_lp_values[optional_loop_out]) {
|
2018-01-10 13:21:06 +01:00
|
|
|
optional_loop_out = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (num_optional_nodes_in + num_optional_nodes_out > 0) {
|
|
|
|
|
CHECK_EQ(rhs_lower_bound, 1);
|
|
|
|
|
// When all optionals of one side are excluded in lp solution, no cut.
|
|
|
|
|
if (num_optional_nodes_in == subset.size() &&
|
|
|
|
|
(optional_loop_in == -1 ||
|
2018-12-03 14:26:31 +01:00
|
|
|
var_lp_values[optional_loop_in] > 1.0 - 1e-6)) {
|
2018-01-10 13:21:06 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (num_optional_nodes_out == num_nodes - subset.size() &&
|
|
|
|
|
(optional_loop_out == -1 ||
|
2018-12-03 14:26:31 +01:00
|
|
|
var_lp_values[optional_loop_out] > 1.0 - 1e-6)) {
|
2018-01-10 13:21:06 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// There is no mandatory node in subset, add optional_loop_in.
|
|
|
|
|
if (num_optional_nodes_in == subset.size()) {
|
|
|
|
|
incoming.vars.push_back(vars[optional_loop_in]);
|
2018-11-05 16:24:47 +01:00
|
|
|
incoming.coeffs.push_back(IntegerValue(1));
|
2018-12-03 14:26:31 +01:00
|
|
|
sum_incoming += var_lp_values[optional_loop_in];
|
2018-01-10 13:21:06 +01:00
|
|
|
|
|
|
|
|
outgoing.vars.push_back(vars[optional_loop_in]);
|
2018-11-05 16:24:47 +01:00
|
|
|
outgoing.coeffs.push_back(IntegerValue(1));
|
2018-12-03 14:26:31 +01:00
|
|
|
sum_outgoing += var_lp_values[optional_loop_in];
|
2018-01-10 13:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// There is no mandatory node out of subset, add optional_loop_out.
|
|
|
|
|
if (num_optional_nodes_out == num_nodes - subset.size()) {
|
|
|
|
|
incoming.vars.push_back(vars[optional_loop_out]);
|
2018-11-05 16:24:47 +01:00
|
|
|
incoming.coeffs.push_back(IntegerValue(1));
|
2018-12-03 14:26:31 +01:00
|
|
|
sum_incoming += var_lp_values[optional_loop_out];
|
2018-01-10 13:21:06 +01:00
|
|
|
|
|
|
|
|
outgoing.vars.push_back(vars[optional_loop_out]);
|
2018-11-05 16:24:47 +01:00
|
|
|
outgoing.coeffs.push_back(IntegerValue(1));
|
2018-12-03 14:26:31 +01:00
|
|
|
sum_outgoing += var_lp_values[optional_loop_out];
|
2018-01-10 13:21:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 15:45:52 +01:00
|
|
|
if (sum_incoming < rhs_lower_bound - 1e-6) {
|
|
|
|
|
cuts->push_back(std::move(incoming));
|
|
|
|
|
}
|
|
|
|
|
if (sum_outgoing < rhs_lower_bound - 1e-6) {
|
|
|
|
|
cuts->push_back(std::move(outgoing));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2017-10-18 11:09:13 +02:00
|
|
|
// We use a basic algorithm to detect components that are not connected to the
|
|
|
|
|
// rest of the graph in the LP solution, and add cuts to force some arcs to
|
|
|
|
|
// enter and leave this component from outside.
|
|
|
|
|
CutGenerator CreateStronglyConnectedGraphCutGenerator(
|
|
|
|
|
int num_nodes, const std::vector<int>& tails, const std::vector<int>& heads,
|
|
|
|
|
const std::vector<IntegerVariable>& vars) {
|
|
|
|
|
CutGenerator result;
|
|
|
|
|
result.vars = vars;
|
2018-12-03 14:26:31 +01:00
|
|
|
result.generate_cuts =
|
|
|
|
|
[num_nodes, tails, heads,
|
|
|
|
|
vars](const gtl::ITIVector<IntegerVariable, double>& lp_values) {
|
|
|
|
|
int num_arcs_in_lp_solution = 0;
|
|
|
|
|
std::vector<double> var_lp_values;
|
|
|
|
|
std::vector<std::vector<int>> graph(num_nodes);
|
|
|
|
|
for (int i = 0; i < vars.size(); ++i) {
|
|
|
|
|
var_lp_values.push_back(lp_values[vars[i]]);
|
|
|
|
|
|
|
|
|
|
// TODO(user): a more advanced algorithm consist of adding the arcs
|
|
|
|
|
// in the decreasing order of their lp_values, and for each strongly
|
|
|
|
|
// connected components S along the way, try to add the corresponding
|
|
|
|
|
// cuts. We can stop as soon as there is only two components left,
|
|
|
|
|
// after adding the corresponding cut.
|
|
|
|
|
if (lp_values[vars[i]] > 1e-6) {
|
|
|
|
|
++num_arcs_in_lp_solution;
|
|
|
|
|
graph[tails[i]].push_back(heads[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::vector<LinearConstraint> cuts;
|
|
|
|
|
std::vector<std::vector<int>> components;
|
|
|
|
|
FindStronglyConnectedComponents(num_nodes, graph, &components);
|
|
|
|
|
if (components.size() == 1) return cuts;
|
|
|
|
|
|
|
|
|
|
VLOG(1) << "num_arcs_in_lp_solution:" << num_arcs_in_lp_solution
|
|
|
|
|
<< " sccs:" << components.size();
|
|
|
|
|
for (const std::vector<int>& component : components) {
|
|
|
|
|
if (component.size() == 1) continue;
|
|
|
|
|
AddIncomingAndOutgoingCutsIfNeeded(num_nodes, component, tails, heads,
|
|
|
|
|
vars, var_lp_values,
|
|
|
|
|
/*rhs_lower_bound=*/1, &cuts);
|
|
|
|
|
|
|
|
|
|
// In this case, the cuts for each component are the same.
|
|
|
|
|
if (components.size() == 2) break;
|
|
|
|
|
}
|
|
|
|
|
return cuts;
|
|
|
|
|
};
|
2017-11-07 15:45:52 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CutGenerator CreateCVRPCutGenerator(int num_nodes,
|
|
|
|
|
const std::vector<int>& tails,
|
|
|
|
|
const std::vector<int>& heads,
|
|
|
|
|
const std::vector<IntegerVariable>& vars,
|
|
|
|
|
const std::vector<int64>& demands,
|
|
|
|
|
int64 capacity) {
|
|
|
|
|
CHECK_GT(capacity, 0);
|
|
|
|
|
int64 total_demands = 0;
|
|
|
|
|
for (const int64 demand : demands) total_demands += demand;
|
|
|
|
|
|
|
|
|
|
CutGenerator result;
|
|
|
|
|
result.vars = vars;
|
2018-12-03 14:26:31 +01:00
|
|
|
result.generate_cuts =
|
|
|
|
|
[num_nodes, tails, heads, total_demands, demands, capacity,
|
|
|
|
|
vars](const gtl::ITIVector<IntegerVariable, double>& lp_values) {
|
|
|
|
|
int num_arcs_in_lp_solution = 0;
|
|
|
|
|
std::vector<double> var_lp_values;
|
|
|
|
|
std::vector<std::vector<int>> graph(num_nodes);
|
|
|
|
|
for (int i = 0; i < vars.size(); ++i) {
|
|
|
|
|
var_lp_values.push_back(lp_values[vars[i]]);
|
|
|
|
|
if (lp_values[vars[i]] > 1e-6) {
|
|
|
|
|
++num_arcs_in_lp_solution;
|
|
|
|
|
graph[tails[i]].push_back(heads[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::vector<LinearConstraint> cuts;
|
|
|
|
|
std::vector<std::vector<int>> components;
|
|
|
|
|
FindStronglyConnectedComponents(num_nodes, graph, &components);
|
|
|
|
|
if (components.size() == 1) return cuts;
|
|
|
|
|
|
|
|
|
|
VLOG(1) << "num_arcs_in_lp_solution:" << num_arcs_in_lp_solution
|
|
|
|
|
<< " sccs:" << components.size();
|
|
|
|
|
for (const std::vector<int>& component : components) {
|
|
|
|
|
if (component.size() == 1) continue;
|
|
|
|
|
|
|
|
|
|
bool contain_depot = false;
|
|
|
|
|
int64 component_demand = 0;
|
|
|
|
|
for (const int node : component) {
|
|
|
|
|
if (node == 0) contain_depot = true;
|
|
|
|
|
component_demand += demands[node];
|
|
|
|
|
}
|
|
|
|
|
const int min_num_vehicles =
|
|
|
|
|
contain_depot
|
|
|
|
|
? (total_demands - component_demand + capacity - 1) / capacity
|
|
|
|
|
: (component_demand + capacity - 1) / capacity;
|
|
|
|
|
CHECK_GE(min_num_vehicles, 1);
|
|
|
|
|
|
|
|
|
|
AddIncomingAndOutgoingCutsIfNeeded(
|
|
|
|
|
num_nodes, component, tails, heads, vars, var_lp_values,
|
|
|
|
|
/*rhs_lower_bound=*/min_num_vehicles, &cuts);
|
|
|
|
|
|
|
|
|
|
// In this case, the cuts for each component are the same.
|
|
|
|
|
if (components.size() == 2) break;
|
|
|
|
|
}
|
|
|
|
|
return cuts;
|
|
|
|
|
};
|
2017-10-18 11:09:13 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-08 14:52:49 +01:00
|
|
|
std::function<LiteralIndex()>
|
|
|
|
|
LinearProgrammingConstraint::HeuristicLPMostInfeasibleBinary(Model* model) {
|
|
|
|
|
IntegerTrail* integer_trail = integer_trail_;
|
|
|
|
|
IntegerEncoder* integer_encoder = model->GetOrCreate<IntegerEncoder>();
|
2017-08-03 10:20:59 -07:00
|
|
|
// Gather all 0-1 variables that appear in some LP.
|
|
|
|
|
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
|
|
|
|
2017-12-08 14:52:49 +01:00
|
|
|
return [this, variables, integer_trail, integer_encoder]() {
|
2017-08-03 10:20:59 -07:00
|
|
|
const double kEpsilon = 1e-6;
|
|
|
|
|
// 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.
|
|
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
|
|
|
|
const IntegerValue lb = integer_trail_->LowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->UpperBound(var);
|
2017-08-03 10:20:59 -07:00
|
|
|
if (lb == ub) continue;
|
|
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
if (fractional_distance < kEpsilon) continue;
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
return integer_encoder
|
|
|
|
|
->GetOrCreateAssociatedLiteral(
|
|
|
|
|
IntegerLiteral::GreaterOrEqual(fractional_var, IntegerValue(1)))
|
|
|
|
|
.Index();
|
|
|
|
|
}
|
|
|
|
|
return kNoLiteralIndex;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-08 14:52:49 +01:00
|
|
|
std::function<LiteralIndex()>
|
|
|
|
|
LinearProgrammingConstraint::HeuristicLPPseudoCostBinary(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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-17 13:11:14 +01:00
|
|
|
VLOG(1) << "HeuristicLPPseudoCostBinary has " << variables.size()
|
|
|
|
|
<< " 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;
|
|
|
|
|
|
|
|
|
|
IntegerEncoder* integer_encoder = model->GetOrCreate<IntegerEncoder>();
|
|
|
|
|
return [=]() mutable {
|
|
|
|
|
const double kEpsilon = 1e-6;
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
|
|
|
|
const IntegerValue lb = integer_trail_->LowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->UpperBound(var);
|
|
|
|
|
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.
|
|
|
|
|
if (std::abs(rc) < kEpsilon) continue;
|
|
|
|
|
|
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.
|
|
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
|
|
|
|
const IntegerValue lb = integer_trail_->LowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->UpperBound(var);
|
|
|
|
|
if (lb == ub) 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) {
|
|
|
|
|
const Literal decision = integer_encoder->GetOrCreateAssociatedLiteral(
|
|
|
|
|
IntegerLiteral::GreaterOrEqual(variables[selected_index],
|
|
|
|
|
IntegerValue(1)));
|
|
|
|
|
return decision.Index();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return kNoLiteralIndex;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 11:36:18 +01:00
|
|
|
std::function<LiteralIndex()>
|
|
|
|
|
LinearProgrammingConstraint::LPReducedCostAverageBranching() {
|
|
|
|
|
if (!compute_reduced_cost_averages_) {
|
|
|
|
|
compute_reduced_cost_averages_ = true;
|
|
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
VLOG(1) << " LPReducedCostAverageBranching has #variables: " << 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [this]() { return this->LPReducedCostAverageDecision(); };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LiteralIndex LinearProgrammingConstraint::LPReducedCostAverageDecision() {
|
|
|
|
|
const int num_vars = integer_variables_.size();
|
|
|
|
|
// 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 = this->integer_variables_[i];
|
|
|
|
|
// Skip ignored and fixed variables.
|
|
|
|
|
if (integer_trail_->IsCurrentlyIgnored(var)) continue;
|
|
|
|
|
const IntegerValue lb = integer_trail_->LowerBound(var);
|
|
|
|
|
const IntegerValue ub = integer_trail_->UpperBound(var);
|
|
|
|
|
if (lb == ub) continue;
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
double cost_i = 0.0;
|
|
|
|
|
if (num_cost_down_[i] > 0 && num_cost_up_[i] > 0) {
|
|
|
|
|
cost_i = std::min(sum_cost_down_[i] / num_cost_down_[i],
|
|
|
|
|
sum_cost_up_[i] / num_cost_up_[i]);
|
|
|
|
|
} else {
|
|
|
|
|
const double divisor = num_cost_down_[i] + num_cost_up_[i];
|
|
|
|
|
if (divisor != 0) {
|
|
|
|
|
cost_i = 0.5 * (sum_cost_down_[i] + sum_cost_up_[i]) / divisor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selected_index == -1 || cost_i > best_cost) {
|
|
|
|
|
best_cost = cost_i;
|
|
|
|
|
selected_index = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selected_index == -1) return kNoLiteralIndex;
|
|
|
|
|
const IntegerVariable var = this->integer_variables_[selected_index];
|
|
|
|
|
|
|
|
|
|
// 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 integer_encoder_
|
|
|
|
|
->GetOrCreateAssociatedLiteral(IntegerLiteral::GreaterOrEqual(var, ub))
|
|
|
|
|
.Index();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 integer_encoder_
|
|
|
|
|
->GetOrCreateAssociatedLiteral(IntegerLiteral::LowerOrEqual(var, lb))
|
|
|
|
|
.Index();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Here lb < value_floor <= value_ceil < ub.
|
|
|
|
|
// Try the most promising split between var <= floor or var >= ceil.
|
|
|
|
|
if (sum_cost_down_[selected_index] / num_cost_down_[selected_index] <
|
|
|
|
|
sum_cost_up_[selected_index] / num_cost_up_[selected_index]) {
|
|
|
|
|
return integer_encoder_
|
|
|
|
|
->GetOrCreateAssociatedLiteral(
|
|
|
|
|
IntegerLiteral::LowerOrEqual(var, value_floor))
|
|
|
|
|
.Index();
|
|
|
|
|
} else {
|
|
|
|
|
return integer_encoder_
|
|
|
|
|
->GetOrCreateAssociatedLiteral(
|
|
|
|
|
IntegerLiteral::GreaterOrEqual(var, value_ceil))
|
|
|
|
|
.Index();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 16:11:06 +02:00
|
|
|
} // namespace sat
|
|
|
|
|
} // namespace operations_research
|