Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
proto_converter.py
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"""Conversion functions for MathOpt protos.
15
16Provides several conversion functions to transform from/to protos exposed in the
17Operations Research API to the internal protos in
18/ortools/math_opt/.*.proto.
19"""
20
21from google.protobuf import message
22from ortools.service.v1 import optimization_pb2
23from ortools.math_opt import rpc_pb2
24from ortools.math_opt.python import normalize
25
26_UNSUPPORTED_SOLVER_SPECIFIC_PARAMETERS = (
27 "gscip",
28 "gurobi",
29 "glop",
30 "cp_sat",
31 "osqp",
32 "glpk",
33 "highs",
34)
35
36
38 request: rpc_pb2.SolveRequest,
39) -> optimization_pb2.SolveMathOptModelRequest:
40 """Converts a `SolveRequest` to a `SolveMathOptModelRequest`.
41
42 Args:
43 request: A `SolveRequest` request built from a MathOpt model.
44
45 Returns:
46 A `SolveMathOptModelRequest` for the Operations Research API.
47
48 Raises:
49 ValueError: If a field that is not supported in the expernal proto is
50 present in the request or if the request can't be parsed to a
51 `SolveMathOptModelRequest`.
52 """
53 normalize.math_opt_normalize_proto(request)
54 if request.HasField("initializer"):
55 raise ValueError(str("initializer is not supported"))
56 for param in _UNSUPPORTED_SOLVER_SPECIFIC_PARAMETERS:
57 if request.parameters.HasField(param):
58 raise ValueError(f"SolveParameters.{param} not supported")
59
60 try:
61 external_request = optimization_pb2.SolveMathOptModelRequest.FromString(
62 request.SerializeToString()
63 )
64 return external_request
65 except (message.DecodeError, message.EncodeError):
66 raise ValueError("request can not be parsed") from None
67
68
70 api_response: optimization_pb2.SolveMathOptModelResponse,
71) -> rpc_pb2.SolveResponse:
72 """Converts a `SolveMathOptModelResponse` to a `SolveResponse`.
73
74 Args:
75 api_response: A `SolveMathOptModelResponse` response built from a MathOpt
76 model.
77
78 Returns:
79 A `SolveResponse` response built from a MathOpt model.
80 """
81 api_response.DiscardUnknownFields()
82 normalize.math_opt_normalize_proto(api_response)
83 response = rpc_pb2.SolveResponse.FromString(api_response.SerializeToString())
84 response.DiscardUnknownFields()
85 return response
rpc_pb2.SolveResponse convert_response(optimization_pb2.SolveMathOptModelResponse api_response)
optimization_pb2.SolveMathOptModelRequest convert_request(rpc_pb2.SolveRequest request)