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

@ -1,9 +1,9 @@
"""Fit perpendicular jogs between nearby, parallel terminal columns."""
"""Construct short links from straight sections and tangent bends."""
from __future__ import annotations
from itertools import islice
from math import isfinite
from math import isfinite, sqrt
from typing import TYPE_CHECKING
from shapely import union_all
@ -12,16 +12,21 @@ from shapely.geometry import box
from inire.constants import TOLERANCE_LINEAR
from inire.geometry.collision import RoutingWorld
from inire.model import resolve_bend_geometry
from inire.results import RoutingResult
from inire.router._columns import _exact_spans
from inire.router._routing_policy import _local_delta
from inire.router._seed_materialization import materialize_path_seed
from inire.seeds import Bend90Seed, PathSeed, SBendSeed, StraightSeed
if TYPE_CHECKING:
from collections.abc import Iterator
from collections.abc import Iterable, Iterator
from shapely.geometry import Polygon
from shapely.geometry.base import BaseGeometry
from inire.geometry.components import ComponentResult
from inire.model import NetSpec, RoutingProblem, SearchOptions
from inire.router._routing_policy import Scorer
from inire.seeds import PathSegmentSeed
@ -103,25 +108,42 @@ def _pack_routes(
order = sorted(candidates, key=lambda net_id: len(candidates[net_id]))
chosen: dict[str, tuple[PathSeed, BaseGeometry]] = {}
remaining = 4096
choices = [iter(candidates[net_id]) for net_id in order]
index = 0
def add(index: int) -> bool:
nonlocal remaining
if index == len(order):
return True
while 0 <= index < len(order):
net_id = order[index]
for seed, footprint in candidates[net_id]:
chosen.pop(net_id, None)
for seed, footprint in choices[index]:
if remaining <= 0:
break
return None
remaining -= 1
if any(footprint.distance(other) < clearance - TOLERANCE_LINEAR for _, other in chosen.values()):
continue
chosen[net_id] = (seed, footprint)
if add(index + 1):
return True
del chosen[net_id]
return False
index += 1
break
else:
choices[index] = iter(candidates[net_id])
index -= 1
return {net_id: value[0] for net_id, value in chosen.items()} if add(0) else None
return {net_id: value[0] for net_id, value in chosen.items()} if index == len(order) else None
def _verified_candidates(
net: NetSpec,
world: RoutingWorld,
board: Polygon,
search: SearchOptions,
seeds: Iterable[PathSeed],
) -> Iterator[tuple[PathSeed, BaseGeometry, tuple[ComponentResult, ...]]]:
for seed in seeds:
path = materialize_path_seed(seed, start=net.start, net_width=net.width, search=search, clearance=world.clearance)
if path[-1].end_port != net.target or not world.verify_path_report(net.net_id, path).is_valid:
continue
footprint = union_all([polygon for component in path for polygon in component.physical_geometry])
if board.covers(footprint):
yield seed, footprint, path
def compact_port_seed_set(problem: RoutingProblem, search: SearchOptions) -> dict[str, PathSeed] | None:
@ -138,17 +160,65 @@ def compact_port_seed_set(problem: RoutingProblem, search: SearchOptions) -> dic
board = box(*problem.bounds)
candidates = {}
for net in problem.nets:
choices = []
for seed in islice(_candidate_jogs(net, problem, search, radii), 512):
path = materialize_path_seed(seed, start=net.start, net_width=net.width, search=search, clearance=problem.clearance)
if path[-1].end_port != net.target or not world.verify_path_report(net.net_id, path).is_valid:
continue
footprint = union_all([polygon for component in path for polygon in component.physical_geometry])
if board.covers(footprint):
choices.append((seed, footprint))
if len(choices) == 20:
break
jogs = islice(_candidate_jogs(net, problem, search, radii), 512)
choices = [(seed, footprint) for seed, footprint, _ in islice(_verified_candidates(net, world, board, search, jogs), 20)]
if not choices:
return None
candidates[net.net_id] = choices
return _pack_routes(candidates, problem.clearance)
def _shift_seeds(forward: int, offset: int, search: SearchOptions) -> Iterator[PathSeed]:
minimum, maximum = search.min_straight_length, search.max_straight_length
if offset == 0:
if minimum <= forward <= maximum:
yield _path(forward)
return
if search.sbend_offsets is not None and offset not in {round(value) for value in search.sbend_offsets}:
return
for radius in search.sbend_radii:
if not isfinite(radius) or not 0 < abs(offset) < 2 * radius:
continue
# The physical arc must meet the snapped straight at the join.
exact = sqrt(abs(offset) * (4 * radius - abs(offset)))
if not isfinite(exact):
continue
span = round(exact)
spare = forward - span
if spare < 0 or abs(exact - span) > 1e-9:
continue
placements = {i * spare // 8 for i in range(9)}
for lead in sorted(placements, key=lambda distance: (abs(2 * distance - spare), distance)):
tail = spare - lead
if all(not length or minimum <= length <= maximum for length in (lead, tail)):
yield _path(lead, SBendSeed(offset, radius), tail)
def short_shift_seed_set(problem: RoutingProblem, search: SearchOptions, score: Scorer) -> dict[str, PathSeed] | None:
"""Fit exact-span links between fixed ports across both axes of a layout."""
if problem.initial_paths or len(problem.nets) < 3 or len({net.start.r % 180 for net in problem.nets}) < 2:
return None
if any(net.start.r != net.target.r for net in problem.nets):
return None
deltas = [_local_delta(net) for net in problem.nets]
if any(forward <= 0 for forward, _ in deltas) or not any(offset for _, offset in deltas):
return None
seeds_by_net = [tuple(_shift_seeds(int(forward), int(offset), search)) for forward, offset in deltas]
if not all(seeds_by_net):
return None
world = RoutingWorld(clearance=problem.clearance, safety_zone_radius=problem.safety_zone_radius)
for obstacle in problem.static_obstacles:
world.add_static_obstacle(obstacle)
board = box(*problem.bounds)
candidates = {}
for net, seeds in zip(problem.nets, seeds_by_net, strict=True):
ranked = [
(score(RoutingResult(net.net_id, path)), seed, footprint)
for seed, footprint, path in _verified_candidates(net, world, board, search, seeds)
]
if not ranked:
return None
ranked.sort(key=lambda choice: choice[0])
candidates[net.net_id] = [(seed, footprint) for _, seed, footprint in ranked]
return _pack_routes(candidates, problem.clearance)