Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gscip_from_mp_model_proto.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// Builds a gSCIP model from an MPModelProto and gives the mapping from proto
15// variables to SCIP_VARs.
16//
17// Typically, prefer using scip_proto_solve.h. This class is useful if you need
18// to set a callback or otherwise customize your model in a way not supported by
19// the proto.
20#ifndef ORTOOLS_MATH_OPT_SOLVERS_GSCIP_GSCIP_FROM_MP_MODEL_PROTO_H_
21#define ORTOOLS_MATH_OPT_SOLVERS_GSCIP_GSCIP_FROM_MP_MODEL_PROTO_H_
22
23#include <cstdint>
24#include <memory>
25#include <string>
26#include <vector>
27
28#include "absl/status/statusor.h"
29#include "absl/strings/string_view.h"
33#include "scip/type_var.h"
34
35namespace operations_research {
36
38 std::unique_ptr<GScip> gscip;
39 std::vector<SCIP_VAR*> variables;
40
41 // The model must be a valid MPModelProto and have no finite coefficients with
42 // absolute value exceeding 1e20 (otherwise undefined behavior will occur).
43 // See the linear_solver model validation methods to test this property.
44 //
45 // In the returned struct, variables will have one element for each of
46 // model.variable() and be in the same order. Note that the underlying gSCIP
47 // model may contain auxiliary variables not listed here when general
48 // constraints are used.
49 static absl::StatusOr<GScipAndVariables> FromMPModelProto(
50 const MPModelProto& model);
51
52 absl::Status AddHint(const PartialVariableAssignment& mp_hint);
53
54 private:
55 std::vector<SCIP_VAR*> TranslateMPVars(absl::Span<const int32_t> mp_vars);
56
57 absl::Status AddLinearConstraint(const MPConstraintProto& lin_constraint);
58 absl::Status AddGeneralConstraint(const MPGeneralConstraintProto& mp_gen);
59 absl::Status AddIndicatorConstraint(absl::string_view name,
60 const MPIndicatorConstraint& mp_ind);
61 absl::Status AddSosConstraint(const std::string& name,
62 const MPSosConstraint& mp_sos);
63 absl::Status AddQuadraticConstraint(const std::string& name,
64 const MPQuadraticConstraint& mp_quad);
65 absl::Status AddAbsConstraint(absl::string_view name,
66 const MPAbsConstraint& mp_abs);
67 absl::Status AddAndConstraint(const std::string& name,
68 const MPArrayConstraint& mp_and);
69 absl::Status AddOrConstraint(const std::string& name,
70 const MPArrayConstraint& mp_or);
71 std::vector<GScipLinearExpr> MpArrayWithConstantToGScipLinearExprs(
72 const MPArrayWithConstantConstraint& mp_array_with_constant);
73 absl::Status AddMinConstraint(absl::string_view name,
74 const MPArrayWithConstantConstraint& mp_min);
75 absl::Status AddMaxConstraint(absl::string_view name,
76 const MPArrayWithConstantConstraint& mp_max);
77
78 // WARNING: YOU MUST SET THE OBJECTIVE DIRECTION BEFORE CALLING THIS, AND NOT
79 // CHANGE IT AFTERWARDS!
80 absl::Status AddQuadraticObjective(const MPQuadraticObjective& quad_obj);
81};
82
83} // namespace operations_research
84
85#endif // ORTOOLS_MATH_OPT_SOLVERS_GSCIP_GSCIP_FROM_MP_MODEL_PROTO_H_
OR-Tools root namespace.
absl::Status AddHint(const PartialVariableAssignment &mp_hint)
static absl::StatusOr< GScipAndVariables > FromMPModelProto(const MPModelProto &model)