mamut_routing_lib.solvers.pyvrp¶
PyVRP-backed solver integration for mamut-routing-lib.
Importing this module requires the [pyvrp] optional dependency:
pip install "mamut-routing-lib[pyvrp]"
Public API
to_vrplib_dict, to_pyvrp_problem -- conversion helpers MethodResult -- solver result dataclass SolveTick, SolveMonitor -- live progress / cancellation hook solve_cvrp, solve_vrptw, solve_instance -- single-instance solvers solve_and_update_bks -- solve + write BKS if improved
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
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.
MethodResult
dataclass
¶
MethodResult(
method: str,
problem_type: str,
objective_function: str,
instance_id: str,
seed: int,
time_limit_s: int,
wall_time: float,
solver_is_feasible: bool,
routes: list[list[int]] = list(),
solver_report: dict[str, Any] | None = None,
route_count: int = 0,
vehicle_penalty: int = 0,
metadata: dict[str, Any] = dict(),
)
metadata
class-attribute
instance-attribute
¶
hydrate_collection_instance
¶
hydrate_collection_instance(
instance: AnyBenchmarkInstance,
instance_path: str | Path | None = None,
arc_costs: list[list[int]]
| list[list[float]]
| None = None,
) -> AnyBenchmarkInstance
An embedded-matrix view of a slim collection instance, usable with the checker.
Instances outside the static collection formats pass through unchanged. For a collection
instance the matrix is arc_costs if given, else resolved from the
instance's arc_costs_source (resolve_arc_costs, which needs
instance_path for sidecar sources). Hydration preserves fractional service times and windows.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/artifacts.py
to_vrplib_dict
¶
to_vrplib_dict(
instance: AnyStaticBenchmarkInstance,
arc_costs: list[list[int]]
| list[list[float]]
| None = None,
) -> dict[str, Any]
Convert a benchmark instance into a VRPLIB-shaped dict consumable by PyVRP's parser.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/pyvrp.py
to_pyvrp_problem
¶
to_pyvrp_problem(
instance: AnyStaticBenchmarkInstance,
round_func: str | _RoundingFunc = "none",
arc_costs: list[list[int]]
| list[list[float]]
| None = None,
) -> ProblemData
Convert a benchmark instance into a PyVRP ProblemData.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/pyvrp.py
extract_routes
¶
Public name of _extract_routes: non-empty routes in library ids.
conversion_plan
¶
conversion_plan(
instance: AnyStaticBenchmarkInstance,
instance_path: str | Path | None,
) -> tuple[list[list[int]] | list[list[float]], str, int]
Public name of _conversion_plan: (arc costs, round_func, cost scale).
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/pyvrp.py
solve_cvrp
¶
solve_cvrp(
instance: BenchmarkInstanceCVRP
| BenchmarkInstanceCVRPCollection,
*,
time_limit_s: int,
seed: int = 42,
display: bool = False,
instance_path: str | Path | None = None,
monitor: SolveMonitor | None = None,
initial_routes: list[list[int]] | None = None,
arc_costs: list[list[int]]
| list[list[float]]
| None = None,
) -> MethodResult
Solve a CVRP instance with PyVRP's ILS (mono-cost objective only).
initial_routes (library ids) warm-start the search. arc_costs is the instance's
matrix when the caller already resolved it (a slim collection instance's sidecar is read
here otherwise).
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/pyvrp.py
solve_vrptw
¶
solve_vrptw(
instance: BenchmarkInstanceVRPTW
| BenchmarkInstanceVRPTWCollection,
*,
objective_function: ObjectiveFunction,
time_limit_s: int,
seed: int = 42,
display: bool = False,
instance_path: str | Path | None = None,
monitor: SolveMonitor | None = None,
initial_routes: list[list[int]] | None = None,
arc_costs: list[list[int]]
| list[list[float]]
| None = None,
) -> MethodResult
Solve a VRPTW instance with PyVRP's ILS.
initial_routes (library ids) warm-start a MonoCost solve; the hierarchical
solve builds its own warm start on the minimised fleet and ignores them.
arc_costs is the instance's matrix when the caller already resolved it.
For ObjectiveFunction.MONO_COST, runs a single solve with the full time budget.
For ObjectiveFunction.HIERARCHICAL_VEHICLE_COST, runs a two-phase solve:
1. Fleet minimization (PyVRP's minimise_fleet) within ~30% of the budget.
2. Cost optimization on the constrained fleet for the remaining budget,
with a per-vehicle fixed cost large enough to dominate routing cost.
Phase 2 is warm-started from a feasible solution re-found on the
constrained fleet: the fixed cost dwarfs the solver's capped
infeasibility penalties, so a cold start can otherwise stabilize on
infeasible solutions that use fewer vehicles.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/pyvrp.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
solve_instance
¶
solve_instance(
instance: AnyStaticBenchmarkInstance,
*,
time_limit_s: int,
seed: int = 42,
objective_function: ObjectiveFunction | None = None,
display: bool = False,
instance_path: str | Path | None = None,
monitor: SolveMonitor | None = None,
initial_routes: list[list[int]] | None = None,
arc_costs: list[list[int]]
| list[list[float]]
| None = None,
) -> MethodResult
Dispatch to solve_cvrp or solve_vrptw based on instance type.
For VRPTW instances, objective_function defaults to MONO_COST when not provided.
For CVRP instances, the objective is fixed to MONO_COST regardless of the argument.
Slim collection instances need instance_path when their arc costs live in
a distances sidecar, unless arc_costs hands the resolved matrix in. Pass a
SolveMonitor to observe the search while it runs.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/solvers/pyvrp.py
solve_and_update_bks
¶
solve_and_update_bks(
instance: AnyStaticBenchmarkInstance,
*,
instance_path: str | Path,
time_limit_s: int,
seed: int = 42,
objective_function: ObjectiveFunction | None = None,
authors: str,
display: bool = False,
) -> tuple[MethodResult, BKSUpdateResult | None]
Solve an instance and, if the solution is feasible, write a BKS file in place.
authors (who to credit in the BKS) is required: a stored BKS is published.
Returns the MethodResult and a BKSUpdateResult (or None if the solver did not
return a feasible solution). The checked BKS submission uses the same solver
metadata and family evaluation as the registry path.