diff --git a/inire/router/_automatic.py b/inire/router/_automatic.py index d13ee20..b2d59c8 100644 --- a/inire/router/_automatic.py +++ b/inire/router/_automatic.py @@ -5,7 +5,7 @@ from __future__ import annotations from contextlib import contextmanager, suppress from dataclasses import replace from functools import partial -from itertools import chain, islice, pairwise, product +from itertools import chain, islice, pairwise from time import perf_counter from typing import TYPE_CHECKING @@ -57,6 +57,9 @@ if TYPE_CHECKING: from inire.router.cost import CostEvaluator from inire.seeds import PathSeed + _Footprint = tuple[PathSeed, BaseGeometry] + _PerimeterPair = tuple[_Footprint, _Footprint] + _INITIAL_NODE_LIMIT = 500 _NEAR_PORT_NODE_LIMIT = 100 @@ -298,8 +301,7 @@ class _AutomaticRun: return world = RoutingWorld.from_problem(problem, board=self.board) - def footprints(net: NetSpec, seeds: Iterable[PathSeed]) -> list[tuple[PathSeed, BaseGeometry]]: - choices = [] + def footprints(net: NetSpec, seeds: Iterable[PathSeed]) -> Iterator[_Footprint]: for seed in seeds: path = materialize_path_seed( seed, start=net.start, net_width=net.width, search=options.search, clearance=problem.clearance, bounds=problem.bounds, @@ -307,22 +309,36 @@ class _AutomaticRun: if not path or path[-1].end_port != net.target or not world.verify_path_report(net.net_id, path).is_valid: continue footprint = shapely.union_all([shape for component in path for shape in component.physical_geometry]) - choices.append((seed, footprint)) - return choices + yield seed, footprint - candidates: list[list[tuple[PathSeed, BaseGeometry]]] = [] - links: list[list[tuple[PathSeed, BaseGeometry]]] = [] + candidates: list[Iterator[_Footprint]] = [] + links: list[list[_Footprint]] = [] for net in problem.nets: supplied = problem.initial_paths.get(net.net_id) simple = supplied if supplied is not None else terminal_link_seed(net, options.search) - links.append(footprints(net, (simple,)) if simple is not None else []) + links.append(list(footprints(net, (simple,))) if simple is not None else []) + if supplied is not None: + candidates.append(iter(links[-1])) + continue single = replace(problem, nets=(net,), initial_paths={}) detours = (seed for _, seed in islice(perimeter_detour_candidates(single, options.search), 32)) - candidates.append(links[-1] if supplied is not None else [*links[-1], *footprints(net, detours)]) + candidates.append(chain(links[-1], footprints(net, detours))) first, second = problem.nets - def reserved_pairs() -> Iterator[tuple[tuple[PathSeed, BaseGeometry], tuple[PathSeed, BaseGeometry]]]: + def ordinary_pairs() -> Iterator[_PerimeterPair]: + left, right = candidates + checked_right: list[_Footprint] = [] + for left_candidate in left: + for right_candidate in checked_right: + yield left_candidate, right_candidate + for right_candidate in right: + checked_right.append(right_candidate) + yield left_candidate, right_candidate + if not checked_right: + return + + def reserved_pairs() -> Iterator[_PerimeterPair]: for index, net in enumerate(problem.nets): if net.net_id in problem.initial_paths or not links[1 - index]: continue @@ -331,9 +347,10 @@ class _AutomaticRun: fixed = FixedRoute(peer.net_id, tuple(shapely.get_parts(peer_link[1]))) reserved = replace(problem, nets=(net,), initial_paths={}, fixed_routes=(*problem.fixed_routes, fixed)) detours = footprints(net, (seed for _, seed in islice(perimeter_detour_candidates(reserved, options.search), 16))) - yield from product(detours, (peer_link,)) if index == 0 else product((peer_link,), detours) + for detour in detours: + yield (detour, peer_link) if index == 0 else (peer_link, detour) - for (first_seed, first_shape), (second_seed, second_shape) in chain(product(*candidates), reserved_pairs()): + for (first_seed, first_shape), (second_seed, second_shape) in chain(ordinary_pairs(), reserved_pairs()): if _violates_net_clearance(first_shape, second_shape, problem.clearance): continue plan = replace(problem, initial_paths={first.net_id: first_seed, second.net_id: second_seed}) diff --git a/inire/tests/test_perimeter.py b/inire/tests/test_perimeter.py index ab1f317..1c8d1ac 100644 --- a/inire/tests/test_perimeter.py +++ b/inire/tests/test_perimeter.py @@ -116,12 +116,15 @@ def test_paired_perimeter_plans_peer_reserved_detours_only_as_a_fallback(monkeyp problem = RoutingProblem((0, 0, 200, 130), (line, returning), obstacles, clearance=0) original_detours = _automatic.perimeter_detour_candidates calls = [] + generated = [] def detours(candidate_problem, search): reserved = bool(candidate_problem.fixed_routes) - calls.append(reserved) + calls.append((candidate_problem.nets[0].net_id, reserved)) if reserved or not requires_reservation: - yield from original_detours(candidate_problem, search) + for candidate in original_detours(candidate_problem, search): + generated.append(reserved) + yield candidate monkeypatch.setattr(_automatic, "perimeter_detour_candidates", detours) @@ -129,4 +132,5 @@ def test_paired_perimeter_plans_peer_reserved_detours_only_as_a_fallback(monkeyp assert run.automatic.selected_planner == "paired-perimeter" assert all(result.is_valid for result in run.results_by_net.values()) - assert calls == ([False, False, True] if requires_reservation else [False, False]) + assert calls == ([("return", False), ("return", True)] if requires_reservation else [("return", False)]) + assert generated == [requires_reservation]