mamut_routing_lib.solvers.external.scaling¶
Integer scaling of instance data for external solvers.
For an explicit integer solver, express all materialized binary64 arc costs, time-window endpoints and service times in their largest common dyadic unit. Every emitted integer multiplied by this unit recovers its input exactly. If the integers exceed the solver's bounds, use its decimal-grid search model, or raise ScaleError when exact input was requested. The fallback reads the operands' shortest decimal spellings and rounds arcs to nearest for CVRP. For VRPTW it rounds travel, service and openings up and closings down on this decimal reading. The benchmark checker prices returned routes independently on the original binary64 data.
Coordinate solvers select from their supported coordinate scales. Floating matrix solvers select a scale that their reader supports and accumulate in floating point. Provenance identifies the search model used in each case.
ScaleError
¶
Bases: ValueError
No scale factor fits the solver's numeric limits for this instance.
Limits
dataclass
¶
Limits(
max_arc_cost: float | None = None,
max_total_cost: float | None = None,
max_time_value: float | None = None,
max_demand: float | None = None,
min_arc_cost: float | None = None,
)
Numeric limits of a solver, in scaled units (None = unbounded).
ScalePlan
dataclass
¶
ScalePlan(
mode: Literal["explicit", "coordinates"],
factor: float | int | None,
exact: bool,
rounding: Literal[
"exact",
"nearest",
"conservative",
"solver",
"float",
],
max_scaled_arc: int | float,
star_bound_scaled: int | float,
max_arc_error: float = 0.0,
coordinate_decimals: int | None = None,
unit: str | None = None,
search_model: str = "",
cost_unit: Fraction | None = None,
total_cost_bound_scaled: int | None = None,
time_bound_scaled: int | None = None,
)
How instance data was scaled for one solver run.
decimal_places
¶
Decimals in the shortest spelling (0.125 -> 3, 7 -> 0), None above limit.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
max_decimals
¶
Smallest k <= limit such that every value is exactly round(v * 10**k) / 10**k (None if none).
A round trip, not a tolerance: 0.123 has 3 decimals; a full-precision
double such as 265.88719412562915 (Sintef2008) or float noise such as
0.30000000000000004 has none within the limit. The round trip is a fast
estimate of the shortest-decimal reading. Confirm the answer against
that reading with :func:agrees_with_checker and return None on disagreement.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
agrees_with_checker
¶
Whether each sampled shortest decimal value is a multiple of 10**-places.
max_decimals decides by a float round trip, and collections declare their
decimals (cvrplib.collection_cost_decimals); an exact scale is only sound if
its shortest-decimal reading agrees. sample values, evenly strided through
values (a flat sequence or rows), are checked with that reading.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
star_bound
¶
sum_i (a[depot][i] + a[i][depot]): the cost of one route per customer.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
plan_explicit_scale
¶
plan_explicit_scale(
arc_costs: Sequence[Sequence[float]],
*,
depot: int = 0,
time_windows: Sequence[Sequence[float]] | None = None,
service_times: Sequence[float] | None = None,
limits: Limits = Limits(),
require_exact: bool = False,
decimals_hint: int | None = None,
) -> ScalePlan
Choose a common operand unit and check the solver's bounds in emitted integer units.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
scale_explicit_data
¶
scale_explicit_data(
plan: ScalePlan,
arc_costs: Sequence[Sequence[float]],
*,
time_windows: Sequence[Sequence[float]] | None = None,
service_times: Sequence[float] | None = None,
) -> tuple[
list[list[int]],
list[tuple[int, int]] | None,
list[int] | None,
]
Apply the selected rational unit to every operand sent to the integer solver.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
unscale_cost
¶
unscale_cost(
raw: int | float | Decimal | None, plan: ScalePlan
) -> tuple[float | None, Fraction | None]
Return a display value and, for exact integer exports, the lossless unscaled report.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
plan_float_matrix_scale
¶
plan_float_matrix_scale(
arc_costs: Sequence[Sequence[float]],
*,
depot: int = 0,
candidates: Sequence[float] = (1.0, 0.1, 0.01, 0.001),
limits: Limits = Limits(),
decimals_hint: int | None = None,
) -> ScalePlan
The first factor of candidates whose largest arc and star bound fit the limits.
At factor one the floating reader recovers the published binary64 values. A shifted decimal spelling can round to a different binary64 significand.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
format_float_matrix
¶
Matrix rows as decimal text for plan: the published values at factor 1, exact decimal
multiples at 10**-k (Decimal arithmetic on each value's shortest text).
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
plan_coordinate_scale
¶
plan_coordinate_scale(
coordinates: Sequence[Sequence[float]],
*,
depot: int = 0,
candidates: Sequence[float] = (1.0,),
limits: Limits = Limits(),
margin: float = 0.9,
distance_model: Literal["float", "nint"] = "float",
published_decimals: int | None = None,
) -> ScalePlan
Choose the coordinate factor for a solver that computes Euclidean distances itself.