inire/inire/tests/routing_expanded_benchmarks.py
Jan Petykiewicz 63acb94642 Fix routing correctness and bound optional routing work
Check physical keepout clearance, retain useful partial paths, and account for all pair repair searches. Verify complete seeds early and recover narrower corridors when shared guides are blocked.

Keep frontier diagnostics outside routing decisions and bound their work by the worker deadline. Correct benchmark scaling and scenario validation, and update the guides and release metadata.
2026-12-02 17:16:34 -08:00

431 lines
21 KiB
Python

"""Deterministic micron-scale layouts with independently specified routes."""
from __future__ import annotations
from dataclasses import replace
from itertools import pairwise, product
from typing import TYPE_CHECKING
from shapely.affinity import scale
from shapely.geometry import Polygon, box
from inire import (
Bend90Seed, CongestionOptions, FixedRoute, NetSpec, ObjectiveWeights, PathSeed, Port, RefinementOptions,
RoutingOptions, RoutingProblem, SBendSeed, SearchOptions, StraightSeed, TopologyOptions,
)
from inire.tests.routing_benchmark_case import BenchmarkCase
from inire.tests.routing_crossing_benchmarks import _crossing_grid
from inire.tests.stress_routing_benchmarks import _Track, _wall, _waypoint_case
if TYPE_CHECKING:
from inire.seeds import PathSegmentSeed
def _on_grid(case: BenchmarkCase, grid_nm: int) -> BenchmarkCase:
"""Convert authored microns to integer layout units before timing the router."""
assert 1000 % grid_nm == 0
factor = 1000 // grid_nm
if factor == 1:
return replace(case, grid_nm=grid_nm)
def port(p: Port) -> Port:
return Port(p.x * factor, p.y * factor, p.r)
def polygon(shape: Polygon) -> Polygon:
return scale(shape, xfact=factor, yfact=factor, origin=(0, 0))
def move(segment: PathSegmentSeed) -> PathSegmentSeed:
if isinstance(segment, StraightSeed):
return StraightSeed(segment.length * factor)
if isinstance(segment, Bend90Seed):
return Bend90Seed(segment.radius * factor, segment.direction)
return SBendSeed(segment.offset * factor, segment.radius * factor)
problem, options = case.problem, case.options
assert not problem.initial_paths
xmin, ymin, xmax, ymax = problem.bounds
scaled_problem = replace(
problem,
bounds=(xmin * factor, ymin * factor, xmax * factor, ymax * factor),
nets=tuple(NetSpec(net.net_id, port(net.start), port(net.target), net.width * factor) for net in problem.nets),
static_obstacles=tuple(polygon(shape) for shape in problem.static_obstacles),
fixed_routes=tuple(FixedRoute(route.net_id, tuple(polygon(shape) for shape in route.polygons)) for route in problem.fixed_routes),
clearance=problem.clearance * factor,
safety_zone_radius=problem.safety_zone_radius * factor,
self_clearance=problem.self_clearance * factor,
)
search = options.search
offsets = search.sbend_offsets
scaled_options = replace(
options,
search=replace(
search,
bend_radii=tuple(r * factor for r in search.bend_radii),
sbend_radii=tuple(r * factor for r in search.sbend_radii),
sbend_offsets=None if offsets is None else tuple(offset * factor for offset in offsets),
min_straight_length=search.min_straight_length * factor,
max_straight_length=search.max_straight_length * factor,
proximity_range=search.proximity_range * factor,
),
# Component scoring divides the bend surcharge by the square root of the radius.
objective=replace(
options.objective,
bend_penalty=options.objective.bend_penalty * factor**1.5,
sbend_penalty=options.objective.sbend_penalty * factor,
),
congestion=replace(options.congestion, base_penalty=options.congestion.base_penalty * factor),
)
reference = tuple((net_id, PathSeed(tuple(move(segment) for segment in seed.segments))) for net_id, seed in case.reference_paths)
return replace(case, problem=scaled_problem, options=scaled_options, reference_paths=reference, grid_nm=grid_nm)
def _forward(name: str, r: int, width: int, slack: int, *, grid: int = 1000, edge_clutter: bool = False) -> BenchmarkCase:
start, y, finish = 2 * r, 4 * r, 6 * r + slack
height = 8 * r
obstacles = (box(4 * r - r // 5, y - r // 2, 4 * r + r // 5, y + r // 2),)
if edge_clutter:
obstacles += (box(3 * r, 0, 4 * r, r // 3), box(3 * r, height - r // 3, 4 * r, height))
route = _Track("through", ((start, y), (3 * r, y), (3 * r, y + 2 * r), (finish - r, y + 2 * r), (finish - r, y), (finish, y)), width)
return _on_grid(_waypoint_case(
f"precision-forward-{name}", "Four turns at the tangency limit, with edge clutter or slack to control available lanes",
(0, 0, finish + 2 * r, height), obstacles, (route,), radius=r, clearance=1,
), grid)
def _return(name: str, r: int, width: int, *, tight: bool = False, peer: bool = False, rise: int = 0, grid: int = 1000) -> BenchmarkCase:
start, finish, y, height = 10 * r, 2 * r, 4 * r, 9 * r
right, left, lane = start + r, finish - r, y - 2 * r
board_width = 13 * r
obstacles = (box(4 * r, 0, 5 * r, r // 3), box(6 * r, height - r // 3, 7 * r, height))
if tight:
edge = width / 2 + 0.25
obstacles += (box(right + edge, y - r, board_width, y + r), box(0, y + rise - r, left - edge, y + rise + r))
tracks = (_Track("return", ((start, y), (right, y), (right, lane), (left, lane), (left, y + rise), (finish, y + rise)), width),)
if peer:
tracks += (_Track("through", ((2 * r, 7 * r), (board_width - r, 7 * r)), width=width),)
return _on_grid(_waypoint_case(
f"precision-return-{name}", "East-facing westward return with limited lead room or a neighboring through track",
(0, 0, board_width, height), obstacles, tracks, radius=r, clearance=0,
), grid)
def _sbend_slot(name: str, radius: int, count: int, width: int, *, pitch: int, sign: int = 1, grid: int = 1000) -> BenchmarkCase:
# A 3-4-5 triangle makes these spans exact on every chosen grid.
offset, span = sign * 4 * radius // 5, 8 * radius // 5
start, turn, end = radius, 3 * radius, 7 * radius
levels = tuple(2 * radius + pitch * i for i in range(count))
shifted = tuple(y + offset for y in levels)
height = max(*levels, *shifted) + 2 * radius
clearance = 2
half_slot = width // 2 + clearance + 1
obstacles = (*_wall(turn - 9, turn - 3, height, tuple((y - half_slot, y + half_slot) for y in levels)),
*_wall(turn + span + 3, turn + span + 9, height, tuple((y - half_slot, y + half_slot) for y in shifted)))
tracks = tuple(
_Track(f"lane_{i}", ((start, a), (turn, a), (turn + span, b), (end, b)), width)
for i, (a, b) in enumerate(zip(levels, shifted, strict=True))
)
case = _waypoint_case(
f"precision-sbend-{name}", "Short bank of exact signed S-bends between aligned narrow port slots",
(0, 0, 8 * radius, height), obstacles, tracks, radius=radius, clearance=clearance,
)
options = replace(case.options, search=replace(case.options.search, bend_radii=(), sbend_offsets=(offset,)))
return _on_grid(replace(case, options=options), grid)
def _aperture(name: str, widths: tuple[int, ...], clearance: int, *, extra: int = 0, grid: int = 1000) -> BenchmarkCase:
lower = 32
cursor = lower + clearance
tracks = []
for i, width in enumerate(widths):
row = cursor + width // 2
tracks.append(_Track(f"lane_{i}", ((20, row), (260, row)), width))
cursor += width + clearance
upper = cursor
height = upper + 64
case = _waypoint_case(
f"precision-aperture-{name}", "Exact-capacity wall opening with sized trace pitches and optional competing ports",
(0, 0, 280, height), _wall(120, 160, height, ((lower, upper),)), tuple(tracks), radius=10, clearance=clearance,
)
if extra:
start = upper + 10
overflow = tuple(NetSpec(f"overflow_{i}", Port(20, start + 10 * i, 0), Port(260, start + 10 * i, 0), widths[0]) for i in range(extra))
case = replace(case, problem=replace(case.problem, nets=(*case.problem.nets, *overflow)), expected_routed=len(widths))
return _on_grid(case, grid)
def _columns(name: str, count: int, walls: int, radius: int, width: int, *, shift_units: int, grid: int = 1000) -> BenchmarkCase:
rise = shift_units * radius
pitch = width + 14
wall_pitch = count * (2 * radius + 10) + 4 * radius + 40
finish = (walls + 1) * wall_pitch + 10
height = 4 * radius + (walls + 1) * rise + count * pitch
rows = tuple(tuple(2 * radius + bank * rise + track * pitch for track in range(count)) for bank in range(walls + 2))
obstacles = tuple(
obstacle
for bank in range(1, walls + 1)
for obstacle in _wall(bank * wall_pitch, bank * wall_pitch + 18, height, ((rows[bank][0] - width, rows[bank][-1] + width),))
)
tracks = []
for i, path_rows in enumerate(zip(*rows, strict=True)):
points = [(20, path_rows[0])]
for bank, (low, high) in enumerate(pairwise(path_rows)):
x = bank * wall_pitch + 26 + radius + (count - 1 - i) * (2 * radius + 10)
points.extend(((x, low), (x, high)))
points.append((finish, path_rows[-1]))
tracks.append(_Track(f"lane_{i}", tuple(reversed(points)) if i % 2 else tuple(points), width))
return _on_grid(_waypoint_case(
f"complex-column-{name}", "Alternating net directions through multiple narrow, ascending aperture banks",
(0, 0, finish + 30, height), obstacles, tuple(tracks), radius=radius, clearance=2,
), grid)
def _residual(name: str, *, width: int, rise: int, fixed: bool, grid: int = 1000) -> BenchmarkCase:
concave = Polygon(((10, 6), (94, 6), (94, 12), (16, 12), (16, 83), (10, 83)))
obstacles = (concave, box(68, 55, 78, 66))
upper = 60 + rise
tracks = (_Track("trunk", ((26, 60), (42, 60), (42, upper), (105, upper), (105, 60), (138, 60)), width),)
if not fixed:
tracks += (_Track("west_branch", ((6, 75), (6, 114))), _Track("east_rail", ((99, 19), (138, 19)), width=4))
case = _waypoint_case(
f"complex-residual-{name}", "A dogleg passes through open space inside a concave obstacle's bounding box",
(0, 0, 152, 128), obstacles, tracks, radius=10, clearance=1,
)
if fixed:
lock = FixedRoute("installed_rail", (box(99, 17, 138, 21),))
case = replace(case, problem=replace(case.problem, fixed_routes=(lock,)))
return _on_grid(case, grid)
def _crossings(name: str, rows: int, columns: int, pitch_x: int, pitch_y: int, *, staggered: bool, grid: int = 1000) -> BenchmarkCase:
return _on_grid(_crossing_grid(
name, f"{rows} by {columns} placed crossing cells with {'staggered' if staggered else 'aligned'} links and opposing port banks",
tuple(160 + pitch_x * col for col in range(columns)), tuple(165 + pitch_y * row for row in range(rows)), staggered=staggered,
), grid)
def _comb(name: str, lanes: int, teeth: int, radius: int, *, grid: int = 1000) -> BenchmarkCase:
pitch = 8 * radius
x_pitch = 5 * radius
finish = (teeth + 1) * x_pitch
width = min(12, radius)
tracks = []
obstacles = []
for i in range(lanes):
base = i * pitch
if i:
obstacles.append(box(0, base - 2, finish + 20, base + 2))
rows = tuple(base + (2 if (j + i) % 2 else 6) * radius for j in range(teeth + 1))
points = [(radius, rows[0])]
for j, (a, b) in enumerate(pairwise(rows), 1):
x = j * x_pitch - 2 * radius
points.extend(((x, a), (x, b)))
if b > a:
obstacles.append(box(j * x_pitch - radius // 2, base, j * x_pitch + radius // 2, base + 4 * radius))
else:
obstacles.append(box(j * x_pitch - radius // 2, base + 4 * radius, j * x_pitch + radius // 2, base + pitch))
points.append((finish, rows[-1]))
tracks.append(_Track(f"lane_{i}", tuple(reversed(points)) if i % 2 else tuple(points), width))
return _on_grid(_waypoint_case(
f"search-comb-{name}", "Alternating teeth force repeated changes of lane in isolated channels",
(0, 0, finish + 20, lanes * pitch), tuple(obstacles), tuple(tracks), radius=radius, clearance=2,
), grid)
def _port_bank(name: str, count: int, *, obstacles: bool, detours: int = 0, grid: int = 1000) -> BenchmarkCase:
width, clearance, pitch = 4, 2, 10
straight = count - detours
height = straight * pitch + 20 + detours * 110
tracks = [
_Track(f"lane_{i:03}", ((20, y), (560, y)) if i % 2 else ((560, y), (20, y)), width)
for i, y in enumerate(range(10, 10 + straight * pitch, pitch))
]
keepouts = [box(160 + 26 * (i % 9), 14 + i * pitch, 195 + 26 * (i % 9), 16 + i * pitch) for i in range(straight)] if obstacles else []
for i in range(detours):
y = straight * pitch + 50 + 110 * i
points = ((20, y), (100, y), (100, y + 45), (440, y + 45), (440, y), (560, y))
tracks.append(_Track(f"bypass_{i}", points if i % 2 else tuple(reversed(points)), width))
keepouts.append(box(190 + 30 * i, y - 10, 260 + 30 * i, y + 10))
description = f"{count} interleaved eastbound and westbound nets"
if obstacles:
description += " with one keepout per row"
if detours:
description += f" and {detours} obstructed approaches"
return _on_grid(_waypoint_case(
f"scale-bank-{name}", description, (0, 0, 580, height), tuple(keepouts), tuple(tracks), radius=20, clearance=clearance,
), grid)
def _obstacle_field(columns: int, rows: int, count: int, *, grid: int) -> BenchmarkCase:
pitch = 24
field_end = 140 + pitch * columns
field_base = 64
height = field_base + pitch * rows + 120
lanes = tuple(field_base + pitch * i + 10 for i in range(count))
obstacles = tuple(
box(140 + pitch * col, field_base + pitch * row + col % 3, 152 + pitch * col, field_base + pitch * row + 13 + col % 3)
for col, row in product(range(columns), range(rows))
)
# Stagger the turns outside the field so that no vertical leg crosses a peer's occupied row.
tracks = []
for i, y in enumerate(lanes):
lead = 32 + 22 * i
tail = field_end + 30 + 22 * (count - 1 - i)
lane = 12 + 10 * i
tracks.append(_Track(f"bus_{i}", ((18, y), (lead, y), (lead, lane), (tail, lane), (tail, y), (field_end + 150, y)), 2))
return _on_grid(_waypoint_case(
f"scale-field-{columns}x{rows}", f"{columns * rows} staggered rectangular obstacles across the straight approaches of {count} buses",
(0, 0, field_end + 170, height), obstacles, tuple(tracks), radius=10, clearance=2,
), grid)
CORNER_CASES = (
_forward("exact-r10", 10, 1, 0),
_forward("plus-one", 10, 4, 1),
_forward("plus-two", 20, 8, 2, grid=500),
_forward("large-radius", 100, 20, 0),
_forward("board-edges", 20, 4, 0, edge_clutter=True),
_forward("board-edges-fine", 10, 2, 2, grid=100, edge_clutter=True),
_forward("wide-exact", 25, 16, 0, grid=250),
_forward("grid-10nm", 10, 2, 0, grid=10),
_return("exact-pivots", 10, 2, tight=True),
_return("wide-exact-pivots", 20, 10, tight=True),
_return("offset", 25, 4, rise=18),
_return("offset-fine", 10, 3, rise=6, grid=100),
_return("paired", 20, 4, peer=True),
_return("paired-tight", 10, 2, tight=True, peer=True),
_return("paired-wide", 30, 12, peer=True, rise=20, grid=500),
_return("large-radius", 100, 20, tight=True, grid=250),
_sbend_slot("single-r25", 25, 1, 2, pitch=32),
_sbend_slot("single-r50", 50, 1, 6, pitch=32, sign=-1, grid=500),
_sbend_slot("single-r100", 100, 1, 8, pitch=32, grid=250),
_sbend_slot("single-10nm", 25, 1, 2, pitch=32, sign=-1, grid=10),
_sbend_slot("paired", 25, 2, 4, pitch=18, grid=500),
_sbend_slot("paired-down", 25, 2, 4, pitch=16, sign=-1),
_sbend_slot("three", 25, 3, 4, pitch=16),
_sbend_slot("four-wide", 50, 4, 8, pitch=22, sign=-1),
_aperture("pair-exact", (2, 2), 2),
_aperture("four-exact", (4,) * 4, 2, grid=500),
_aperture("six-exact", (2,) * 6, 3),
_aperture("mixed-tight", (2, 8, 4, 12), 2, grid=250),
_aperture("mixed-wide", (16, 4, 20), 3),
_aperture("overloaded-three", (2,) * 3, 3, extra=1),
_aperture("overloaded-five", (4,) * 5, 2, extra=2),
_aperture("overloaded-fine", (2,) * 2, 2, extra=1, grid=100),
)
COMPLEX_CASES = (
_columns("3x1", 3, 1, 10, 4, shift_units=3),
_columns("3x2", 3, 2, 10, 4, shift_units=3),
_columns("4x2", 4, 2, 10, 6, shift_units=3),
_columns("4x3", 4, 3, 10, 6, shift_units=3),
_columns("5x2", 5, 2, 15, 6, shift_units=3, grid=500),
_columns("5x3", 5, 3, 15, 8, shift_units=3),
_columns("6x2-wide", 6, 2, 20, 12, shift_units=4),
_columns("6x3", 6, 3, 20, 8, shift_units=4),
_residual("narrow-dogleg", width=2, rise=30, fixed=False),
_residual("wide-dogleg", width=10, rise=44, fixed=False),
_residual("midwidth-fine", width=6, rise=38, fixed=False, grid=250),
_residual("wide-fine", width=12, rise=45, fixed=False, grid=500),
_residual("fixed-rail", width=4, rise=34, fixed=True),
_residual("fixed-rail-wide", width=12, rise=44, fixed=True),
_residual("fixed-rail-fine", width=2, rise=34, fixed=True, grid=100),
_residual("fixed-rail-midwidth", width=8, rise=40, fixed=True, grid=500),
_crossings("complex-crossing-2x2-aligned", 2, 2, 130, 154, staggered=False),
_crossings("complex-crossing-2x3-staggered", 2, 3, 110, 128, staggered=True),
_crossings("complex-crossing-3x2-fine", 3, 2, 118, 132, staggered=True, grid=250),
_crossings("complex-crossing-3x3-uneven-pitch", 3, 3, 108, 147, staggered=True),
_crossings("complex-crossing-3x4-aligned", 3, 4, 108, 97, staggered=False),
_crossings("complex-crossing-3x4-staggered", 3, 4, 102, 116, staggered=True),
_crossings("complex-crossing-4x3-tight", 4, 3, 90, 96, staggered=True),
_crossings("complex-crossing-4x4", 4, 4, 116, 108, staggered=True),
)
def _low_length_cost_case(
name: str,
description: str,
start: Port,
target: Port,
keepouts: tuple[Polygon, ...],
radii: tuple[int, int, int],
reference: PathSeed,
*,
node_limit: int,
bend_penalty: float,
) -> BenchmarkCase:
problem = RoutingProblem((0, 0, 200, 140), (NetSpec("track", start, target, width=4),), keepouts, clearance=2)
options = RoutingOptions(
search=SearchOptions(
node_limit=node_limit, bend_radii=radii, sbend_radii=radii[1:], distance_heuristic_weight=10,
),
objective=ObjectiveWeights(
unit_length_cost=0.05, bend_penalty=bend_penalty, sbend_penalty=2 * bend_penalty, danger_weight=0,
),
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False, use_tiered_strategy=False),
refinement=RefinementOptions(enabled=False),
topology=TopologyOptions(mode="off"),
)
return BenchmarkCase(f"search-low-length-cost-{name}", description, problem, options, (("track", reference),), grid_nm=1000)
LOW_LENGTH_COST_CASES = (
_low_length_cost_case(
"north-launch",
"A north-facing launch bends around staggered keepouts to an east-facing target",
Port(16, 89, 90), Port(157, 61, 0),
(box(101, 79, 143, 133), box(62, 58, 106, 122), box(85, 64, 112, 100)),
(15, 20, 25),
PathSeed((
Bend90Seed(25, "CW"), Bend90Seed(15, "CW"), StraightSeed(38), Bend90Seed(25, "CCW"), StraightSeed(38), SBendSeed(25, 20),
)),
node_limit=80,
bend_penalty=30,
),
_low_length_cost_case(
"blocked-straight",
"A low-cost straight approach must turn around a keepout before shifting onto the arrival row",
Port(13, 25, 0), Port(152, 36, 0),
(box(91, 14, 121, 41), box(101, 74, 110, 96), box(87, 60, 127, 90)),
(10, 20, 25),
PathSeed((
StraightSeed(49), StraightSeed(13), Bend90Seed(10, "CCW"), Bend90Seed(20, "CW"), SBendSeed(-19, 25), StraightSeed(7),
)),
node_limit=80,
bend_penalty=250,
),
_low_length_cost_case(
"south-launch",
"A south-facing launch must reach a high east-facing target past several offset keepouts",
Port(15, 79, 270), Port(184, 119, 0),
(box(97, 44, 123, 65), box(96, 94, 129, 134), box(44, 9, 58, 52)),
(11, 20, 25),
PathSeed((
Bend90Seed(20, "CCW"), Bend90Seed(25, "CCW"), StraightSeed(35), Bend90Seed(11, "CW"), Bend90Seed(20, "CW"),
StraightSeed(9), StraightSeed(5), Bend90Seed(20, "CCW"), SBendSeed(43, 25), StraightSeed(23),
)),
node_limit=130,
bend_penalty=5,
),
)
SEARCH_CASES = (
_comb("1x2", 1, 2, 10),
_comb("1x4", 1, 4, 12),
_comb("2x3", 2, 3, 10),
_comb("2x5-fine", 2, 5, 10, grid=250),
_comb("3x4", 3, 4, 15),
_comb("4x6", 4, 6, 15),
_comb("5x5", 5, 5, 20),
_comb("6x7", 6, 7, 20),
*LOW_LENGTH_COST_CASES,
)
SCALING_CASES = (
_crossings("scale-crossing-10x10-aligned", 10, 10, 120, 132, staggered=False),
_crossings("scale-crossing-10x11-staggered", 10, 11, 120, 132, staggered=True),
_port_bank("224", 224, obstacles=False),
_port_bank("256-fine", 256, obstacles=False, grid=100),
_port_bank("216-with-keepouts", 216, obstacles=True),
_obstacle_field(16, 16, 3, grid=1000),
_port_bank("240-with-keepouts", 240, obstacles=True, detours=4, grid=500),
_obstacle_field(20, 16, 4, grid=500),
)