Skip to content

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.

MAX_DECIMALS module-attribute

MAX_DECIMALS = 9

MIN_EXPONENT module-attribute

MIN_EXPONENT = -3

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).

max_arc_cost class-attribute instance-attribute

max_arc_cost: float | None = None

max_total_cost class-attribute instance-attribute

max_total_cost: float | None = None

max_time_value class-attribute instance-attribute

max_time_value: float | None = None

max_demand class-attribute instance-attribute

max_demand: float | None = None

min_arc_cost class-attribute instance-attribute

min_arc_cost: float | None = None

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.

mode instance-attribute

mode: Literal['explicit', 'coordinates']

factor instance-attribute

factor: float | int | None

exact instance-attribute

exact: bool

rounding instance-attribute

rounding: Literal[
    "exact", "nearest", "conservative", "solver", "float"
]

max_scaled_arc instance-attribute

max_scaled_arc: int | float

star_bound_scaled instance-attribute

star_bound_scaled: int | float

max_arc_error class-attribute instance-attribute

max_arc_error: float = 0.0

coordinate_decimals class-attribute instance-attribute

coordinate_decimals: int | None = None

unit class-attribute instance-attribute

unit: str | None = None

search_model class-attribute instance-attribute

search_model: str = ''

cost_unit class-attribute instance-attribute

cost_unit: Fraction | None = None

total_cost_bound_scaled class-attribute instance-attribute

total_cost_bound_scaled: int | None = None

time_bound_scaled class-attribute instance-attribute

time_bound_scaled: int | None = None

family_units_per_solver_unit property

family_units_per_solver_unit: Fraction

Size of the reported solver unit in family units.

to_json

to_json() -> dict[str, Any]
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
def to_json(self) -> dict[str, Any]:
    data = {key: value for key, value in asdict(self).items() if value is not None}
    if self.cost_unit is not None:
        data["cost_unit"] = {"numerator": str(self.cost_unit.numerator), "denominator": str(self.cost_unit.denominator)}
    return data

decimal_places

decimal_places(
    value: int | float, *, limit: int = MAX_DECIMALS
) -> int | None

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
def decimal_places(value: int | float, *, limit: int = MAX_DECIMALS) -> int | None:
    """Decimals in the shortest spelling (``0.125`` -> 3, ``7`` -> 0), ``None`` above ``limit``."""
    denominator = _exact_value(value).denominator
    twos = fives = 0
    while denominator % 2 == 0:
        denominator //= 2
        twos += 1
    while denominator % 5 == 0:
        denominator //= 5
        fives += 1
    places = max(twos, fives)
    return places if places <= limit else None

max_decimals

max_decimals(
    values: Any, *, limit: int = MAX_DECIMALS
) -> int | None

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
def max_decimals(values: Any, *, limit: int = MAX_DECIMALS) -> int | None:
    """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.
    """
    flat = _flatten(values)
    if not flat:
        return 0
    found: int | None = None
    if _np is not None:
        array = _np.asarray(flat, dtype=_np.float64)
        for places in range(0, limit + 1):
            factor = 10.0**places
            if _np.array_equal(_np.round(array * factor) / factor, array):
                found = places
                break
    else:
        for places in range(0, limit + 1):
            factor = 10.0**places
            if all(math.isfinite(v * factor) and round(v * factor) / factor == v for v in flat):
                found = places
                break
    if found is None or not agrees_with_checker(flat, found):
        return None
    return found

agrees_with_checker

