Limit exact S-bend span searches by lateral room

This commit is contained in:
Jan Petykiewicz 2026-11-30 09:52:16 -08:00
commit 5c8306c636
5 changed files with 45 additions and 10 deletions

View file

@ -18,19 +18,21 @@ def sbend_grid_span(offset: float, radius: float) -> int:
def exact_sbend_spans(
radius: int, *, max_span: int, offsets: tuple[float, ...] | None = None,
radius: int, *, max_span: int, max_offset: int | None = None, offsets: tuple[float, ...] | None = None,
) -> dict[int, int]:
"""Map allowed offset magnitudes to exact spans that fit the available length."""
"""Map allowed offset magnitudes to exact spans that fit the available room."""
if max_span < 1:
return {}
diameter = 2 * radius
# offset * (4 * radius - offset) = diameter² - (diameter - offset)².
remaining = diameter * diameter - max_span * max_span
max_offset = diameter - (isqrt(remaining - 1) + 1 if remaining > 0 else 1)
candidates = range(1, max_offset + 1) if offsets is None else sorted({int(abs(offset)) for offset in offsets})
offset_limit = diameter - (isqrt(remaining - 1) + 1 if remaining > 0 else 1)
if max_offset is not None:
offset_limit = min(offset_limit, max_offset)
candidates = range(1, offset_limit + 1) if offsets is None else sorted({int(abs(offset)) for offset in offsets})
spans = {}
for offset in candidates:
if not 0 < offset <= max_offset:
if not 0 < offset <= offset_limit:
continue
squared = offset * (4 * radius - offset)
span = isqrt(squared)

View file

@ -118,6 +118,8 @@ def _assign_shifts(
pitch: int,
floor_lane: int,
search: SearchOptions,
*,
max_offset: int,
) -> tuple[dict[str, list[_Shift]], int]:
short, lower, upper = layout.short, layout.lower, layout.upper
middle, *ascending, shelf = upper
@ -158,7 +160,7 @@ def _assign_shifts(
first_top = ceil(last_upper[3] + margin + 0.1)
top_lanes = [first_top + i * pitch for i in range(len(ascending))]
max_span = int(max(net.target.x for net in upper) - short.start.x)
offsets = tuple(exact_sbend_spans(radius, max_span=max_span, offsets=search.sbend_offsets))
offsets = tuple(exact_sbend_spans(radius, max_span=max_span, max_offset=max_offset, offsets=search.sbend_offsets))
shelf_options = [
int(shelf.target.y) + offset
for offset in offsets
@ -237,7 +239,8 @@ def plan_ordered_bundle(problem: RoutingProblem, search: SearchOptions, *, infer
# destination and of the first barrier, so the bundle launches east.
problem = _eastbound_launches(problem)
layout = _identify_layout(problem, margin)
shifts, launch = _assign_shifts(layout, radius, margin, pitch, floor_lane, search)
max_offset = floor(problem.bounds[3]) - ceil(problem.bounds[1])
shifts, launch = _assign_shifts(layout, radius, margin, pitch, floor_lane, search, max_offset=max_offset)
seeds = {}
updated_nets = []

View file

@ -544,7 +544,8 @@ def column_seed_candidates(
return
radius = int(bend)
max_span = max(before - after - 4 for (_, after), (before, _) in pairwise(layout.spans))
spans = exact_sbend_spans(radius, max_span=max_span, offsets=search.sbend_offsets)
max_offset = floor(problem.bounds[3]) - ceil(problem.bounds[1])
spans = exact_sbend_spans(radius, max_span=max_span, max_offset=max_offset, offsets=search.sbend_offsets)
if not spans:
return
allowed_offsets = None if search.sbend_offsets is None else frozenset(search.sbend_offsets)

View file

@ -3,7 +3,7 @@
from __future__ import annotations
from itertools import islice
from math import isfinite
from math import ceil, floor, isfinite
from typing import TYPE_CHECKING
from shapely import union_all
@ -66,6 +66,7 @@ def _candidate_jogs(
yield _path(dx)
return
direction = 1 if dy > 0 else -1
max_offset = floor(problem.bounds[2]) - ceil(problem.bounds[0])
for radius in radii:
reach = abs(dy) - 2 * radius
if reach < 0 or dx < 2 * radius:
@ -79,7 +80,7 @@ def _candidate_jogs(
continue
# Move the middle of the jog between two independently positioned
# turn lanes. The S-bend stays away from the terminal access cutouts.
spans = exact_sbend_spans(radius, max_span=int(reach), offsets=search.sbend_offsets)
spans = exact_sbend_spans(radius, max_span=int(reach), max_offset=max_offset, offsets=search.sbend_offsets)
for offset, span in reversed(tuple(spans.items())):
available = reach - span
for before in _shift_positions(problem, start.y, direction, radius, span, available):

View file

@ -4,6 +4,7 @@ import pytest
from shapely.geometry import box
from inire import CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SBendSeed, SearchOptions, route
from inire.geometry import sbend
from inire.geometry.sbend import exact_sbend_spans
from inire.router import _columns
from inire.router._columns import _jog_pivots, _schedules, column_seed_candidates
@ -77,6 +78,33 @@ def test_sbend_sequences_use_the_common_offset_stride() -> None:
def test_exact_sbend_spans_filter_by_window_and_configured_offsets() -> None:
assert exact_sbend_spans(25, max_span=40, offsets=(-20, 10, 40, 0)) == {10: 30, 20: 40}
assert exact_sbend_spans(25, max_span=39, offsets=(-20,)) == {}
assert exact_sbend_spans(25, max_span=40, max_offset=10, offsets=(-20, 10, 40, 0)) == {10: 30}
def test_wide_column_windows_only_scan_offsets_within_the_board_height(monkeypatch) -> None:
square_root = sbend.isqrt
scans = 0
def count_scan(value):
nonlocal scans
scans += 1
assert scans <= 120
return square_root(value)
monkeypatch.setattr(sbend, "isqrt", count_scan)
nets = tuple(NetSpec(str(y), Port(5, y, 0), Port(399_999_990, y, 0)) for y in (30, 60, 90))
problem = RoutingProblem((0, 0, 400_000_000, 120), nets, (box(190_000_000, 0, 190_000_020, 10),), clearance=0)
options = RoutingOptions(
search=SearchOptions(node_limit=1, bend_radii=(), sbend_radii=(100_000_000,)),
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
refinement=RefinementOptions(enabled=False),
)
run = route(problem, options=options)
assert 0 < scans <= 120
assert all(run.results_by_net[net.net_id].is_valid for net in nets)
assert run.metrics.nodes_expanded == 0
def test_large_unused_sbend_radius_skips_column_enumeration(monkeypatch) -> None: