Skip to content

mamut_routing_lib.checker

Check and price static routes under the family's binary64-exact-v1 profile.

SolutionCheckResult

Bases: BaseModel

Checked route count, display cost and lossless exact evaluation for a valid solution.

A valid result carries a nonnegative num_routes. Its routing_cost is present, except when exact_evaluation reports display_overflow: the exact cost then has no finite binary64 display and routing_cost is None. A solution that names another instance or declares another objective is not checked at all. The checker raises ValueError for it instead of returning a status.

status instance-attribute

status: SolutionCheckStatus

routing_cost instance-attribute

routing_cost: int | float | None

num_routes instance-attribute

num_routes: int | None

error_message class-attribute instance-attribute

error_message: str = ''

exact_evaluation class-attribute instance-attribute

exact_evaluation: dict | None = None

is_valid

is_valid() -> bool

True when the status is valid.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def is_valid(self) -> bool:
    """True when the status is ``valid``."""
    return self.status == SolutionCheckStatus.VALID

make_invalid classmethod

make_invalid(
    status: SolutionCheckStatus, error_message: str
) -> "SolutionCheckResult"

A rejection with status and a human-readable error_message.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
@classmethod
def make_invalid(cls, status: SolutionCheckStatus, error_message: str) -> "SolutionCheckResult":
    """A rejection with ``status`` and a human-readable ``error_message``."""
    return cls(status=status, routing_cost=None, num_routes=None, error_message=error_message)

make_valid classmethod

make_valid(
    routing_cost: int | float, num_routes: int
) -> "SolutionCheckResult"

A valid result carrying the priced cost and route count.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
@classmethod
def make_valid(cls, routing_cost: int | float, num_routes: int) -> "SolutionCheckResult":
    """A ``valid`` result carrying the priced cost and route count."""
    return cls(status=SolutionCheckStatus.VALID, routing_cost=routing_cost, num_routes=num_routes)

compute_route_cost

compute_route_cost(
    instance: LoadedStaticInstance,
    route: list[int],
    *,
    backend: ExactBackend = "native",
) -> Fraction

Return the exact cost of the listed depot-to-depot arcs, including a partial tour.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def compute_route_cost(instance: LoadedStaticInstance, route: list[int], *, backend: ExactBackend = "native") -> Fraction:
    """Return the exact cost of the listed depot-to-depot arcs, including a partial tour."""
    return compute_solution_cost(instance, [route], backend=backend)

compute_solution_cost

compute_solution_cost(
    instance: LoadedStaticInstance,
    solution_or_routes: BenchmarkSolution
    | BenchmarkBKS
    | list[list[int]],
    *,
    backend: ExactBackend = "native",
) -> Fraction

Sum the traversed arcs under binary64-exact-v1. Use check_solution to also check feasibility.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def compute_solution_cost(
    instance: LoadedStaticInstance,
    solution_or_routes: BenchmarkSolution | BenchmarkBKS | list[list[int]],
    *,
    backend: ExactBackend = "native",
) -> Fraction:
    """Sum the traversed arcs under binary64-exact-v1. Use check_solution to also check feasibility."""
    from mamut_routing_lib.exact_static import prepare_static

    _require_static_matrix(instance)
    return prepare_static(instance, backend=backend).routing_cost(_iter_routes(solution_or_routes))

canonical_route_order

canonical_route_order(
    routes: Iterable[list[int]],
) -> list[list[int]]

Routes sorted by their first customer: the canonical storage and summation order of every BKS.

Each route must be a nonempty list of positive integer customer ids, and no customer may appear twice, so that the order is unique. An empty route list is returned as is. Invalid input raises ValueError. This checks only the shape of the routes, not their feasibility on an instance.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def canonical_route_order(routes: Iterable[list[int]]) -> list[list[int]]:
    """Routes sorted by their first customer: the canonical storage and summation order of every BKS.

    Each route must be a nonempty list of positive integer customer ids, and no
    customer may appear twice, so that the order is unique. An empty route list
    is returned as is. Invalid input raises ``ValueError``. This checks only the
    shape of the routes, not their feasibility on an instance.
    """
    ordered = list(routes)
    seen: set[int] = set()
    for route in ordered:
        if not isinstance(route, (list, tuple)) or not route:
            raise ValueError("canonical_route_order requires nonempty routes")
        for customer in route:
            if isinstance(customer, bool) or not isinstance(customer, Integral) or customer <= 0:
                raise ValueError(f"canonical_route_order requires positive integer customer ids, got {customer!r}")
            if customer in seen:
                raise ValueError(f"canonical_route_order requires distinct customers, {customer} appears twice")
            seen.add(customer)
    ordered.sort(key=lambda route: route[0])
    return ordered

