Generate ordinary perimeter pairs on demand

This commit is contained in:
Jan Petykiewicz 2026-11-30 12:15:30 -08:00
commit 2ffd12da5f
2 changed files with 36 additions and 15 deletions

View file

@ -5,7 +5,7 @@ from __future__ import annotations
from contextlib import contextmanager, suppress from contextlib import contextmanager, suppress
from dataclasses import replace from dataclasses import replace
from functools import partial from functools import partial
from itertools import chain, islice, pairwise, product from itertools import chain, islice, pairwise
from time import perf_counter from time import perf_counter
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -57,6 +57,9 @@ if TYPE_CHECKING:
from inire.router.cost import CostEvaluator from inire.router.cost import CostEvaluator
from inire.seeds import PathSeed from inire.seeds import PathSeed
_Footprint = tuple[PathSeed, BaseGeometry]
_PerimeterPair = tuple[_Footprint, _Footprint]
_INITIAL_NODE_LIMIT = 500 _INITIAL_NODE_LIMIT = 500
_NEAR_PORT_NODE_LIMIT = 100 _NEAR_PORT_NODE_LIMIT = 100
@ -298,8 +301,7 @@ class _AutomaticRun:
return return
world = RoutingWorld.from_problem(problem, board=self.board) world = RoutingWorld.from_problem(problem, board=self.board)
def footprints(net: NetSpec, seeds: Iterable[PathSeed]) -> list[tuple[PathSeed, BaseGeometry]]: def footprints(net: NetSpec, seeds: Iterable[PathSeed]) -> Iterator[_Footprint]:
choices = []
for seed in seeds: for seed in seeds:
path = materialize_path_seed( path = materialize_path_seed(
seed, start=net.start, net_width=net.width, search=options.search, clearance=problem.clearance, bounds=problem.bounds, 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: if not path or path[-1].end_port != net.target or not world.verify_path_report(net.net_id, path).is_valid:
continue continue
footprint = shapely.union_all([shape for component in path for shape in component.physical_geometry]) footprint = shapely.union_all([shape for component in path for shape in component.physical_geometry])
choices.append((seed, footprint)) yield seed, footprint
return choices
candidates: list[list[tuple[PathSeed, BaseGeometry]]] = [] candidates: list[Iterator[_Footprint]] = []
links: list[list[tuple[PathSeed, BaseGeometry]]] = [] links: list[list[_Footprint]] = []
for net in problem.nets: for net in problem.nets:
supplied = problem.initial_paths.get(net.net_id) supplied = problem.initial_paths.get(net.net_id)
simple = supplied if supplied is not None else terminal_link_seed(net, options.search) 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={}) single = replace(problem, nets=(net,), initial_paths={})
detours = (seed for _, seed in islice(perimeter_detour_candidates(single, options.search), 32)) 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 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): for index, net in enumerate(problem.nets):
if net.net_id in problem.initial_paths or not links[1 - index]: if net.net_id in problem.initial_paths or not links[1 - index]:
continue continue
@ -331,9 +347,10 @@ class _AutomaticRun:
fixed = FixedRoute(peer.net_id, tuple(shapely.get_parts(peer_link[1]))) 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)) 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))) 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): if _violates_net_clearance(first_shape, second_shape, problem.clearance):
continue continue
plan = replace(problem, initial_paths={first.net_id: first_seed, second.net_id: second_seed}) plan = replace(problem, initial_paths={first.net_id: first_seed, second.net_id: second_seed})

View file

@ -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) problem = RoutingProblem((0, 0, 200, 130), (line, returning), obstacles, clearance=0)
original_detours = _automatic.perimeter_detour_candidates original_detours = _automatic.perimeter_detour_candidates
calls = [] calls = []
generated = []
def detours(candidate_problem, search): def detours(candidate_problem, search):
reserved = bool(candidate_problem.fixed_routes) reserved = bool(candidate_problem.fixed_routes)
calls.append(reserved) calls.append((candidate_problem.nets[0].net_id, reserved))
if reserved or not requires_reservation: 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) 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 run.automatic.selected_planner == "paired-perimeter"
assert all(result.is_valid for result in run.results_by_net.values()) 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]