Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gscip_solver.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#ifndef ORTOOLS_MATH_OPT_SOLVERS_GSCIP_SOLVER_H_
15#define ORTOOLS_MATH_OPT_SOLVERS_GSCIP_SOLVER_H_
16
17#include <cstdint>
18#include <memory>
19#include <optional>
20#include <utility>
21#include <vector>
22
23#include "absl/base/nullability.h"
24#include "absl/container/flat_hash_map.h"
25#include "absl/container/flat_hash_set.h"
26#include "absl/status/status.h"
27#include "absl/status/statusor.h"
28#include "absl/types/span.h"
46#include "scip/type_cons.h"
47#include "scip/type_var.h"
48
49namespace operations_research {
50namespace math_opt {
51
52class GScipSolver : public SolverInterface {
53 public:
54 static absl::StatusOr<std::unique_ptr<SolverInterface>> New(
55 const ModelProto& model, const InitArgs& init_args);
56
57 absl::StatusOr<SolveResultProto> Solve(
58 const SolveParametersProto& parameters,
59 const ModelSolveParametersProto& model_parameters,
60 MessageCallback message_cb,
61 const CallbackRegistrationProto& callback_registration, Callback cb,
62 const SolveInterrupter* absl_nullable interrupter) override;
63 absl::StatusOr<bool> Update(const ModelUpdateProto& model_update) override;
64 absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
66 const SolveParametersProto& parameters, MessageCallback message_cb,
67 const SolveInterrupter* absl_nullable interrupter) override;
68
69 // Returns the merged parameters and a list of warnings for unsupported
70 // parameters.
71 static absl::StatusOr<GScipParameters> MergeParameters(
72 const SolveParametersProto& solve_parameters);
73
74 private:
75 // A simple class for managing extra variables and constraints introduced to
76 // model higher-level constructs.
77 //
78 // This is useful when auxiliary variables and constraints are introduced
79 // transparently to the user, and must be deleted when the corresponding
80 // higher-level construct is deleted.
81 struct AuxiliaryStructureHandler {
82 // Removes all registered slack variables and constraints from the model.
83 // Those deleted variables and constraints are then dropped from this
84 // handler (i.e., this function is idempotent).
85 absl::Status DeleteStructure(GScip& gscip);
86
87 std::vector<SCIP_VAR*> variables;
88 std::vector<SCIP_CONS*> constraints;
89 };
90
91 explicit GScipSolver(std::unique_ptr<GScip> gscip);
92
93 // Adds the new variables.
94 absl::Status AddVariables(const VariablesProto& variables,
95 const absl::flat_hash_map<int64_t, double>&
96 linear_objective_coefficients);
97
98 // Update existing variables' parameters. Returns false if the update cannot
99 // be performed (namely, if attempting to update bounds on a binary variable).
100 absl::StatusOr<bool> UpdateVariables(
101 const VariableUpdatesProto& variable_updates);
102
103 absl::Status AddQuadraticObjectiveTerms(
104 const SparseDoubleMatrixProto& new_qp_terms, bool maximize);
105
106 // Adds the new linear constraints.
107 absl::Status AddLinearConstraints(
108 const LinearConstraintsProto& linear_constraints,
109 const SparseDoubleMatrixProto& linear_constraint_matrix);
110
111 // Updates the existing constraints and the coefficients of the existing
112 // variables of these constraints.
113 absl::Status UpdateLinearConstraints(
114 LinearConstraintUpdatesProto linear_constraint_updates,
115 const SparseDoubleMatrixProto& linear_constraint_matrix,
116 std::optional<int64_t> first_new_var_id,
117 std::optional<int64_t> first_new_cstr_id);
118
119 absl::Status AddQuadraticConstraints(
120 const google::protobuf::Map<int64_t, QuadraticConstraintProto>&
121 quadratic_constraints);
122
123 absl::Status AddIndicatorConstraints(
124 const google::protobuf::Map<int64_t, IndicatorConstraintProto>&
125 indicator_constraints);
126
127 // Given a linear `expression`, add a new `variable` and constraint such that
128 // `variable == expression`. Returns the associated SCIP pointers to the new
129 // slack variable and constraint.
130 absl::StatusOr<std::pair<SCIP_VAR*, SCIP_CONS*>>
131 AddSlackVariableEqualToExpression(const LinearExpressionProto& expression);
132
133 // Unpacks a `SosConstraintProto` into the associated data for GScip. As the
134 // MathOpt protos allow SOS terms to be arbitrary linear expressions, slack
135 // variables and constraints to represent nontrivial expressions are added to
136 // the model and tracked by the returned `AuxiliaryStructureHandler`.
137 absl::StatusOr<std::pair<GScipSOSData, AuxiliaryStructureHandler>>
138 ProcessSosProto(const SosConstraintProto& sos_constraint);
139
140 absl::Status AddSos1Constraints(
141 const google::protobuf::Map<int64_t, SosConstraintProto>&
142 sos1_constraints);
143 absl::Status AddSos2Constraints(
144 const google::protobuf::Map<int64_t, SosConstraintProto>&
145 sos2_constraints);
146
147 absl::flat_hash_set<SCIP_VAR*> LookupAllVariables(
148 absl::Span<const int64_t> variable_ids);
149 absl::StatusOr<SolveResultProto> CreateSolveResultProto(
150 GScipResult gscip_result,
151 const ModelSolveParametersProto& model_parameters,
152 std::optional<double> cutoff);
153
154 // Returns the ids of variables and linear constraints with inverted bounds.
155 InvertedBounds ListInvertedBounds() const;
156
157 // Returns the indicator constraints with non-binary indicator variables.
158 InvalidIndicators ListInvalidIndicators() const;
159
160 // Warning: it is critical that GScipConstraintHandlerData outlive its
161 // associated SCIP_CONS*. When GScip fails, we want this to be cleaned up
162 // after gscip_. See documentation on
163 // GScipConstraintHandler::AddCallbackConstraint().
164 std::unique_ptr<GScipSolverConstraintData> constraint_data_;
165 const std::unique_ptr<GScip> gscip_;
166 GScipSolverConstraintHandler constraint_handler_;
167
169 bool has_quadratic_objective_ = false;
170 absl::flat_hash_map<int64_t, SCIP_CONS*> linear_constraints_;
171 absl::flat_hash_map<int64_t, SCIP_CONS*> quadratic_constraints_;
172 // Values, if unset, correspond to indicator constraints with unset indicator
173 // variables. If set, tracks the constraint pointer and indicator variable ID.
174 // The constraint pointer must be non-null.
175 absl::flat_hash_map<int64_t, std::optional<std::pair<SCIP_CONS*, int64_t>>>
176 indicator_constraints_;
177 absl::flat_hash_map<int64_t, std::pair<SCIP_CONS*, AuxiliaryStructureHandler>>
178 sos1_constraints_;
179 absl::flat_hash_map<int64_t, std::pair<SCIP_CONS*, AuxiliaryStructureHandler>>
180 sos2_constraints_;
181};
182
183} // namespace math_opt
184} // namespace operations_research
185
186#endif // ORTOOLS_MATH_OPT_SOLVERS_GSCIP_SOLVER_H_
absl::StatusOr< SolveResultProto > Solve(const SolveParametersProto &parameters, const ModelSolveParametersProto &model_parameters, MessageCallback message_cb, const CallbackRegistrationProto &callback_registration, Callback cb, const SolveInterrupter *absl_nullable interrupter) override
absl::StatusOr< ComputeInfeasibleSubsystemResultProto > ComputeInfeasibleSubsystem(const SolveParametersProto &parameters, MessageCallback message_cb, const SolveInterrupter *absl_nullable interrupter) override
absl::StatusOr< bool > Update(const ModelUpdateProto &model_update) override
static absl::StatusOr< GScipParameters > MergeParameters(const SolveParametersProto &solve_parameters)
static absl::StatusOr< std::unique_ptr< SolverInterface > > New(const ModelProto &model, const InitArgs &init_args)
std::function< void(const std::vector< std::string > &)> MessageCallback
std::function< absl::StatusOr< CallbackResultProto >( const CallbackDataProto &)> Callback
OR-Tools root namespace.