Share routing preparation and report benchmark worker options

This commit is contained in:
Jan Petykiewicz 2026-12-01 22:58:43 -08:00
commit 8c8cbe569b

View file

@ -16,9 +16,10 @@ from typing import TYPE_CHECKING
import shapely
from shapely.geometry import box, mapping
from inire import RoutingOptions, RoutingStage, route
from inire import RoutingOptions, RoutingStage, _dispatch_route, _route, route
from inire.router._routing_policy import LockPlan, _best_trial, _residual_problem
from inire.router._routing_policy import _trial as _summarize_trial
from inire.router._stack import route_once
from inire.tests.direct_routing_policies import DIRECT_POLICIES
from inire.tests.direct_routing_policies import direct_trials as _direct_trials
from inire.tests.routing_corpus import (
@ -35,7 +36,7 @@ from inire.tests.routing_corpus import (
if TYPE_CHECKING:
from inire import CapacityLimit, RoutingProblem, RoutingTiming, TopologyReport
from inire.router._routing_policy import Trial
from inire.router._routing_policy import RouteCall, Trial
from inire.tests.routing_benchmark_case import BenchmarkCase
@ -56,6 +57,7 @@ class RunSample:
route_iterations: int
topology: TopologyReport | None
stages: tuple[RoutingStage, ...]
attempt_options: tuple[RoutingOptions, ...]
bundle_rejection: str | None = None
bundle_duration_s: float | None = None
selected_stage: int = 0
@ -73,15 +75,17 @@ _trial = partial(_summarize_trial, score=benchmark_weighted_length)
def _mode_options(options: RoutingOptions, strategy: str, profile: str = "matched-budget") -> RoutingOptions:
if strategy in AUTOMATIC_POLICIES:
return automatic_options(options, profile, strategy)
return replace(options, topology=replace(options.topology, mode="off" if strategy in POLICIES else strategy))
if strategy in POLICIES:
return options
return replace(options, topology=replace(options.topology, mode=strategy))
def _order_trials(problem: RoutingProblem, options: RoutingOptions, strategy: str) -> list[Trial]:
def _order_trials(problem: RoutingProblem, options: RoutingOptions, strategy: str, *, run_route: RouteCall = route) -> list[Trial]:
limit = options.search.node_limit
first_limit = max(1, limit // 2)
first_order = options.congestion.net_order
first_options = replace(options, search=replace(options.search, node_limit=first_limit), topology=replace(options.topology, mode="seed"))
run = route(problem, options=first_options)
run = run_route(problem, options=first_options)
first = _trial(run, RoutingStage("seed", (), run.metrics.nodes_expanded, first_limit, net_order=first_order))
if len(first.valid_costs) == len(problem.nets) or limit == 1:
return [first]
@ -102,7 +106,7 @@ def _order_trials(problem: RoutingProblem, options: RoutingOptions, strategy: st
else:
second_problem = problem
run = route(second_problem, options=second_options)
run = run_route(second_problem, options=second_options)
if strategy == "seed-order-locked":
run = replace(run, results_by_net={**incumbents, **run.results_by_net})
stage = RoutingStage("seed", (), run.metrics.nodes_expanded, second_limit, net_order=second_order, incumbent_net_ids=tuple(incumbents))
@ -111,11 +115,14 @@ def _order_trials(problem: RoutingProblem, options: RoutingOptions, strategy: st
def _measure(case: BenchmarkCase, options: RoutingOptions, strategy: str) -> RunSample:
board = box(*case.problem.bounds)
attempt_options: list[RoutingOptions] = []
run_route = partial(_route, run_router=partial(_dispatch_route, attempt_callback=attempt_options.append))
run_worker = partial(route_once, attempt_callback=attempt_options.append)
started = perf_counter()
bundle_rejection = None
bundle_duration = None
if strategy in AUTOMATIC_POLICIES:
run = route(case.problem, options=options)
run = run_route(case.problem, options=options)
report = run.automatic
assert report is not None
selected = report.stages[report.selected_stage]
@ -131,6 +138,7 @@ def _measure(case: BenchmarkCase, options: RoutingOptions, strategy: str) -> Run
route_iterations=run.metrics.route_iterations,
topology=run.topology,
stages=report.stages,
attempt_options=tuple(attempt_options),
selected_stage=report.selected_stage,
capacity_limit=report.capacity_limit,
planner_rejections=report.planner_rejections,
@ -147,23 +155,23 @@ def _measure(case: BenchmarkCase, options: RoutingOptions, strategy: str) -> Run
refinement=replace(options.refinement, enabled=strategy == "bundle-first-refine"),
)
try:
run = route(case.problem, options=bundle_options)
run = run_route(case.problem, options=bundle_options)
except ValueError as exc:
bundle_rejection = str(exc)
bundle_duration = perf_counter() - started
trials = _direct_trials(case.fixed_headings_problem(), board, options, "direct-probe-near-port")
trials = _direct_trials(case.fixed_headings_problem(), board, options, "direct-probe-near-port", run_route=run_worker)
else:
bundle_duration = perf_counter() - started
stage = RoutingStage("bundle", (), run.metrics.nodes_expanded, options.search.node_limit, kind="bundle")
trials = [_trial(run, stage)]
elif strategy in DIRECT_POLICIES:
problem = case.fixed_headings_problem()
trials = _direct_trials(problem, board, options, strategy)
trials = _direct_trials(problem, board, options, strategy, run_route=run_worker)
elif strategy in ORDER_POLICIES:
problem = case.fixed_headings_problem()
trials = _order_trials(problem, options, strategy)
trials = _order_trials(problem, options, strategy, run_route=run_route)
else:
run = route(case.fixed_headings_problem(), options=options)
run = run_route(case.fixed_headings_problem(), options=options)
trials = [_trial(run, RoutingStage(strategy, (), run.metrics.nodes_expanded, options.search.node_limit))]
best = _best_trial(trials)
duration = perf_counter() - started
@ -179,6 +187,7 @@ def _measure(case: BenchmarkCase, options: RoutingOptions, strategy: str) -> Run
route_iterations=sum(trial.run.metrics.route_iterations for trial in trials),
topology=run.topology,
stages=tuple(trial.stage for trial in trials),
attempt_options=tuple(attempt_options),
bundle_rejection=bundle_rejection,
bundle_duration_s=bundle_duration,
selected_stage=next(i for i, trial in enumerate(trials) if trial is best),
@ -371,11 +380,12 @@ def main() -> None:
samples[mode].append(_measure(case, mode_options[mode], mode))
for mode in modes:
_print_summary(mode, samples[mode])
requested_mode = mode_options[mode].topology.mode if mode not in POLICIES or mode in AUTOMATIC_POLICIES else None
records.append(
{
"case": case.name,
"strategy": mode,
"requested_mode": mode_options[mode].topology.mode,
"requested_mode": requested_mode,
"grid_nm": case.grid_nm,
"reference_net_ids": [net_id for net_id, _ in case.reference_paths],
"options": asdict(mode_options[mode]),