ortools.math_opt.python.ipc.proto_converter
Conversion functions for MathOpt protos.
Provides several conversion functions to transform from/to protos exposed in the Operations Research API to the internal protos in ortools/math_opt/.*.proto.
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 38def convert_request( 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 70def convert_response( 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
def
convert_request( request: ortools.math_opt.rpc_pb2.SolveRequest) -> ortools.service.v1.optimization_pb2.SolveMathOptModelRequest:
39def convert_request( 40 request: rpc_pb2.SolveRequest, 41) -> optimization_pb2.SolveMathOptModelRequest: 42 """Converts a `SolveRequest` to a `SolveMathOptModelRequest`. 43 44 Args: 45 request: A `SolveRequest` request built from a MathOpt model. 46 47 Returns: 48 A `SolveMathOptModelRequest` for the Operations Research API. 49 50 Raises: 51 ValueError: If a field that is not supported in the expernal proto is 52 present in the request or if the request can't be parsed to a 53 `SolveMathOptModelRequest`. 54 """ 55 normalize.math_opt_normalize_proto(request) 56 if request.HasField("initializer"): 57 raise ValueError(str("initializer is not supported")) 58 for param in _UNSUPPORTED_SOLVER_SPECIFIC_PARAMETERS: 59 if request.parameters.HasField(param): 60 raise ValueError(f"SolveParameters.{param} not supported") 61 62 try: 63 external_request = optimization_pb2.SolveMathOptModelRequest.FromString( 64 request.SerializeToString() 65 ) 66 return external_request 67 except (message.DecodeError, message.EncodeError): 68 raise ValueError("request can not be parsed") from None
Converts a SolveRequest to a SolveMathOptModelRequest.
Arguments:
- request: A
SolveRequestrequest built from a MathOpt model.
Returns:
A
SolveMathOptModelRequestfor the Operations Research API.
Raises:
- ValueError: If a field that is not supported in the expernal proto is
present in the request or if the request can't be parsed to a
SolveMathOptModelRequest.
def
convert_response( api_response: ortools.service.v1.optimization_pb2.SolveMathOptModelResponse) -> ortools.math_opt.rpc_pb2.SolveResponse:
71def convert_response( 72 api_response: optimization_pb2.SolveMathOptModelResponse, 73) -> rpc_pb2.SolveResponse: 74 """Converts a `SolveMathOptModelResponse` to a `SolveResponse`. 75 76 Args: 77 api_response: A `SolveMathOptModelResponse` response built from a MathOpt 78 model. 79 80 Returns: 81 A `SolveResponse` response built from a MathOpt model. 82 """ 83 api_response.DiscardUnknownFields() 84 normalize.math_opt_normalize_proto(api_response) 85 response = rpc_pb2.SolveResponse.FromString(api_response.SerializeToString()) 86 response.DiscardUnknownFields() 87 return response
Converts a SolveMathOptModelResponse to a SolveResponse.
Arguments:
- api_response: A
SolveMathOptModelResponseresponse built from a MathOpt model.
Returns:
A
SolveResponseresponse built from a MathOpt model.