Complete feasible stress layouts with verified routing fallbacks

This commit is contained in:
Jan Petykiewicz 2026-11-26 15:23:24 -08:00
commit a2dfb73c33

View file

@ -5,13 +5,14 @@ from __future__ import annotations
from collections import defaultdict
from dataclasses import dataclass
from functools import lru_cache
from itertools import combinations_with_replacement, pairwise, product
from math import acos, ceil, cos, floor, gcd, isfinite, isqrt, sin
from itertools import combinations_with_replacement, islice, pairwise, product
from math import acos, ceil, comb, cos, floor, gcd, isfinite, isqrt, pi, sin
from typing import TYPE_CHECKING
from shapely.geometry import LineString, box
from inire.model import resolve_bend_geometry
from inire.router._waypoints import compile_waypoints
from inire.seeds import PathSeed, SBendSeed, StraightSeed
if TYPE_CHECKING:
@ -19,12 +20,15 @@ if TYPE_CHECKING:
from inire.geometry.primitives import Port
from inire.model import NetSpec, RoutingProblem, SearchOptions
from inire.router._waypoints import XY
from inire.seeds import PathSegmentSeed
Profile = tuple[int, ...]
Gap = tuple[float, float]
Window = tuple[int, int]
JogPivots = tuple[int | None, ...]
JogSchedule = tuple[float, tuple[Profile, ...], tuple[JogPivots, ...]]
@dataclass(frozen=True, slots=True)
@ -201,6 +205,8 @@ def _column_profiles(
wall_slack: int,
modulus: int,
reachability: tuple[tuple[Profile, ...], ...] = (),
*,
combinations_limit: int | None = None,
) -> list[Profile]:
profiles = set()
n = len(widths)
@ -222,7 +228,15 @@ def _column_profiles(
tuple(candidate[left:right] for candidate in reachability),
)
groups.append(band_cache[key])
profiles.update(tuple(y for group in chosen for y in group) for chosen in product(*groups))
combinations = product(*groups)
if combinations_limit is None:
profiles.update(tuple(y for group in chosen for y in group) for chosen in combinations)
else:
for chosen in islice(combinations, combinations_limit):
profiles.add(tuple(y for group in chosen for y in group))
combinations_limit -= 1
if combinations_limit == 0:
break
return sorted(profiles, key=lambda row: (sum(abs(a - b) for a, b in zip(row, desired, strict=True)), row))[:140]
@ -463,3 +477,132 @@ def column_seed_candidates(
if signature not in seen:
seen.add(signature)
yield _seeds(tracks, windows, moves, radius)
def _jog_pivots(
left: Profile,
right: Profile,
window: Window,
widths: tuple[float, ...],
clearance: float,
radius: int,
minimum: float,
) -> JogPivots | None:
# For an upward shift, move the upper tracks first. For a downward
# shift, move the lower tracks first. Opposite shifts have disjoint
# lateral sweeps when both endpoint rows preserve track order.
upward = [i for i in reversed(range(len(left))) if left[i] < right[i]]
downward = [i for i in range(len(left)) if right[i] < left[i]]
pivots: list[int | None] = [None] * len(left)
scheduled: list[int] = []
for i in (*upward, *downward):
low, high = sorted((left[i], right[i]))
straight = high - low - 2 * radius
if straight < 0 or 0 < straight < minimum:
return None
margin = ceil(radius + max(minimum, clearance + widths[i] / 2))
pivot = window[0] + margin
for j in scheduled:
pitch = clearance + (widths[i] + widths[j]) / 2
other_low, other_high = sorted((left[j], right[j]))
if min(high, other_high) + pitch > max(low, other_low):
previous = pivots[j]
assert previous is not None
pivot = max(pivot, previous + ceil(2 * radius + max(minimum, pitch)))
if pivot > window[1] - margin:
return None
pivots[i] = pivot
scheduled.append(i)
return tuple(pivots)
def _jog_schedules(
profiles: list[list[Profile]],
windows: tuple[Window, ...],
widths: tuple[float, ...],
clearance: float,
radius: int,
minimum: float,
) -> list[JogSchedule]:
start = profiles[0][0]
approaches: dict[Profile, list[JogSchedule]] = {start: [(0.0, (start,), ())]}
for window, rows in zip(windows, profiles[1:], strict=True):
after = {}
for row in rows:
proposals = []
for previous, histories in approaches.items():
pivots = _jog_pivots(previous, row, window, widths, clearance, radius, minimum)
if pivots is None:
continue
length = sum(abs(a - b) for a, b in zip(previous, row, strict=True))
length += sum(pivot is not None for pivot in pivots) * (pi - 2) * radius
proposals.extend((score + length, (*history, row), (*moves, pivots)) for score, history, moves in histories)
if proposals:
after[row] = sorted(proposals, key=lambda proposal: (proposal[0], proposal[1]))[:3]
approaches = after
if not approaches:
break
return approaches.get(profiles[-1][0], [])
def _jog_seeds(
tracks: tuple[_Track, ...], rows: tuple[Profile, ...], moves: tuple[JogPivots, ...], radius: int, search: SearchOptions
) -> dict[str, PathSeed] | None:
seeds = {}
for i, track in enumerate(tracks):
points: list[XY] = [(int(track.left.x), int(track.left.y))]
for (left, right), pivots in zip(pairwise(rows), moves, strict=True):
pivot = pivots[i]
if pivot is not None:
points.extend(((pivot, left[i]), (pivot, right[i])))
points.append((int(track.right.x), int(track.right.y)))
if track.westbound:
points.reverse()
seed, end = compile_waypoints(tuple(points), track.net.start, radius)
if end != track.net.target or any(
isinstance(move, StraightSeed) and not search.min_straight_length <= move.length <= search.max_straight_length
for move in seed.segments
):
return None
seeds[track.net.net_id] = seed
return seeds
def column_jog_candidates(problem: RoutingProblem, search: SearchOptions) -> Iterator[dict[str, PathSeed]]:
"""Allocate tracks through ordered apertures using staggered 90-degree jogs."""
if problem.initial_paths:
return
tracks = _ordered_tracks(problem)
if len(tracks) < 3 or any(not isfinite(track.net.width) or track.net.width <= 0 for track in tracks):
return
radii = sorted({int(r) for r in search.bend_radii if isfinite(r) and r > 0 and r == int(r)})
if not radii:
return
start, finish = max(int(track.left.x) for track in tracks), min(int(track.right.x) for track in tracks)
columns = _columns(problem, start, finish)
if not columns or any(comb(len(tracks) + len(column.openings) - 1, len(column.openings) - 1) > 4096 for column in columns):
return
launch = tuple(int(track.left.y) for track in tracks)
arrival = tuple(int(track.right.y) for track in tracks)
widths = tuple(track.net.width for track in tracks)
spans = ((start, start), *(column.span for column in columns), (finish, finish))
windows = tuple((after, before) for (_, after), (before, _) in pairwise(spans))
preferences = []
for column in columns:
fraction = (sum(column.span) / 2 - start) / (finish - start)
preferences.append(tuple(a + fraction * (b - a) for a, b in zip(launch, arrival, strict=True)))
for slack in (1, 0):
# Modulus one allows any integer track level, independent of the
# set of exact S-bend spans.
interior = [
_column_profiles(problem, column, widths, (0,) * len(tracks), desired, slack, slack, 1, combinations_limit=8192)
for column, desired in zip(columns, preferences, strict=True)
]
for radius in radii:
schedules = _jog_schedules(
[[launch], *interior, [arrival]], windows, widths, problem.clearance, radius, search.min_straight_length
)
for _, rows, moves in schedules:
seeds = _jog_seeds(tracks, rows, moves, radius, search)
if seeds is not None:
yield seeds