mamut_routing_lib.solvers.base¶
Solver-agnostic types shared by every solver integration.
Nothing here imports a solver: PyVRP, KAYROS and external executables plug in
through the :class:Solver protocol and are looked up by name in
:mod:mamut_routing_lib.solvers.registry. The contract, in three parts:
- Capabilities (:class:
SolverCapabilities) say what a solver can take: problem types and objectives, symmetric or asymmetric costs, coordinates or an explicit matrix, a fixed fleet, warm starts, live progress. - A request (:class:
SolveRequest) carries one instance, its path, the objective, the wall-clock budget, the seed and optional warm-start routes. - A result (:class:
SolveResult) carries the routes in library ids (depot 0, customers 1..n), a status, the solver's own cost brought back to instance units, and provenance. The solver's cost is informational: callers re-price every route set with the reference checker (solvers.run.run_solver).
SolveMonitor is the one progress/cancellation hook for all drivers: PyVRP
calls it per iteration as a stopping criterion, the subprocess runner polls it,
and KAYROS's incumbent hook feeds it.
SolveStatus
module-attribute
¶
SolverUnavailableError
¶
Bases: RuntimeError
The solver cannot run here: missing Python extra, executable not found, bad configuration.
InstanceNotSupportedError
¶
Bases: ValueError
The solver cannot take this instance/objective (see Support.reasons).
Distinct from cvrplib.UnsupportedInstanceError, which is about classic export formats.
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.
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-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-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-lib/src/mamut_routing_lib/solvers/base.py
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-lib/src/mamut_routing_lib/solvers/base.py
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).
Support
dataclass
¶
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.
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-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-lib/src/mamut_routing_lib/solvers/base.py
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.
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-lib/src/mamut_routing_lib/solvers/base.py
capability_support
¶
capability_support(
capabilities: SolverCapabilities,
profile: InstanceProfile,
objective: ObjectiveFunction,
*,
solver_label: str,
) -> Support
The checks every solver shares: problem type, objective, symmetry, coordinates, fleet.