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# 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
37def convert_request(
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
69def convert_response(
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
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

Converts a SolveRequest to a SolveMathOptModelRequest.

Arguments:
  • request: A SolveRequest request built from a MathOpt model.
Returns:

A SolveMathOptModelRequest for 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.
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

Converts a SolveMathOptModelResponse to a SolveResponse.

Arguments:
  • api_response: A SolveMathOptModelResponse response built from a MathOpt model.
Returns:

A SolveResponse response built from a MathOpt model.