mamut_routing_lib.solvers¶
Solver integrations: a registry of solvers behind one protocol.
from mamut_routing_lib.solvers import run_solver
solved = run_solver("pyvrp", "path/to/instance.vrp.json", time_limit_s=30, seed=1)
solved.valid, solved.cost, solved.result.routes
Importing this package never imports PyVRP or KAYROS; see solvers.registry
for the solver sources and solvers.base for the protocol.
BUILTIN_SOLVERS
module-attribute
¶
Availability
dataclass
¶
Availability(
available: bool,
reason: str | None = None,
version: str | None = None,
executable: str | None = None,
hint: str | None = None,
note: str | None = None,
)
Whether a solver can run here (reason explains a False).
hint says how to make it available (install, configure); note is a standing
requirement worth knowing either way (a patched build, for instance).
InstanceNotSupportedError
¶
Bases: ValueError
The solver cannot take this instance/objective (see Support.reasons).
Distinct from cvrplib.UnsupportedInstanceError, which is about classic export formats.
InstanceProfile
dataclass
¶
InstanceProfile(
problem_type: ProblemType,
benchmark_name: str | None,
metric_variant: str | None,
num_customers: int,
depot: int,
fixed_fleet: int | None,
has_coordinates: bool,
coordinates_define_costs: bool,
symmetric: bool | None,
has_fleet_fixed_cost: bool = False,
)
Cheap facts about an instance, enough to decide solver support without solving.
symmetric is True for the euclidean metric (symmetric by construction), False or
True when a matrix was inspected, and None when unknown; solvers that need symmetry check
the matrix at solve time.
SolveMonitor
¶
SolveMonitor(
on_tick: Callable[[SolveTick], bool] | None = None,
*,
scale: float = 1,
interval_s: float = 1.0,
budget_s: float | None = None,
record_trajectory: bool = False,
trajectory_min_gap_s: float = 0.5,
cancelled: Callable[[], bool] | None = None,
)
Progress and cancellation hook shared by every solver driver.
observe(best_cost, iterations=...)is the generic entry point: it checks for a stop request on every call, records the trajectory, and relays a throttled :class:SolveTicktoon_tick. It returns True when the solve must stop (on_tickreturned True orrequest_stopwas called).__call__(best_cost)is the PyVRP stopping-criterion protocol: PyVRP calls it once per iteration with the incumbent's cost. It counts the iteration, maps PyVRP's infeasible sentinel toNoneand delegates toobserve. Compose it beforeMaxRuntimein aMultipleCriteria: that class usesany(), which short-circuits, so a monitor placed second would go silent the moment the budget expires.
best_cost is in solver units; rescale sets the factor that brings it back to instance units.
cancelled is a cheap poll (a job's cancel flag, say) read by stop_requested and by every
observe call, unthrottled: a driver checks it between its own steps (an external solver
before and after writing a large input), not only when a tick is due.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/base.py
phase
¶
Name the stage the solve has just entered, and tick on the next observation so the label lands.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/base.py
rescale
¶
Adopt the solve's cost scale, which is only known once the instance is converted.
set_budget
¶
restart_clock
¶
Measure elapsed time from now (the monitor may be built well before the solve starts).
request_stop
¶
observe
¶
observe(
best_cost: float | None = None,
*,
iterations: int | None = None,
force: bool = False,
) -> bool
Record one observation; True means the solve must stop.
best_cost is the incumbent in solver units (None when nothing feasible is known);
iterations overrides the internal counter for drivers that report their own.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/base.py
SolveRequest
dataclass
¶
SolveRequest(
instance: Any,
instance_path: Path | None,
objective: ObjectiveFunction,
time_limit_s: float,
seed: int = 42,
initial_routes: list[list[int]] | None = None,
workdir: Path | None = None,
keep_artifacts: bool = False,
display: bool = False,
options: Mapping[str, Any] = dict(),
)
One solve: the instance (already loaded), where it lives, and the run parameters.
options
class-attribute
instance-attribute
¶
checkable
¶
The instance in the form the reference checker reads, loaded once per request.
A static instance with its collection matrix hydrated from the sidecar, or the
LoadedTDInstance of a time-dependent one (solutions.load_checkable). The adapter that
writes the solver's input and run_solver's check share it, so one solve reads a large
matrix once. Raises what the loaders raise (a sidecar that is missing, say).
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/base.py
SolveResult
dataclass
¶
SolveResult(
solver: str,
method: str,
problem_type: str,
objective_function: str,
instance_id: str,
seed: int,
time_limit_s: float,
wall_time: float,
status: SolveStatus,
routes: list[list[int]] = list(),
solver_report: dict[str, Any] | None = None,
cost_scale: int | float | None = 1.0,
stopped_early: bool = False,
error: str | None = None,
solver_version: str | None = None,
metadata: dict[str, Any] = dict(),
provenance: dict[str, Any] = dict(),
)
What a solver returned, before (and independent of) the reference checker's verdict.
solver_report preserves the reported objective, value, numeric encoding and unit.
solver_cost is the optional nearest-binary64 projection of its routing component
in family units. The checker computes the route set's authoritative cost separately.
metadata
class-attribute
instance-attribute
¶
provenance
class-attribute
instance-attribute
¶
to_json
¶
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/base.py
SolveTick
dataclass
¶
SolveTick(
phase: str,
iterations: int | None,
elapsed_s: float,
best_cost: float | None,
budget_s: float | None = None,
)
One observation of a running solve.
Solver
¶
Bases: Protocol
A solver integration. solve never raises for a solver failure (timeout, crash, garbage
output): it returns a SolveResult with status failed or timeout. It raises
SolverUnavailableError / InstanceNotSupportedError only for usage errors.
SolverCapabilities
dataclass
¶
SolverCapabilities(
objectives: Mapping[
ProblemType, frozenset[ObjectiveFunction]
],
input: Literal[
"model", "coordinates", "explicit"
] = "model",
symmetric_only: bool = False,
integer_costs: bool = False,
fleet_limit: bool = True,
warm_start: bool = False,
live_progress: Literal[
"none", "elapsed", "incumbent"
] = "none",
cancellable: bool = True,
in_process: bool = False,
)
What a solver can take. objectives maps each supported problem type to its objectives.
input
class-attribute
instance-attribute
¶
live_progress
class-attribute
instance-attribute
¶
to_json
¶
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/base.py
SolverUnavailableError
¶
Bases: RuntimeError
The solver cannot run here: missing Python extra, executable not found, bad configuration.
Support
dataclass
¶
UnknownSolverError
¶
Bases: ValueError
No solver of that name; the message lists the known ones.
CheckedSolve
dataclass
¶
CheckedSolve(
result: SolveResult,
check: Any | None,
objective: ObjectiveFunction,
instance_path: Path,
instance_name: str,
support_warnings: tuple[str, ...] = (),
extra: dict[str, Any] = dict(),
checkable: Any = None,
)
A solver result plus the reference checker's verdict on its routes.
checkable
class-attribute
instance-attribute
¶
to_solution
¶
The routes as a BenchmarkSolution (cost left to the checker).
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/run.py
bks_metadata
¶
Provenance for a BKS built from this solve (no hostname: BKS files are published).
The keys are :data:BKS_PROVENANCE_KEYS. extra adds keys of the caller (a campaign
name, a run key); it cannot replace a provenance key or set a key of the BKS store.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/run.py
to_json
¶
A JSON-ready summary: the checker's verdict next to the solver result.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/run.py
profile_instance
¶
profile_instance(
instance: Any,
instance_path: str | Path | None = None,
*,
inspect_matrix: bool = False,
) -> InstanceProfile
Profile an instance (static or time-dependent) for Solver.supports.
With inspect_matrix the arc costs are hydrated and checked for symmetry and for being a
rounding of the coordinates' Euclidean distances (O(n²); done at solve time, not for listings).
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/base.py
get_solver
¶
The solver called name; UnknownSolverError lists the known names.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/registry.py
list_solvers
¶
Every known solver, available or not, in solver_names order.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/registry.py
register_solver
¶
Register a solver instance or zero-argument factory under name (default: its name).
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/registry.py
reload_solvers
¶
Forget cached solver objects, entry points and the parsed user config.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/registry.py
solver_names
¶
Every known solver name: built-ins first, then plugins, registrations and configured solvers.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/registry.py
prepare_solve
¶
prepare_solve(
solver: str | Solver,
instance_path: str | Path,
*,
objective_function: ObjectiveFunction | None = None,
instance: Any = None,
config: Any = None,
) -> tuple[Solver, Any, ObjectiveFunction, tuple[str, ...]]
Resolve and vet a solve without running it: (solver, instance, objective, support warnings).
Raises SolverUnavailableError or InstanceNotSupportedError with the reasons.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/run.py
run_solver
¶
run_solver(
solver: str | Solver,
instance_path: str | Path,
*,
time_limit_s: float,
seed: int = 42,
objective_function: ObjectiveFunction | None = None,
instance: Any = None,
monitor: SolveMonitor | None = None,
initial_routes: list[list[int]] | None = None,
workdir: str | Path | None = None,
keep_artifacts: bool = False,
display: bool = False,
options: Mapping[str, Any] | None = None,
config: Any = None,
) -> CheckedSolve
Solve the instance at instance_path with solver (a name or a Solver) and check it.