From 6c5ee351c85b8068a67ede2d012ba93d2828b74b Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 30 Nov 2026 11:23:55 -0800 Subject: [PATCH] Choose eligible radii for constructive routing plans --- inire/router/_bundle.py | 10 ++++---- inire/router/_columns.py | 39 ++++++++++++++++++------------ inire/router/_perimeter.py | 33 ++++++++++++++----------- inire/tests/test_columns.py | 37 ++++++++++++++++++++++++++++ inire/tests/test_perimeter.py | 17 +++++++++++++ inire/tests/test_terminal_bench.py | 22 +++++++++++++++++ 6 files changed, 124 insertions(+), 34 deletions(-) diff --git a/inire/router/_bundle.py b/inire/router/_bundle.py index 226da3b..9fad88c 100644 --- a/inire/router/_bundle.py +++ b/inire/router/_bundle.py @@ -220,17 +220,17 @@ def plan_ordered_bundle(problem: RoutingProblem, search: SearchOptions, *, infer if not problem.nets: return problem _, physical_geometry = resolve_bend_geometry(search) - bend = min(search.bend_radii, default=0.0) + common_radii = {r for r in search.bend_radii if isfinite(r) and r > 0 and r == int(r) and r in search.sbend_radii} if ( - not isfinite(bend) or bend <= 0 or bend != int(bend) or bend not in search.sbend_radii - or physical_geometry != "arc" + not common_radii or physical_geometry != "arc" or search.has_applicable_sbend_outline(net.width for net in problem.nets) ): raise ValueError("Bundle routing needs a common integer radius for circular bends and S-bends") - radius = int(bend) width = problem.nets[0].width - if not bend_fits_width(radius, width): + bend = min((r for r in common_radii if bend_fits_width(r, width)), default=0.0) + if not bend: raise ValueError("Bundle bend radius must be at least half the net width") + radius = int(bend) margin = width / 2 + problem.clearance pitch = ceil(width + problem.clearance) + 1 floor_lane = ceil(problem.bounds[1] + width / 2) diff --git a/inire/router/_columns.py b/inire/router/_columns.py index 0897c8a..fff441a 100644 --- a/inire/router/_columns.py +++ b/inire/router/_columns.py @@ -12,6 +12,7 @@ from typing import TYPE_CHECKING from shapely.geometry import LineString +from inire.geometry.components import bend_fits_width from inire.geometry.intervals import Gap, open_intervals from inire.geometry.sbend import ExactSBendScanLimitError, exact_sbend_spans from inire.model import resolve_bend_geometry @@ -414,6 +415,7 @@ def _schedules( spans, radius, weights, max_usable=max_usable, weighted=weighted, sbend_penalty=sbend_penalty, allowed_offsets=allowed_offsets, ) needs = tuple((a + b) / 2 + clearance for a, b in pairwise(widths)) + straight_tracks = tuple(i for i, width in enumerate(widths) if not bend_fits_width(radius, width)) # The track order allows a local check of each neighboring pair. The # final router still verifies the exact arcs and every pair of routes. @@ -442,6 +444,8 @@ def _schedules( scores = [] for previous, histories in approaches.items(): offsets = tuple(b - a for a, b in zip(previous, row, strict=True)) + if straight_tracks and any(offsets[i] for i in straight_tracks): + continue sequences = [choose(d, window - 4) for d in offsets] if any(sequence is None for sequence in sequences): continue @@ -564,28 +568,33 @@ def column_seed_candidates( ) -> Iterator[dict[str, PathSeed]]: """Yield candidate routes from configured circular bends and problem geometry.""" _, physical_geometry = resolve_bend_geometry(search) - bend = min(search.sbend_radii, default=0.0) - if problem.initial_paths or physical_geometry != "arc" or not isfinite(bend) or bend <= 0 or bend != int(bend): + if problem.initial_paths or physical_geometry != "arc": + return + radii = sorted({int(r) for r in search.sbend_radii if isfinite(r) and r > 0 and r == int(r)}) + if not radii: return layout = _column_layout(problem) if layout is None: return - radius = int(bend) max_span = max(before - after - 4 for (_, after), (before, _) in pairwise(layout.spans)) max_offset = floor(problem.bounds[3]) - ceil(problem.bounds[1]) - try: - spans = exact_sbend_spans(radius, max_span=max_span, max_offset=max_offset, offsets=search.sbend_offsets) - except ExactSBendScanLimitError: - return - if not spans: - return + shifting_width = max((w for a, b, w in zip(layout.launch, layout.arrival, layout.widths, strict=True) if a != b), default=0.0) allowed_offsets = None if search.sbend_offsets is None else frozenset(search.sbend_offsets) - for moves in _column_sbend_schedules( - problem, layout, radius, spans, weights, weighted=weighted, sbend_penalty=sbend_penalty, allowed_offsets=allowed_offsets, - ): - seeds = _seeds(layout.tracks, layout.spans, moves, radius) - if all(search.seed_geometry_error(seed) is None for seed in seeds.values()): - yield seeds + for radius in radii: + if not bend_fits_width(radius, shifting_width): + continue + try: + spans = exact_sbend_spans(radius, max_span=max_span, max_offset=max_offset, offsets=search.sbend_offsets) + except ExactSBendScanLimitError: + continue + if not spans: + continue + for moves in _column_sbend_schedules( + problem, layout, radius, spans, weights, weighted=weighted, sbend_penalty=sbend_penalty, allowed_offsets=allowed_offsets, + ): + seeds = _seeds(layout.tracks, layout.spans, moves, radius) + if all(search.seed_geometry_error(seed) is None for seed in seeds.values()): + yield seeds def _jog_pivots( diff --git a/inire/router/_perimeter.py b/inire/router/_perimeter.py index a308c4e..39e89e1 100644 --- a/inire/router/_perimeter.py +++ b/inire/router/_perimeter.py @@ -7,11 +7,12 @@ from itertools import combinations, pairwise, product from math import ceil, floor, isfinite from typing import TYPE_CHECKING +from inire.geometry.components import bend90_uses_circular_geometry, bend_fits_width from inire.geometry.primitives import rotate_cardinal from inire.model import resolve_bend_geometry from inire.router._seed_materialization import materialize_path_seed from inire.router._waypoints import _direction, compile_waypoints, export_waypoints -from inire.seeds import PathSeed, StraightSeed +from inire.seeds import Bend90Seed, PathSeed, StraightSeed if TYPE_CHECKING: from collections.abc import Iterator @@ -118,7 +119,9 @@ def bundle_turnaround_candidates( returning, inferred = mismatches[0] if returning.start != inferred.start or returning.target.r != 0 or inferred.target.r != 180: return - radius = int(min(search.bend_radii)) + radius = next((int(move.radius) for move in proposal.initial_paths[returning.net_id].segments if isinstance(move, Bend90Seed)), None) + if radius is None: + return farthest = max(net.target.x for net in problem.nets) shelf = max((net for net in problem.nets if net.target.x == farthest), key=lambda net: net.target.y) if shelf == returning: @@ -189,15 +192,13 @@ def terminal_link_seed(net: NetSpec, search: SearchOptions) -> PathSeed | None: if start.r % 180 == target.r % 180: return None corner = (b[0], a[1]) if start.r % 180 == 0 else (a[0], b[1]) - radius = min(search.bend_radii, default=0.0) - if ( - not isfinite(radius) - or radius <= 0 - or corner in (a, b) - or _direction(a, corner) != start.r - or _direction(corner, b) != target.r - or min(abs(b[0] - a[0]), abs(b[1] - a[1])) < radius - ): + if corner in (a, b) or _direction(a, corner) != start.r or _direction(corner, b) != target.r: + return None + proxy, physical = resolve_bend_geometry(search) + circular = bend90_uses_circular_geometry(proxy, physical) + available = min(abs(b[0] - a[0]), abs(b[1] - a[1])) + radius = min((r for r in search.bend_radii if r <= available and (not circular or bend_fits_width(r, net.width))), default=0.0) + if not radius: return None seed, end = compile_waypoints((a, corner, b), start, radius) return seed if end == target else None @@ -235,14 +236,18 @@ def _return_detours( def perimeter_detour_candidates(problem: RoutingProblem, search: SearchOptions) -> Iterator[tuple[NetSpec, PathSeed]]: """Try four-turn detours around obstacles or behind east-facing ports.""" _, physical_geometry = resolve_bend_geometry(search) - bend = min(search.bend_radii, default=0.0) - if problem.initial_paths or physical_geometry != "arc" or not isfinite(bend) or bend <= 0 or bend != int(bend): + if problem.initial_paths or physical_geometry != "arc": + 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 obstacles = tuple(shape.bounds for shape in problem._blocking_polygons if not shape.is_empty) - radius = int(bend) for net in problem.nets: if net.start.r != 0 or net.target.r != 0: continue + radius = next((r for r in radii if bend_fits_width(r, net.width)), None) + if radius is None: + continue if net.start.x > net.target.x: yield from ((net, seed) for seed in _return_detours(problem, net, obstacles, radius)) continue diff --git a/inire/tests/test_columns.py b/inire/tests/test_columns.py index e626abc..2c63305 100644 --- a/inire/tests/test_columns.py +++ b/inire/tests/test_columns.py @@ -45,6 +45,43 @@ def test_automatic_column_routes_use_only_enabled_signed_sbends(sign) -> None: assert bends == (SBendSeed(sign * 2, 25),) * 5 +@pytest.mark.parametrize("smaller_radius", [10, 15]) +def test_column_routes_use_a_later_radius_when_the_smallest_cannot_make_the_shift(smaller_radius) -> None: + nets = tuple(NetSpec(str(y), Port(5, y, 0), Port(240, y + 20, 0), width=30) for y in (35, 110, 185)) + problem = RoutingProblem((0, 0, 250, 250), nets, (box(130, 0, 150, 10),), clearance=0) + options = RoutingOptions( + search=SearchOptions(node_limit=1, bend_radii=(), sbend_radii=(smaller_radius, 25), sbend_offsets=(20,)), + congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False), + refinement=RefinementOptions(enabled=False), + ) + + run = route(problem, options=options) + + assert run.automatic.selected_planner == "column" + assert run.metrics.nodes_expanded == 0 + for result in run.results_by_net.values(): + assert result.is_valid + assert {move.radius for move in result.as_seed().segments if isinstance(move, SBendSeed)} == {25} + + +def test_column_routes_allow_wide_straight_tracks_alongside_smaller_bends() -> None: + straight = NetSpec("wide", Port(5, 30, 0), Port(220, 30, 0), width=30) + shifted = tuple(NetSpec(str(y), Port(5, y, 0), Port(220, y + 8, 0)) for y in (90, 150)) + problem = RoutingProblem((0, 0, 230, 180), (straight, *shifted), (box(130, 0, 150, 10),), clearance=0) + options = RoutingOptions( + search=SearchOptions(node_limit=1, bend_radii=(), sbend_radii=(10, 25), sbend_offsets=(8,)), + congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False), + refinement=RefinementOptions(enabled=False), + ) + + run = route(problem, options=options) + + assert run.automatic.selected_planner == "column" + assert all(result.is_valid for result in run.results_by_net.values()) + for net in shifted: + assert {move.radius for move in run.results_by_net[net.net_id].as_seed().segments if isinstance(move, SBendSeed)} == {10} + + def test_sbend_sequence_skips_table_growth_for_shifts_larger_than_the_window(monkeypatch) -> None: make_state = Mock(wraps=_columns._BendState) monkeypatch.setattr(_columns, "_BendState", make_state) diff --git a/inire/tests/test_perimeter.py b/inire/tests/test_perimeter.py index fe2252b..171b93a 100644 --- a/inire/tests/test_perimeter.py +++ b/inire/tests/test_perimeter.py @@ -1,3 +1,6 @@ +from dataclasses import replace + +import pytest from shapely.geometry import box from inire import Bend90Seed, CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, route @@ -32,3 +35,17 @@ def test_forward_perimeter_detour_can_fit_at_exactly_four_radii() -> None: assert result.is_valid assert run.automatic.selected_planner == "perimeter" assert sum(isinstance(move, Bend90Seed) for move in result.as_seed().segments) == 4 + + +@pytest.mark.parametrize("smaller_radius", [10, 17.5]) +def test_perimeter_detours_choose_a_radius_usable_by_the_planner(smaller_radius) -> None: + net = NetSpec("wide", Port(20, 70, 0), Port(220, 70, 0), width=30) + problem = RoutingProblem((0, 0, 250, 180), (net,), (box(100, 40, 130, 100),), clearance=0) + options = replace(OPTIONS, search=replace(OPTIONS.search, bend_radii=(smaller_radius, 20))) + + run = route(problem, options=options) + + result = run.results_by_net[net.net_id] + assert result.is_valid + assert run.automatic.selected_planner == "perimeter" + assert {move.radius for move in result.as_seed().segments if isinstance(move, Bend90Seed)} == {20} diff --git a/inire/tests/test_terminal_bench.py b/inire/tests/test_terminal_bench.py index 0bf3f4d..23d3a1e 100644 --- a/inire/tests/test_terminal_bench.py +++ b/inire/tests/test_terminal_bench.py @@ -134,6 +134,28 @@ def test_bundle_preserves_explicit_terminal_headings_by_default() -> None: route(terminal_bench_problem(), options=options) +@pytest.mark.parametrize("mode", ["bundle", "automatic"]) +def test_bundle_and_turnaround_use_the_smallest_common_radius(mode) -> None: + options = terminal_bench_options(node_limit=1, max_iterations=1) + options = replace( + options, + search=replace(options.search, bend_radii=(BEND_RADIUS / 2, BEND_RADIUS)), + refinement=replace(options.refinement, enabled=False), + topology=TopologyOptions(mode=mode, infer_terminal_headings=(mode == "bundle")), + ) + + run = route(terminal_bench_problem(), options=options) + + assert all(result.is_valid for result in run.results_by_net.values()) + assert run.metrics.nodes_expanded == 0 + if mode == "automatic": + assert run.automatic.selected_planner == "bundle-turnaround" + for result in run.results_by_net.values(): + for move in result.as_seed().segments: + if isinstance(move, Bend90Seed | SBendSeed): + assert move.radius == BEND_RADIUS + + def test_bundle_plans_tracks_without_coincident_nets() -> None: problem = terminal_bench_problem() port = problem.nets[0].start