491 lines
17 KiB
Python
491 lines
17 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, replace
|
|
from functools import partial
|
|
from time import perf_counter
|
|
from typing import TYPE_CHECKING
|
|
from collections.abc import Callable
|
|
|
|
from shapely.geometry import Polygon, box
|
|
|
|
from inire import (
|
|
CongestionOptions,
|
|
DiagnosticsOptions,
|
|
NetSpec,
|
|
ObjectiveWeights,
|
|
RoutingOptions,
|
|
RoutingProblem,
|
|
RoutingResult,
|
|
RoutingRunResult,
|
|
SearchOptions,
|
|
route,
|
|
)
|
|
from inire.geometry.primitives import Port
|
|
from inire.results import RouteMetrics, _sum_route_metrics
|
|
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
|
|
from inire.tests.routing_benchmark_case import BenchmarkCase
|
|
|
|
ScenarioOutcome = tuple[float, int, int, int]
|
|
ScenarioRun = Callable[[], ScenarioOutcome]
|
|
ScenarioSnapshotRun = Callable[[], "ScenarioSnapshot"]
|
|
TraceScenarioRun = Callable[[], RoutingRunResult]
|
|
|
|
|
|
@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 _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 _snapshot_worker(name: str, pathfinder: PathFinder) -> ScenarioSnapshot:
|
|
start = perf_counter()
|
|
results = pathfinder.route_all()
|
|
return _make_snapshot(name, results, perf_counter() - start, pathfinder.metrics.snapshot())
|
|
|
|
|
|
def snapshot_example_01() -> ScenarioSnapshot:
|
|
problem = RoutingProblem(
|
|
bounds=(0, 0, 100, 100),
|
|
nets=(NetSpec("net1", Port(10, 50, 0), Port(90, 50, 0)),),
|
|
)
|
|
options = RoutingOptions(search=SearchOptions(bend_radii=(10.0,)))
|
|
return _snapshot_worker("example_01_simple_route", build_routing_stack(problem, options).finder)
|
|
|
|
|
|
def snapshot_example_02() -> ScenarioSnapshot:
|
|
problem = RoutingProblem(
|
|
bounds=(0, 0, 100, 100),
|
|
nets=(
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0)),
|
|
NetSpec("vertical_up", Port(45, 10, 90), Port(45, 90, 90)),
|
|
NetSpec("vertical_down", Port(55, 90, 270), Port(55, 10, 270)),
|
|
),
|
|
)
|
|
options = RoutingOptions(
|
|
search=SearchOptions(bend_radii=(10.0,), sbend_radii=(10.0,)),
|
|
objective=ObjectiveWeights(bend_penalty=50.0, sbend_penalty=150.0),
|
|
congestion=CongestionOptions(base_penalty=1000.0),
|
|
)
|
|
return _snapshot_worker("example_02_congestion_resolution", build_routing_stack(problem, options).finder)
|
|
|
|
|
|
def snapshot_example_03() -> ScenarioSnapshot:
|
|
problem_a = RoutingProblem(
|
|
bounds=(0, -50, 100, 50),
|
|
nets=(NetSpec("netA", Port(10, 0, 0), Port(90, 0, 0)),),
|
|
)
|
|
options = RoutingOptions(
|
|
search=SearchOptions(bend_radii=(10.0,)),
|
|
objective=ObjectiveWeights(danger_weight=0.0),
|
|
)
|
|
finder_a = build_routing_stack(problem_a, options).finder
|
|
t0 = perf_counter()
|
|
results_a = finder_a.route_all()
|
|
problem_b = RoutingProblem(
|
|
bounds=problem_a.bounds,
|
|
nets=(NetSpec("netB", Port(50, -20, 90), Port(50, 20, 90)),),
|
|
static_obstacles=results_a["netA"].locked_geometry,
|
|
)
|
|
finder_b = build_routing_stack(problem_b, options).finder
|
|
results_b = finder_b.route_all()
|
|
t1 = perf_counter()
|
|
combined_metrics = _sum_route_metrics((finder_a.metrics.snapshot(), finder_b.metrics.snapshot()))
|
|
return _make_snapshot("example_03_locked_paths", {**results_a, **results_b}, t1 - t0, combined_metrics)
|
|
|
|
|
|
def snapshot_example_04() -> ScenarioSnapshot:
|
|
problem = RoutingProblem(
|
|
bounds=(0, 0, 100, 100),
|
|
nets=(
|
|
NetSpec("sbend_only", Port(10, 50, 0), Port(60, 55, 0)),
|
|
NetSpec("multi_radii", Port(10, 10, 0), Port(90, 90, 0)),
|
|
),
|
|
)
|
|
options = RoutingOptions(
|
|
search=SearchOptions(node_limit=50_000, bend_radii=(10.0, 30.0), sbend_offsets=(5.0,)),
|
|
objective=ObjectiveWeights(bend_penalty=10.0, sbend_penalty=20.0),
|
|
)
|
|
return _snapshot_worker("example_04_sbends_and_radii", build_routing_stack(problem, options).finder)
|
|
|
|
|
|
def _build_example_05_worker(diagnostics: DiagnosticsOptions) -> PathFinder:
|
|
problem = RoutingProblem(
|
|
bounds=(0, 0, 200, 200),
|
|
nets=(
|
|
NetSpec("u_turn", Port(50, 50, 0), Port(50, 70, 180)),
|
|
NetSpec("loop", Port(100, 100, 90), Port(100, 80, 270)),
|
|
NetSpec("zig_zag", Port(20, 150, 0), Port(180, 150, 0)),
|
|
),
|
|
)
|
|
options = RoutingOptions(
|
|
search=SearchOptions(bend_radii=(20.0,)),
|
|
objective=ObjectiveWeights(bend_penalty=50.0, sbend_penalty=100.0),
|
|
diagnostics=diagnostics,
|
|
)
|
|
return build_routing_stack(problem, options).finder
|
|
|
|
|
|
def snapshot_example_05() -> ScenarioSnapshot:
|
|
return _snapshot_worker("example_05_orientation_stress", _build_example_05_worker(DiagnosticsOptions()))
|
|
|
|
|
|
def trace_example_05() -> RoutingRunResult:
|
|
pathfinder = _build_example_05_worker(DiagnosticsOptions(capture_conflict_trace=True, capture_frontier_trace=True))
|
|
results = pathfinder.route_all()
|
|
return collect_worker_result(pathfinder, results)
|
|
|
|
|
|
def snapshot_example_06() -> ScenarioSnapshot:
|
|
bounds = (-20, -20, 170, 170)
|
|
obstacles = (
|
|
box(40, 110, 60, 130),
|
|
box(40, 60, 60, 80),
|
|
box(40, 10, 60, 30),
|
|
)
|
|
custom_physical = Polygon([(0, -11), (11, -11), (11, 0), (9, 0), (9, -9), (0, -9)])
|
|
search = SearchOptions(bend_radii=(10.0,))
|
|
options = RoutingOptions(
|
|
objective=ObjectiveWeights(bend_penalty=50.0, sbend_penalty=150.0),
|
|
congestion=CongestionOptions(use_tiered_strategy=False),
|
|
)
|
|
scenarios = (
|
|
(NetSpec("arc_model", Port(10, 120, 0), Port(90, 140, 90)), search),
|
|
(NetSpec("bbox_model", Port(10, 70, 0), Port(90, 90, 90)), replace(search, bend_collision_type="bbox")),
|
|
(
|
|
NetSpec("custom_geometry", Port(10, 20, 0), Port(90, 40, 90)),
|
|
replace(search, bend_physical_geometry=custom_physical, bend_proxy_geometry=custom_physical),
|
|
),
|
|
)
|
|
finders = [
|
|
build_routing_stack(
|
|
RoutingProblem(bounds=bounds, nets=(net,), static_obstacles=obstacles),
|
|
replace(options, search=scenario_search),
|
|
).finder
|
|
for net, scenario_search in scenarios
|
|
]
|
|
|
|
t0 = perf_counter()
|
|
combined_results: dict[str, RoutingResult] = {}
|
|
route_metrics: list[RouteMetrics] = []
|
|
for pathfinder in finders:
|
|
combined_results.update(pathfinder.route_all())
|
|
route_metrics.append(pathfinder.metrics.snapshot())
|
|
t1 = perf_counter()
|
|
return _make_snapshot(
|
|
"example_06_bend_collision_models",
|
|
combined_results,
|
|
t1 - t0,
|
|
_sum_route_metrics(route_metrics),
|
|
)
|
|
|
|
|
|
def snapshot_example_07() -> ScenarioSnapshot:
|
|
return _snapshot_example_07_variant(
|
|
"example_07_large_scale_routing",
|
|
warm_start_enabled=True,
|
|
)
|
|
|
|
|
|
def snapshot_example_07_no_warm_start() -> ScenarioSnapshot:
|
|
return _snapshot_example_07_variant(
|
|
"example_07_large_scale_routing_no_warm_start",
|
|
warm_start_enabled=False,
|
|
)
|
|
|
|
|
|
def snapshot_example_07_no_warm_start_seed43() -> ScenarioSnapshot:
|
|
return _snapshot_example_07_variant(
|
|
"example_07_large_scale_routing_no_warm_start_seed43",
|
|
warm_start_enabled=False,
|
|
seed=43,
|
|
)
|
|
|
|
|
|
def trace_example_07() -> RoutingRunResult:
|
|
return _trace_example_07_variant(warm_start_enabled=True)
|
|
|
|
|
|
def trace_example_07_no_warm_start() -> RoutingRunResult:
|
|
return _trace_example_07_variant(warm_start_enabled=False)
|
|
|
|
|
|
def trace_example_07_no_warm_start_seed43() -> RoutingRunResult:
|
|
return _trace_example_07_variant(warm_start_enabled=False, seed=43)
|
|
|
|
|
|
def _build_example_07_variant_stack(
|
|
*,
|
|
num_nets: int,
|
|
seed: int,
|
|
warm_start_enabled: bool,
|
|
capture_conflict_trace: bool = False,
|
|
capture_frontier_trace: bool = False,
|
|
capture_iteration_trace: bool = False,
|
|
capture_pre_pair_frontier_trace: bool = False,
|
|
) -> tuple[CostEvaluator, AStarMetrics, PathFinder]:
|
|
bounds = (0, 0, 1000, 1000)
|
|
obstacles = (
|
|
box(450, 0, 550, 400),
|
|
box(450, 600, 550, 1000),
|
|
)
|
|
start_x = 50
|
|
start_y_base = 500 - (num_nets * 10.0) / 2.0
|
|
end_x = 950
|
|
end_y_base = 100
|
|
end_y_pitch = 800.0 / (num_nets - 1)
|
|
|
|
nets = []
|
|
for index in range(num_nets):
|
|
sy = int(round(start_y_base + index * 10.0))
|
|
ey = int(round(end_y_base + index * end_y_pitch))
|
|
nets.append(NetSpec(f"net_{index:02d}", Port(start_x, sy, 0), Port(end_x, ey, 0)))
|
|
problem = RoutingProblem(
|
|
bounds=bounds,
|
|
nets=tuple(nets),
|
|
static_obstacles=obstacles,
|
|
clearance=6.0,
|
|
)
|
|
options = RoutingOptions(
|
|
search=SearchOptions(
|
|
node_limit=2_000_000,
|
|
bend_radii=(50.0,),
|
|
sbend_radii=(50.0,),
|
|
greedy_h_weight=1.5,
|
|
bend_clip_margin=10.0,
|
|
),
|
|
objective=ObjectiveWeights(
|
|
unit_length_cost=0.1,
|
|
bend_penalty=100.0,
|
|
sbend_penalty=400.0,
|
|
),
|
|
congestion=CongestionOptions(
|
|
max_iterations=15,
|
|
base_penalty=100.0,
|
|
multiplier=1.4,
|
|
net_order="shortest",
|
|
shuffle_nets=True,
|
|
seed=seed,
|
|
warm_start_enabled=warm_start_enabled,
|
|
),
|
|
diagnostics=DiagnosticsOptions(
|
|
capture_expanded=True,
|
|
capture_conflict_trace=capture_conflict_trace,
|
|
capture_frontier_trace=capture_frontier_trace,
|
|
capture_iteration_trace=capture_iteration_trace,
|
|
capture_pre_pair_frontier_trace=capture_pre_pair_frontier_trace,
|
|
),
|
|
)
|
|
stack = build_routing_stack(problem, options)
|
|
return stack.evaluator, stack.finder.metrics, stack.finder
|
|
|
|
|
|
def _run_example_07_variant(
|
|
*,
|
|
num_nets: int,
|
|
seed: int,
|
|
warm_start_enabled: bool,
|
|
capture_conflict_trace: bool = False,
|
|
capture_frontier_trace: bool = False,
|
|
capture_iteration_trace: bool = False,
|
|
capture_pre_pair_frontier_trace: bool = False,
|
|
) -> RoutingRunResult:
|
|
evaluator, metrics, pathfinder = _build_example_07_variant_stack(
|
|
num_nets=num_nets,
|
|
seed=seed,
|
|
warm_start_enabled=warm_start_enabled,
|
|
capture_conflict_trace=capture_conflict_trace,
|
|
capture_frontier_trace=capture_frontier_trace,
|
|
capture_iteration_trace=capture_iteration_trace,
|
|
capture_pre_pair_frontier_trace=capture_pre_pair_frontier_trace,
|
|
)
|
|
|
|
def iteration_callback(idx: int, current_results: dict[str, RoutingResult]) -> None:
|
|
_ = current_results
|
|
new_greedy = max(1.1, 1.5 - ((idx + 1) / 10.0) * 0.4)
|
|
evaluator.greedy_h_weight = new_greedy
|
|
metrics.reset_per_route()
|
|
|
|
results = pathfinder.route_all(iteration_callback=iteration_callback)
|
|
return collect_worker_result(pathfinder, results)
|
|
|
|
|
|
def _snapshot_example_07_variant(
|
|
name: str,
|
|
*,
|
|
warm_start_enabled: bool,
|
|
seed: int = 42,
|
|
) -> ScenarioSnapshot:
|
|
t0 = perf_counter()
|
|
run = _run_example_07_variant(
|
|
num_nets=10,
|
|
seed=seed,
|
|
warm_start_enabled=warm_start_enabled,
|
|
)
|
|
t1 = perf_counter()
|
|
return _make_snapshot(name, run.results_by_net, t1 - t0, run.metrics)
|
|
|
|
|
|
def _trace_example_07_variant(
|
|
*,
|
|
warm_start_enabled: bool,
|
|
seed: int = 42,
|
|
) -> RoutingRunResult:
|
|
return _run_example_07_variant(
|
|
num_nets=10,
|
|
seed=seed,
|
|
warm_start_enabled=warm_start_enabled,
|
|
capture_conflict_trace=True,
|
|
capture_frontier_trace=True,
|
|
capture_iteration_trace=True,
|
|
capture_pre_pair_frontier_trace=True,
|
|
)
|
|
|
|
|
|
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,))
|
|
options = RoutingOptions(
|
|
search=SearchOptions(bend_radii=(10.0,), sbend_radii=()),
|
|
objective=ObjectiveWeights(bend_penalty=50.0, sbend_penalty=150.0),
|
|
congestion=CongestionOptions(max_iterations=1, use_tiered_strategy=False),
|
|
)
|
|
custom_physical = Polygon([(0, -11), (11, -11), (11, 0), (9, 0), (9, -9), (0, -9)])
|
|
custom_problem = replace(problem, nets=(replace(net, net_id="custom_geometry_and_proxy"),))
|
|
custom_options = replace(
|
|
options,
|
|
search=replace(options.search, bend_physical_geometry=custom_physical, bend_proxy_geometry=box(0, -11, 11, 0)),
|
|
)
|
|
return build_routing_stack(problem, options).finder, build_routing_stack(custom_problem, custom_options).finder
|
|
|
|
|
|
def snapshot_example_08() -> ScenarioSnapshot:
|
|
t0 = perf_counter()
|
|
pathfinder_std, pathfinder_custom = _build_example_08_workers()
|
|
results_std = pathfinder_std.route_all()
|
|
results_custom = pathfinder_custom.route_all()
|
|
t1 = perf_counter()
|
|
combined_metrics = _sum_route_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 snapshot_example_09() -> ScenarioSnapshot:
|
|
problem = RoutingProblem(
|
|
bounds=(0, 0, 100, 100),
|
|
nets=(NetSpec("budget_limited_net", Port(10, 50, 0), Port(85, 60, 180)),),
|
|
static_obstacles=(box(35, 35, 45, 65), box(55, 35, 65, 65)),
|
|
)
|
|
options = RoutingOptions(
|
|
search=SearchOptions(node_limit=3, bend_radii=(10.0,)),
|
|
objective=ObjectiveWeights(bend_penalty=50.0, sbend_penalty=150.0),
|
|
congestion=CongestionOptions(warm_start_enabled=False, max_iterations=1),
|
|
)
|
|
return _snapshot_worker("example_09_unroutable_best_effort", build_routing_stack(problem, options).finder)
|
|
|
|
|
|
def snapshot_terminal_bench_pr345() -> ScenarioSnapshot:
|
|
problem = terminal_bench_problem()
|
|
start = perf_counter()
|
|
run = route(problem, options=terminal_bench_options())
|
|
return _make_snapshot("terminal_bench_pr345", run.results_by_net, perf_counter() - start, run.metrics)
|
|
|
|
|
|
def _snapshot_routing_benchmark(case: BenchmarkCase) -> ScenarioSnapshot:
|
|
start = perf_counter()
|
|
run = route(case.problem, options=profile_options(case, "fixture"))
|
|
return _make_snapshot(f"routing_benchmark_{case.name.replace('-', '_')}", run.results_by_net, perf_counter() - start, run.metrics)
|
|
|
|
|
|
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 _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),
|
|
("terminal_bench_pr345", snapshot_terminal_bench_pr345),
|
|
*(
|
|
(f"routing_benchmark_{case.name.replace('-', '_')}", partial(_snapshot_routing_benchmark, case))
|
|
for case in SNAPSHOT_CASES
|
|
),
|
|
)
|
|
|
|
TRACE_SCENARIO_RUNS: tuple[tuple[str, TraceScenarioRun], ...] = (
|
|
("example_05_orientation_stress", trace_example_05),
|
|
("example_07_large_scale_routing", trace_example_07),
|
|
)
|
|
|
|
TRACE_PERFORMANCE_SCENARIO_RUNS: tuple[tuple[str, TraceScenarioRun], ...] = (
|
|
("example_07_large_scale_routing_no_warm_start", trace_example_07_no_warm_start),
|
|
("example_07_large_scale_routing_no_warm_start_seed43", trace_example_07_no_warm_start_seed43),
|
|
)
|
|
|
|
|
|
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)
|