diff --git a/inire/router/_automatic.py b/inire/router/_automatic.py index 8d9de02..2dd9fb9 100644 --- a/inire/router/_automatic.py +++ b/inire/router/_automatic.py @@ -437,7 +437,7 @@ class _AutomaticRun: seeds = {net_id: result.as_seed() for net_id, result in retained.items()} planner = "residual-dogleg" 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}) trial = self._verify(candidate, planner, policy=planner) if len(trial.valid_costs) == len(problem.nets): diff --git a/inire/router/_perimeter.py b/inire/router/_perimeter.py index a470329..efb690d 100644 --- a/inire/router/_perimeter.py +++ b/inire/router/_perimeter.py @@ -3,7 +3,7 @@ from __future__ import annotations from dataclasses import replace -from itertools import combinations, pairwise, product +from itertools import combinations, islice, pairwise, product from math import ceil, floor, isfinite from typing import TYPE_CHECKING @@ -21,7 +21,7 @@ if TYPE_CHECKING: 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.""" if len(problem.nets) != 1 or problem.initial_paths: return @@ -66,39 +66,44 @@ def residual_dogleg_candidates(problem: RoutingProblem, search: SearchOptions) - lanes.update((floor(bottom), ceil(top))) 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)) - minimum = search.min_straight_length - remaining = 16384 - 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) - if last - first < 2 * radius: - continue - pivots = {first, last} - for left, _, right, _ in obstacles: - 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): + radii = sorted({int(r) for r in search.bend_radii if isfinite(r) and r > 0 and r == int(r)}) + seen: set[PathSeed] = set() + + def seed_pass(minimum: float) -> Iterator[PathSeed]: + remaining = 16384 + for radius in radii: + first, last = ceil(start_x + radius + minimum), floor(end_x - radius - minimum) + if last - first < 2 * radius: continue - lower, upper = rises - departures = [x for x in entrances if straight_clear(x, lower[0] + radius, x, lower[1] - radius)] - arrivals = [x for x in exits if straight_clear(x, upper[0] + radius, x, upper[1] - radius)] - for a, b in product(departures, arrivals): - if not remaining: - return - remaining -= 1 - if b - a < 2 * radius + minimum: + pivots = {first, last} + for left, _, right, _ in obstacles: + pivots.update((floor(left - radius), ceil(right + radius))) + ordered_pivots = sorted(x for x in pivots if first <= x <= last) + entrances = [x for x in ordered_pivots if straight_clear(start_x, start_y, x - radius, start_y)] + exits = [x for x in reversed(ordered_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 - if not straight_clear(a + radius, y, b - radius, y): - continue - 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))) - seed, end = compile_waypoints(points, net.start, radius) - if end == net.target and all( - not isinstance(move, StraightSeed) or minimum <= move.length for move in seed.segments - ): - yield seed + lower, upper = rises + departures = [x for x in entrances if straight_clear(x, lower[0] + radius, x, lower[1] - radius)] + arrivals = [x for x in exits if straight_clear(x, upper[0] + radius, x, upper[1] - radius)] + for a, b in product(departures, arrivals): + if not remaining: + return + remaining -= 1 + 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( diff --git a/inire/tests/test_perimeter.py b/inire/tests/test_perimeter.py index 8a95105..7f842c0 100644 --- a/inire/tests/test_perimeter.py +++ b/inire/tests/test_perimeter.py @@ -3,7 +3,9 @@ from dataclasses import replace import pytest 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( @@ -61,3 +63,18 @@ def test_return_detours_snap_board_pivots_inward_before_generating_seeds() -> No assert result.is_valid assert run.automatic.selected_planner == "perimeter" 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