Generate ordinary perimeter pairs on demand

This commit is contained in:
Jan Petykiewicz 2026-11-30 12:15:30 -08:00
commit 2ffd12da5f

View file

@ -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})