Skip to content

mamut_routing_tools.bks_campaign.plan

From a campaign spec to the list of runs still to do.

Every (instance, run entry, seed) becomes a task with its budget, objective and run key. Tasks whose record already exists are done (resumption) unless the instance file changed since that run (stale: they run again), tasks a solver cannot take are listed with the reason instead of failing at run time, and unavailable solvers are reported once. A key listed twice (repeated seeds, two identical run entries) is planned once. Longest budgets go first so the tail is short.

warm_start = "best" runs start from the campaign's best result so far, so they are planned after every other run and the runner starts each one only once the other runs of its instance and objective have finished. Their start routes are looked up at that moment (best_known_routes), not here: a plan made before the campaign ran would otherwise freeze the stored BKS as their start.

Task dataclass

Task(
    key: str,
    campaign: str,
    out: str,
    instance_id: str,
    instance_name: str,
    instance_path: str,
    num_customers: int,
    problem_type: str,
    objective: str,
    kind: str,
    solver: str,
    seed: int,
    time_limit_s: int,
    memory_gb: float,
    solvers_config: str | None = None,
    options: dict[str, Any] = dict(),
    warm_start: str = "none",
    nice: int | None = None,
)

group property

group: tuple[str, str]

What a warm start waits for: the other runs of this instance and objective.

default_memory_gb

default_memory_gb(solver: str, num_customers: int) -> float

Rough RSS per run, from the Mamut2026 campaigns (PyVRP 1.6 GB at n = 4000; AILS-II -Xmx4g).

Source code in MAMUT-routing-tools/src/mamut_routing_tools/bks_campaign/plan.py
def default_memory_gb(solver: str, num_customers: int) -> float:
    """Rough RSS per run, from the Mamut2026 campaigns (PyVRP 1.6 GB at n = 4000; AILS-II -Xmx4g)."""
    if solver == "pyvrp":
        return 0.2 + 0.4e-3 * num_customers
    if solver == "ails2":
        return 4.5
    if solver == "hgs-cvrp":
        return 0.1 + 8.0 * num_customers**2 / 1e9
    return 0.5 + 8.0 * num_customers**2 / 1e9

select_instances

select_instances(spec: CampaignSpec) -> list[Any]

The instances the spec selects (DiscoveredBenchmarkInstance), static and TD alike.

Source code in MAMUT-routing-tools/src/mamut_routing_tools/bks_campaign/plan.py
def select_instances(spec: CampaignSpec) -> list[Any]:
    """The instances the spec selects (``DiscoveredBenchmarkInstance``), static and TD alike."""
    from mamut_routing_lib.artifacts import discover_benchmark_instances

    select = spec.select
    found = discover_benchmark_instances(
        select.benchmarks_dir,
        problem_types=select.problem_types or None,
        benchmark_names=select.families or None,
        metric_variants=select.metrics or None,
        instance_ids=select.instance_ids or None,
    )
    pattern = re.compile(select.name_regex) if select.name_regex else None
    chosen = []
    for item in found:
        n = item.num_customers
        if n is None:
            n = int(item.load().num_customers)
        if select.min_n is not None and n < select.min_n:
            continue
        if select.max_n is not None and n > select.max_n:
            continue
        if pattern is not None and not (pattern.search(item.instance_name) or pattern.search(item.instance_id)):
            continue
        chosen.append(item)
    return chosen

archived_cost

archived_cost(
    record: dict[str, Any],
) -> Fraction | int | float | None

Recorded checker cost. Older campaign records kept only its display.

Source code in MAMUT-routing-tools/src/mamut_routing_tools/bks_campaign/plan.py
def archived_cost(record: dict[str, Any]) -> Fraction | int | float | None:
    """Recorded checker cost. Older campaign records kept only its display."""
    rational = record.get("authoritative_cost")
    if rational is None:
        return record.get("validated_cost")
    return read_result_fraction(rational)

objective_rank

objective_rank(
    objective: str,
    cost: Fraction | int | float,
    num_routes: int,
) -> tuple

