Add and measure a 57-board routing benchmark corpus

This commit is contained in:
Jan Petykiewicz 2026-11-26 11:57:49 -08:00
commit 2f2add98ec

View file

@ -0,0 +1,858 @@
from __future__ import annotations
from dataclasses import dataclass, replace
from itertools import pairwise, product
from json import loads
from math import pi
from pathlib import Path
from typing import TYPE_CHECKING
from shapely.geometry import Polygon, box
from inire import Bend90Seed, NetSpec, PathSeed, Port, RoutingOptions, RoutingProblem, SBendSeed, StraightSeed
from inire.router._bundle import ordered_bundle_terminal_headings
from inire.router._waypoints import compile_waypoints
from inire.tests.terminal_bench_scenario import LENGTH_COST, OBSTACLES, terminal_bench_options, terminal_bench_problem
if TYPE_CHECKING:
from inire.seeds import PathSegmentSeed
@dataclass(frozen=True, slots=True)
class BenchmarkCase:
name: str
description: str
problem: RoutingProblem
options: RoutingOptions
reference_paths: tuple[tuple[str, PathSeed], ...] = ()
def fixed_headings_problem(self) -> RoutingProblem:
if self.options.topology.infer_terminal_headings:
return ordered_bundle_terminal_headings(self.problem)
return self.problem
def _options(bend_radius: float, sbend_radius: float | None = None) -> RoutingOptions:
options = terminal_bench_options()
return replace(
options,
search=replace(options.search, bend_radii=(bend_radius,), sbend_radii=(sbend_radius or bend_radius,)),
objective=replace(
options.objective,
bend_penalty=(LENGTH_COST["bend90"] - 1) * (pi * bend_radius / 2) * (bend_radius / 10) ** 0.5,
),
)
def _open_or_shared_gate(name: str, description: str, heights: tuple[int, ...], *, wall: bool = False) -> BenchmarkCase:
# A 34-unit aperture with 2-unit guides and 6-unit clearance leaves 20 units
# for centerlines. At 8-unit pitch this admits at most three routes.
obstacles = (box(200, 0, 250, 132), box(200, 166, 250, 300)) if wall else ()
nets = tuple(NetSpec(f"track_{y}", Port(25, y, 0), Port(425, y, 0)) for y in heights)
reference = tuple((net.net_id, PathSeed((StraightSeed(400),))) for net in nets if not wall or 139 <= net.start.y <= 159)
return BenchmarkCase(name, description, RoutingProblem((0, 0, 450, 300), nets, obstacles, clearance=6), _options(25), reference)
def _competing_detours() -> BenchmarkCase:
nets = tuple(NetSpec(net_id, Port(60, y, 0), Port(740, y, 0)) for net_id, y in (("lower", 270), ("upper", 300)))
problem = RoutingProblem((0, 0, 800, 600), nets, (box(300, 170, 480, 460),), clearance=6)
reference = []
for net_id, first_turn, second_turn, vertical in (("lower", "CW", "CCW", 75), ("upper", "CCW", "CW", 130)):
reference.append(
(
net_id,
PathSeed(
(
StraightSeed(100),
Bend90Seed(25, first_turn),
StraightSeed(vertical),
Bend90Seed(25, second_turn),
StraightSeed(350),
Bend90Seed(25, second_turn),
StraightSeed(vertical),
Bend90Seed(25, first_turn),
StraightSeed(130),
)
),
)
)
return BenchmarkCase(
"competing-detours", "Two nets compete for a short detour around the same obstacle", problem, _options(25), tuple(reference)
)
def _offset_gates(mirrored: bool = False) -> BenchmarkCase:
# A 25-unit S-bend radius and 20-unit offset give an exact 40-unit forward span.
launch_y, arrival_y = (120, 100) if mirrored else (100, 120)
problem = RoutingProblem(
bounds=(0, 0, 600, 260),
nets=(NetSpec("offset", Port(30, launch_y, 0), Port(570, arrival_y, 0)),),
static_obstacles=(
box(200, 0, 240, launch_y - 10),
box(200, launch_y + 10, 240, 260),
box(400, 0, 440, arrival_y - 10),
box(400, arrival_y + 10, 440, 260),
),
clearance=6,
)
seed = PathSeed((StraightSeed(220), SBendSeed(arrival_y - launch_y, 25), StraightSeed(280)))
name = "offset-gates-mirrored" if mirrored else "offset-gates"
return BenchmarkCase(name, "A lateral shift between two narrow apertures", problem, _options(30, 25), (("offset", seed),))
def _reversed_heading() -> BenchmarkCase:
problem = RoutingProblem(
bounds=(0, 0, 700, 320),
nets=(NetSpec("return", Port(520, 150, 0), Port(200, 70, 0)),),
static_obstacles=(box(280, 90, 430, 215),),
clearance=6,
)
bend = Bend90Seed(30, "CCW")
seed = PathSeed((StraightSeed(60), bend, StraightSeed(40), bend, StraightSeed(440), bend, StraightSeed(120), bend, StraightSeed(60)))
return BenchmarkCase(
"reversed-heading", "East-facing ports require westward travel past an obstacle", problem, _options(30), (("return", seed),)
)
def _mixed_width_gates() -> BenchmarkCase:
nets = (
NetSpec("narrow", Port(25, 100, 0), Port(425, 100, 0), width=2),
NetSpec("wide", Port(25, 200, 0), Port(425, 200, 0), width=8),
)
obstacles = (box(200, 0, 250, 92), box(200, 108, 250, 181), box(200, 219, 250, 300))
problem = RoutingProblem((0, 0, 450, 300), nets, obstacles, clearance=6)
reference = tuple((net.net_id, PathSeed((StraightSeed(400),))) for net in nets)
return BenchmarkCase(
"mixed-width-gates",
"A narrow aperture fits only the narrow net",
problem,
_options(25),
reference,
)
def _blocking_straight(*, turn_limited: bool = False) -> BenchmarkCase:
inset = 14 if turn_limited else 2
vertical_start, vertical_target = (130, 170) if turn_limited else (30, 270)
problem = RoutingProblem(
bounds=(0, 0, 400, 300),
nets=(
NetSpec("horizontal", Port(inset, 150, 0), Port(400 - inset, 150, 0)),
NetSpec("vertical", Port(200, vertical_start, 90), Port(200, vertical_target, 90)),
),
clearance=6,
)
down, up = Bend90Seed(25, "CW"), Bend90Seed(25, "CCW")
detour = PathSeed(
(StraightSeed(42 - inset), down, StraightSeed(82), up, StraightSeed(215), up, StraightSeed(82), down, StraightSeed(43 - inset))
)
reference = (("horizontal", detour), ("vertical", PathSeed((StraightSeed(vertical_target - vertical_start),))))
if turn_limited:
return BenchmarkCase(
"blocking-turn-room", "A straight leaves side passages but challenges the other net's launch turn", problem, _options(25), reference
)
return BenchmarkCase("blocking-straight", "A legal straight divides the board and traps a second net", problem, _options(25), reference)
def _crossing_with_room() -> BenchmarkCase:
problem = RoutingProblem(
bounds=(0, 0, 400, 300),
nets=(
NetSpec("horizontal", Port(80, 150, 0), Port(320, 150, 0)),
NetSpec("vertical", Port(200, 30, 90), Port(200, 270, 90)),
),
clearance=6,
)
cw, ccw = Bend90Seed(25, "CW"), Bend90Seed(25, "CCW")
bypass = PathSeed((StraightSeed(10), cw, StraightSeed(85), ccw, StraightSeed(120), ccw, StraightSeed(85), cw, StraightSeed(10)))
reference = (("horizontal", PathSeed((StraightSeed(240),))), ("vertical", bypass))
return BenchmarkCase(
"crossing-with-room", "An unlocked junction with room to route around either straight", problem, _options(25), reference
)
def _detour() -> BenchmarkCase:
problem = RoutingProblem(
bounds=(0, 0, 800, 600),
clearance=6,
static_obstacles=(box(300, 170, 480, 430),),
nets=(NetSpec("detour", Port(60, 300, 0), Port(740, 300, 0)),),
)
down, up = Bend90Seed(50, "CW"), Bend90Seed(50, "CCW")
seed = PathSeed((StraightSeed(100), down, StraightSeed(50), up, StraightSeed(280), up, StraightSeed(50), down, StraightSeed(100)))
return BenchmarkCase("detour", "One net with a choice of two obstacle bypasses", problem, terminal_bench_options(), (("detour", seed),))
_TERMINAL_BENCH = terminal_bench_problem()
def _terminal_bench_point_cases() -> tuple[BenchmarkCase, ...]:
options = terminal_bench_options()
options = replace(
options,
topology=replace(options.topology, mode="bundle", infer_terminal_headings=True),
refinement=replace(options.refinement, enabled=False),
)
x0, _, x1, y1 = OBSTACLES[-1]
one_track = replace(_TERMINAL_BENCH, static_obstacles=(*_TERMINAL_BENCH.static_obstacles[:-1], box(x0, 16, x1, y1)))
return (
BenchmarkCase("terminal-bench-point-terminals", "PR 345 with freely oriented point terminals", _TERMINAL_BENCH, options),
BenchmarkCase(
"terminal-bench-clearance-5",
"Point terminals with the clearance reduced from 6 to 5",
replace(_TERMINAL_BENCH, clearance=5),
options,
),
BenchmarkCase(
"terminal-bench-narrow-bottom",
"Point terminals with a 16-unit opening below the tower",
one_track,
options,
),
)
_TERMINAL_BENCH_POINT_CASES = _terminal_bench_point_cases()
def _ordered_bundle_benchmarks() -> tuple[BenchmarkCase, ...]:
base = _TERMINAL_BENCH_POINT_CASES[0]
def obstacles(changes: dict[int, tuple[float, float, float, float]]) -> RoutingProblem:
return replace(base.problem, static_obstacles=tuple(box(*changes.get(i, bounds)) for i, bounds in enumerate(OBSTACLES)))
radius_40 = _options(40)
return (
replace(
base,
name="bundle-offset-barriers",
description="Move both paired barriers toward the launches, changing turning room before and after the barriers",
problem=obstacles({0: (410, 80, 510, 480), 1: (410, 520, 510, 920), 2: (1865, 130, 1970, 491.5), 3: (1865, 510, 1970, 851.5)}),
),
replace(
base,
name="bundle-three-bottom-tracks",
description="The taller lower opening admits the return and two through tracks below the tower",
problem=obstacles({6: (1350, 27, 1537, 620)}),
),
replace(
base,
name="bundle-tall-wide-tower",
description="A taller and wider tower constrains the middle crossover and the lower tracks' descent",
problem=obstacles({6: (1320, 18, 1610, 644)}),
),
replace(
base,
name="bundle-shifted-shelf",
description="Shift the north and south obstacles to the east and widen the south obstacle",
problem=obstacles({4: (962, 680, 1062, 983), 5: (994, 50, 1134, 475)}),
),
replace(
base,
name="bundle-radius-40",
description="Reduce the bend radius to 40 and narrow the tower's bottom opening to 16",
problem=obstacles({6: (1350, 16, 1537, 620)}),
options=replace(base.options, search=radius_40.search, objective=radius_40.objective),
),
)
@dataclass(frozen=True, slots=True)
class _Gate:
left: int
right: int
openings: tuple[tuple[int, ...], ...]
@property
def tracks(self) -> tuple[int, ...]:
return tuple(y for opening in self.openings for y in opening)
def _band(first: int, pitch: int = 18, count: int = 9) -> tuple[int, ...]:
return tuple(range(first, first + pitch * count, pitch))
def _gate_path(columns: tuple[tuple[int, int], ...], tracks: tuple[int, ...], start_x: int, finish_x: int) -> PathSeed:
"""Construct a feasibility witness with S-bends centered between columns."""
segments: list[PathSegmentSeed] = []
x = start_x
for ((_, after), (before, _)), (start_y, end_y) in zip(pairwise(columns), pairwise(tracks), strict=True):
if start_y == end_y:
continue
sign = 1 if end_y > start_y else -1
remaining = abs(end_y - start_y)
bends = []
advance = 0
# These offsets have integer forward spans at radius 50.
for offset, span in ((40, 80), (20, 60), (4, 28)):
count, remaining = divmod(remaining, offset)
bends.extend([SBendSeed(sign * offset, 50)] * count)
advance += span * count
if remaining or before - after <= advance:
raise ValueError("The benchmark's track shift must fit between its columns")
entrance = (after + before - advance) // 2
segments.append(StraightSeed(entrance - x))
segments.extend(bends)
x = entrance + advance
segments.append(StraightSeed(finish_x - x))
return PathSeed(tuple(segments))
def _gate_benchmark(
name: str,
description: str,
launch: tuple[int, ...],
arrival: tuple[int, ...],
gates: tuple[_Gate, ...],
*,
widths: tuple[int, ...] = (2,) * 9,
westbound: tuple[int, ...] = (),
edge_walls: bool = True,
terminal_stagger: int = 0,
) -> BenchmarkCase:
bounds = (0, 0, 2400, 1000)
launch_x, arrival_x = 40, 2360
clearance = 6
obstacles = []
for gate in gates:
openings = []
first = 0
for tracks in gate.openings:
margin = max(widths[first : first + len(tracks)]) / 2 + clearance + 1
openings.append((tracks[0] - margin, tracks[-1] + margin))
first += len(tracks)
intervals = [(end, start) for (_, end), (start, _) in pairwise(openings)]
if edge_walls:
intervals.extend(((0, openings[0][0]), (openings[-1][1], bounds[3])))
if any(end <= start for start, end in intervals):
raise ValueError("The benchmark's gate openings must leave room for their walls")
obstacles.extend(box(gate.left, start, gate.right, end) for start, end in intervals)
first_column = launch_x + 2 * terminal_stagger
last_column = arrival_x - 2 * terminal_stagger
columns = ((first_column, first_column), *((gate.left, gate.right) for gate in gates), (last_column, last_column))
profiles = (launch, *(gate.tracks for gate in gates), arrival)
nets = []
references = []
for i, (width, track) in enumerate(zip(widths, zip(*profiles, strict=True), strict=True)):
net_id = f"net_{i:02d}"
left_x = launch_x + terminal_stagger * (i % 3)
right_x = arrival_x - terminal_stagger * ((i + 1) % 3)
path = _gate_path(columns, track, left_x, right_x)
if i in westbound:
start, target = Port(right_x, track[-1], 180), Port(left_x, track[0], 180)
# Reversal rotates the local frame by 180 degrees, preserving the signed S-bend offset.
path = PathSeed(path.segments[::-1])
else:
start, target = Port(left_x, track[0], 0), Port(right_x, track[-1], 0)
nets.append(NetSpec(net_id, start, target, width=width))
references.append((net_id, path))
options = _options(50)
options = replace(options, refinement=replace(options.refinement, enabled=False))
problem = RoutingProblem(bounds, tuple(nets), tuple(obstacles), clearance=clearance)
return BenchmarkCase(name, description, problem, options, tuple(references))
def _serial_gate_benchmarks() -> tuple[BenchmarkCase, ...]:
return (
_gate_benchmark(
"serial-alternating",
"Three single apertures make the entire bundle shift up, down, and up",
_band(300),
_band(340),
(_Gate(450, 520, (_band(340),)), _Gate(990, 1060, (_band(300),)), _Gate(1640, 1720, (_band(340),))),
),
_gate_benchmark(
"serial-staircase",
"Four apertures step a densely packed bundle upward in 20-unit increments",
_band(250, 12),
_band(350, 12),
tuple(_Gate(x, x + 65, (_band(y, 12),)) for x, y in zip((420, 850, 1290, 1760), (270, 290, 310, 330), strict=True)),
),
_gate_benchmark(
"serial-compression",
"Four apertures compress a bundle from 18-unit track pitch to 10 and allow a staged fan-out",
_band(300),
_band(300),
(
_Gate(450, 520, (_band(300),)),
_Gate(860, 930, (_band(316, 14),)),
_Gate(1340, 1400, (_band(332, 10),)),
_Gate(1820, 1880, (_band(316, 14),)),
),
),
_gate_benchmark(
"serial-terminal-jogs",
"Staggered ports bring small lateral shifts close to both ends of the gate sequence",
(296, 320, 344, 356, 380, 404, 416, 440, 464),
(344, 360, 376, 404, 420, 436, 464, 480, 496),
(_Gate(290, 350, (_band(300, 20),)), _Gate(1070, 1130, (_band(320, 20),)), _Gate(2030, 2100, (_band(340, 20),))),
),
_gate_benchmark(
"serial-close-walls",
"Four closely spaced walls limit the run-up for alternating 40-unit lateral shifts",
_band(500),
_band(500),
(
_Gate(720, 820, (_band(500),)),
_Gate(920, 1000, (_band(540),)),
_Gate(1110, 1190, (_band(500),)),
_Gate(1310, 1430, (_band(540),)),
),
),
)
def _split_gate_benchmarks() -> tuple[BenchmarkCase, ...]:
return (
_gate_benchmark(
"split-four-five",
"A single opening divides into four- and five-track branches and then merges",
_band(360),
_band(360),
(_Gate(470, 550, (_band(360),)), _Gate(1040, 1170, (_band(320, count=4), _band(472, count=5))), _Gate(1690, 1770, (_band(360),))),
),
_gate_benchmark(
"split-moving-divider",
"The first divider splits two and seven tracks, and the next splits five and four",
_band(350, 20),
_band(370, 20),
(
_Gate(420, 495, (_band(310, 20, 2), _band(410, 20, 7))),
_Gate(1000, 1110, (_band(330, 20, 5), _band(470, 20, 4))),
_Gate(1670, 1750, (_band(370, 20),)),
),
),
_gate_benchmark(
"split-three-branches",
"Nine tracks split into three apertures before two of the branches merge",
_band(380),
_band(380),
(
_Gate(460, 530, (_band(340, count=3), _band(434, count=3), _band(528, count=3))),
_Gate(1110, 1190, (_band(380, count=6), _band(528, count=3))),
_Gate(1710, 1790, (_band(380),)),
),
),
_gate_benchmark(
"split-mixed-width",
"The branch openings have distinct clearances for width-2, width-4, and width-6 tracks",
_band(270, 22),
_band(290, 22),
(
_Gate(450, 550, (_band(270, 22),)),
_Gate(1070, 1140, (_band(250, 22, 3), _band(336, 22, 3), _band(442, 22, 3))),
_Gate(1720, 1820, (_band(290, 22),)),
),
widths=(2, 2, 2, 4, 4, 4, 6, 6, 6),
),
_gate_benchmark(
"split-asymmetric-merge",
"Three unequal apertures close down to two as the central track joins the lower branch",
_band(470, 20),
_band(490, 20),
(
_Gate(470, 560, (_band(430, 20, 2), (510,), _band(570, 20, 6))),
_Gate(1000, 1080, (_band(470, 20, 3), _band(550, 20, 6))),
_Gate(1620, 1710, (_band(490, 20),)),
),
),
)
def _bypass_benchmarks() -> tuple[BenchmarkCase, ...]:
return (
_gate_benchmark(
"bypass-opposed-banks",
"A four-net westbound bank and a five-net eastbound bank detour around three free-standing obstacles",
_band(380),
_band(380),
(
_Gate(430, 620, (_band(340, count=4), _band(492, count=5))),
_Gate(1070, 1250, (_band(380, count=4), _band(472, count=5))),
_Gate(1720, 1890, (_band(340, count=4), _band(492, count=5))),
),
westbound=(0, 1, 2, 3),
edge_walls=False,
),
_gate_benchmark(
"bypass-interleaved-directions",
"Interleaved eastbound and westbound terminals detour around blockers with alternating cut points",
_band(370, 20),
_band(370, 20),
(
_Gate(410, 580, (_band(350, 20, 3), _band(450, 20, 6))),
_Gate(1010, 1200, (_band(350, 20, 5), _band(490, 20, 4))),
_Gate(1640, 1800, (_band(350, 20, 3), _band(450, 20, 6))),
),
westbound=(1, 3, 5, 7),
edge_walls=False,
),
_gate_benchmark(
"bypass-long-dividers",
"Long free-standing dividers leave short gaps to adjust lanes before the next bypass",
_band(300),
_band(340),
(
_Gate(290, 680, (_band(280, count=5), _band(430, count=4))),
_Gate(820, 1210, (_band(300, count=5), _band(430, count=4))),
_Gate(1380, 1790, (_band(320, count=5), _band(450, count=4))),
),
westbound=(5, 6, 7, 8),
edge_walls=False,
),
_gate_benchmark(
"bypass-three-branches",
"Two central obstacles per column create three detour choices with mixed travel directions",
_band(330, 20),
_band(330, 20),
(
_Gate(370, 530, (_band(290, 20, 3), _band(390, 20, 3), _band(490, 20, 3))),
_Gate(990, 1170, (_band(310, 20, 3), _band(390, 20, 3), _band(470, 20, 3))),
_Gate(1580, 1770, (_band(290, 20, 3), _band(390, 20, 3), _band(490, 20, 3))),
),
westbound=(3, 4, 5),
edge_walls=False,
),
_gate_benchmark(
"bypass-mixed-width",
"Wide westbound routes share the bypass area with six narrower eastbound routes",
_band(520, 22),
_band(500, 22),
(
_Gate(450, 600, (_band(500, 22, 3), _band(606, 22, 6))),
_Gate(1010, 1190, (_band(480, 22, 3), _band(606, 22, 6))),
_Gate(1650, 1810, (_band(480, 22, 6), _band(672, 22, 3))),
),
widths=(2, 2, 2, 3, 3, 3, 6, 6, 6),
westbound=(6, 7, 8),
edge_walls=False,
),
)
COMPLEX_BENCHMARK_CASES = (
*_ordered_bundle_benchmarks(),
*_serial_gate_benchmarks(),
*_split_gate_benchmarks(),
*_bypass_benchmarks(),
)
def _validation_benchmarks() -> tuple[BenchmarkCase, ...]:
return (
_gate_benchmark(
"holdout-staggered-five-gates",
"Staggered terminals and five unequal walls with alternating apertures",
_band(280, 16),
_band(320, 16),
tuple(
_Gate(left, right, (_band(y, 16),))
for left, right, y in ((300, 365, 300), (610, 710, 320), (930, 1020, 280), (1240, 1340, 300), (1760, 1840, 320))
),
terminal_stagger=31,
),
_gate_benchmark(
"holdout-staggered-opposed",
"Opposing travel with staggered banks and narrow gaps between three walls",
_band(520, 16),
_band(500, 16),
(_Gate(670, 820, (_band(520, 16),)), _Gate(925, 1040, (_band(480, 16),)), _Gate(1145, 1220, (_band(520, 16),))),
westbound=(0, 3, 5, 6),
terminal_stagger=24,
),
_gate_benchmark(
"holdout-uneven-split",
"Interleaved waveguide widths through three apertures and a single merge",
_band(360, 24),
_band(380, 24),
(
_Gate(440, 610, (_band(340, 24, 2), _band(408, 24, 4), _band(544, 24, 3))),
_Gate(1030, 1110, (_band(360, 24),)),
_Gate(1530, 1770, (_band(340, 24, 2), _band(408, 24, 4), _band(544, 24, 3))),
),
widths=(2, 5, 2, 4, 3, 6, 2, 5, 3),
terminal_stagger=23,
),
_gate_benchmark(
"holdout-uneven-bypass",
"Asymmetric free-standing obstacles with mixed widths and opposing launches",
_band(370, 24),
_band(350, 24),
(
_Gate(350, 510, (_band(330, 24, 4), _band(486, 24, 5))),
_Gate(810, 1150, (_band(350, 24, 3), _band(462, 24, 6))),
_Gate(1600, 1920, (_band(330, 24, 4), _band(486, 24, 5))),
),
widths=(4, 2, 6, 2, 5, 2, 3, 4, 2),
westbound=(1, 2, 7),
edge_walls=False,
terminal_stagger=41,
),
)
VALIDATION_BENCHMARK_CASES = _validation_benchmarks()
def _fallback_benchmarks() -> tuple[BenchmarkCase, ...]:
detours = _competing_detours()
detours = replace(detours, options=replace(detours.options, refinement=replace(detours.options.refinement, enabled=False)))
irregular = Polygon([(300, 190), (328, 170), (458, 182), (480, 225), (464, 415), (425, 440), (340, 460), (310, 381)])
channel = _gate_benchmark(
"fallback-beveled-gates",
"Nine nets through three staggered apertures with beveled obstacle faces",
_band(350),
_band(370),
(_Gate(470, 550, (_band(350),)), _Gate(1070, 1170, (_band(390),)), _Gate(1670, 1760, (_band(370),))),
)
beveled = tuple(
Polygon(
[
(left, low + 12),
(left + 12, low),
(right - 12, low),
(right, low + 12),
(right, high - 12),
(right - 12, high),
(left + 12, high),
(left, high - 12),
]
)
for left, low, right, high in (shape.bounds for shape in channel.problem.static_obstacles)
)
custom_nets = tuple(
NetSpec(f"turn_{i}", Port(20, y, 0), Port(x, y + 50, 90)) for i, (x, y) in enumerate(((240, 40), (200, 110), (160, 180)))
)
custom_routes = tuple(
(net.net_id, PathSeed((StraightSeed(net.target.x - net.start.x - 10), Bend90Seed(10, "CCW"), StraightSeed(40)))) for net in custom_nets
)
custom_problem = RoutingProblem((0, 0, 320, 300), custom_nets, (box(90, 65, 130, 90), box(110, 137, 160, 153)), clearance=4)
custom_options = _options(10)
custom_options = replace(custom_options, refinement=replace(custom_options.refinement, enabled=False))
custom_outline = Polygon([(0, -11), (11, -11), (11, 0), (9, 0), (9, -9), (0, -9)])
custom_proxy = box(0, -11, 11, 0)
proxy_options = replace(
custom_options,
search=replace(custom_options.search, bend_physical_geometry="arc", bend_proxy_geometry=custom_proxy, sbend_radii=()),
)
physical_options = replace(proxy_options, search=replace(proxy_options.search, bend_physical_geometry=custom_outline))
parallel = _open_or_shared_gate(
"fallback-partial-seed", "One complete and one partial caller seed alongside an unseeded net", (122, 142, 162)
)
complete_id, partial_id, _ = (net.net_id for net in parallel.problem.nets)
parallel_seeds = {complete_id: dict(parallel.reference_paths)[complete_id], partial_id: PathSeed((StraightSeed(45),))}
return (
replace(
detours,
name="fallback-irregular-island",
description="Two competing detours around an irregular polygon",
problem=replace(detours.problem, static_obstacles=(irregular,)),
),
replace(channel, problem=replace(channel.problem, static_obstacles=beveled)),
BenchmarkCase(
"fallback-custom-proxy", "Three arc bends with rectangular collision proxies", custom_problem, proxy_options, custom_routes
),
BenchmarkCase(
"fallback-custom-physical", "Three bends realized with an explicit physical polygon", custom_problem, physical_options, custom_routes
),
replace(
detours,
name="fallback-seeded-detour",
description="Caller supplies one feasible detour around a shared obstacle",
problem=replace(detours.problem, initial_paths=dict(detours.reference_paths[:1])),
),
replace(
parallel,
problem=replace(parallel.problem, initial_paths=parallel_seeds),
options=replace(parallel.options, refinement=replace(parallel.options.refinement, enabled=False)),
),
)
FALLBACK_BENCHMARK_CASES = _fallback_benchmarks()
def _external_benchmarks() -> tuple[BenchmarkCase, ...]:
fixture = Path(__file__).with_name("data") / "external_routing_benchmarks.json"
cases = []
for row in loads(fixture.read_text(encoding="utf-8"))["cases"]:
problem = RoutingProblem(
tuple(row["bounds"]),
tuple(NetSpec(net["id"], Port(*net["start"]), Port(*net["target"]), width=net["width"]) for net in row["nets"]),
tuple(Polygon(parts[0], parts[1:]) for parts in row["obstacles"]),
clearance=row["clearance"],
)
options = _options(row["bend_radius"])
options = replace(
options,
search=replace(options.search, min_straight_length=row["min_straight"]),
refinement=replace(options.refinement, enabled=False),
)
cases.append(BenchmarkCase(row["name"], f"Fixed-placement routing from {row['source']}", problem, options))
return tuple(cases)
EXTERNAL_BENCHMARK_CASES = _external_benchmarks()
_CROSSING_RADIUS = 25
_CROSSING_PORT_OFFSET = 14
_CROSSING_CLEARANCE = 4
def _crossing_keepout(x: int, y: int) -> Polygon:
# Recessed ports sit at the ends of access slots in a fixed 40-unit device.
# The remaining polygon reserves the crossing's core for the placed cell.
face, port = 20, _CROSSING_PORT_OFFSET
slot = _CROSSING_CLEARANCE + 2 # Unit guide half-width and access margin.
body = box(x - face, y - face, x + face, y + face)
for access in (
box(x - face, y - slot, x - port + slot, y + slot),
box(x + port - slot, y - slot, x + face, y + slot),
box(x - slot, y - face, x + slot, y - port + slot),
box(x - slot, y + port - slot, x + slot, y + face),
):
body = body.difference(access)
return body
def _crossing_leg_seed(net: NetSpec) -> PathSeed:
dx, dy = net.target.x - net.start.x, net.target.y - net.start.y
forward, offset = ((dx, dy), (dy, -dx), (-dx, -dy), (-dy, dx))[int(net.start.r) // 90]
if not offset:
return PathSeed((StraightSeed(forward),))
# Each stagger in the crossing grids uses a 20-unit offset. At radius
# 25 its forward span is exactly 40, leaving clearance at both ports.
assert abs(offset) == 20
assert forward > 40
first = (forward - 40) // 2
return PathSeed((StraightSeed(first), SBendSeed(offset, _CROSSING_RADIUS), StraightSeed(forward - first - 40)))
def _crossing_grid(
name: str,
description: str,
xs: tuple[int, ...],
ys: tuple[int, ...],
*,
staggered: bool = False,
) -> BenchmarkCase:
width, height = xs[-1] + 180, ys[-1] + 180
offset = _CROSSING_PORT_OFFSET
stagger = 20 if staggered else 0
cells = {
(row, column): (x + stagger * (row % 2), y + stagger * (column % 2)) for (row, y), (column, x) in product(enumerate(ys), enumerate(xs))
}
nets = []
def add_bank(label: str, ports: list[tuple[str, Port]], *, reverse: bool) -> None:
for (source, start), (sink, target) in zip(ports[::2], ports[1::2], strict=True):
if reverse:
nets.append(NetSpec(f"{label}:{sink}->{source}", target.translate(0, 0, 180), start.translate(0, 0, 180)))
else:
nets.append(NetSpec(f"{label}:{source}->{sink}", start, target))
for row in range(len(ys)):
left_y = cells[row, 0][1] - stagger
right_y = cells[row, len(xs) - 1][1] + stagger
ports = [("west", Port(30, left_y, 0))]
for column in range(len(xs)):
x, y = cells[row, column]
cell = f"x{row}_{column}"
ports.extend(((f"{cell}.w", Port(x - offset, y, 0)), (f"{cell}.e", Port(x + offset, y, 0))))
ports.append(("east", Port(width - 30, right_y, 0)))
add_bank(f"h{row}", ports, reverse=bool(row % 2))
for column in range(len(xs)):
bottom_x = cells[0, column][0] + stagger
top_x = cells[len(ys) - 1, column][0] - stagger
ports = [("south", Port(bottom_x, 30, 90))]
for row in range(len(ys)):
x, y = cells[row, column]
cell = f"x{row}_{column}"
ports.extend(((f"{cell}.s", Port(x, y - offset, 90)), (f"{cell}.n", Port(x, y + offset, 90))))
ports.append(("north", Port(top_x, height - 30, 90)))
add_bank(f"v{column}", ports, reverse=bool(column % 2))
problem = RoutingProblem(
(0, 0, width, height), tuple(nets), tuple(_crossing_keepout(*center) for center in cells.values()), clearance=_CROSSING_CLEARANCE
)
reference = tuple((net.net_id, _crossing_leg_seed(net)) for net in nets)
return BenchmarkCase(name, description, problem, _options(_CROSSING_RADIUS), reference)
def _crossing_obstructed_approaches() -> BenchmarkCase:
offset = _CROSSING_PORT_OFFSET
nets = (
NetSpec("west->x0.w", Port(30, 300, 0), Port(300 - offset, 300, 0)),
NetSpec("x0.e->south-east", Port(300 + offset, 300, 0), Port(530, 204, 270)),
NetSpec("south->x0.s", Port(280, 35, 90), Port(300, 300 - offset, 90)),
NetSpec("x0.n->north", Port(300, 300 + offset, 90), Port(300, 570, 90)),
)
west, east, south, north = nets
west_seed, _ = compile_waypoints(
((30, 300), (66, 300), (66, 215), (220, 215), (220, 300), (west.target.x, 300)), west.start, _CROSSING_RADIUS
)
east_seed, _ = compile_waypoints(((east.start.x, 300), (530, 300), (530, 204)), east.start, _CROSSING_RADIUS)
problem = RoutingProblem(
(0, 0, 600, 600),
nets,
(_crossing_keepout(300, 300), box(98, 265, 153, 335), box(402, 222, 468, 280)),
clearance=_CROSSING_CLEARANCE,
)
reference = (
(west.net_id, west_seed),
(east.net_id, east_seed),
(south.net_id, _crossing_leg_seed(south)),
(north.net_id, _crossing_leg_seed(north)),
)
return BenchmarkCase(
"crossing-obstructed-approaches",
"A fixed crossing with a blocked approach, offset launch, and turning exit",
problem,
_options(_CROSSING_RADIUS),
reference,
)
CROSSING_BENCHMARK_CASES = (
_crossing_grid("crossing-single", "Four straight legs at one placed crossing", (160,), (160,)),
_crossing_obstructed_approaches(),
_crossing_grid("crossing-aligned-grid", "Six placed crossings linked between opposite port banks", (160, 263, 366), (165, 285)),
_crossing_grid(
"crossing-staggered-grid",
"Nine staggered crossings with short S-bend links and offset terminal banks",
(175, 280, 395),
(160, 266, 388),
staggered=True,
),
)
DETOUR = _detour()
BENCHMARK_CASES = (
_open_or_shared_gate("open-bundle", "Four parallel routes on an open board", (122, 131, 140, 149)),
DETOUR,
_competing_detours(),
_open_or_shared_gate("shared-gate", "Three nets fit through one aperture", (140, 149, 158), wall=True),
_open_or_shared_gate("overloaded-gate", "Four nets contend for a three-track aperture", (131, 140, 149, 158), wall=True),
_mixed_width_gates(),
_blocking_straight(),
_blocking_straight(turn_limited=True),
_crossing_with_room(),
_offset_gates(),
_offset_gates(mirrored=True),
_reversed_heading(),
BenchmarkCase(
"terminal-bench-pair",
"Nets 03 and 04 share obstacles and compete for space",
replace(_TERMINAL_BENCH, nets=_TERMINAL_BENCH.nets[3:5]),
terminal_bench_options(),
),
BenchmarkCase("terminal-bench", "Nine-net PR 345 board with fixed east headings", _TERMINAL_BENCH, terminal_bench_options()),
*_TERMINAL_BENCH_POINT_CASES,
*COMPLEX_BENCHMARK_CASES,
)