shortest_decimal_cost_value

shortest_decimal_cost_value(value: int | float) -> Fraction

Keep integers exact and interpret a float's shortest round-trip decimal spelling.

For example, the float 0.1 maps to 1/10. Multiple source decimal tokens can round to that same float, so the spelling alone cannot recover them.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def shortest_decimal_cost_value(value: int | float) -> Fraction:
    """Keep integers exact and interpret a float's shortest round-trip decimal spelling.

    For example, the float 0.1 maps to 1/10. Multiple source decimal tokens
    can round to that same float, so the spelling alone cannot recover them.
    """
    if isinstance(value, bool):
        raise TypeError("a cost cannot be a boolean")
    if isinstance(value, Integral):
        return Fraction(int(value))
    return Fraction(repr(float(value)))

compute_shortest_decimal_solution_cost

compute_shortest_decimal_solution_cost(
    instance: LoadedStaticInstance,
    solution_or_routes: BenchmarkSolution
    | BenchmarkBKS
    | list[list[int]],
) -> Fraction

Exact rational routing cost: the sum of shortest_decimal_cost_value over every arc, depot to depot.

Independent of route order and, on a symmetric matrix, of route direction. No feasibility check.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def compute_shortest_decimal_solution_cost(
    instance: LoadedStaticInstance,
    solution_or_routes: BenchmarkSolution | BenchmarkBKS | list[list[int]],
) -> Fraction:
    """Exact rational routing cost: the sum of ``shortest_decimal_cost_value`` over every arc, depot to depot.

    Independent of route order and, on a symmetric matrix, of route direction.
    No feasibility check.
    """
    _require_static_matrix(instance)
    total = Fraction(0)
    depot = instance.depot
    for route in _iter_routes(solution_or_routes):
        previous_node = depot
        for customer in route:
            total += shortest_decimal_cost_value(instance.arc_costs[previous_node][customer])
            previous_node = customer
        total += shortest_decimal_cost_value(instance.arc_costs[previous_node][depot])
    return total

check_solution

check_solution(
    instance: LoadedStaticInstance,
    solution: BenchmarkSolution | BenchmarkBKS,
    *,
    backend: ExactBackend = "native",
    objective_function: ObjectiveFunction | None = None,
    instance_path: str | Path | None = None,
) -> SolutionCheckResult

Check an embedded static instance under binary64-exact-v1. Supply its path to verify a certified BKS.

Raises ValueError when the solution names another instance or declares another objective, since no check status describes a mismatched submission.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def check_solution(
    instance: LoadedStaticInstance,
    solution: BenchmarkSolution | BenchmarkBKS,
    *,
    backend: ExactBackend = "native",
    objective_function: ObjectiveFunction | None = None,
    instance_path: str | Path | None = None,
) -> SolutionCheckResult:
    """Check an embedded static instance under binary64-exact-v1. Supply its path to verify a certified BKS.

    Raises ``ValueError`` when the solution names another instance or declares
    another objective, since no check status describes a mismatched submission.
    """
    from mamut_routing_lib.solutions import _check_solution_model

    _require_static_matrix(instance)
    return _check_solution_model(instance, instance_path, instance, solution, objective_function, backend)

check_cvrp_solution

check_cvrp_solution(
    instance: BenchmarkInstanceCVRP,
    solution: BenchmarkSolution | BenchmarkBKS,
    *,
    backend: ExactBackend = "native",
    objective_function: ObjectiveFunction | None = None,
    instance_path: str | Path | None = None,
) -> SolutionCheckResult

Check an embedded CVRP solution under the family profile.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def check_cvrp_solution(
    instance: BenchmarkInstanceCVRP,
    solution: BenchmarkSolution | BenchmarkBKS,
    *,
    backend: ExactBackend = "native",
    objective_function: ObjectiveFunction | None = None,
    instance_path: str | Path | None = None,
) -> SolutionCheckResult:
    """Check an embedded CVRP solution under the family profile."""
    if not isinstance(instance, BenchmarkInstanceCVRP):
        raise TypeError("check_cvrp_solution requires an embedded CVRP matrix.")
    return check_solution(instance, solution, backend=backend, objective_function=objective_function, instance_path=instance_path)

