40 endpoint: Optional[str] = _DEFAULT_ENDPOINT,
41 api_key: Optional[str] =
None,
42 deadline_sec: Optional[float] = _DEFAULT_DEADLINE_SEC,
45 """Solves a MathOpt model via HTTP request to the OR API.
48 model: The optimization model.
49 solver_type: The underlying solver to use.
50 params: Optional configuration of the underlying solver.
51 model_params: Optional configuration of the solver that is model specific.
52 endpoint: An URI identifying the service for remote solves.
53 api_key: Key to the OR API.
54 deadline_sec: The number of seconds before the request times out.
55 resources: Hints on resources requested for the solve.
58 A SolveResult containing the termination reason, solution(s) and stats.
59 A list of messages with the logs (if specified in the `params`).
62 OptimizationServiceError: if an HTTP error is returned while solving a
67 raise ValueError(
"api_key can't be None when solving remotely")
72 response = session.post(
79 http_error = json.loads(response.content)[
"error"]
80 raise OptimizationServiceError(
81 f
'status code {http_error["code"]}: {http_error["message"]}'
117 model: mathopt.Model,
123 """Builds a JSON payload.
126 model: The optimization model.
127 solver_type: The underlying solver to use.
128 params: Optional configuration of the underlying solver.
129 model_params: Optional configuration of the solver that is model specific.
130 resources: Hints on resources requested for the solve.
133 A JSON object with a MathOpt model and corresponding parameters.
136 SerializationError: If building the OR API proto is not successful or
137 deserializing to JSON fails.
143 request = rpc_pb2.SolveRequest(
144 model=model.export_model(),
145 solver_type=solver_type.value,
146 resources=resources.to_proto(),
147 parameters=params.to_proto(),
148 model_parameters=model_params.to_proto(),
150 api_request = proto_converter.convert_request(request)
151 except ValueError
as err:
152 raise ValueError
from err
154 return json.loads(json_format.MessageToJson(api_request))
160 """Parses a JSON representation of a response to a SolveResult object.
163 json_response: bytes representing the `SolveMathOptModelResponse` in JSON
165 model: The optimization model that was solved
168 A SolveResult of the model.
169 A list of messages with the logs.
172 SerializationError: If parsing the json response fails or if converting the
173 OR API response to the internal MathOpt response fails.
176 api_response = json_format.Parse(
177 json_response, optimization_pb2.SolveMathOptModelResponse()
179 except json_format.ParseError
as json_err:
181 "API response is not a valid SolveMathOptModelResponse JSON"
184 response = proto_converter.convert_response(api_response)
185 return mathopt.parse_solve_result(response.result, model), list(response.messages)