Google OR-Tools v9.11
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-2024 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 "ortools/gscip/gscip.h"
35#include "scip/scip.h"
36#include "scip/scip_prob.h"
37#include "scip/type_cons.h"
38#include "scip/type_scip.h"
39#include "scip/type_var.h"
40
41namespace operations_research {
42
43// Adds the constraint y = abs(x). May create auxiliary variables. Supports
44// unbounded x.
45absl::Status GScipCreateAbs(GScip* gscip, SCIP_Var* x, SCIP_Var* abs_x,
46 absl::string_view name = "");
47
48// TODO(user): delete this type and the methods below, use a generic version
49// templated on the variable type that supports operator overloads.
51 absl::flat_hash_map<SCIP_VAR*, double> terms;
52 double offset = 0.0;
53
54 GScipLinearExpr() = default;
55 explicit GScipLinearExpr(SCIP_VAR* variable);
56 explicit GScipLinearExpr(double offset);
57};
58
59// Returns left - right.
61 const GScipLinearExpr& right);
62
63// Returns -expr.
65
66// Returns the range -inf <= left.terms - right.terms <= right.offset -
67// left.offset
68GScipLinearRange GScipLe(GScipLinearExpr left, const GScipLinearExpr& right);
69
70// Adds the constraint resultant = maximum(terms). Supports unbounded variables
71// in terms.
72absl::Status GScipCreateMaximum(GScip* gscip, const GScipLinearExpr& resultant,
73 const std::vector<GScipLinearExpr>& terms,
74 absl::string_view name = "");
75
76// Adds the constraint resultant = minimum(terms). Supports unbounded variables
77// in terms.
78absl::Status GScipCreateMinimum(GScip* gscip, const GScipLinearExpr& resultant,
79 const std::vector<GScipLinearExpr>& terms,
80 absl::string_view name = "");
81
82// Models the constraint z = 1 => lb <= ax <= ub
83// If negate_indicator, then instead: z = 0 => lb <= ax <= ub
85 SCIP_VAR* indicator_variable = nullptr;
86 bool negate_indicator = false;
87 GScipLinearRange range;
88};
89
90// Supports unbounded variables in indicator_range.range.variables.
91absl::Status GScipCreateIndicatorRange(
92 GScip* gscip, const GScipIndicatorRangeConstraint& indicator_range,
93 absl::string_view name = "",
94 const GScipConstraintOptions& options = GScipConstraintOptions());
95
96// WARNING: DO NOT CHANGE THE OBJECTIVE DIRECTION AFTER CALLING THIS METHOD.
97//
98// This is implemented by modeling the quadratic term with an an inequality
99// constraint and a single extra variable, which is then added to the objective.
100// The inequality will be in the wrong direction if you change the objective
101// direction after calling this method.
103 GScip* gscip, std::vector<SCIP_Var*> quadratic_variables1,
104 std::vector<SCIP_Var*> quadratic_variables2,
105 std::vector<double> quadratic_coefficients, absl::string_view name = "");
106
107} // namespace operations_research
108
109#endif // OR_TOOLS_GSCIP_GSCIP_EXT_H_
const std::string name
A name for logging purposes.
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:43
absl::Status GScipCreateMinimum(GScip *gscip, const GScipLinearExpr &resultant, const std::vector< GScipLinearExpr > &terms, absl::string_view name)
Definition gscip_ext.cc:138
GScipLinearExpr GScipNegate(GScipLinearExpr expr)
Returns -expr.
Definition gscip_ext.cc:52
GScipLinearRange GScipLe(const GScipLinearExpr left, const GScipLinearExpr &right)
Definition gscip_ext.cc:62
absl::Status GScipCreateAbs(GScip *gscip, SCIP_Var *x, SCIP_Var *abs_x, absl::string_view name)
Definition gscip_ext.cc:75
absl::Status GScipCreateMaximum(GScip *gscip, const GScipLinearExpr &resultant, const std::vector< GScipLinearExpr > &terms, absl::string_view name)
Definition gscip_ext.cc:82
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:179
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:149
const Variable x
Definition qp_tests.cc:127
absl::flat_hash_map< SCIP_VAR *, double > terms
Definition gscip_ext.h:51