diff --git a/inire/router/_bundle.py b/inire/router/_bundle.py index 9fad88c..e6a6262 100644 --- a/inire/router/_bundle.py +++ b/inire/router/_bundle.py @@ -227,10 +227,9 @@ def plan_ordered_bundle(problem: RoutingProblem, search: SearchOptions, *, infer ): raise ValueError("Bundle routing needs a common integer radius for circular bends and S-bends") width = problem.nets[0].width - bend = min((r for r in common_radii if bend_fits_width(r, width)), default=0.0) - if not bend: + radii = sorted(int(r) for r in common_radii if bend_fits_width(r, width)) + if not radii: 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) @@ -239,6 +238,31 @@ def plan_ordered_bundle(problem: RoutingProblem, search: SearchOptions, *, infer # destination and of the first barrier, so the bundle launches east. problem = _eastbound_launches(problem) layout = _identify_layout(problem, margin) + first_error = None + for radius in radii: + try: + return _build_bundle( + problem, layout, search, radius, margin=margin, pitch=pitch, floor_lane=floor_lane, + infer_terminal_headings=infer_terminal_headings, + ) + except ValueError as error: + if first_error is None: + first_error = error + assert first_error is not None + raise first_error + + +def _build_bundle( + problem: RoutingProblem, + layout: _Layout, + search: SearchOptions, + radius: int, + *, + margin: float, + pitch: int, + floor_lane: int, + infer_terminal_headings: bool, +) -> RoutingProblem: max_offset = floor(problem.bounds[3]) - ceil(problem.bounds[1]) shifts, launch = _assign_shifts(layout, radius, margin, pitch, floor_lane, search, max_offset=max_offset) diff --git a/inire/tests/test_terminal_bench.py b/inire/tests/test_terminal_bench.py index 23d3a1e..ef0f2a6 100644 --- a/inire/tests/test_terminal_bench.py +++ b/inire/tests/test_terminal_bench.py @@ -156,6 +156,29 @@ def test_bundle_and_turnaround_use_the_smallest_common_radius(mode) -> None: assert move.radius == BEND_RADIUS +@pytest.mark.parametrize("mode", ["bundle", "automatic"]) +def test_bundle_retries_the_next_common_radius_when_the_shelf_cannot_use_the_first(mode) -> None: + options = terminal_bench_options(node_limit=1, max_iterations=1) + radii = (BEND_RADIUS / 2, BEND_RADIUS) + options = replace( + options, + search=replace(options.search, bend_radii=radii, sbend_radii=radii, sbend_offsets=(-40, 40)), + 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" + assert { + move.radius for result in run.results_by_net.values() for move in result.as_seed().segments + if isinstance(move, Bend90Seed | SBendSeed) + } == {BEND_RADIUS} + + def test_bundle_plans_tracks_without_coincident_nets() -> None: problem = terminal_bench_problem() port = problem.nets[0].start