inire/inire/tests/routing_gate_benchmarks.py

352 lines
14 KiB
Python

"""Serial, split, bypass, and held-out gate benchmark families."""
from __future__ import annotations
from dataclasses import dataclass, replace
from itertools import pairwise
from typing import TYPE_CHECKING
from shapely.geometry import box
from inire import NetSpec, PathSeed, Port, RoutingProblem, SBendSeed, StraightSeed
from inire.tests.routing_benchmark_case import BenchmarkCase, benchmark_options
if TYPE_CHECKING:
from inire.seeds import PathSegmentSeed
@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 = benchmark_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,
),
)
GATE_BENCHMARK_CASES = (
*_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()