mamut_routing_lib.bks¶
Creating and replacing best-known solutions (static families).
Two guarantees: the stored cost is always the checker's value
(create_bks_from_solution re-prices and raises on an invalid solution),
and a stored BKS is replaced only by a strictly better one under its
objective, after the stored file was itself re-validated
(save_bks_if_improved). The store is a plain read/compare/write with no
lock: concurrent writers for the same instance must be serialized by the
caller (one coordinator per instance), otherwise a worse candidate can win
the race. Time-dependent families use mamut_routing_lib.td.bks.
DEFAULT_BKS_AUTHORS
module-attribute
¶
BKSUpdateResult
dataclass
¶
BKSUpdateResult(
action: str,
path: Path,
previous_path: Path | None,
candidate_cost: int | float,
candidate_num_routes: int,
)
What a store call did: action is created, replaced or kept_existing; path is the BKS file.
create_bks_from_solution
¶
create_bks_from_solution(
instance: AnyBenchmarkInstance,
solution: BenchmarkSolution,
objective_function: ObjectiveFunction,
*,
authors: str,
metadata: dict[str, Any] | None = None,
) -> BenchmarkBKS
Validate solution on instance and wrap it as a BenchmarkBKS.
Raises ValueError when the checker rejects the solution or authors
is empty. The BKS cost is the checker's value; metadata gets
authors, validated_cost, validated_num_routes and an ISO date
(UTC) unless already present. instance must be an embedded-matrix model
(hydrate collection instances first).
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/bks.py
save_bks_if_improved
¶
save_bks_if_improved(
instance: AnyBenchmarkInstance,
bks: BenchmarkBKS,
instance_path: str | Path,
) -> BKSUpdateResult
Write bks next to instance_path unless a strictly better BKS is stored there.
The existing file, if any, is re-validated on instance (an invalid
stored BKS raises rather than being silently overwritten), then compared
with checker.is_better_solution under bks.objective_function.
Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/bks.py
save_solution_as_bks_if_improved
¶
save_solution_as_bks_if_improved(
instance_path: str | Path,
objective_function: ObjectiveFunction,
solution: BenchmarkSolution,
*,
authors: str,
metadata: dict[str, Any] | None = None,
) -> BKSUpdateResult
Load, hydrate, validate and store in one call.
Loads the instance at instance_path (slim collection instances are
hydrated from their sidecars), builds the BKS with
create_bks_from_solution and applies save_bks_if_improved. Raises
TypeError for time-dependent instances.