282 lines
11 KiB
Python
282 lines
11 KiB
Python
"""Feasible fixed placements that stress automatic routing and refinement."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, replace
|
|
from itertools import pairwise
|
|
from typing import TYPE_CHECKING
|
|
|
|
from shapely.geometry import Polygon, box
|
|
|
|
from inire import NetSpec, Port, RoutingProblem
|
|
from inire.router._waypoints import _direction, compile_waypoints
|
|
from inire.tests.routing_benchmark_case import BenchmarkCase, benchmark_options
|
|
from inire.tests.routing_crossing_benchmarks import _crossing_keepout
|
|
|
|
if TYPE_CHECKING:
|
|
from inire.router._waypoints import XY
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class _Track:
|
|
name: str
|
|
points: tuple[XY, ...]
|
|
width: float = 2
|
|
radius: float | None = None
|
|
|
|
|
|
def _waypoint_case(
|
|
name: str,
|
|
description: str,
|
|
bounds: tuple[int, int, int, int],
|
|
obstacles: tuple[Polygon, ...],
|
|
tracks: tuple[_Track, ...],
|
|
*,
|
|
radius: float = 20,
|
|
sbend_radius: float | None = None,
|
|
clearance: float = 5,
|
|
extra_bend_radius: float | None = None,
|
|
) -> BenchmarkCase:
|
|
nets = []
|
|
reference = []
|
|
for track in tracks:
|
|
heading = _direction(*track.points[:2])
|
|
assert heading is not None
|
|
start = Port(*track.points[0], heading)
|
|
seed, end = compile_waypoints(track.points, start, track.radius if track.radius is not None else radius)
|
|
nets.append(NetSpec(track.name, start, end, track.width))
|
|
reference.append((track.name, seed))
|
|
options = benchmark_options(radius, sbend_radius)
|
|
if extra_bend_radius is not None:
|
|
options = replace(options, search=replace(options.search, bend_radii=(radius, extra_bend_radius)))
|
|
problem = RoutingProblem(bounds, tuple(nets), obstacles, clearance=clearance)
|
|
return BenchmarkCase(name, description, problem, options, tuple(reference))
|
|
|
|
|
|
def _alternating_doglegs() -> BenchmarkCase:
|
|
tracks = []
|
|
for i in range(3):
|
|
launch, upper, lower, arrival = 147 + 17 * i, 248 + 18 * i, 88 + 18 * i, 173 + 17 * i
|
|
rise, drop, recover = 208 - 45 * i, 446 + 45 * i, 800 - 45 * i
|
|
tracks.append(_Track(
|
|
f"track_{i}",
|
|
(
|
|
(34, launch), (rise, launch), (rise, upper), (drop, upper), (drop, lower),
|
|
(recover, lower), (recover, arrival), (1030, arrival),
|
|
),
|
|
width=(2, 4, 3)[i],
|
|
))
|
|
return _waypoint_case(
|
|
"stress-refine-alternating-doglegs",
|
|
"Two alternating walls force six-bend routes with overlapping refinement windows and mixed widths",
|
|
(0, 0, 1080, 410),
|
|
(box(284, 0, 379, 238), box(594, 139, 682, 410)),
|
|
tuple(tracks),
|
|
extra_bend_radius=24,
|
|
)
|
|
|
|
|
|
def _return_shelf() -> BenchmarkCase:
|
|
return _waypoint_case(
|
|
"stress-refine-return-shelf",
|
|
"A folded return skirts a shelf above a neighboring route, with tempting shortcuts across its own arms",
|
|
(0, 0, 790, 480),
|
|
(box(428, 320, 502, 480), box(250, 145, 340, 168)),
|
|
(
|
|
_Track(
|
|
"folded_return",
|
|
(
|
|
(640, 155), (720, 155), (720, 392), (540, 392), (540, 300),
|
|
(390, 300), (390, 392), (150, 392), (150, 230), (555, 230),
|
|
),
|
|
),
|
|
_Track("shelf_edge", ((201, 272), (595, 272)), width=4),
|
|
_Track("lower_return", ((585, 178), (64, 178))),
|
|
_Track("east_shelf", ((675, 438), (610, 438))),
|
|
),
|
|
extra_bend_radius=27,
|
|
)
|
|
|
|
|
|
def _straight_decoy() -> BenchmarkCase:
|
|
return _waypoint_case(
|
|
"stress-retry-straight-decoy",
|
|
"A tempting straight trunk seals two perpendicular branches unless released; the lower bypass has limited room",
|
|
(0, 0, 760, 440),
|
|
(box(0, 222, 15, 238), box(745, 222, 760, 238), box(216, 114, 282, 186), box(463, 114, 539, 199)),
|
|
(
|
|
_Track("trunk", ((25, 230), (169, 230), (169, 99), (610, 99), (610, 230), (735, 230)), width=4),
|
|
_Track("branch_west", ((326, 144), (326, 339))),
|
|
_Track("branch_east", ((398, 358), (398, 170)), width=3),
|
|
_Track("lower_rail", ((58, 79), (708, 79))),
|
|
),
|
|
)
|
|
|
|
|
|
def _compact_shift_bank() -> BenchmarkCase:
|
|
return _waypoint_case(
|
|
"stress-retry-compact-shift-bank",
|
|
"One port pair has only 66 forward units and a large sideways shift; longer peers have obstructed approaches",
|
|
(0, 0, 720, 720),
|
|
(
|
|
box(0, 230, 720, 242), box(0, 389, 720, 401), box(0, 549, 720, 561),
|
|
box(113, 96, 202, 158), box(258, 264, 345, 298), box(337, 426, 447, 460), box(285, 581, 402, 616),
|
|
),
|
|
(
|
|
_Track("short", ((45, 117), (78, 117), (78, 202), (111, 202)), width=3),
|
|
_Track("middle", ((33, 274), (190, 274), (190, 347), (650, 347)), width=4),
|
|
_Track("upper", ((56, 445), (251, 445), (251, 513), (670, 513))),
|
|
_Track("top", ((63, 598), (196, 598), (196, 669), (630, 669))),
|
|
),
|
|
)
|
|
|
|
|
|
def _mixed_port_bank() -> BenchmarkCase:
|
|
return _waypoint_case(
|
|
"stress-retry-mixed-port-bank",
|
|
"Compact shifts, a perpendicular exit, and a return share a board with narrow turn access",
|
|
(0, 0, 680, 540),
|
|
(
|
|
box(133, 53, 194, 131), box(452, 221, 498, 291), box(564, 171, 624, 230),
|
|
Polygon(((190, 185), (339, 175), (353, 218), (315, 283), (199, 290))),
|
|
box(256, 302, 348, 337), box(108, 408, 310, 443),
|
|
),
|
|
(
|
|
_Track("east_compact", ((40, 75), (80, 75), (80, 158), (115, 158)), width=3),
|
|
_Track("west_compact", ((590, 261), (550, 261), (550, 182), (516, 182)), width=4),
|
|
_Track("turning_exit", ((290, 50), (290, 113), (437, 113))),
|
|
_Track("upper_shift", ((40, 315), (196, 315), (196, 395), (450, 395))),
|
|
_Track("return", ((535, 475), (60, 475))),
|
|
),
|
|
)
|
|
|
|
|
|
def _wall(left: int, right: int, height: int, openings: tuple[tuple[int, int], ...]) -> tuple[Polygon, ...]:
|
|
return tuple(box(left, lower, right, upper) for (_, lower), (upper, _) in pairwise(((0, 0), *openings, (height, height))))
|
|
|
|
|
|
def _passage_tracks(
|
|
banks: tuple[tuple[int, ...], ...],
|
|
turn_columns: tuple[int, ...],
|
|
finish: int,
|
|
widths: tuple[int, ...],
|
|
*,
|
|
westbound: frozenset[int] = frozenset(),
|
|
) -> tuple[_Track, ...]:
|
|
tracks = []
|
|
for i, levels in enumerate(zip(*banks, strict=True)):
|
|
points = [(30, levels[0])]
|
|
for column, (start, end) in zip(turn_columns, pairwise(levels), strict=True):
|
|
# The lower tracks turn later while shifting upward.
|
|
x = column + 46 * (len(widths) - 1 - i)
|
|
points.extend(((x, start), (x, end)))
|
|
points.append((finish, levels[-1]))
|
|
tracks.append(_Track(f"lane_{i}", tuple(reversed(points)) if i in westbound else tuple(points), width=widths[i]))
|
|
return tuple(tracks)
|
|
|
|
|
|
def _width_key() -> BenchmarkCase:
|
|
return _waypoint_case(
|
|
"stress-passage-width-key",
|
|
"A one-track slit excludes the widest guides while five guides share the upper aperture and its turn banks",
|
|
(0, 0, 1210, 720),
|
|
_wall(440, 492, 720, ((246, 260), (340, 443))),
|
|
_passage_tracks(
|
|
((64, 81, 99, 118, 135, 153), (253, 354, 372, 390, 408, 426), (498, 515, 533, 551, 569, 587)),
|
|
(100, 635),
|
|
1190,
|
|
(2, 7, 4, 8, 5, 6),
|
|
westbound=frozenset({0, 2, 5}),
|
|
),
|
|
)
|
|
|
|
|
|
def _split_merge() -> BenchmarkCase:
|
|
return _waypoint_case(
|
|
"stress-passage-split-merge",
|
|
"Two staggered three-track apertures feed a common passage with opposing launches and mixed guide widths",
|
|
(0, 0, 1710, 880),
|
|
(*_wall(422, 470, 880, ((243, 293), (374, 439))), *_wall(930, 982, 880, ((523, 618),))),
|
|
_passage_tracks(
|
|
(
|
|
(53, 70, 87, 105, 123, 141), (252, 269, 285, 384, 403, 426),
|
|
(534, 548, 562, 577, 592, 606), (679, 697, 716, 734, 752, 772),
|
|
),
|
|
(104, 596, 1110),
|
|
1670,
|
|
(3, 5, 2, 4, 6, 3),
|
|
westbound=frozenset({0, 3, 5}),
|
|
),
|
|
)
|
|
|
|
|
|
def _competing_sbends() -> BenchmarkCase:
|
|
return _waypoint_case(
|
|
"stress-sbend-competing-slots",
|
|
"Opposed exact-span S-bends compete for placement room while two perpendicular links use a fixed crossing",
|
|
(0, 0, 450, 320),
|
|
(box(104, 130, 120, 135), _crossing_keepout(350, 160)),
|
|
(
|
|
_Track("lower_east", ((35, 112), (155, 112), (195, 132), (235, 132)), width=3),
|
|
_Track("upper_west", ((235, 141), (95, 141), (55, 121), (35, 121)), width=3),
|
|
_Track("south_access", ((330, 52), (330, 74), (350, 114), (350, 146))),
|
|
_Track("north_access", ((350, 174), (350, 212), (330, 252), (330, 284))),
|
|
),
|
|
radius=25,
|
|
clearance=4,
|
|
)
|
|
|
|
|
|
def _off_lattice_crossing() -> BenchmarkCase:
|
|
return _waypoint_case(
|
|
"stress-crossing-off-lattice-slot",
|
|
"An odd offset defeats exact S-bend placement while a second approach must bend inside a short launch pocket",
|
|
(0, 0, 500, 460),
|
|
(_crossing_keepout(260, 230), box(245, 272, 273, 317), box(145, 193, 176, 214)),
|
|
(
|
|
_Track("west_odd_shift", ((34, 213), (106, 213), (106, 230), (246, 230))),
|
|
_Track("east_exact_shift", ((274, 230), (330, 230), (370, 250), (465, 250)), radius=25),
|
|
_Track("north_pocket", ((260, 244), (260, 263), (309, 263), (309, 344), (260, 344), (260, 429)), width=3),
|
|
_Track("south_exact_shift", ((240, 30), (240, 115), (260, 155), (260, 216)), radius=25),
|
|
),
|
|
radius=6,
|
|
sbend_radius=25,
|
|
clearance=4,
|
|
extra_bend_radius=15,
|
|
)
|
|
|
|
|
|
def _crossings_with_detour() -> BenchmarkCase:
|
|
return _waypoint_case(
|
|
"stress-crossings-shared-outside-detour",
|
|
"Two preplaced crossings have easy local links, but a long net must detour around the entire south approach bank",
|
|
(0, 0, 625, 397),
|
|
(_crossing_keepout(220, 180), _crossing_keepout(355, 180), box(160, 69, 415, 81), box(102, 216, 144, 261)),
|
|
(
|
|
_Track("west_link", ((32, 160), (113, 160), (153, 180), (206, 180)), radius=25),
|
|
_Track("device_link", ((234, 180), (341, 180))),
|
|
_Track("east_link", ((369, 180), (459, 180), (499, 200), (581, 200)), radius=25),
|
|
_Track("west_south", ((220, 87), (220, 166))),
|
|
_Track("east_south", ((355, 166), (355, 90))),
|
|
_Track("west_north", ((220, 194), (220, 224), (200, 264), (200, 317)), radius=25),
|
|
_Track("east_north", ((375, 315), (375, 265), (355, 225), (355, 194)), radius=25),
|
|
_Track("outside_lower", ((35, 130), (100, 130), (100, 51), (505, 51), (505, 130), (590, 130)), width=4),
|
|
_Track("outside_upper", ((589, 353), (33, 353))),
|
|
),
|
|
sbend_radius=25,
|
|
clearance=4,
|
|
)
|
|
|
|
|
|
STRESS_BENCHMARK_CASES = (
|
|
_alternating_doglegs(),
|
|
_return_shelf(),
|
|
_straight_decoy(),
|
|
_compact_shift_bank(),
|
|
_mixed_port_bank(),
|
|
_width_key(),
|
|
_split_merge(),
|
|
_competing_sbends(),
|
|
_off_lattice_crossing(),
|
|
_crossings_with_detour(),
|
|
)
|