Retry residual doglegs with relaxed straight spacing

This commit is contained in:
Jan Petykiewicz 2026-11-30 11:59:00 -08:00
commit 466c68024f
3 changed files with 57 additions and 35 deletions

View file

@ -437,7 +437,7 @@ class _AutomaticRun:
seeds = {net_id: result.as_seed() for net_id, result in retained.items()} seeds = {net_id: result.as_seed() for net_id, result in retained.items()}
planner = "residual-dogleg" planner = "residual-dogleg"
with self._time("plan", planner=planner, policy=planner): with self._time("plan", planner=planner, policy=planner):
for seed in islice(residual_dogleg_candidates(residual, options.search), 8): for seed in residual_dogleg_candidates(residual, options.search, limit_per_pass=8):
candidate = replace(problem, initial_paths={**seeds, net.net_id: seed}) candidate = replace(problem, initial_paths={**seeds, net.net_id: seed})
trial = self._verify(candidate, planner, policy=planner) trial = self._verify(candidate, planner, policy=planner)
if len(trial.valid_costs) == len(problem.nets): if len(trial.valid_costs) == len(problem.nets):

View file

@ -3,7 +3,7 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import replace from dataclasses import replace
from itertools import combinations, pairwise, product from itertools import combinations, islice, pairwise, product
from math import ceil, floor, isfinite from math import ceil, floor, isfinite
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -21,7 +21,7 @@ if TYPE_CHECKING:
from inire.router._waypoints import XY from inire.router._waypoints import XY
def residual_dogleg_candidates(problem: RoutingProblem, search: SearchOptions) -> Iterator[PathSeed]: def residual_dogleg_candidates(problem: RoutingProblem, search: SearchOptions, *, limit_per_pass: int | None = None) -> Iterator[PathSeed]:
"""Try four-turn routes through interior gaps with the incumbent geometry locked.""" """Try four-turn routes through interior gaps with the incumbent geometry locked."""
if len(problem.nets) != 1 or problem.initial_paths: if len(problem.nets) != 1 or problem.initial_paths:
return return
@ -66,39 +66,44 @@ def residual_dogleg_candidates(problem: RoutingProblem, search: SearchOptions) -
lanes.update((floor(bottom), ceil(top))) lanes.update((floor(bottom), ceil(top)))
lanes = {y for y in lanes if floor_y + half_width <= y <= ceiling_y - half_width} lanes = {y for y in lanes if floor_y + half_width <= y <= ceiling_y - half_width}
ordered_lanes = sorted(lanes, key=lambda y: (abs(y - start_y) + abs(y - end_y), y)) ordered_lanes = sorted(lanes, key=lambda y: (abs(y - start_y) + abs(y - end_y), y))
minimum = search.min_straight_length radii = sorted({int(r) for r in search.bend_radii if isfinite(r) and r > 0 and r == int(r)})
remaining = 16384 seen: set[PathSeed] = set()
for radius in sorted({int(r) for r in search.bend_radii if isfinite(r) and r > 0 and r == int(r)}):
first, last = ceil(start_x + radius + minimum), floor(end_x - radius - minimum) def seed_pass(minimum: float) -> Iterator[PathSeed]:
if last - first < 2 * radius: remaining = 16384
continue for radius in radii:
pivots = {first, last} first, last = ceil(start_x + radius + minimum), floor(end_x - radius - minimum)
for left, _, right, _ in obstacles: if last - first < 2 * radius:
pivots.update((floor(left - radius), ceil(right + radius)))
pivots = sorted(x for x in pivots if first <= x <= last)
entrances = [x for x in pivots if straight_clear(start_x, start_y, x - radius, start_y)]
exits = [x for x in reversed(pivots) if straight_clear(x + radius, end_y, end_x, end_y)]
for y in ordered_lanes:
rises = [sorted((port_y, y)) for port_y in (start_y, end_y)]
if any(high - low < 2 * radius + minimum for low, high in rises):
continue continue
lower, upper = rises pivots = {first, last}
departures = [x for x in entrances if straight_clear(x, lower[0] + radius, x, lower[1] - radius)] for left, _, right, _ in obstacles:
arrivals = [x for x in exits if straight_clear(x, upper[0] + radius, x, upper[1] - radius)] pivots.update((floor(left - radius), ceil(right + radius)))
for a, b in product(departures, arrivals): ordered_pivots = sorted(x for x in pivots if first <= x <= last)
if not remaining: entrances = [x for x in ordered_pivots if straight_clear(start_x, start_y, x - radius, start_y)]
return exits = [x for x in reversed(ordered_pivots) if straight_clear(x + radius, end_y, end_x, end_y)]
remaining -= 1 for y in ordered_lanes:
if b - a < 2 * radius + minimum: rises = [sorted((port_y, y)) for port_y in (start_y, end_y)]
if any(high - low < 2 * radius + minimum for low, high in rises):
continue continue
if not straight_clear(a + radius, y, b - radius, y): lower, upper = rises
continue departures = [x for x in entrances if straight_clear(x, lower[0] + radius, x, lower[1] - radius)]
points = tuple(physical(x, h) for x, h in ((start_x, start_y), (a, start_y), (a, y), (b, y), (b, end_y), (end_x, end_y))) arrivals = [x for x in exits if straight_clear(x, upper[0] + radius, x, upper[1] - radius)]
seed, end = compile_waypoints(points, net.start, radius) for a, b in product(departures, arrivals):
if end == net.target and all( if not remaining:
not isinstance(move, StraightSeed) or minimum <= move.length for move in seed.segments return
): remaining -= 1
yield seed if b - a < 2 * radius + minimum or not straight_clear(a + radius, y, b - radius, y):
continue
local_points = ((start_x, start_y), (a, start_y), (a, y), (b, y), (b, end_y), (end_x, end_y))
seed, end = compile_waypoints(tuple(physical(x, h) for x, h in local_points), net.start, radius)
if end == net.target and seed not in seen and all(
not isinstance(move, StraightSeed) or minimum <= move.length for move in seed.segments
):
seen.add(seed)
yield seed
for minimum in dict.fromkeys((search.min_straight_length, 0.0)):
yield from islice(seed_pass(minimum), limit_per_pass)
def bundle_turnaround_candidates( def bundle_turnaround_candidates(

View file

@ -3,7 +3,9 @@ from dataclasses import replace
import pytest import pytest
from shapely.geometry import box from shapely.geometry import box
from inire import Bend90Seed, CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, route from inire import (
Bend90Seed, CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, StraightSeed, route,
)
OPTIONS = RoutingOptions( OPTIONS = RoutingOptions(
@ -61,3 +63,18 @@ def test_return_detours_snap_board_pivots_inward_before_generating_seeds() -> No
assert result.is_valid assert result.is_valid
assert run.automatic.selected_planner == "perimeter" assert run.automatic.selected_planner == "perimeter"
assert result.path[-1].end_port == net.target assert result.path[-1].end_port == net.target
@pytest.mark.parametrize(("forward", "needs_short_straight"), [(43, True), (60, False)])
def test_residual_doglegs_prefer_the_lattice_minimum_but_can_relax_it(forward, needs_short_straight) -> None:
trunk = NetSpec("trunk", Port(20, 50, 0), Port(20 + forward, 50, 0))
north = NetSpec("north", Port(10, 60, 90), Port(10, 100, 90))
rail = NetSpec("rail", Port(85, 100, 0), Port(110, 100, 0))
problem = RoutingProblem((0, 0, 120, 120), (trunk, north, rail), (box(40, 47, 43, 53),), clearance=0)
run = route(problem, options=OPTIONS)
assert run.automatic.selected_planner == "residual-dogleg"
assert all(run.results_by_net[net.net_id].is_valid for net in problem.nets)
lengths = [move.length for move in run.results_by_net["trunk"].as_seed().segments if isinstance(move, StraightSeed)]
assert any(length < OPTIONS.search.min_straight_length for length in lengths) is needs_short_straight