Skip to content

mamut_routing_tools.generation.writers

Instance artifact writers and parsers (CVRPLIB .vrp, workbench _meta.json, _manifest.json, and the lib-contract .vrp.json payloads).

instance_path_plan

instance_path_plan(
    city: str,
    method: str,
    n_customers: int,
    route_count: int,
    output_root: str | Path,
    name_suffix: str = "",
) -> tuple[Path, str]

Folder and base name of an instance.

name_suffix disambiguates instances that would otherwise share a name -- bulk rows differing only by seed, for example. It is empty in the common case, so the established <city>_<abbr>-n<N>-k<K> naming is unchanged.

Source code in MAMUT-routing-tools/src/mamut_routing_tools/generation/writers.py
def instance_path_plan(
    city: str,
    method: str,
    n_customers: int,
    route_count: int,
    output_root: str | Path,
    name_suffix: str = "",
) -> tuple[Path, str]:
    """Folder and base name of an instance.

    ``name_suffix`` disambiguates instances that would otherwise share a name --
    bulk rows differing only by seed, for example. It is empty in the common
    case, so the established ``<city>_<abbr>-n<N>-k<K>`` naming is unchanged.
    """
    city_slug = slugify(city)
    n_nodes = n_customers + 1
    folder = Path(output_root) / "osm" / city_slug / f"n{n_nodes}"
    base = f"{city_slug}_{method_abbrev(method)}-n{n_nodes}-k{route_count}{name_suffix}"
    return folder, base

cvrplib_text

cvrplib_text(
    name: str,
    comment: str,
    coords: list[tuple[float, float]],
    demands: list[int],
    matrix: list[list[int]],
    capacity: int,
) -> str

The CVRPLIB .vrp text :func:write_cvrplib writes.

Source code in MAMUT-routing-tools/src/mamut_routing_tools/generation/writers.py
def cvrplib_text(
    name: str,
    comment: str,
    coords: list[tuple[float, float]],
    demands: list[int],
    matrix: list[list[int]],
    capacity: int,
) -> str:
    """The CVRPLIB ``.vrp`` text :func:`write_cvrplib` writes."""
    lines = [
        f"NAME : {name}",
        "TYPE : CVRP",
        f"COMMENT : {comment}",
        f"DIMENSION : {len(demands)}",
        f"CAPACITY : {capacity}",
        "EDGE_WEIGHT_TYPE : EXPLICIT",
        "EDGE_WEIGHT_FORMAT : FULL_MATRIX",
        "EDGE_WEIGHT_SECTION",
    ]
    lines.extend(" ".join(str(value) for value in row) for row in matrix)
    lines.append("NODE_COORD_SECTION")
    lines.extend(f"{i + 1} {x} {y}" for i, (x, y) in enumerate(coords))
    lines.append("DEMAND_SECTION")
    lines.extend(f"{i + 1} {demand}" for i, demand in enumerate(demands))
    lines.append("DEPOT_SECTION\n1\n-1\nEOF")
    return "\n".join(lines) + "\n"