Share the return candidate budget across lanes

This commit is contained in:
Jan Petykiewicz 2026-11-30 12:42:15 -08:00
commit 393c01704a
2 changed files with 32 additions and 8 deletions

View file

@ -245,6 +245,9 @@ def _return_detours(
high = max(start.y, target.y) + 2 * radius high = max(start.y, target.y) + 2 * radius
bottom = min(low, min((floor(bottom - margin) for _, bottom, _, _ in obstacles), default=low)) bottom = min(low, min((floor(bottom - margin) for _, bottom, _, _ in obstacles), default=low))
top = max(high, max((ceil(top + margin) for _, _, _, top in obstacles), default=high)) top = max(high, max((ceil(top + margin) for _, _, _, top in obstacles), default=high))
lanes = tuple(y for y in dict.fromkeys((bottom, top, low, high)) if ymin + half_width <= y <= ymax - half_width)
if not lanes:
return
left_edge = ceil(xmin + half_width) left_edge = ceil(xmin + half_width)
right_edge = floor(xmax - half_width) right_edge = floor(xmax - half_width)
pivots = {left_edge, right_edge, start.x + radius + 1, target.x - radius - 1} pivots = {left_edge, right_edge, start.x + radius + 1, target.x - radius - 1}
@ -252,10 +255,8 @@ def _return_detours(
pivots.update((floor(left - margin), ceil(right + margin))) pivots.update((floor(left - margin), ceil(right + margin)))
departures = sorted(x for x in pivots if start.x + radius <= x <= right_edge) departures = sorted(x for x in pivots if start.x + radius <= x <= right_edge)
arrivals = sorted((x for x in pivots if left_edge <= x <= target.x - radius), reverse=True) arrivals = sorted((x for x in pivots if left_edge <= x <= target.x - radius), reverse=True)
for y in dict.fromkeys((bottom, top, low, high)): # Try each lane for a pivot pair before spending the budget on more pivots.
if not ymin + half_width <= y <= ymax - half_width: for first, last, y in product(departures, arrivals, lanes):
continue
for first, last in product(departures, arrivals):
points = ((start.x, start.y), (first, start.y), (first, y), (last, y), (last, target.y), (target.x, target.y)) points = ((start.x, start.y), (first, start.y), (first, y), (last, y), (last, target.y), (target.x, target.y))
seed, end = compile_waypoints(points, start, radius) seed, end = compile_waypoints(points, start, radius)
if end == target: if end == target:

View file

@ -115,6 +115,29 @@ def test_return_detours_try_inner_lanes_when_keepouts_reach_board_edges() -> Non
assert run.results_by_net[net.net_id].is_valid assert run.results_by_net[net.net_id].is_valid
def test_return_detours_try_both_sides_within_the_candidate_budget(monkeypatch) -> None:
net = NetSpec("return", Port(130, 60, 0), Port(40, 60, 0))
obstacles = (box(135, 42, 199, 48), *(box(x, 100, x + 1, 104) for x in (*range(10, 29, 3), *range(142, 181, 4))))
problem = RoutingProblem((0, 0, 200, 130), (net,), obstacles, clearance=0)
original_detours = _automatic.perimeter_detour_candidates
generated = 0
def detours(candidate_problem, search):
nonlocal generated
for candidate in original_detours(candidate_problem, search):
generated += 1
yield candidate
monkeypatch.setattr(_automatic, "perimeter_detour_candidates", detours)
run = route(problem, options=OPTIONS)
result = run.results_by_net[net.net_id]
assert run.automatic.selected_planner == "perimeter"
assert result.is_valid
assert next(move.direction for move in result.as_seed().segments if isinstance(move, Bend90Seed)) == "CCW"
assert 0 < generated <= 32
@pytest.mark.parametrize(("forward", "needs_short_straight"), [(43, True), (60, False)]) @pytest.mark.parametrize(("forward", "needs_short_straight"), [(43, True), (60, False)])
def test_residual_doglegs_prefer_the_lattice_minimum_but_can_relax_it(forward, needs_short_straight) -> None: def test_residual_doglegs_prefer_the_lattice_minimum_but_can_relax_it(forward, needs_short_straight) -> None:
trunk = NetSpec("trunk", Port(20, 50, 0), Port(20 + forward, 50, 0)) trunk = NetSpec("trunk", Port(20, 50, 0), Port(20 + forward, 50, 0))