39 endpoint: Optional[str] = _DEFAULT_ENDPOINT,
40 api_key: Optional[str] =
None,
41 deadline_sec: Optional[float] = _DEFAULT_DEADLINE_SEC,
43 """Solves a MathOpt model via HTTP request to the OR API.
46 model: The optimization model.
47 solver_type: The underlying solver to use.
48 params: Optional configuration of the underlying solver.
49 model_params: Optional configuration of the solver that is model specific.
50 endpoint: An URI identifying the service for remote solves.
51 api_key: Key to the OR API.
52 deadline_sec: The number of seconds before the request times out.
55 A SolveResult containing the termination reason, solution(s) and stats.
56 A list of messages with the logs (if specified in the `params`).
59 OptimizationServiceError: if an HTTP error is returned while solving a
64 raise ValueError(
"api_key can't be None when solving remotely")
69 response = session.post(
76 http_error = json.loads(response.content)[
"error"]
77 raise OptimizationServiceError(
78 f
'status code {http_error["code"]}: {http_error["message"]}'
114 model: mathopt.Model,
119 """Builds a JSON payload.
122 model: The optimization model.
123 solver_type: The underlying solver to use.
124 params: Optional configuration of the underlying solver.
125 model_params: Optional configuration of the solver that is model specific.
128 A JSON object with a MathOpt model and corresponding parameters.
131 SerializationError: If building the OR API proto is not successful or
132 deserializing to JSON fails.
137 request = rpc_pb2.SolveRequest(
138 model=model.export_model(),
139 solver_type=solver_type.value,
140 parameters=params.to_proto(),
141 model_parameters=model_params.to_proto(),
143 api_request = proto_converter.convert_request(request)
144 except ValueError
as err:
145 raise ValueError
from err
147 return json.loads(json_format.MessageToJson(api_request))
153 """Parses a JSON representation of a response to a SolveResult object.
156 json_response: bytes representing the `SolveMathOptModelResponse` in JSON
158 model: The optimization model that was solved
161 A SolveResult of the model.
162 A list of messages with the logs.
165 SerializationError: If parsing the json response fails or if converting the
166 OR API response to the internal MathOpt response fails.
169 api_response = json_format.Parse(
170 json_response, optimization_pb2.SolveMathOptModelResponse()
172 except json_format.ParseError
as json_err:
174 "API response is not a valid SolveMathOptModelResponse JSON"
177 response = proto_converter.convert_response(api_response)
178 return mathopt.parse_solve_result(response.result, model), list(response.messages)