Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gscip_ext.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <cmath>
17#include <limits>
18#include <string>
19#include <vector>
20
21#include "absl/strings/string_view.h"
22#include "absl/types/span.h"
25
26namespace operations_research {
27
28namespace {
29
30std::string MaybeExtendName(absl::string_view base_name,
31 absl::string_view extension) {
32 if (base_name.empty()) {
33 return "";
34 }
35 return absl::StrCat(base_name, "/", extension);
36}
37
38} // namespace
39
40GScipLinearExpr::GScipLinearExpr(SCIP_VAR* variable) { terms[variable] = 1.0; }
41
43
45 const GScipLinearExpr& right) {
46 left.offset -= right.offset;
47 for (const auto& term : right.terms) {
48 left.terms[term.first] -= term.second;
49 }
50 return left;
51}
52
54 expr.offset = -expr.offset;
55 for (auto& term : expr.terms) {
56 term.second = -term.second;
57 }
58 return expr;
59}
60
61// Returns the range -inf <= left.terms - right.terms <= right.offset -
62// left.offset
63GScipLinearRange GScipLe(const GScipLinearExpr left,
64 const GScipLinearExpr& right) {
65 GScipLinearExpr diff = GScipDifference(left, right);
66 GScipLinearRange result;
67 result.lower_bound = -std::numeric_limits<double>::infinity();
68 result.upper_bound = -diff.offset;
69 for (const auto& term : diff.terms) {
70 result.variables.push_back(term.first);
71 result.coefficients.push_back(term.second);
72 }
73 return result;
74}
75
76absl::Status GScipCreateAbs(GScip* gscip, SCIP_Var* x, SCIP_Var* abs_x,
77 absl::string_view name) {
78 return GScipCreateMaximum(
79 gscip, GScipLinearExpr(abs_x),
81}
82
83absl::Status GScipCreateMaximum(GScip* gscip, const GScipLinearExpr& resultant,
84 absl::Span<const GScipLinearExpr> terms,
85 absl::string_view name) {
86 // TODO(user): it may be better to write this in terms of the disjuntive
87 // constraint, we need to support disjunctions in gscip.h to do this.
88 //
89 // z_i in {0,1}, indicates if y = x_i
90 //
91 // x_i <= y
92 // z_i => y <= x_i
93 // \sum_i z_i == 1
94 std::vector<SCIP_VAR*> indicators;
95 for (int i = 0; i < terms.size(); ++i) {
96 auto z = gscip->AddVariable(0.0, 1.0, 0.0, GScipVarType::kInteger,
97 MaybeExtendName(name, absl::StrCat("z_", i)));
98 RETURN_IF_ERROR(z.status());
99 indicators.push_back(*z);
100 }
101
102 for (int i = 0; i < terms.size(); ++i) {
103 // x_i <= y
105 gscip
106 ->AddLinearConstraint(
107 GScipLe(terms.at(i), resultant),
108 MaybeExtendName(name, absl::StrCat("x_", i, "_le_y")))
109 .status());
110 // z_i => y <= x_i
111 {
112 GScipLinearRange y_less_x = GScipLe(resultant, terms.at(i));
113 CHECK_EQ(y_less_x.lower_bound, -std::numeric_limits<double>::infinity());
114 GScipIndicatorConstraint ind;
115 ind.indicator_variable = indicators.at(i);
116 ind.variables = y_less_x.variables;
117 ind.coefficients = y_less_x.coefficients;
118 ind.upper_bound = y_less_x.upper_bound;
120 gscip
121 ->AddIndicatorConstraint(
122 ind, MaybeExtendName(
123 name, absl::StrCat("y_le__x_", i, "_if_z_", i)))
124 .status());
125 }
126 }
127
128 // sum_i z_i = 1.
129 GScipLinearRange z_use;
130 z_use.upper_bound = 1.0;
131 z_use.lower_bound = 1.0;
132 z_use.variables = indicators;
133 z_use.coefficients = std::vector<double>(indicators.size(), 1.0);
134
135 return gscip->AddLinearConstraint(z_use, MaybeExtendName(name, "one_z"))
136 .status();
137}
138
139absl::Status GScipCreateMinimum(GScip* gscip, const GScipLinearExpr& resultant,
140 absl::Span<const GScipLinearExpr> terms,
141 absl::string_view name) {
142 std::vector<GScipLinearExpr> negated_terms;
143 negated_terms.reserve(terms.size());
144 for (const GScipLinearExpr& e : terms) {
145 negated_terms.push_back(GScipNegate(e));
146 }
147 return GScipCreateMaximum(gscip, GScipNegate(resultant), negated_terms, name);
148}
149
151 GScip* gscip, std::vector<SCIP_Var*> quadratic_variables1,
152 std::vector<SCIP_Var*> quadratic_variables2,
153 std::vector<double> quadratic_coefficients, absl::string_view name) {
154 constexpr double kInf = std::numeric_limits<double>::infinity();
155 auto obj_term =
156 gscip->AddVariable(-kInf, kInf, 1.0, GScipVarType::kContinuous,
157 MaybeExtendName(name, "obj"));
158 RETURN_IF_ERROR(obj_term.status());
159 GScipQuadraticRange range;
160 range.quadratic_variables1 = quadratic_variables1;
161 range.quadratic_variables2 = quadratic_variables2;
162 range.quadratic_coefficients = quadratic_coefficients;
163 range.linear_coefficients = {-1.0};
164 range.linear_variables = {*obj_term};
165 if (gscip->ObjectiveIsMaximize()) {
166 // maximize z
167 // z <= Q(x, y)
168 // => 0 <= Q(x, y) - z <= inf
169 range.lower_bound = 0.0;
170 } else {
171 // minimize z
172 // z >= Q(x, y)
173 // => 0 >= Q(x, y) - z >= -inf
174 range.upper_bound = 0.0;
175 }
176 return gscip->AddQuadraticConstraint(range, MaybeExtendName(name, "cons"))
177 .status();
178}
179
181 GScip* gscip, const GScipIndicatorRangeConstraint& indicator_range,
182 absl::string_view name, const GScipConstraintOptions& options) {
183 if (std::isfinite(indicator_range.range.upper_bound)) {
184 GScipIndicatorConstraint ub_constraint;
185 ub_constraint.upper_bound = indicator_range.range.upper_bound;
186 ub_constraint.variables = indicator_range.range.variables;
187 ub_constraint.coefficients = indicator_range.range.coefficients;
188 ub_constraint.indicator_variable = indicator_range.indicator_variable;
189 ub_constraint.negate_indicator = indicator_range.negate_indicator;
190 RETURN_IF_ERROR(gscip
191 ->AddIndicatorConstraint(
192 ub_constraint, MaybeExtendName(name, "ub"), options)
193 .status());
194 }
195 if (std::isfinite(indicator_range.range.lower_bound)) {
196 // want z -> lb <= a * x
197 // <=> z -> -lb >= -a * x
198 GScipIndicatorConstraint lb_constraint;
199 lb_constraint.upper_bound = -indicator_range.range.lower_bound;
200 lb_constraint.variables = indicator_range.range.variables;
201 for (const double c : indicator_range.range.coefficients) {
202 lb_constraint.coefficients.push_back(-c);
203 }
204 lb_constraint.indicator_variable = indicator_range.indicator_variable;
205 lb_constraint.negate_indicator = indicator_range.negate_indicator;
206 RETURN_IF_ERROR(gscip
207 ->AddIndicatorConstraint(
208 lb_constraint, MaybeExtendName(name, "lb"), options)
209 .status());
210 }
211 return absl::OkStatus();
212}
213
214} // namespace operations_research
#define RETURN_IF_ERROR(expr)
In SWIG mode, we don't want anything besides these top-level includes.
GScipLinearExpr GScipDifference(GScipLinearExpr left, const GScipLinearExpr &right)
Returns left - right.
Definition gscip_ext.cc:44
absl::Status GScipCreateMaximum(GScip *gscip, const GScipLinearExpr &resultant, absl::Span< const GScipLinearExpr > terms, absl::string_view name)
Definition gscip_ext.cc:83
GScipLinearExpr GScipNegate(GScipLinearExpr expr)
Returns -expr.
Definition gscip_ext.cc:53
GScipLinearRange GScipLe(const GScipLinearExpr left, const GScipLinearExpr &right)
Definition gscip_ext.cc:63
absl::Status GScipCreateAbs(GScip *gscip, SCIP_Var *x, SCIP_Var *abs_x, absl::string_view name)
Definition gscip_ext.cc:76
absl::Status GScipCreateMinimum(GScip *gscip, const GScipLinearExpr &resultant, absl::Span< const GScipLinearExpr > terms, absl::string_view name)
Definition gscip_ext.cc:139
absl::Status GScipCreateIndicatorRange(GScip *gscip, const GScipIndicatorRangeConstraint &indicator_range, absl::string_view name, const GScipConstraintOptions &options)
Supports unbounded variables in indicator_range.range.variables.
Definition gscip_ext.cc:180
absl::Status GScipAddQuadraticObjectiveTerm(GScip *gscip, std::vector< SCIP_Var * > quadratic_variables1, std::vector< SCIP_Var * > quadratic_variables2, std::vector< double > quadratic_coefficients, absl::string_view name)
Definition gscip_ext.cc:150
absl::flat_hash_map< SCIP_VAR *, double > terms
Definition gscip_ext.h:52