Sort key of a solution under objective: fleet first for HierarchicalVehicleCost.

Source code in MAMUT-routing-tools/src/mamut_routing_tools/bks_campaign/plan.py
def objective_rank(objective: str, cost: Fraction | int | float, num_routes: int) -> tuple:
    """Sort key of a solution under ``objective``: fleet first for ``HierarchicalVehicleCost``."""
    if objective == "HierarchicalVehicleCost":
        return (int(num_routes), cost)
    return (cost,)

best_known_routes

best_known_routes(
    out: Path,
    instance_path: Path,
    instance_id: str,
    objective: Any,
    instance_sha256: str | None = None,
    *,
    archive: ArchiveIndex | None = None,
) -> tuple[list[list[int]] | None, str | None]

Routes of the best valid archived run or the stored BKS (warm starts), with their origin.

Runs of an older version of the instance file are ignored; "best" follows the objective (fewer routes first under HierarchicalVehicleCost). Without archive, only the records of this instance and objective are read from out (a worker about to start); the planner passes its one-pass index (_valid_runs_by_group) instead.

Source code in MAMUT-routing-tools/src/mamut_routing_tools/bks_campaign/plan.py
def best_known_routes(
    out: Path,
    instance_path: Path,
    instance_id: str,
    objective: Any,
    instance_sha256: str | None = None,
    *,
    archive: ArchiveIndex | None = None,
) -> tuple[list[list[int]] | None, str | None]:
    """Routes of the best valid archived run or the stored BKS (warm starts), with their origin.

    Runs of an older version of the instance file are ignored; "best" follows the objective
    (fewer routes first under ``HierarchicalVehicleCost``). Without ``archive``, only the records
    of this instance and objective are read from ``out`` (a worker about to start); the planner
    passes its one-pass index (``_valid_runs_by_group``) instead.
    """
    from mamut_routing_lib.artifacts import get_bks_path_for_instance, load_bks

    if archive is None:
        records = [
            record
            for record in group_runs(out, instance_id, objective.value)
            if record.get("valid") and archived_cost(record) is not None and record.get("routes")
        ]
    else:
        records = archive.get((instance_id, objective.value), [])
    best: tuple[tuple, list[list[int]], str] | None = None
    for record in records:
        if instance_sha256 and record.get("instance_sha256") not in (None, instance_sha256):
            continue
        rank = objective_rank(objective.value, archived_cost(record), len(record["routes"]))
        if best is None or rank < best[0]:
            best = (rank, record["routes"], f"run:{record['key']}")
    bks_path = get_bks_path_for_instance(instance_path, objective)
    if bks_path.is_file():
        bks = load_bks(bks_path)
        evaluation = bks.metadata.get("standard") or bks.metadata.get("exact_evaluation") or {}
        cost = archived_cost({**evaluation, "validated_cost": bks.cost})
        if cost is not None:
            rank = objective_rank(objective.value, cost, len(bks.routes))
            if best is None or rank < best[0]:
                best = (rank, bks.routes, f"bks:{bks.metadata.get('method') or 'stored'}")
    if best is None:
        return None, None
    return [list(route) for route in best[1]], best[2]

build_plan

build_plan(
    spec: CampaignSpec,
    *,
    only: str | None = None,
    retry_failed: bool = False,
) -> CampaignPlan

Expand the spec into tasks, skipping done runs and unsupported (instance, solver) pairs.

