diff --git a/inire/router/_automatic.py b/inire/router/_automatic.py index 7eb1db1..8d9de02 100644 --- a/inire/router/_automatic.py +++ b/inire/router/_automatic.py @@ -477,7 +477,7 @@ class _AutomaticRun: if target != len(problem.nets) or problem.initial_paths: return False with self._time("plan", planner="column-jogs"): - for seeds in islice(column_jog_candidates(problem, options.search), 8): + for seeds in column_jog_candidates(problem, options.search, limit_per_pass=8): trial = self._verify(replace(problem, initial_paths=seeds), "column-jogs") if len(trial.valid_costs) == target: trials.append(trial) diff --git a/inire/router/_columns.py b/inire/router/_columns.py index 556586c..e29ac2e 100644 --- a/inire/router/_columns.py +++ b/inire/router/_columns.py @@ -664,7 +664,7 @@ def _jog_schedules( def _jog_seeds( - tracks: tuple[_Track, ...], rows: tuple[Profile, ...], moves: tuple[JogPivots, ...], radius: int, search: SearchOptions + tracks: tuple[_Track, ...], rows: tuple[Profile, ...], moves: tuple[JogPivots, ...], radius: int, minimum: float, ) -> dict[str, PathSeed] | None: seeds = {} for i, track in enumerate(tracks): @@ -677,10 +677,7 @@ def _jog_seeds( if track.westbound: points.reverse() seed, end = compile_waypoints(tuple(points), track.net.start, radius) - if end != track.net.target or any( - isinstance(move, StraightSeed) and move.length < search.min_straight_length - for move in seed.segments - ): + if end != track.net.target or any(isinstance(move, StraightSeed) and move.length < minimum for move in seed.segments): return None seeds[track.net.net_id] = seed return seeds @@ -691,27 +688,19 @@ def _column_jog_schedules( layout: _ColumnLayout, radii: list[int], minimum: float, + profiles_for_slack: Callable[[int], list[list[Profile]]], ) -> Iterator[tuple[int, tuple[Profile, ...], tuple[JogPivots, ...]]]: - columns, widths, preferences = layout.columns, layout.widths, layout.preferences - launch, arrival = layout.launch, layout.arrival windows = tuple((after, before) for (_, after), (before, _) in pairwise(layout.spans)) for slack in (1, 0): - # Modulus one allows any integer track level, independent of the - # set of exact S-bend spans. - interior = [ - _column_profiles(problem, column, widths, (0,) * len(widths), desired, slack, slack, 1, combinations_limit=8192) - for column, desired in zip(columns, preferences, strict=True) - ] + profiles = profiles_for_slack(slack) for radius in radii: - schedules = _jog_schedules( - [[launch], *interior, [arrival]], windows, widths, problem.clearance, radius, minimum - ) + schedules = _jog_schedules(profiles, windows, layout.widths, problem.clearance, radius, minimum) for _, rows, moves in schedules: yield radius, rows, moves -def column_jog_candidates(problem: RoutingProblem, search: SearchOptions) -> Iterator[dict[str, PathSeed]]: +def column_jog_candidates(problem: RoutingProblem, search: SearchOptions, *, limit_per_pass: int | None = None) -> Iterator[dict[str, PathSeed]]: """Allocate tracks through ordered apertures using staggered 90-degree jogs.""" if problem.initial_paths: return @@ -721,7 +710,28 @@ def column_jog_candidates(problem: RoutingProblem, search: SearchOptions) -> Ite layout = _column_layout(problem) if layout is None: return - for radius, rows, moves in _column_jog_schedules(problem, layout, radii, search.min_straight_length): - seeds = _jog_seeds(layout.tracks, rows, moves, radius, search) - if seeds is not None: + @cache + def profiles_for_slack(slack: int) -> list[list[Profile]]: + widths = layout.widths + # Modulus one allows any integer track level. + interior = [ + _column_profiles(problem, column, widths, (0,) * len(widths), desired, slack, slack, 1, combinations_limit=8192) + for column, desired in zip(layout.columns, layout.preferences, strict=True) + ] + return [[layout.launch], *interior, [layout.arrival]] + + seen: set[tuple[tuple[str, PathSeed], ...]] = set() + + def seed_pass(minimum: float) -> Iterator[dict[str, PathSeed]]: + for radius, rows, moves in _column_jog_schedules(problem, layout, radii, minimum, profiles_for_slack): + seeds = _jog_seeds(layout.tracks, rows, moves, radius, minimum) + if seeds is None: + continue + signature = tuple(seeds.items()) + if signature in seen: + continue + seen.add(signature) yield seeds + + for minimum in dict.fromkeys((search.min_straight_length, 0.0)): + yield from islice(seed_pass(minimum), limit_per_pass) diff --git a/inire/tests/test_columns.py b/inire/tests/test_columns.py index f6667f7..519b9f8 100644 --- a/inire/tests/test_columns.py +++ b/inire/tests/test_columns.py @@ -324,3 +324,23 @@ def test_column_jog_seeds_can_use_straights_longer_than_the_lattice_scan() -> No for result in run.results_by_net.values(): assert result.is_valid assert any(isinstance(move, StraightSeed) and move.length > 40 for move in result.as_seed().segments) + + +@pytest.mark.parametrize(("shift", "needs_short_straights"), [(23, True), (27, False)]) +def test_column_jogs_prefer_the_lattice_minimum_but_can_relax_it(shift, needs_short_straights) -> None: + nets = tuple(NetSpec(str(y), Port(5, y, 0), Port(1000, y + shift, 0)) for y in (30, 85, 140)) + problem = RoutingProblem((0, 0, 1010, 180), nets, (box(980, 0, 990, 10),), clearance=0) + options = RoutingOptions( + search=SearchOptions(node_limit=1, bend_radii=(10,), sbend_radii=(), min_straight_length=5), + congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False), + refinement=RefinementOptions(enabled=False), + ) + + run = route(problem, options=options) + + assert run.automatic.selected_planner == "column-jogs" + assert run.metrics.nodes_expanded == 0 + for result in run.results_by_net.values(): + assert result.is_valid + lengths = [move.length for move in result.as_seed().segments if isinstance(move, StraightSeed)] + assert any(length < options.search.min_straight_length for length in lengths) is needs_short_straights