Skip to content

mamut_routing_tools.generation.td

Standalone per-instance TDVRP / TDVRPTW derivation.

The per-instance counterpart of the family builder's build_td stage (mamut_routing_tools.family.family). Where build_td publishes the whole Poryos2026 family into a marker-rooted collection tree, this module derives the time-dependent twins of a single already-generated instance in place, next to its generate single / derive-vrptw artifacts.

Given a folder holding

  • <base>_meta.json (from generate single),
  • <base>_fastest.cvrptw.vrp (from generate derive-vrptw: the PROVIDED time windows + service times to reuse), and
  • <base>_vrptw_manifest.json (its feasible anchor routes),

:func:derive_td_from_vrptw rebuilds the TD bridge for the instance's city, assembles the trimmed road-graph sidecar and one traffic overlay per model x intensity combination, lifts the provided deadlines just enough to certify the anchor routes under every derived overlay (earliest bounds are never reduced), and writes the canonical road-graph v2 TD twins:

  • <base>-<model>-<intensity>.vrp.json (TDVRPTW: lifted windows),
  • <base>-<model>-<intensity>.tdvrp.vrp.json (TDVRP: no windows).

Both mirror build_td's twin payload (same td block, sha-pinned road + traffic sidecars and materialized atf_sha256), so each loads and self-verifies through mamut_routing_lib.td.load_td_instance(path, verify_sha256=True). A mamut-collection.json marker is dropped in the folder so the collection-root-relative sidecar refs resolve with no extra arguments (the road-graph td model always resolves its sidecars against a collection root).

Anchors (0.6.0): the capacity-, horizon- and window-feasible routes derive-vrptw persists (derivation.anchor_routes), or for older manifests routes rebuilt to respect the provided windows (a legacy single anchor_route tour is ignored: beyond about 80 customers it cannot return by the horizon, and it lifted reachable_interval deadlines by hours). Under traffic a route that can no longer return by the horizon is split at its longest returning prefix. All files are written through a :class:~mamut_routing_tools.staging.StagedTree and verified before they replace anything; failures raise :class:TDDerivationError. The twins record metadata.derived_from (the input files' sha256 and the traffic seed), so a re-run after the base was regenerated re-derives instead of keeping stale twins.

TDDerivationError

Bases: ValueError

Raised when a standalone TD derivation cannot produce certified twins.

Inconsistent inputs, and (since 0.6.0) anchor routes that cannot be certified under traffic: the audit's :class:AnchorAuditError, the lib's PWLFError / RoadGraphFormatError are wrapped so the CLI and the GUI report one error instead of a traceback.

derive_td_from_vrptw

derive_td_from_vrptw(
    folder: str | Path,
    base: str,
    *,
    model: str = "bpr",
    intensity: str = "moderate",
    all_combos: bool = False,
    seed: int = 42,
    force: bool = False,
    verify: bool = True,
) -> dict[str, Any]

Derive the TDVRP + TDVRPTW twins of a generated instance in place.

Reuses the PROVIDED VRPTW windows (lifting deadlines to time-dependent feasibility, never regenerating them) and produces canonical road-graph v2 twins that self-verify through load_td_instance(path, verify_sha256=True). Existing twins are kept only when they were derived from the current inputs with the same traffic seed.

Source code in MAMUT-routing-tools/src/mamut_routing_tools/generation/td.py
def derive_td_from_vrptw(
    folder: str | Path,
    base: str,
    *,
    model: str = "bpr",
    intensity: str = "moderate",
    all_combos: bool = False,
    seed: int = 42,
    force: bool = False,
    verify: bool = True,
) -> dict[str, Any]:
    """Derive the TDVRP + TDVRPTW twins of a generated instance in place.

    Reuses the PROVIDED VRPTW windows (lifting deadlines to time-dependent
    feasibility, never regenerating them) and produces canonical road-graph
    v2 twins that self-verify through ``load_td_instance(path,
    verify_sha256=True)``. Existing twins are kept only when they were
    derived from the current inputs with the same traffic seed.
    """
    try:
        return _derive_td_from_vrptw(
            folder,
            base,
            model=model,
            intensity=intensity,
            all_combos=all_combos,
            seed=seed,
            force=force,
            verify=verify,
        )
    except (AnchorAuditError, PWLFError, RoadGraphFormatError) as error:
        raise TDDerivationError(f"{base}: the anchor routes cannot be certified under traffic: {error}") from error
    except AssertionError as error:
        raise TDDerivationError(f"{base}: {error}") from error