OR-Tools  9.6
gscip_ext.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 "ortools/gscip/gscip.h"
34 #include "scip/scip.h"
35 #include "scip/scip_prob.h"
36 #include "scip/type_cons.h"
37 #include "scip/type_scip.h"
38 #include "scip/type_var.h"
39 
40 namespace operations_research {
41 
42 // Adds the constraint y = abs(x). May create auxiliary variables. Supports
43 // unbounded x.
44 absl::Status GScipCreateAbs(GScip* gscip, SCIP_Var* x, SCIP_Var* abs_x,
45  const std::string& name = "");
46 
47 // TODO(user): delete this type and the methods below, use a generic version
48 // templated on the variable type that supports operator overloads.
50  absl::flat_hash_map<SCIP_VAR*, double> terms;
51  double offset = 0.0;
52 
53  GScipLinearExpr() = default;
54  explicit GScipLinearExpr(SCIP_VAR* variable);
55  explicit GScipLinearExpr(double offset);
56 };
57 
58 // Returns left - right.
60  const GScipLinearExpr& right);
61 
62 // Returns -expr.
64 
65 // Returns the range -inf <= left.terms - right.terms <= right.offset -
66 // left.offset
68  const GScipLinearExpr& right);
69 
70 // Adds the constraint resultant = maximum(terms). Supports unbounded variables
71 // in terms.
72 absl::Status GScipCreateMaximum(GScip* gscip, const GScipLinearExpr& resultant,
73  const std::vector<GScipLinearExpr>& terms,
74  const std::string& name = "");
75 
76 // Adds the constraint resultant = minimum(terms). Supports unbounded variables
77 // in terms.
78 absl::Status GScipCreateMinimum(GScip* gscip, const GScipLinearExpr& resultant,
79  const std::vector<GScipLinearExpr>& terms,
80  const std::string& 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;
88 };
89 
90 // Supports unbounded variables in indicator_range.range.variables.
91 absl::Status GScipCreateIndicatorRange(
92  GScip* gscip, const GScipIndicatorRangeConstraint& indicator_range,
93  const std::string& name = "",
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.
102 absl::Status GScipAddQuadraticObjectiveTerm(
103  GScip* gscip, std::vector<SCIP_Var*> quadratic_variables1,
104  std::vector<SCIP_Var*> quadratic_variables2,
105  std::vector<double> quadratic_coefficients, const std::string& name = "");
106 
107 } // namespace operations_research
108 
109 #endif // OR_TOOLS_GSCIP_GSCIP_EXT_H_
const std::string name
Collection of objects used to extend the Constraint Solver library.
GScipLinearRange GScipLe(const GScipLinearExpr left, const GScipLinearExpr &right)
Definition: gscip_ext.cc:62
GScipLinearExpr GScipDifference(GScipLinearExpr left, const GScipLinearExpr &right)
Definition: gscip_ext.cc:43
absl::Status GScipCreateMaximum(GScip *gscip, const GScipLinearExpr &resultant, const std::vector< GScipLinearExpr > &terms, const std::string &name)
Definition: gscip_ext.cc:82
absl::Status GScipCreateAbs(GScip *gscip, SCIP_Var *x, SCIP_Var *abs_x, const std::string &name)
Definition: gscip_ext.cc:75
absl::Status GScipAddQuadraticObjectiveTerm(GScip *gscip, std::vector< SCIP_Var * > quadratic_variables1, std::vector< SCIP_Var * > quadratic_variables2, std::vector< double > quadratic_coefficients, const std::string &name)
Definition: gscip_ext.cc:149
absl::Status GScipCreateMinimum(GScip *gscip, const GScipLinearExpr &resultant, const std::vector< GScipLinearExpr > &terms, const std::string &name)
Definition: gscip_ext.cc:138
absl::Status GScipCreateIndicatorRange(GScip *gscip, const GScipIndicatorRangeConstraint &indicator_range, const std::string &name, const GScipConstraintOptions &options)
Definition: gscip_ext.cc:179
GScipLinearExpr GScipNegate(GScipLinearExpr expr)
Definition: gscip_ext.cc:52
absl::flat_hash_map< SCIP_VAR *, double > terms
Definition: gscip_ext.h:50