Honor signed offsets when planning column S-bends

This commit is contained in:
Jan Petykiewicz 2026-11-30 09:51:18 -08:00
commit 3ffe7cfc5d
2 changed files with 53 additions and 7 deletions

View file

@ -292,7 +292,21 @@ def _bend_sequences(
*,
weighted: bool,
sbend_penalty: float,
allowed_offsets: frozenset[float] | None = None,
) -> Callable[[int, int], _BendSequence | None]:
if allowed_offsets is not None:
positive = {d: span for d, span in spans.items() if d in allowed_offsets}
negative = {d: span for d, span in spans.items() if -d in allowed_offsets}
if positive != negative:
choose_up = _bend_sequences(positive, radius, weights, weighted=weighted, sbend_penalty=sbend_penalty)
choose_down = _bend_sequences(negative, radius, weights, weighted=weighted, sbend_penalty=sbend_penalty)
def choose_signed(delta: int, usable: int) -> _BendSequence | None:
return (choose_down if delta < 0 else choose_up)(delta, usable)
return choose_signed
spans = positive
# Each S-bend replaces a straight of its forward span in this window.
bend_costs = {
d: sbend_penalty + weights["sbend"] * 2 * radius * acos(1 - d / (2 * radius)) - weights["straight"] * span for d, span in spans.items()
@ -372,8 +386,9 @@ def _schedules(
*,
weighted: bool,
sbend_penalty: float,
allowed_offsets: frozenset[float] | None = None,
) -> Iterator[tuple[tuple[Profile, ...], tuple[tuple[_BendSequence, ...], ...]]]:
choose = _bend_sequences(spans, radius, weights, weighted=weighted, sbend_penalty=sbend_penalty)
choose = _bend_sequences(spans, radius, weights, weighted=weighted, sbend_penalty=sbend_penalty, allowed_offsets=allowed_offsets)
needs = tuple((a + b) / 2 + clearance for a, b in pairwise(widths))
# The track order allows a local check of each neighboring pair. The
@ -463,6 +478,7 @@ def _column_sbend_schedules(
*,
weighted: bool,
sbend_penalty: float,
allowed_offsets: frozenset[float] | None,
) -> Iterator[tuple[tuple[_BendSequence, ...], ...]]:
modulus = gcd(*spans)
launch, arrival = layout.launch, layout.arrival
@ -484,10 +500,14 @@ def _column_sbend_schedules(
]
if weighted:
adjacent = [[launch], *interior, [arrival]]
reachable_offsets = (0, *(d for offset in active_spans for d in (-offset, offset)))
reachable_offsets = (0, *(
d for offset in active_spans for d in (-offset, offset) if allowed_offsets is None or d in allowed_offsets
))
return_offsets = tuple(-d for d in reachable_offsets)
for i, (column, preference) in enumerate(zip(columns, preferences, strict=True)):
anchors = (*adjacent[i][:2], *adjacent[i + 2][:2])
reachable = tuple(tuple(tuple(y + d for d in reachable_offsets) for y in anchor) for anchor in anchors)
anchors = [(anchor, reachable_offsets) for anchor in adjacent[i][:2]]
anchors.extend((anchor, return_offsets) for anchor in adjacent[i + 2][:2])
reachable = tuple(tuple(tuple(y + d for d in offsets) for y in anchor) for anchor, offsets in anchors)
seeded = _column_profiles(problem, column, widths, residues, preference, spacing_slack, wall_slack, modulus, reachable)
interior[i] = sorted(set(interior[i] + seeded))
for i, (column, preference) in enumerate(zip(columns, preferences, strict=True)):
@ -496,7 +516,8 @@ def _column_sbend_schedules(
interior[i] = sorted(set(interior[i] + roomier[:45]))
profiles = [[launch], *interior, [arrival]]
for rows, moves in _schedules(
windows, profiles, widths, problem.clearance, radius, active_spans, weights, weighted=weighted, sbend_penalty=sbend_penalty
windows, profiles, widths, problem.clearance, radius, active_spans, weights,
weighted=weighted, sbend_penalty=sbend_penalty, allowed_offsets=allowed_offsets,
):
signature = rows, moves
if signature not in seen:
@ -526,7 +547,10 @@ def column_seed_candidates(
spans = exact_sbend_spans(radius, max_span=max_span, offsets=search.sbend_offsets)
if not spans:
return
for moves in _column_sbend_schedules(problem, layout, radius, spans, weights, weighted=weighted, sbend_penalty=sbend_penalty):
allowed_offsets = None if search.sbend_offsets is None else frozenset(search.sbend_offsets)
for moves in _column_sbend_schedules(
problem, layout, radius, spans, weights, weighted=weighted, sbend_penalty=sbend_penalty, allowed_offsets=allowed_offsets,
):
seeds = _seeds(layout.tracks, layout.spans, moves, radius)
if all(search.seed_geometry_error(seed) is None for seed in seeds.values()):
yield seeds

View file

@ -1,8 +1,9 @@
from unittest.mock import Mock
import pytest
from shapely.geometry import box
from inire import CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, route
from inire import CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SBendSeed, SearchOptions, route
from inire.geometry.sbend import exact_sbend_spans
from inire.router import _columns
from inire.router._columns import _jog_pivots, _schedules, column_seed_candidates
@ -22,6 +23,27 @@ def test_sbend_schedule_requires_a_window_long_enough_for_exact_spans() -> None:
assert [(sequence.span, sequence.offsets) for sequence in transitions[0]] == [(40, (20,))] * 3
@pytest.mark.parametrize("sign", [1, -1])
def test_automatic_column_routes_use_only_enabled_signed_sbends(sign) -> None:
east = tuple(NetSpec(str(y), Port(5, y, 0), Port(220, y + sign * 10, 0)) for y in (30, 60))
west = NetSpec("west", Port(220, 90 + sign * 10, 180), Port(5, 90, 180))
problem = RoutingProblem((0, 0, 230, 130), (*east, west), (box(190, 0, 210, 10),), clearance=0)
options = RoutingOptions(
search=SearchOptions(node_limit=1, bend_radii=(), sbend_radii=(25,), sbend_offsets=(sign * 2, -sign * 10)),
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
refinement=RefinementOptions(enabled=False),
)
run = route(problem, options=options)
assert run.automatic.selected_planner == "column"
assert run.metrics.nodes_expanded == 0
for result in run.results_by_net.values():
assert result.is_valid
bends = tuple(move for move in result.as_seed().segments if isinstance(move, SBendSeed))
assert bends == (SBendSeed(sign * 2, 25),) * 5
def test_sbend_sequence_skips_table_growth_for_shifts_larger_than_the_window(monkeypatch) -> None:
make_sequence = Mock(wraps=_columns._BendSequence)
monkeypatch.setattr(_columns, "_BendSequence", make_sequence)