Share worker setup, residual preparation, and scenario capture

This commit is contained in:
Jan Petykiewicz 2026-11-27 00:38:00 -08:00
commit 0c059cc7d6

View file

@ -22,11 +22,13 @@ from inire import (
)
from inire.geometry.primitives import Port
from inire.results import RouteMetrics, _sum_route_metrics
from inire.router._stack import build_routing_stack
from inire.router._stack import build_routing_stack, collect_worker_result
from inire.tests.routing_corpus import SNAPSHOT_CASES, profile_options
from inire.tests.terminal_bench_scenario import terminal_bench_options, terminal_bench_problem
if TYPE_CHECKING:
from collections.abc import Iterable
from inire.router._astar_types import AStarMetrics
from inire.router._router import PathFinder
from inire.router.cost import CostEvaluator
@ -56,15 +58,6 @@ class ScenarioSnapshot:
)
def _summarize(results: dict[str, RoutingResult], duration_s: float) -> ScenarioOutcome:
return (
duration_s,
len(results),
sum(1 for result in results.values() if result.is_valid),
sum(1 for result in results.values() if result.reached_target),
)
def _make_snapshot(
name: str,
results: dict[str, RoutingResult],
@ -81,21 +74,6 @@ def _make_snapshot(
)
def _make_run_result(
results: dict[str, RoutingResult],
pathfinder: PathFinder,
) -> RoutingRunResult:
return RoutingRunResult(
results_by_net=results,
metrics=pathfinder.metrics.snapshot(),
expanded_nodes=tuple(pathfinder.accumulated_expanded_nodes),
conflict_trace=tuple(pathfinder.conflict_trace),
frontier_trace=tuple(pathfinder.frontier_trace),
pre_pair_frontier_trace=pathfinder.pre_pair_frontier_trace,
iteration_trace=tuple(pathfinder.iteration_trace),
)
def _snapshot_worker(name: str, pathfinder: PathFinder) -> ScenarioSnapshot:
start = perf_counter()
results = pathfinder.route_all()
@ -111,10 +89,6 @@ def snapshot_example_01() -> ScenarioSnapshot:
return _snapshot_worker("example_01_simple_route", build_routing_stack(problem, options).finder)
def run_example_01() -> ScenarioOutcome:
return snapshot_example_01().as_outcome()
def snapshot_example_02() -> ScenarioSnapshot:
problem = RoutingProblem(
bounds=(0, 0, 100, 100),
@ -132,10 +106,6 @@ def snapshot_example_02() -> ScenarioSnapshot:
return _snapshot_worker("example_02_congestion_resolution", build_routing_stack(problem, options).finder)
def run_example_02() -> ScenarioOutcome:
return snapshot_example_02().as_outcome()
def snapshot_example_03() -> ScenarioSnapshot:
problem_a = RoutingProblem(
bounds=(0, -50, 100, 50),
@ -160,10 +130,6 @@ def snapshot_example_03() -> ScenarioSnapshot:
return _make_snapshot("example_03_locked_paths", {**results_a, **results_b}, t1 - t0, combined_metrics)
def run_example_03() -> ScenarioOutcome:
return snapshot_example_03().as_outcome()
def snapshot_example_04() -> ScenarioSnapshot:
problem = RoutingProblem(
bounds=(0, 0, 100, 100),
@ -179,10 +145,6 @@ def snapshot_example_04() -> ScenarioSnapshot:
return _snapshot_worker("example_04_sbends_and_radii", build_routing_stack(problem, options).finder)
def run_example_04() -> ScenarioOutcome:
return snapshot_example_04().as_outcome()
def _build_example_05_worker(diagnostics: DiagnosticsOptions) -> PathFinder:
problem = RoutingProblem(
bounds=(0, 0, 200, 200),
@ -204,14 +166,10 @@ def snapshot_example_05() -> ScenarioSnapshot:
return _snapshot_worker("example_05_orientation_stress", _build_example_05_worker(DiagnosticsOptions()))
def run_example_05() -> ScenarioOutcome:
return snapshot_example_05().as_outcome()
def trace_example_05() -> RoutingRunResult:
pathfinder = _build_example_05_worker(DiagnosticsOptions(capture_conflict_trace=True, capture_frontier_trace=True))
results = pathfinder.route_all()
return _make_run_result(results, pathfinder)
return collect_worker_result(pathfinder, results)
def snapshot_example_06() -> ScenarioSnapshot:
@ -258,10 +216,6 @@ def snapshot_example_06() -> ScenarioSnapshot:
)
def run_example_06() -> ScenarioOutcome:
return snapshot_example_06().as_outcome()
def snapshot_example_07() -> ScenarioSnapshot:
return _snapshot_example_07_variant(
"example_07_large_scale_routing",
@ -389,7 +343,7 @@ def _run_example_07_variant(
metrics.reset_per_route()
results = pathfinder.route_all(iteration_callback=iteration_callback)
return _make_run_result(results, pathfinder)
return collect_worker_result(pathfinder, results)
def _snapshot_example_07_variant(
@ -424,10 +378,6 @@ def _trace_example_07_variant(
)
def run_example_07() -> ScenarioOutcome:
return snapshot_example_07().as_outcome()
def _build_example_08_workers() -> tuple[PathFinder, PathFinder]:
net = NetSpec("standard_arc", Port(20, 20, 0), Port(100, 100, 90))
problem = RoutingProblem(bounds=(0, 0, 150, 150), nets=(net,))
@ -460,10 +410,6 @@ def snapshot_example_08() -> ScenarioSnapshot:
)
def run_example_08() -> ScenarioOutcome:
return snapshot_example_08().as_outcome()
def snapshot_example_09() -> ScenarioSnapshot:
problem = RoutingProblem(
bounds=(0, 0, 100, 100),
@ -478,10 +424,6 @@ def snapshot_example_09() -> ScenarioSnapshot:
return _snapshot_worker("example_09_unroutable_best_effort", build_routing_stack(problem, options).finder)
def run_example_09() -> ScenarioOutcome:
return snapshot_example_09().as_outcome()
def snapshot_terminal_bench_pr345() -> ScenarioSnapshot:
problem = terminal_bench_problem()
start = perf_counter()
@ -495,18 +437,6 @@ def _snapshot_routing_benchmark(case: BenchmarkCase) -> ScenarioSnapshot:
return _make_snapshot(f"routing_benchmark_{case.name.replace('-', '_')}", run.results_by_net, perf_counter() - start, run.metrics)
SCENARIOS: tuple[tuple[str, ScenarioRun], ...] = (
("example_01_simple_route", run_example_01),
("example_02_congestion_resolution", run_example_02),
("example_03_locked_paths", run_example_03),
("example_04_sbends_and_radii", run_example_04),
("example_05_orientation_stress", run_example_05),
("example_06_bend_collision_models", run_example_06),
("example_07_large_scale_routing", run_example_07),
("example_08_custom_bend_geometry", run_example_08),
("example_09_unroutable_best_effort", run_example_09),
)
SCENARIO_SNAPSHOTS: tuple[tuple[str, ScenarioSnapshotRun], ...] = (
("example_01_simple_route", snapshot_example_01),
("example_02_congestion_resolution", snapshot_example_02),
@ -519,6 +449,15 @@ SCENARIO_SNAPSHOTS: tuple[tuple[str, ScenarioSnapshotRun], ...] = (
("example_09_unroutable_best_effort", snapshot_example_09),
)
def _snapshot_outcome(capture: ScenarioSnapshotRun) -> ScenarioOutcome:
return capture().as_outcome()
SCENARIOS: tuple[tuple[str, ScenarioRun], ...] = tuple(
(name, partial(_snapshot_outcome, capture)) for name, capture in SCENARIO_SNAPSHOTS
)
PERFORMANCE_SCENARIO_SNAPSHOTS: tuple[tuple[str, ScenarioSnapshotRun], ...] = (
("example_07_large_scale_routing_no_warm_start", snapshot_example_07_no_warm_start),
("example_07_large_scale_routing_no_warm_start_seed43", snapshot_example_07_no_warm_start_seed43),
@ -540,5 +479,13 @@ TRACE_PERFORMANCE_SCENARIO_RUNS: tuple[tuple[str, TraceScenarioRun], ...] = (
)
def capture_all_scenario_snapshots() -> tuple[ScenarioSnapshot, ...]:
return tuple(run() for _, run in SCENARIO_SNAPSHOTS)
def capture_scenario_snapshots(
selected_scenarios: Iterable[str] | None = None,
*,
include_performance_only: bool = False,
) -> tuple[ScenarioSnapshot, ...]:
registry = SCENARIO_SNAPSHOTS
if include_performance_only:
registry += PERFORMANCE_SCENARIO_SNAPSHOTS
allowed = None if selected_scenarios is None else set(selected_scenarios)
return tuple(capture() for name, capture in registry if allowed is None or name in allowed)