agrees_with_checker(
    values: Any, places: int, *, sample: int = 4096
) -> bool

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
def agrees_with_checker(values: Any, places: int, *, sample: int = 4096) -> bool:
    """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.
    """
    rows = values if isinstance(values, (list, tuple)) else ([] if values is None else list(values))
    if not rows:
        return True
    items = iter(rows) if isinstance(rows[0], numbers.Real) else itertools.chain.from_iterable(rows)
    total = len(rows) if isinstance(rows[0], numbers.Real) else sum(len(row) for row in rows)
    step = max(1, total // max(1, sample))
    scale = 10**places
    return all((_exact_value(value) * scale).denominator == 1 for value in itertools.islice(items, 0, None, step))

star_bound

star_bound(
    arc_costs: Sequence[Sequence[float]], depot: int = 0
) -> float

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
def star_bound(arc_costs: Sequence[Sequence[float]], depot: int = 0) -> float:
    """``sum_i (a[depot][i] + a[i][depot])``: the cost of one route per customer."""
    return float(sum(float(arc_costs[depot][i]) + float(arc_costs[i][depot]) for i in range(len(arc_costs)) if i != depot))

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
def 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."""
    if (time_windows is None) != (service_times is None):
        raise ScaleError("time windows and service times must be supplied together")
    all_values = itertools.chain(
        itertools.chain.from_iterable(arc_costs),
        itertools.chain.from_iterable(time_windows or ()),
        service_times or (),
    )
    exact_unit = _common_dyadic_unit(all_values)
    row_maxima = [max(row) for row in arc_costs]
    customers = [i for i in range(len(arc_costs)) if i != depot]
    vrptw = time_windows is not None

    def candidate(unit: Fraction, exact: bool) -> ScalePlan | None:
        rounding = "exact" if exact else ("conservative" if vrptw else "nearest")
        weight = _integer_weights(unit, "up" if rounding == "conservative" else rounding)
        arc = weight(max(row_maxima))
        # Each customer has one outgoing arc, and each depot departure reaches
        # a distinct customer. This bound also covers nonmetric/asymmetric data.
        departures = sum(weight(arc_costs[depot][i]) for i in customers)
        total = departures + sum(weight(row_maxima[i]) for i in customers)
        time_bound = None
        if time_windows is not None:
            close = weight if exact else _integer_weights(unit, "down")
            # A route can depart by the depot deadline and wait at any opening.
            travel = min(total, (len(customers) + 1) * arc)
            start = max(close(time_windows[depot][1]), max(weight(ready) for ready, _ in time_windows))
            elapsed = start + travel + sum(map(weight, service_times or ()))
            time_bound = max(elapsed, max(close(due) for _, due in time_windows))
        if not _fits(limits, arc=arc, total=total, time=time_bound):
            return None
        reciprocal = 1 / unit
        try:
            factor = reciprocal.numerator if reciprocal.denominator == 1 else float(reciprocal)
        except OverflowError:
            factor = None
        return ScalePlan(
            mode="explicit", factor=factor, exact=exact, rounding=rounding,
            search_model="binary64-exact-integer" if exact else "shortest-decimal-grid",
            cost_unit=unit, max_scaled_arc=arc,
            star_bound_scaled=departures + sum(weight(arc_costs[i][depot]) for i in customers),
            total_cost_bound_scaled=total, time_bound_scaled=time_bound,
            max_arc_error=0.0 if exact else float(unit) * (1.0 if vrptw else 0.5),
        )

    plan = candidate(exact_unit, True)
    if plan is not None:
        return plan
    if require_exact:
        raise ScaleError(f"no exact binary64 integer scale fits the solver's limits (unit {exact_unit})")
    decimals = _declared_or_measured(arc_costs, decimals_hint)
    if decimals is not None and time_windows is not None:
        windows = max_decimals(time_windows)
        services = max_decimals(service_times or ())
        decimals = None if windows is None or services is None else max(decimals, windows, services)
    top = MAX_DECIMALS if decimals is None else min(decimals, MAX_DECIMALS)
    for exponent in range(top, MIN_EXPONENT - 1, -1):
        plan = candidate(Fraction(10) ** -exponent, False)
        if plan is not None:
            return plan
    raise ScaleError(f"no integer scale between 10^{top} and 10^{MIN_EXPONENT} fits the solver's limits")

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
def 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."""
    if plan.cost_unit is None:
        raise ScaleError("integer export needs an explicit integer scale plan")
    weight = _integer_weights(plan.cost_unit, "up" if plan.rounding == "conservative" else plan.rounding)
    matrix = [[weight(value) for value in row] for row in arc_costs]
    if time_windows is None:
        return matrix, None, None
    close = _integer_weights(plan.cost_unit, "down") if plan.rounding == "conservative" else weight
    windows = [(weight(ready), close(due)) for ready, due in time_windows]
    for vertex, (ready, due) in enumerate(windows):
        if ready > due:
            raise ScaleError(f"integer unit {plan.cost_unit} empties the time window at vertex {vertex}: [{ready}, {due}]")
    return matrix, windows, [weight(value) for value in (service_times or ())]

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
def 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."""
    if raw is None:
        return None, None
    if isinstance(raw, Decimal):
        # Even the extreme binary64 units shift the display by at most 324
        # decimal places. Never expand a compact exponent into a huge Fraction.
        if not raw.is_finite() or raw.adjusted() > 700:
            return None, None
        if raw.adjusted() < -700:
            return -0.0 if raw.is_signed() else 0.0, None
        if raw.as_tuple().exponent < -2000:
            # A malformed fractional report can contain arbitrary precision.
            # Its written text remains available in the run provenance.
            return None, None
        unit = plan.cost_unit
        if unit is None:
            assert plan.factor is not None
            unit = 1 / Fraction(plan.factor)
        try:
            return float(Fraction(raw) * unit), None
        except OverflowError:
            return None, None
    if plan.cost_unit is None:
        assert plan.factor is not None
        try:
            display = float(raw) / plan.factor
        except OverflowError:
            return None, None
        return (display if math.isfinite(display) else None), None
    report = Fraction(raw)
    value = report * plan.cost_unit
    try:
        display = float(value)
    except OverflowError:
        display = None
    return display, value if plan.exact and report.denominator == 1 else None

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
def 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.
    """
    largest_arc = _max_arc(arc_costs)
    star = star_bound(arc_costs, depot)
    for factor in candidates:
        factor = float(factor)
        if not _fits(limits, arc=largest_arc * factor, total=star * factor, time=None):
            continue
        exact = factor == 1.0
        return ScalePlan(
            mode="explicit",
            factor=factor,
            exact=exact,
            rounding="float",
            search_model="floating-matrix",
            max_scaled_arc=largest_arc * factor,
            star_bound_scaled=star * factor,
            max_arc_error=0.0 if exact else 1e-15 * largest_arc,
        )
    raise ScaleError(
        f"no matrix scale among {list(candidates)} fits the solver's limits "
        f"(largest arc {largest_arc:g}, star bound {star:g})"
    )

format_float_matrix

format_float_matrix(
    arc_costs: Sequence[Sequence[float]], plan: ScalePlan
) -> Iterable[str]

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
def format_float_matrix(arc_costs: Sequence[Sequence[float]], plan: ScalePlan) -> Iterable[str]:
    """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)."""
    from decimal import Decimal

    factor = float(plan.factor)
    if factor == 1.0:
        for row in arc_costs:
            yield " ".join(_plain_number(value) for value in row)
        return
    shift = round(math.log10(factor))
    if not math.isclose(10.0**shift, factor):
        for row in arc_costs:
            yield " ".join(repr(float(value) * factor) for value in row)
        return
    for row in arc_costs:
        yield " ".join(format(Decimal(_plain_number(value)).scaleb(shift), "f") for value in row)

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.

Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
def 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."""
    xs = [float(point[0]) for point in coordinates]
    ys = [float(point[1]) for point in coordinates]
    diagonal = math.hypot(max(xs) - min(xs), max(ys) - min(ys))
    dx, dy = float(coordinates[depot][0]), float(coordinates[depot][1])
    star = sum(2.0 * math.hypot(x - dx, y - dy) for index, (x, y) in enumerate(zip(xs, ys)) if index != depot)
    for factor in candidates:
        factor = float(factor)
        arc = diagonal * factor
        arc_limit = None if limits.max_arc_cost is None else limits.max_arc_cost * margin
        if arc_limit is not None and arc > arc_limit:
            continue
        if limits.min_arc_cost is not None and arc < limits.min_arc_cost:
            continue
        if limits.max_total_cost is not None and star * factor > limits.max_total_cost:
            continue
        decimals = max(0, 6 - int(round(math.log10(factor))))
        published = 0.0 if published_decimals is None else 0.5 * 10.0 ** (-published_decimals)
        error = published + (0.5 / factor if distance_model == "nint" else 0.0)
        return ScalePlan(
            mode="coordinates",
            factor=factor,
            exact=False,
            rounding="solver",
            search_model="coordinate-distances",
            max_scaled_arc=arc,
            star_bound_scaled=star * factor,
            max_arc_error=error,
            coordinate_decimals=decimals,
            unit=_UNITS.get(factor),
        )
    raise ScaleError(
        f"no coordinate scale among {list(candidates)} fits the solver's limits "
        f"(bounding-box diagonal {diagonal:g}, star bound {star:g})"
    )

scale_coordinates

scale_coordinates(
    coordinates: Sequence[Sequence[float]], plan: ScalePlan
) -> list[tuple[float, float]]
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/scaling.py
def scale_coordinates(coordinates: Sequence[Sequence[float]], plan: ScalePlan) -> list[tuple[float, float]]:
    return [(float(x) * plan.factor, float(y) * plan.factor) for x, y in coordinates]