Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gscip_ext.h
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
14// Additional nonlinear constraints not supported directly by SCIP.
15//
16// The primary purpose of this file is to support the nonlinear constraints of
17// MPSolver proto API.
18//
19// WARNING(rander): as these constraints are not natively supported in SCIP,
20// they will generally not be a single SCIP_CONS* created, but will typically
21// result in multiple SCIP_CONS* and SCIP_VAR* being created. Direct access to
22// these intermediate variables and constraints is currently not provided.
23//
24// TODO(user): either implement with SCIP constraint handlers or use a solver
25// independent implementation.
26#ifndef OR_TOOLS_GSCIP_GSCIP_EXT_H_
27#define OR_TOOLS_GSCIP_GSCIP_EXT_H_
28
29#include <string>
30#include <vector>
31
32#include "absl/status/status.h"
33#include "absl/strings/string_view.h"
34#include "absl/types/span.h"
35#include "ortools/gscip/gscip.h"
36#include "scip/scip.h"
37#include "scip/scip_prob.h"
38#include "scip/type_cons.h"
39#include "scip/type_scip.h"
40#include "scip/type_var.h"
41
42namespace operations_research {
43
44// Adds the constraint y = abs(x). May create auxiliary variables. Supports
45// unbounded x.
46absl::Status GScipCreateAbs(GScip* gscip, SCIP_Var* x, SCIP_Var* abs_x,
47 absl::string_view name = "");
48
49// TODO(user): delete this type and the methods below, use a generic version
50// templated on the variable type that supports operator overloads.
52 absl::flat_hash_map<SCIP_VAR*, double> terms;
53 double offset = 0.0;
54
55 GScipLinearExpr() = default;
56 explicit GScipLinearExpr(SCIP_VAR* variable);
57 explicit GScipLinearExpr(double offset);
58};
59
60// Returns left - right.
62 const GScipLinearExpr& right);
63
64// Returns -expr.
66
67// Returns the range -inf <= left.terms - right.terms <= right.offset -
68// left.offset
69GScipLinearRange GScipLe(GScipLinearExpr left, const GScipLinearExpr& right);
70
71// Adds the constraint resultant = maximum(terms). Supports unbounded variables
72// in terms.
73absl::Status GScipCreateMaximum(GScip* gscip, const GScipLinearExpr& resultant,
74 absl::Span<const GScipLinearExpr> terms,
75 absl::string_view name = "");
76
77// Adds the constraint resultant = minimum(terms). Supports unbounded variables
78// in terms.
79absl::Status GScipCreateMinimum(GScip* gscip, const GScipLinearExpr& resultant,
80 absl::Span<const GScipLinearExpr> terms,
81 absl::string_view name = "");
82
83// Models the constraint z = 1 => lb <= ax <= ub
84// If negate_indicator, then instead: z = 0 => lb <= ax <= ub
86 SCIP_VAR* indicator_variable = nullptr;
87 bool negate_indicator = false;
88 GScipLinearRange range;
89};
90
91// Supports unbounded variables in indicator_range.range.variables.
92absl::Status GScipCreateIndicatorRange(
93 GScip* gscip, const GScipIndicatorRangeConstraint& indicator_range,
94 absl::string_view name = "",
95 const GScipConstraintOptions& options = GScipConstraintOptions());
96
97// WARNING: DO NOT CHANGE THE OBJECTIVE DIRECTION AFTER CALLING THIS METHOD.
98//
99// This is implemented by modeling the quadratic term with an an inequality
100// constraint and a single extra variable, which is then added to the objective.
101// The inequality will be in the wrong direction if you change the objective
102// direction after calling this method.
104 GScip* gscip, std::vector<SCIP_Var*> quadratic_variables1,
105 std::vector<SCIP_Var*> quadratic_variables2,
106 std::vector<double> quadratic_coefficients, absl::string_view name = "");
107
108} // namespace operations_research
109
110#endif // OR_TOOLS_GSCIP_GSCIP_EXT_H_
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