-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,191 additions
and
471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2010-2022 Google LLC | ||
// 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. | ||
|
||
#include "ortools/math_opt/labs/general_constraint_to_mip.h" | ||
|
||
#include <limits> | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/strings/str_format.h" | ||
#include "ortools/math_opt/cpp/math_opt.h" | ||
#include "ortools/math_opt/labs/linear_expr_util.h" | ||
|
||
namespace operations_research::math_opt { | ||
|
||
constexpr double kInf = std::numeric_limits<double>::infinity(); | ||
|
||
absl::Status FormulateIndicatorConstraintAsMip( | ||
Model& model, const IndicatorConstraint indicator_constraint) { | ||
if (indicator_constraint.storage() != model.storage()) { | ||
return absl::InvalidArgumentError( | ||
absl::StrFormat("Indicator constraint %s is associated with wrong " | ||
"model (expected: %s, actual: %s)", | ||
indicator_constraint.name(), model.name(), | ||
indicator_constraint.storage()->name())); | ||
} | ||
if (!indicator_constraint.indicator_variable()) { | ||
model.DeleteIndicatorConstraint(indicator_constraint); | ||
return absl::OkStatus(); | ||
} | ||
const Variable indicator_variable = | ||
*indicator_constraint.indicator_variable(); | ||
if (!indicator_variable.is_integer() || | ||
indicator_variable.lower_bound() < 0 || | ||
indicator_variable.upper_bound() > 1) { | ||
return absl::InvalidArgumentError(absl::StrFormat( | ||
"In indicator constraint %s: indicator variable %s is " | ||
"not a binary variable", | ||
indicator_constraint.name(), indicator_variable.name())); | ||
} | ||
const BoundedLinearExpression implied_constraint = | ||
indicator_constraint.ImpliedConstraint(); | ||
// One if the implied constraint should hold; zero otherwise. | ||
const LinearExpression activated_expr = | ||
indicator_constraint.activate_on_zero() ? 1 - indicator_variable | ||
: indicator_variable; | ||
if (implied_constraint.lower_bound > -kInf) { | ||
const double expr_lower_bound = LowerBound(implied_constraint.expression); | ||
if (expr_lower_bound == -kInf) { | ||
return absl::InvalidArgumentError( | ||
absl::StrFormat("In indicator constraint %s: cannot prove that " | ||
"implied constraint is unbounded from below", | ||
indicator_constraint.name())); | ||
} | ||
model.AddLinearConstraint( | ||
implied_constraint.expression >= | ||
expr_lower_bound + (implied_constraint.lower_bound - expr_lower_bound) * | ||
activated_expr); | ||
} | ||
if (implied_constraint.upper_bound < kInf) { | ||
const double expr_upper_bound = UpperBound(implied_constraint.expression); | ||
if (expr_upper_bound == kInf) { | ||
return absl::InvalidArgumentError( | ||
absl::StrFormat("In indicator constraint %s: cannot prove that " | ||
"implied constraint is unbounded from above", | ||
indicator_constraint.name())); | ||
} | ||
model.AddLinearConstraint( | ||
implied_constraint.expression <= | ||
expr_upper_bound + (implied_constraint.upper_bound - expr_upper_bound) * | ||
activated_expr); | ||
} | ||
model.DeleteIndicatorConstraint(indicator_constraint); | ||
return absl::OkStatus(); | ||
} | ||
|
||
} // namespace operations_research::math_opt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2010-2022 Google LLC | ||
// 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. | ||
|
||
#ifndef OR_TOOLS_MATH_OPT_LABS_GENERAL_CONSTRAINT_TO_MIP_H_ | ||
#define OR_TOOLS_MATH_OPT_LABS_GENERAL_CONSTRAINT_TO_MIP_H_ | ||
|
||
#include "absl/status/status.h" | ||
#include "ortools/math_opt/cpp/math_opt.h" | ||
|
||
namespace operations_research::math_opt { | ||
|
||
// Takes a `model` and an `indicator_constraint` from that same model, and | ||
// models that constraint using mixed-integer programming (MIP). This entails | ||
// deleting `indicator_constraint` from `model` and adding new linear | ||
// constraints. | ||
// | ||
// As of 2023-10-03, this formulation is a simple big-M formulation: | ||
// | ||
// Indicator constraint: x = 1 --> lb ≤ <a, y> ≤ ub | ||
// Becomes: if lb > -∞: <a, y> ≥ lb + (LowerBound(<a, y>) - lb) (1 - x) | ||
// if ub < +∞: <a, y> ≤ ub + (UpperBound(<a, y>) - ub) (1 - x), | ||
// | ||
// where LowerBound() and UpperBound() are from linear_expr_util. | ||
// | ||
// Will return an error if `indicator_constraint` is not valid or associated | ||
// with `model`, or if the simple bound computations are not able to prove that | ||
// the indicator constraint is MIP representable (namely, if LowerBound() and/or | ||
// UpperBound() return -∞ or +∞, respectively). | ||
absl::Status FormulateIndicatorConstraintAsMip( | ||
Model& model, IndicatorConstraint indicator_constraint); | ||
|
||
} // namespace operations_research::math_opt | ||
|
||
#endif // OR_TOOLS_MATH_OPT_LABS_GENERAL_CONSTRAINT_TO_MIP_H_ |
Oops, something went wrong.