Solving on the optimaru API¶
optimaru._api ¶
Solving on the optimaru API instead of on this machine.
This package solves where it is installed. The optimaru API is the same engine run as a
hosted service (optimaru Engine Enterprise), and the body of its solve request is the
Python call: the first argument and the keywords of solve() or solve_request(),
as JSON. So a program that already uses this package moves onto the API with one
setting, an API key, given in the environment or in code::
export OPTIMARU_API_KEY=om_live_...
import optimaru
optimaru.use_api("om_live_...")
With a key, optimaru.<use case>.solve(model, ...) and solve_request(request, ...)
send the problem to the API, wait for the solve job and return what they return on this
machine: a new solved model, or the response model. Without one nothing changes.
What a use case's __init__.py and models.py do with this module::
from .. import _api
@_api.model_solve("knapsack", KnapsackProblemModel)
def solve(problem, *, time_limit_seconds=None, ..., config=None, constraints=None): ...
@_api.request_solve("vehicle-routing", "RouteSolution")
def solve_request(request, *, constraints=None): ...
model_solve is for a solve() that takes the use case's pydantic model (the API calls
solve), request_solve for a solve_request() that the API calls itself,
solve_by_request for a solve() that takes a dict and is sent as the request of that
solve_request() (school timetabling), and compiled_solve for a solve() that
takes compiled objects only. The decorated function is unchanged while no key is set,
and __wrapped__ is the function that always solves here.
Only the standard library and pydantic are used, and both are imported when a solve is
sent, so import optimaru stays as light as it was.
ApiError ¶
Bases: RuntimeError
The optimaru API refused a solve, or failed at it.
status is the HTTP status of the API's error response, or None for any other error:
the API could not be reached or answered with something other than a solve job or a
result this version can read, or a job that was accepted then failed, was cancelled or
did not finish in time. code is the API's error code, such as
"INVALID_CREDENTIALS", "PLAN_LIMIT_EXCEEDED" or "VALIDATION_ERROR";
"JOB_FAILED", "JOB_CANCELLED" and "JOB_TIMEOUT" are a job that ended without
a result, and "BAD_ANSWER" an answer that is not the API's JSON. detail is what
the API sent with the error: for a plan limit, a dict with the reason and the plan's
entitlements. retry_after is the number of seconds the API asked to wait before
trying again (its Retry-After), or None.
use_api ¶
use_api(key: str | None = None, *, url: str | None = None) -> None
Solve on the optimaru API from now on, in this process.
key is the API key (it starts with om_); None reads the environment variable
OPTIMARU_API_KEY at each solve. url is the API to call when it is not the
default one, https://app.optimaru.com; None reads OPTIMARU_API_URL, then the
default. It must be an https URL (http is accepted for localhost). It may have a path
(an API behind a prefix): every address of a solve, the job's included, is that URL
and a path.
Setting OPTIMARU_API_KEY alone does the same without a line of code; this call is
for a key that comes from somewhere else, and it wins over the environment.
use_local turns the API off again.
With the API on, solve(model, ...) and solve_request(request, ...) send the
problem and return what they return on this machine. The settings travel as what was
given: a keyword or a SolverConfig field left at its default takes the default of
the engine the API runs; a request carries all its own fields. A solve waits for its
job, from when the job is created, for at most twice its time limit (times its parallel
starts, for a request) and ten minutes more, then cancels it. The first solve
sent to an API is logged at INFO on the optimaru logger. What cannot be sent is
refused instead of being solved here without notice: a compiled problem (pass its
model), a callback (on_new_best, select) and a non-finite number. The compiled
Solver class used directly keeps solving on this machine: the setting moves
solve and solve_request. The setting applies to this process: a worker process that
starts fresh needs its own call, or the environment variable.
Raises ValueError for an empty key, a missing one (no key and no
OPTIMARU_API_KEY) and a url that is not accepted.
use_local ¶
use_local() -> None
Solve on this machine from now on, in this process, whatever OPTIMARU_API_KEY says.