Skip to content

mamut_routing_lib.td.bks

Create and compare TD incumbents under td-binary64-exact-v1.

create_td_bks_from_solution

create_td_bks_from_solution(
    loaded: LoadedTDInstance | str | Path,
    solution: BenchmarkSolution,
    *,
    authors: str,
    metadata: dict[str, Any] | None = None,
    objective_function: ObjectiveFunction | None = None,
    backend: str = "native",
) -> BenchmarkBKS

Price a candidate, check any declared cost and bind exact witnesses to canonical routes.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/td/bks.py
def create_td_bks_from_solution(
    loaded: LoadedTDInstance | str | Path,
    solution: BenchmarkSolution,
    *,
    authors: str,
    metadata: dict[str, Any] | None = None,
    objective_function: ObjectiveFunction | None = None,
    backend: str = 'native',
) -> BenchmarkBKS:
    """Price a candidate, check any declared cost and bind exact witnesses to canonical routes."""
    objective_function = _td_objective(loaded, objective_function)
    loaded, manifest = _published_inputs(loaded, objective_function, solution.routes, allow_in_memory=True)
    checker = _checker(loaded, objective_function, backend)
    return _candidate(loaded, checker, solution, authors, metadata, manifest)[0]

save_td_solution_as_bks_if_improved

save_td_solution_as_bks_if_improved(
    loaded: LoadedTDInstance | str | Path,
    solution: BenchmarkSolution,
    *,
    authors: str,
    metadata: dict[str, Any] | None = None,
    objective_function: ObjectiveFunction | None = None,
    backend: str = "native",
) -> BKSUpdateResult

Re-evaluate both route sets and store only a strict exact improvement.

A legacy incumbent keeps its original bytes on ties and worse proposals. Replacing an incumbent with a claim first archives its original record. Writers serialize per instance.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/td/bks.py
def save_td_solution_as_bks_if_improved(
    loaded: LoadedTDInstance | str | Path,
    solution: BenchmarkSolution,
    *,
    authors: str,
    metadata: dict[str, Any] | None = None,
    objective_function: ObjectiveFunction | None = None,
    backend: str = 'native',
) -> BKSUpdateResult:
    """Re-evaluate both route sets and store only a strict exact improvement.

    A legacy incumbent keeps its original bytes on ties and worse proposals. Replacing an
    incumbent with a claim first archives its original record. Writers serialize per instance.
    """
    from mamut_routing_lib.exact_store import preserve_claim_record, verify_stored

    instance_path = loaded.instance_path if isinstance(loaded, LoadedTDInstance) else loaded
    objective_function = _td_objective(loaded, objective_function)
    path = get_bks_path_for_instance(instance_path, objective_function)
    incumbent_bks = load_bks(path) if path.exists() else None
    all_routes = [*solution.routes, *incumbent_bks.routes] if incumbent_bks is not None else solution.routes
    loaded, manifest = _published_inputs(loaded, objective_function, all_routes)
    checker = _checker(loaded, objective_function, backend)
    candidate_bks, candidate_evaluation = _candidate(loaded, checker, solution, authors, metadata, manifest)
    previous = None
    tie = False
    action: BKSUpdateAction = 'created'
    if incumbent_bks is not None:
        previous = path
        if incumbent_bks.instance_name != candidate_bks.instance_name or incumbent_bks.objective_function != checker.objective:
            raise ValueError(f'Stored BKS identifies a different instance or objective at {path}')
        try:
            incumbent_evaluation = checker.evaluate(incumbent_bks.routes)
            verify_stored(incumbent_bks, incumbent_evaluation, manifest)
        except ValueError as error:
            raise ValueError(f'Stored BKS is invalid at {path}: {error}') from error
        candidate_objective_key = candidate_evaluation.objective_key
        incumbent_objective_key = incumbent_evaluation.objective_key
        tie = candidate_objective_key == incumbent_objective_key
        if candidate_objective_key < incumbent_objective_key:
            preserve_claim_record(path, incumbent_bks)
            action = 'replaced'
        else:
            action = 'kept_existing'
    if action != 'kept_existing':
        write_bks_file(candidate_bks, path)
    return BKSUpdateResult(action=action, path=path, previous_path=previous, tie=tie,
                           candidate_cost=candidate_evaluation.display_cost, candidate_num_routes=len(candidate_evaluation.routes))

annotate_td_bks_optimality

annotate_td_bks_optimality(
    loaded: LoadedTDInstance | str | Path,
    optimality: SolverReportedOptimality | dict[str, Any],
    objective_function: ObjectiveFunction | None = None,
    *,
    backend: str = "native",
) -> Path

Attach a solver claim of optimality to the existing incumbent under the declared objective.

The claim must be a SolverReportedOptimality whose target_profile is the TD profile, and the incumbent must carry an exact evaluation that still verifies. The legacy proven: true stamp is refused. Reported bounds do not certify the incumbent.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/td/bks.py
def annotate_td_bks_optimality(
    loaded: LoadedTDInstance | str | Path,
    optimality: SolverReportedOptimality | dict[str, Any],
    objective_function: ObjectiveFunction | None = None,
    *,
    backend: str = 'native',
) -> Path:
    """Attach a solver claim of optimality to the existing incumbent under the declared objective.

    The claim must be a ``SolverReportedOptimality`` whose ``target_profile`` is the
    TD profile, and the incumbent must carry an exact evaluation that still verifies.
    The legacy ``proven: true`` stamp is refused. Reported bounds do not certify the incumbent.
    """
    from mamut_routing_lib.exact_store import preserve_claim_record, verify_stored
    from mamut_routing_lib.exact_types import TD_PROFILE

    objective_function = _td_objective(loaded, objective_function)
    instance_path = loaded.instance_path if isinstance(loaded, LoadedTDInstance) else loaded
    path = get_bks_path_for_instance(instance_path, objective_function)
    if not path.exists():
        raise FileNotFoundError(f'No stored {objective_function.value} BKS to annotate at {path}')
    bks = load_bks(path)
    loaded, manifest = _published_inputs(loaded, objective_function, bks.routes)
    if bks.instance_name != loaded.instance.instance_name or bks.objective_function != objective_function:
        raise ValueError(f'Stored BKS identifies a different instance or objective at {path}')
    payload = optimality if isinstance(optimality, dict) else optimality.model_dump(mode='json', exclude_none=True)
    claim = SolverReportedOptimality.model_validate(payload)
    if claim.target_profile != TD_PROFILE:
        raise ValueError('solver claim must target the profile of the stored TD result')
    if 'exact_evaluation' not in bks.metadata and 'standard' not in bks.metadata:
        raise ValueError(f'A solver claim requires an incumbent evaluated under {TD_PROFILE} at {path}')
    checker = _checker(loaded, objective_function, backend)
    verify_stored(bks, checker.evaluate(bks.routes), manifest)
    record = claim.model_dump(mode='json', exclude_none=True)
    if record != bks.metadata.get('optimality'):
        stamped = BenchmarkBKS(**{**bks.model_dump(), 'metadata': {**bks.metadata, 'optimality': record}})
        preserve_claim_record(path, bks)
        write_bks_file(stamped, path)
    return path