Source code in MAMUT-routing-tools/src/mamut_routing_tools/bks_campaign/plan.py
def build_plan(spec: CampaignSpec, *, only: str | None = None, retry_failed: bool = False) -> CampaignPlan:
    """Expand the spec into tasks, skipping done runs and unsupported (instance, solver) pairs."""
    from mamut_routing_lib.artifacts import load_benchmark_instance
    from mamut_routing_lib.objectives import base_objective
    from mamut_routing_lib.solvers import get_solver, profile_instance
    from mamut_routing_lib.solvers.provenance import file_sha256

    only_pattern = re.compile(only) if only else None
    config = str(spec.campaign.solvers_config) if spec.campaign.solvers_config else None
    solvers = {}
    unavailable: dict[str, str] = {}
    for run in spec.runs:
        if run.solver in solvers or run.solver in unavailable:
            continue
        solver = get_solver(run.solver, config=config)
        availability = solver.availability()
        if not availability.available:
            unavailable[run.solver] = availability.describe()
            continue
        if run.warm_start == "best" and not solver.capabilities.warm_start:
            raise ValueError(f"[[runs]] {run.run_kind}: {run.solver} does not take warm starts")
        solvers[run.solver] = solver

    tasks: list[Task] = []
    done: list[str] = []
    retried: list[str] = []
    stale: list[str] = []
    planned: set[str] = set()
    unsupported: list[tuple[str, str, str]] = []
    archive: ArchiveIndex | None = None  # read once, on the first warm start
    for item in select_instances(spec):
        if only_pattern and not (only_pattern.search(item.instance_id) or only_pattern.search(item.instance_name)):
            continue
        instance = load_benchmark_instance(item.instance_path)
        problem = getattr(item.problem_type, "value", str(item.problem_type))
        objective = spec.select.objective.get(item.problem_type) or base_objective(instance)
        profile = profile_instance(instance, item.instance_path)
        n = int(instance.num_customers)
        current_sha = file_sha256(item.instance_path)
        cold_planned = False  # a run of this instance is still to do: a warm start can wait for it
        # Cold entries first, whatever their order in the spec: a warm start needs to know of them.
        for run in sorted(spec.runs, key=lambda entry: entry.warm_start == "best"):
            solver = solvers.get(run.solver)
            if solver is None:
                continue
            support = solver.supports(profile, objective)
            if not support.supported:
                unsupported.append((item.instance_id, run.solver, "; ".join(support.reasons)))
                continue
            budget = spec.budget.seconds(n, run.budget_factor)
            if run.warm_start == "best" and not cold_planned:
                # Nothing of this instance will run first: there must be something to start from already.
                if archive is None:
                    archive = _valid_runs_by_group(spec)
                known, _ = best_known_routes(
                    spec.out, item.instance_path, item.instance_id, objective, current_sha, archive=archive
                )
                if known is None:
                    unsupported.append(
                        (item.instance_id, run.solver, "warm_start = best but no archived run, stored BKS or planned run to start from")
                    )
                    continue
            for seed in run.seed_list():
                key = run_key(item.instance_id, objective.value, run.run_kind, seed, budget)
                if key in planned:  # the same run listed twice would share its raw directory
                    continue
                planned.add(key)
                record_path = run_path(spec.out, key)
                if record_path.is_file():
                    record = _read_record(record_path)
                    if record.get("instance_sha256") not in (None, current_sha):
                        stale.append(key)  # a run of an older instance file: run it again
                    elif retry_failed and _failed(record):
                        retried.append(key)
                    else:
                        done.append(key)
                        continue
                tasks.append(
                    Task(
                        key=key,
                        campaign=spec.campaign.name,
                        out=str(spec.out),
                        instance_id=item.instance_id,
                        instance_name=item.instance_name,
                        instance_path=str(item.instance_path),
                        num_customers=n,
                        problem_type=problem,
                        objective=objective.value,
                        kind=run.run_kind,
                        solver=run.solver,
                        seed=seed,
                        time_limit_s=budget,
                        memory_gb=run.memory_gb if run.memory_gb is not None else default_memory_gb(run.solver, n),
                        solvers_config=config,
                        options=dict(run.options),
                        warm_start=run.warm_start,
                        nice=spec.resources.nice,
                    )
                )
                cold_planned = cold_planned or run.warm_start != "best"
    # Warm starts last (they start from the other runs' results); longest budgets first within each.
    tasks.sort(key=lambda task: (task.warm_start == "best", -task.time_limit_s, task.key))
    return CampaignPlan(
        tasks=tasks, done=done, unsupported=unsupported, unavailable=unavailable, retried=retried, stale=stale
    )