Skip to content

mamut_routing_tools.generation.vrptw

VRPTW derivation from generated CVRP instances (port of the workbench route_centered / reachable_interval time-window synthesis).

stable_seed

stable_seed(*parts: Any) -> int

Deterministic cross-run seed from arbitrary parts (replaces Julia's hash(tuple); values differ from Julia by design, determinism within Python is the contract).

Source code in MAMUT-routing-tools/src/mamut_routing_tools/generation/vrptw.py
def stable_seed(*parts: Any) -> int:
    """Deterministic cross-run seed from arbitrary parts (replaces Julia's
    ``hash(tuple)``; values differ from Julia by design, determinism within
    Python is the contract)."""
    text = "".join(repr(part) for part in parts)
    return int.from_bytes(hashlib.sha256(text.encode()).digest()[:8], "big")

derive_vrptw_from_cvrp

derive_vrptw_from_cvrp(folder: str | Path, base: str, *, tw_method: str = DEFAULT_TW_METHOD, horizon_start: int = HORIZON_START, horizon_end: int = HORIZON_END, place_slug: str = '', source_seed: int = 0) -> dict[str, Any]

Derive the fastest-metric VRPTW twin next to a generated CVRP base.

Reads <base>_fastest.vrp, synthesizes service times + time windows on the fastest travel times, and writes <base>_fastest.cvrptw.vrp plus a <base>_vrptw_manifest.json describing the derivation.

Source code in MAMUT-routing-tools/src/mamut_routing_tools/generation/vrptw.py
def derive_vrptw_from_cvrp(
    folder: str | Path,
    base: str,
    *,
    tw_method: str = DEFAULT_TW_METHOD,
    horizon_start: int = HORIZON_START,
    horizon_end: int = HORIZON_END,
    place_slug: str = "",
    source_seed: int = 0,
) -> dict[str, Any]:
    """Derive the fastest-metric VRPTW twin next to a generated CVRP base.

    Reads ``<base>_fastest.vrp``, synthesizes service times + time windows on
    the fastest travel times, and writes ``<base>_fastest.cvrptw.vrp`` plus a
    ``<base>_vrptw_manifest.json`` describing the derivation.
    """
    folder = Path(folder)
    fastest_path = folder / f"{base}_fastest.vrp"
    if not fastest_path.is_file():
        raise FileNotFoundError(f"CVRP fastest .vrp not found at {fastest_path}")
    parsed = parse_cvrp_vrp(fastest_path)
    depot_index = parsed.depot_node_index - 1

    seed_parts = (base, place_slug, source_seed, tw_method, horizon_start, horizon_end, "vrptw_workbench_v1")
    service_times, time_windows, stochastic_params = generate_vrptw_fields(
        seed_parts, parsed.arc_costs, horizon_start, horizon_end, tw_method, depot=depot_index
    )

    vrptw_name = f"{base}_fastest_vrptw"
    vrptw_filename = f"{base}_fastest.cvrptw.vrp"
    write_cvrptw_vrp(
        folder / vrptw_filename,
        parsed,
        vrptw_name,
        f"VRPTW derived from {base}_fastest ({tw_method})",
        service_times,
        time_windows,
    )
    manifest = {
        "generated_at": datetime.now().isoformat(),
        "base_name": base,
        "source_file": fastest_path.name,
        "vrptw_file": vrptw_filename,
        "derivation": stochastic_params,
    }
    write_json(folder / f"{base}_vrptw_manifest.json", manifest)
    return {
        "ok": True,
        "vrptw_file": vrptw_filename,
        "manifest": f"{base}_vrptw_manifest.json",
        "derivation": stochastic_params,
    }