check_vrptw_solution

check_vrptw_solution(
    instance: BenchmarkInstanceVRPTW,
    solution: BenchmarkSolution | BenchmarkBKS,
    *,
    backend: ExactBackend = "native",
    objective_function: ObjectiveFunction | None = None,
    instance_path: str | Path | None = None,
) -> SolutionCheckResult

Check an embedded VRPTW solution under the family profile.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def check_vrptw_solution(
    instance: BenchmarkInstanceVRPTW,
    solution: BenchmarkSolution | BenchmarkBKS,
    *,
    backend: ExactBackend = "native",
    objective_function: ObjectiveFunction | None = None,
    instance_path: str | Path | None = None,
) -> SolutionCheckResult:
    """Check an embedded VRPTW solution under the family profile."""
    if not isinstance(instance, BenchmarkInstanceVRPTW):
        raise TypeError("check_vrptw_solution requires an embedded VRPTW matrix with time windows.")
    return check_solution(instance, solution, backend=backend, objective_function=objective_function, instance_path=instance_path)

get_objective_tuple

get_objective_tuple(
    routes: list[list[int]],
    cost: int | float | Fraction,
    objective_function: ObjectiveFunction,
) -> tuple[int | float | Fraction, ...]

The comparison key of a solution under objective_function: (cost,) or (num_routes, cost) for the hierarchical objective.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def get_objective_tuple(
    routes: list[list[int]],
    cost: int | float | Fraction,
    objective_function: ObjectiveFunction,
) -> tuple[int | float | Fraction, ...]:
    """The comparison key of a solution under ``objective_function``: ``(cost,)`` or ``(num_routes, cost)`` for the hierarchical objective."""
    if objective_function in (
        ObjectiveFunction.MONO_COST,
        ObjectiveFunction.DURATION,
        ObjectiveFunction.FLEET_COST_DURATION,
    ):
        # Mono-objective: for FleetCostDuration the vehicle count is already
        # priced into the cost (duration + fleet_fixed_cost * num_routes), so
        # there is no lexicographic tier.
        return (cost,)
    return (len(routes), cost)

is_better_solution

is_better_solution(
    candidate_routes: list[list[int]],
    candidate_cost: int | float | Fraction,
    existing_routes: list[list[int]],
    existing_cost: int | float | Fraction,
    objective_function: ObjectiveFunction,
) -> bool

True when the candidate strictly precedes the existing solution under objective_function (see get_objective_tuple).

Pass the authoritative rational costs to compare under an exact family profile.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def is_better_solution(
    candidate_routes: list[list[int]],
    candidate_cost: int | float | Fraction,
    existing_routes: list[list[int]],
    existing_cost: int | float | Fraction,
    objective_function: ObjectiveFunction,
) -> bool:
    """True when the candidate strictly precedes the existing solution under ``objective_function`` (see ``get_objective_tuple``).

    Pass the authoritative rational costs to compare under an exact family profile.
    """
    return get_objective_tuple(candidate_routes, candidate_cost, objective_function) < get_objective_tuple(
        existing_routes,
        existing_cost,
        objective_function,
    )

is_better_exact

is_better_exact(
    candidate_routes: list[list[int]],
    candidate_exact_cost: Fraction,
    existing_routes: list[list[int]],
    existing_exact_cost: Fraction,
    objective_function: ObjectiveFunction,
) -> bool

Compare the given rational costs under the objective, retaining the incumbent on a tie.

Source code in MAMUT-routing-tools/MAMUT-routing-lib/src/mamut_routing_lib/checker.py
def is_better_exact(
    candidate_routes: list[list[int]],
    candidate_exact_cost: Fraction,
    existing_routes: list[list[int]],
    existing_exact_cost: Fraction,
    objective_function: ObjectiveFunction,
) -> bool:
    """Compare the given rational costs under the objective, retaining the incumbent on a tie."""
    return get_objective_tuple(candidate_routes, candidate_exact_cost, objective_function) < get_objective_tuple(
        existing_routes,
        existing_exact_cost,
        objective_function,
    )