Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
proto_converter.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright 2010-2025 Google LLC
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Conversion functions for MathOpt protos.
16
17Provides several conversion functions to transform from/to protos exposed in the
18Operations Research API to the internal protos in
19ortools/math_opt/.*.proto.
20"""
21
22from google.protobuf import message
23from ortools.service.v1 import optimization_pb2
24from ortools.math_opt import rpc_pb2
25from ortools.math_opt.python import normalize
26
27_UNSUPPORTED_SOLVER_SPECIFIC_PARAMETERS = (
28 "gscip",
29 "gurobi",
30 "glop",
31 "cp_sat",
32 "osqp",
33 "glpk",
34 "highs",
35)
36
37
39 request: rpc_pb2.SolveRequest,
40) -> optimization_pb2.SolveMathOptModelRequest:
41 """Converts a `SolveRequest` to a `SolveMathOptModelRequest`.
42
43 Args:
44 request: A `SolveRequest` request built from a MathOpt model.
45
46 Returns:
47 A `SolveMathOptModelRequest` for the Operations Research API.
48
49 Raises:
50 ValueError: If a field that is not supported in the expernal proto is
51 present in the request or if the request can't be parsed to a
52 `SolveMathOptModelRequest`.
53 """
54 normalize.math_opt_normalize_proto(request)
55 if request.HasField("initializer"):
56 raise ValueError(str("initializer is not supported"))
57 for param in _UNSUPPORTED_SOLVER_SPECIFIC_PARAMETERS:
58 if request.parameters.HasField(param):
59 raise ValueError(f"SolveParameters.{param} not supported")
60
61 try:
62 external_request = optimization_pb2.SolveMathOptModelRequest.FromString(
63 request.SerializeToString()
64 )
65 return external_request
66 except (message.DecodeError, message.EncodeError):
67 raise ValueError("request can not be parsed") from None
68
69
71 api_response: optimization_pb2.SolveMathOptModelResponse,
72) -> rpc_pb2.SolveResponse:
73 """Converts a `SolveMathOptModelResponse` to a `SolveResponse`.
74
75 Args:
76 api_response: A `SolveMathOptModelResponse` response built from a MathOpt
77 model.
78
79 Returns:
80 A `SolveResponse` response built from a MathOpt model.
81 """
82 api_response.DiscardUnknownFields()
83 normalize.math_opt_normalize_proto(api_response)
84 response = rpc_pb2.SolveResponse.FromString(api_response.SerializeToString())
85 response.DiscardUnknownFields()
86 return response
rpc_pb2.SolveResponse convert_response(optimization_pb2.SolveMathOptModelResponse api_response)
optimization_pb2.SolveMathOptModelRequest convert_request(rpc_pb2.SolveRequest request)