Skip to content

mamut_routing_lib.bks

Reprice and store static incumbents using the exact binary64 objective.

An existing record keeps its place on an exact tie. Historical displayed costs can come from earlier arithmetic rules, so compare their submitted routes again on the current instance. Callers serialize writers per instance.

DEFAULT_BKS_AUTHORS module-attribute

DEFAULT_BKS_AUTHORS = (
    "Florian Rascoussier (0nyr) and Adrien Pichon (Anzury)"
)

BKSUpdateAction module-attribute

BKSUpdateAction = Literal[
    "created", "replaced", "kept_existing"
]

What a store write did: wrote a first record, replaced a worse one, or kept the incumbent.

BKSPreviewAction module-attribute

BKSPreviewAction = Literal[
    "would_create",
    "would_replace",
    "would_keep",
    "invalid",
    "stored_invalid",
]

What preview_bks_update reports a store write would do, without writing.

BKSUpdateResult dataclass

BKSUpdateResult(
    action: BKSUpdateAction,
    path: Path,
    previous_path: Path | None,
    candidate_cost: int | float | None,
    candidate_num_routes: int,
    tie: bool = False,
)

The candidate's display cost and route count, even when the incumbent wins.

A display overflow gives None. If an incumbent existed, previous_path is its store path, including when that path now holds the replacement. previous_path therefore does not identify a backup of the replaced record. Only an incumbent that carries an optimality claim is archived, under .bks-history/ next to it. Any other replaced record is gone.

action instance-attribute

action: BKSUpdateAction

path instance-attribute

path: Path

previous_path instance-attribute

previous_path: Path | None

candidate_cost instance-attribute

candidate_cost: int | float | None

candidate_num_routes instance-attribute

candidate_num_routes: int

tie class-attribute instance-attribute

tie: bool = False

create_bks_from_solution

create_bks_from_solution(
    instance: LoadedStaticInstance,
    solution: BenchmarkSolution,
    objective_function: ObjectiveFunction,
    *,
    authors: str,
    metadata: dict[str, Any] | None = None,
    backend: ExactBackend = "native",
) -> BenchmarkBKS

Create a canonical exact BKS from a solution and a materialized instance.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/bks.py
def create_bks_from_solution(
    instance: LoadedStaticInstance,
    solution: BenchmarkSolution,
    objective_function: ObjectiveFunction,
    *,
    authors: str,
    metadata: dict[str, Any] | None = None,
    backend: ExactBackend = "native",
) -> BenchmarkBKS:
    """Create a canonical exact BKS from a solution and a materialized instance."""
    prepared = prepare_static(instance, backend=backend, objective_function=objective_function)
    candidate_bks, _ = _candidate(prepared, get_instance_identifier(instance), solution, authors, metadata)
    return candidate_bks

save_bks_if_improved

save_bks_if_improved(
    instance: LoadedStaticInstance,
    candidate_bks: BenchmarkBKS,
    instance_path: str | Path,
    *,
    backend: ExactBackend = "native",
) -> BKSUpdateResult

Recheck the candidate and incumbent and replace only on a strict improvement.

Without a manifest, the supplied instance must equal the descriptor (and its hydrated matrix) stored at instance_path. Otherwise this raises ValueError.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/bks.py
def save_bks_if_improved(
    instance: LoadedStaticInstance,
    candidate_bks: BenchmarkBKS,
    instance_path: str | Path,
    *,
    backend: ExactBackend = "native",
) -> BKSUpdateResult:
    """Recheck the candidate and incumbent and replace only on a strict improvement.

    Without a manifest, the supplied instance must equal the descriptor (and its
    hydrated matrix) stored at ``instance_path``. Otherwise this raises ``ValueError``.
    """
    return _save_bks_if_improved(instance, candidate_bks, instance_path, backend=backend, check_descriptor=True)

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,
    backend: ExactBackend = "native",
    supplied_checkable: LoadedStaticInstance | None = None,
) -> BKSUpdateResult

Load and prepare the instance once, check both submissions and store the winner.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/bks.py
def save_solution_as_bks_if_improved(
    instance_path: str | Path,
    objective_function: ObjectiveFunction,
    solution: BenchmarkSolution,
    *,
    authors: str,
    metadata: dict[str, Any] | None = None,
    backend: ExactBackend = "native",
    supplied_checkable: LoadedStaticInstance | None = None,
) -> BKSUpdateResult:
    """Load and prepare the instance once, check both submissions and store the winner."""
    from mamut_routing_lib.manifests import adjacent_manifest
    manifest = adjacent_manifest(instance_path, objective_function, supplied_checkable=supplied_checkable)
    instance = manifest.checkable if manifest else hydrate_collection_instance(load_benchmark_instance(instance_path), instance_path)
    if manifest is None and supplied_checkable is not None and supplied_checkable != instance:
        raise ValueError(f"Supplied materialized input differs from the descriptor or matrix at {instance_path}")
    prepared = prepare_static(instance, backend=backend, objective_function=objective_function)
    candidate_bks, candidate_evaluation = _candidate(
        prepared, get_instance_identifier(instance), solution, authors, metadata, manifest,
    )
    return _store(get_bks_path_for_instance(instance_path, prepared.objective), prepared, candidate_bks, candidate_evaluation, manifest)