update docs and perf metrics

This commit is contained in:
Jan Petykiewicz 2026-03-31 17:26:00 -07:00
commit 725980e694

View file

@ -1,5 +1,6 @@
from __future__ import annotations
from dataclasses import dataclass
from time import perf_counter
from collections.abc import Callable
@ -18,6 +19,7 @@ from inire import (
)
from inire.geometry.collision import RoutingWorld
from inire.geometry.primitives import Port
from inire.results import RouteMetrics
from inire.router._astar_types import AStarContext, AStarMetrics
from inire.router._router import PathFinder
from inire.router.cost import CostEvaluator
@ -31,6 +33,25 @@ _OBJECTIVE_FIELDS = set(ObjectiveWeights.__dataclass_fields__)
ScenarioOutcome = tuple[float, int, int, int]
ScenarioRun = Callable[[], ScenarioOutcome]
ScenarioSnapshotRun = Callable[[], "ScenarioSnapshot"]
@dataclass(frozen=True, slots=True)
class ScenarioSnapshot:
name: str
duration_s: float
total_results: int
valid_results: int
reached_targets: int
metrics: RouteMetrics
def as_outcome(self) -> ScenarioOutcome:
return (
self.duration_s,
self.total_results,
self.valid_results,
self.reached_targets,
)
def _summarize(results: dict[str, RoutingResult], duration_s: float) -> ScenarioOutcome:
@ -42,6 +63,32 @@ def _summarize(results: dict[str, RoutingResult], duration_s: float) -> Scenario
)
def _make_snapshot(
name: str,
results: dict[str, RoutingResult],
duration_s: float,
metrics: RouteMetrics,
) -> ScenarioSnapshot:
return ScenarioSnapshot(
name=name,
duration_s=duration_s,
total_results=len(results),
valid_results=sum(1 for result in results.values() if result.is_valid),
reached_targets=sum(1 for result in results.values() if result.reached_target),
metrics=metrics,
)
def _sum_metrics(metrics_list: tuple[RouteMetrics, ...]) -> RouteMetrics:
metric_names = RouteMetrics.__dataclass_fields__
return RouteMetrics(
**{
name: sum(getattr(metrics, name) for metrics in metrics_list)
for name in metric_names
}
)
def _build_evaluator(
bounds: tuple[float, float, float, float],
*,
@ -93,13 +140,15 @@ def _build_pathfinder(
metrics: AStarMetrics | None = None,
**request_kwargs: object,
) -> PathFinder:
resolved_metrics = AStarMetrics() if metrics is None else metrics
return PathFinder(
AStarContext(
evaluator,
RoutingProblem(bounds=bounds, nets=nets),
_build_options(**request_kwargs),
metrics=resolved_metrics,
),
metrics=metrics,
metrics=resolved_metrics,
)
@ -133,7 +182,7 @@ def _build_routing_stack(
return engine, evaluator, metrics, pathfinder
def run_example_01() -> ScenarioOutcome:
def snapshot_example_01() -> ScenarioSnapshot:
netlist = {"net1": (Port(10, 50, 0), Port(90, 50, 0))}
widths = {"net1": 2.0}
_, _, _, pathfinder = _build_routing_stack(
@ -145,10 +194,14 @@ def run_example_01() -> ScenarioOutcome:
t0 = perf_counter()
results = pathfinder.route_all()
t1 = perf_counter()
return _summarize(results, t1 - t0)
return _make_snapshot("example_01_simple_route", results, t1 - t0, pathfinder.metrics.snapshot())
def run_example_02() -> ScenarioOutcome:
def run_example_01() -> ScenarioOutcome:
return snapshot_example_01().as_outcome()
def snapshot_example_02() -> ScenarioSnapshot:
netlist = {
"horizontal": (Port(10, 50, 0), Port(90, 50, 0)),
"vertical_up": (Port(45, 10, 90), Port(45, 90, 90)),
@ -173,10 +226,14 @@ def run_example_02() -> ScenarioOutcome:
t0 = perf_counter()
results = pathfinder.route_all()
t1 = perf_counter()
return _summarize(results, t1 - t0)
return _make_snapshot("example_02_congestion_resolution", results, t1 - t0, pathfinder.metrics.snapshot())
def run_example_03() -> ScenarioOutcome:
def run_example_02() -> ScenarioOutcome:
return snapshot_example_02().as_outcome()
def snapshot_example_03() -> ScenarioSnapshot:
netlist_a = {"netA": (Port(10, 0, 0), Port(90, 0, 0))}
widths_a = {"netA": 2.0}
engine, evaluator, _, pathfinder = _build_routing_stack(
@ -187,19 +244,26 @@ def run_example_03() -> ScenarioOutcome:
)
t0 = perf_counter()
results_a = pathfinder.route_all()
metrics_a = pathfinder.metrics.snapshot()
for polygon in results_a["netA"].locked_geometry:
engine.add_static_obstacle(polygon)
results_b = _build_pathfinder(
pathfinder_b = _build_pathfinder(
evaluator,
bounds=(0, -50, 100, 50),
nets=_net_specs({"netB": (Port(50, -20, 90), Port(50, 20, 90))}, {"netB": 2.0}),
bend_radii=[10.0],
).route_all()
)
results_b = pathfinder_b.route_all()
t1 = perf_counter()
return _summarize({**results_a, **results_b}, t1 - t0)
combined_metrics = _sum_metrics((metrics_a, pathfinder_b.metrics.snapshot()))
return _make_snapshot("example_03_locked_paths", {**results_a, **results_b}, t1 - t0, combined_metrics)
def run_example_04() -> ScenarioOutcome:
def run_example_03() -> ScenarioOutcome:
return snapshot_example_03().as_outcome()
def snapshot_example_04() -> ScenarioSnapshot:
netlist = {
"sbend_only": (Port(10, 50, 0), Port(60, 55, 0)),
"multi_radii": (Port(10, 10, 0), Port(90, 90, 0)),
@ -223,10 +287,14 @@ def run_example_04() -> ScenarioOutcome:
t0 = perf_counter()
results = pathfinder.route_all()
t1 = perf_counter()
return _summarize(results, t1 - t0)
return _make_snapshot("example_04_sbends_and_radii", results, t1 - t0, pathfinder.metrics.snapshot())
def run_example_05() -> ScenarioOutcome:
def run_example_04() -> ScenarioOutcome:
return snapshot_example_04().as_outcome()
def snapshot_example_05() -> ScenarioSnapshot:
netlist = {
"u_turn": (Port(50, 50, 0), Port(50, 70, 180)),
"loop": (Port(100, 100, 90), Port(100, 80, 270)),
@ -243,10 +311,14 @@ def run_example_05() -> ScenarioOutcome:
t0 = perf_counter()
results = pathfinder.route_all()
t1 = perf_counter()
return _summarize(results, t1 - t0)
return _make_snapshot("example_05_orientation_stress", results, t1 - t0, pathfinder.metrics.snapshot())
def run_example_06() -> ScenarioOutcome:
def run_example_05() -> ScenarioOutcome:
return snapshot_example_05().as_outcome()
def snapshot_example_06() -> ScenarioSnapshot:
bounds = (-20, -20, 170, 170)
obstacles = [
box(40, 110, 60, 130),
@ -282,6 +354,7 @@ def run_example_06() -> ScenarioOutcome:
t0 = perf_counter()
combined_results: dict[str, RoutingResult] = {}
route_metrics: list[RouteMetrics] = []
for evaluator, netlist, net_widths, request_kwargs in scenarios:
pathfinder = _build_pathfinder(
evaluator,
@ -290,11 +363,21 @@ def run_example_06() -> ScenarioOutcome:
**request_kwargs,
)
combined_results.update(pathfinder.route_all())
route_metrics.append(pathfinder.metrics.snapshot())
t1 = perf_counter()
return _summarize(combined_results, t1 - t0)
return _make_snapshot(
"example_06_bend_collision_models",
combined_results,
t1 - t0,
_sum_metrics(tuple(route_metrics)),
)
def run_example_07() -> ScenarioOutcome:
def run_example_06() -> ScenarioOutcome:
return snapshot_example_06().as_outcome()
def snapshot_example_07() -> ScenarioSnapshot:
bounds = (0, 0, 1000, 1000)
obstacles = [
box(450, 0, 550, 400),
@ -349,10 +432,14 @@ def run_example_07() -> ScenarioOutcome:
t0 = perf_counter()
results = pathfinder.route_all(iteration_callback=iteration_callback)
t1 = perf_counter()
return _summarize(results, t1 - t0)
return _make_snapshot("example_07_large_scale_routing", results, t1 - t0, pathfinder.metrics.snapshot())
def run_example_08() -> ScenarioOutcome:
def run_example_07() -> ScenarioOutcome:
return snapshot_example_07().as_outcome()
def snapshot_example_08() -> ScenarioSnapshot:
bounds = (0, 0, 150, 150)
netlist = {"standard_arc": (Port(20, 20, 0), Port(100, 100, 90))}
widths = {"standard_arc": 2.0}
@ -360,7 +447,7 @@ def run_example_08() -> ScenarioOutcome:
custom_proxy = box(0, -11, 11, 0)
t0 = perf_counter()
results_std = _build_pathfinder(
pathfinder_std = _build_pathfinder(
_build_evaluator(bounds),
bounds=bounds,
nets=_net_specs(netlist, widths),
@ -369,8 +456,9 @@ def run_example_08() -> ScenarioOutcome:
max_iterations=1,
use_tiered_strategy=False,
metrics=AStarMetrics(),
).route_all()
results_custom = _build_pathfinder(
)
results_std = pathfinder_std.route_all()
pathfinder_custom = _build_pathfinder(
_build_evaluator(bounds),
bounds=bounds,
nets=_net_specs({"custom_geometry_and_proxy": netlist["standard_arc"]}, {"custom_geometry_and_proxy": 2.0}),
@ -381,12 +469,23 @@ def run_example_08() -> ScenarioOutcome:
max_iterations=1,
use_tiered_strategy=False,
metrics=AStarMetrics(),
).route_all()
)
results_custom = pathfinder_custom.route_all()
t1 = perf_counter()
return _summarize({**results_std, **results_custom}, t1 - t0)
combined_metrics = _sum_metrics((pathfinder_std.metrics.snapshot(), pathfinder_custom.metrics.snapshot()))
return _make_snapshot(
"example_08_custom_bend_geometry",
{**results_std, **results_custom},
t1 - t0,
combined_metrics,
)
def run_example_09() -> ScenarioOutcome:
def run_example_08() -> ScenarioOutcome:
return snapshot_example_08().as_outcome()
def snapshot_example_09() -> ScenarioSnapshot:
obstacles = [
box(35, 35, 45, 65),
box(55, 35, 65, 65),
@ -404,7 +503,11 @@ def run_example_09() -> ScenarioOutcome:
t0 = perf_counter()
results = pathfinder.route_all()
t1 = perf_counter()
return _summarize(results, t1 - t0)
return _make_snapshot("example_09_unroutable_best_effort", results, t1 - t0, pathfinder.metrics.snapshot())
def run_example_09() -> ScenarioOutcome:
return snapshot_example_09().as_outcome()
SCENARIOS: tuple[tuple[str, ScenarioRun], ...] = (
@ -418,3 +521,19 @@ SCENARIOS: tuple[tuple[str, ScenarioRun], ...] = (
("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),
("example_03_locked_paths", snapshot_example_03),
("example_04_sbends_and_radii", snapshot_example_04),
("example_05_orientation_stress", snapshot_example_05),
("example_06_bend_collision_models", snapshot_example_06),
("example_07_large_scale_routing", snapshot_example_07),
("example_08_custom_bend_geometry", snapshot_example_08),
("example_09_unroutable_best_effort", snapshot_example_09),
)
def capture_all_scenario_snapshots() -> tuple[ScenarioSnapshot, ...]:
return tuple(run() for _, run in SCENARIO_SNAPSHOTS)