Defer peer-reserved perimeter detours until needed

This commit is contained in:
Jan Petykiewicz 2026-11-30 11:59:45 -08:00
commit a15693ac4f
2 changed files with 38 additions and 13 deletions

View file

@ -320,8 +320,9 @@ class _AutomaticRun:
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(links[-1] if supplied is not None else [*links[-1], *footprints(net, detours)])
pairs: Iterable[tuple[tuple[PathSeed, BaseGeometry], tuple[PathSeed, BaseGeometry]]] = product(*candidates)
first, second = problem.nets first, second = problem.nets
def reserved_pairs() -> Iterator[tuple[tuple[PathSeed, BaseGeometry], tuple[PathSeed, BaseGeometry]]]:
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
@ -330,10 +331,9 @@ 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)))
reserved_pairs = product(detours, (peer_link,)) if index == 0 else product((peer_link,), detours) yield from product(detours, (peer_link,)) if index == 0 else product((peer_link,), detours)
pairs = chain(pairs, reserved_pairs)
for (first_seed, first_shape), (second_seed, second_shape) in pairs: for (first_seed, first_shape), (second_seed, second_shape) in chain(product(*candidates), 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

@ -6,6 +6,7 @@ from shapely.geometry import box
from inire import ( from inire import (
Bend90Seed, CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, StraightSeed, route, Bend90Seed, CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, StraightSeed, route,
) )
from inire.router import _automatic
OPTIONS = RoutingOptions( OPTIONS = RoutingOptions(
@ -78,3 +79,27 @@ def test_residual_doglegs_prefer_the_lattice_minimum_but_can_relax_it(forward, n
assert all(run.results_by_net[net.net_id].is_valid for net in problem.nets) assert all(run.results_by_net[net.net_id].is_valid for net in problem.nets)
lengths = [move.length for move in run.results_by_net["trunk"].as_seed().segments if isinstance(move, StraightSeed)] lengths = [move.length for move in run.results_by_net["trunk"].as_seed().segments if isinstance(move, StraightSeed)]
assert any(length < OPTIONS.search.min_straight_length for length in lengths) is needs_short_straight assert any(length < OPTIONS.search.min_straight_length for length in lengths) is needs_short_straight
@pytest.mark.parametrize("requires_reservation", [False, True])
def test_paired_perimeter_plans_peer_reserved_detours_only_as_a_fallback(monkeypatch, requires_reservation) -> None:
line = NetSpec("line", Port(5, 75, 0), Port(195, 75, 0))
returning = NetSpec("return", Port(130, 50, 0), Port(40, 50, 0))
obstacles = tuple(box(x, 100, x + 1, 104) for x in (*range(10, 29, 3), *range(142, 181, 4)))
problem = RoutingProblem((0, 0, 200, 130), (line, returning), obstacles, clearance=0)
original_detours = _automatic.perimeter_detour_candidates
calls = []
def detours(candidate_problem, search):
reserved = bool(candidate_problem.fixed_routes)
calls.append(reserved)
if reserved or not requires_reservation:
yield from original_detours(candidate_problem, search)
monkeypatch.setattr(_automatic, "perimeter_detour_candidates", detours)
run = route(problem, options=OPTIONS)
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])