Refactor routing internals and archive retry benchmarks
This commit is contained in:
parent
a8720ac8c9
commit
ea5cfe7f31
1 changed files with 3481 additions and 2583 deletions
|
|
@ -6,11 +6,13 @@ from collections import defaultdict
|
|||
from dataclasses import dataclass
|
||||
from functools import lru_cache
|
||||
from itertools import combinations_with_replacement, islice, pairwise, product
|
||||
from math import acos, ceil, comb, cos, floor, gcd, isfinite, isqrt, pi, sin
|
||||
from math import acos, ceil, comb, cos, floor, gcd, isfinite, pi, sin
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from shapely.geometry import LineString, box
|
||||
from shapely.geometry import LineString
|
||||
|
||||
from inire.geometry.intervals import Gap, open_intervals
|
||||
from inire.geometry.sbend import exact_sbend_spans
|
||||
from inire.model import resolve_bend_geometry
|
||||
from inire.router._waypoints import compile_waypoints
|
||||
from inire.seeds import PathSeed, SBendSeed, StraightSeed
|
||||
|
|
@ -25,7 +27,6 @@ if TYPE_CHECKING:
|
|||
|
||||
|
||||
Profile = tuple[int, ...]
|
||||
Gap = tuple[float, float]
|
||||
Window = tuple[int, int]
|
||||
JogPivots = tuple[int | None, ...]
|
||||
JogSchedule = tuple[float, tuple[Profile, ...], tuple[JogPivots, ...]]
|
||||
|
|
@ -45,6 +46,17 @@ class _Column:
|
|||
openings: tuple[Gap, ...]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _ColumnLayout:
|
||||
tracks: tuple[_Track, ...]
|
||||
columns: tuple[_Column, ...]
|
||||
spans: tuple[Window, ...]
|
||||
launch: Profile
|
||||
arrival: Profile
|
||||
widths: tuple[float, ...]
|
||||
preferences: tuple[tuple[float, ...], ...]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _BendSequence:
|
||||
offsets: tuple[int, ...]
|
||||
|
|
@ -52,27 +64,6 @@ class _BendSequence:
|
|||
cost: float
|
||||
|
||||
|
||||
def rectangular_obstacle_bounds(problem: RoutingProblem) -> tuple[tuple[float, float, float, float], ...] | None:
|
||||
bounds = tuple(tuple(shape.bounds) for shape in problem.static_obstacles)
|
||||
if any(shape.is_empty or not shape.equals(box(*extent)) for shape, extent in zip(problem.static_obstacles, bounds, strict=True)):
|
||||
return None
|
||||
return bounds
|
||||
|
||||
|
||||
def open_intervals(solid: list[Gap], ymin: float, ymax: float) -> tuple[Gap, ...]:
|
||||
openings = []
|
||||
cursor = ymin
|
||||
for bottom, top in sorted(solid):
|
||||
if top <= ymin or bottom >= ymax:
|
||||
continue
|
||||
if cursor < bottom:
|
||||
openings.append((cursor, bottom))
|
||||
cursor = max(cursor, top)
|
||||
if cursor < ymax:
|
||||
openings.append((cursor, ymax))
|
||||
return tuple(openings)
|
||||
|
||||
|
||||
def _columns(problem: RoutingProblem, start: int, finish: int) -> tuple[_Column, ...]:
|
||||
# A bounding rectangle only reserves extra space in this candidate
|
||||
# planner. Capacity proofs still require the actual rectangular obstacles.
|
||||
|
|
@ -113,6 +104,25 @@ def _ordered_tracks(problem: RoutingProblem) -> tuple[_Track, ...]:
|
|||
return tuple(tracks)
|
||||
|
||||
|
||||
def _column_layout(problem: RoutingProblem) -> _ColumnLayout | None:
|
||||
tracks = _ordered_tracks(problem)
|
||||
if len(tracks) < 3 or any(not isfinite(track.net.width) or track.net.width <= 0 for track in tracks):
|
||||
return None
|
||||
start = max(int(track.left.x) for track in tracks)
|
||||
finish = min(int(track.right.x) for track in tracks)
|
||||
columns = _columns(problem, start, finish)
|
||||
if not columns:
|
||||
return None
|
||||
launch = tuple(int(track.left.y) for track in tracks)
|
||||
arrival = tuple(int(track.right.y) for track in tracks)
|
||||
spans = ((start, start), *(column.span for column in columns), (finish, finish))
|
||||
preferences = []
|
||||
for column in columns:
|
||||
fraction = (sum(column.span) / 2 - start) / (finish - start)
|
||||
preferences.append(tuple(a + fraction * (b - a) for a, b in zip(launch, arrival, strict=True)))
|
||||
return _ColumnLayout(tracks, columns, spans, launch, arrival, tuple(track.net.width for track in tracks), tuple(preferences))
|
||||
|
||||
|
||||
def _round_up(y: float, residue: int, modulus: int) -> int:
|
||||
return ceil((y - residue) / modulus) * modulus + residue
|
||||
|
||||
|
|
@ -240,16 +250,6 @@ def _column_profiles(
|
|||
return sorted(profiles, key=lambda row: (sum(abs(a - b) for a, b in zip(row, desired, strict=True)), row))[:140]
|
||||
|
||||
|
||||
def _exact_spans(radius: int) -> dict[int, int]:
|
||||
spans = {}
|
||||
for offset in range(1, 2 * radius):
|
||||
squared = offset * (4 * radius - offset)
|
||||
span = isqrt(squared)
|
||||
if span * span == squared:
|
||||
spans[offset] = span
|
||||
return spans
|
||||
|
||||
|
||||
def _bend_sequences(
|
||||
spans: dict[int, int],
|
||||
radius: int,
|
||||
|
|
@ -410,41 +410,24 @@ def _seeds(
|
|||
return seeds
|
||||
|
||||
|
||||
def column_seed_candidates(
|
||||
def _column_sbend_schedules(
|
||||
problem: RoutingProblem,
|
||||
search: SearchOptions,
|
||||
layout: _ColumnLayout,
|
||||
radius: int,
|
||||
weights: Mapping[str, float],
|
||||
*,
|
||||
weighted: bool,
|
||||
sbend_penalty: float = 0.0,
|
||||
) -> Iterator[dict[str, PathSeed]]:
|
||||
"""Yield candidate routes from configured circular bends and problem geometry."""
|
||||
_, physical_geometry = resolve_bend_geometry(search)
|
||||
bend = min(search.sbend_radii, default=0.0)
|
||||
if problem.initial_paths or physical_geometry != "arc" or not isfinite(bend) or bend <= 0 or bend != int(bend):
|
||||
return
|
||||
tracks = _ordered_tracks(problem)
|
||||
if len(tracks) < 3 or any(not isfinite(track.net.width) or track.net.width <= 0 for track in tracks):
|
||||
return
|
||||
start, finish = max(track.left.x for track in tracks), min(track.right.x for track in tracks)
|
||||
columns = _columns(problem, start, finish)
|
||||
if not columns:
|
||||
return
|
||||
radius = int(bend)
|
||||
spans = _exact_spans(radius)
|
||||
sbend_penalty: float,
|
||||
) -> Iterator[tuple[tuple[_BendSequence, ...], ...]]:
|
||||
spans = exact_sbend_spans(radius)
|
||||
if not spans:
|
||||
return
|
||||
modulus = gcd(*spans)
|
||||
launch, arrival = tuple(track.left.y for track in tracks), tuple(track.right.y for track in tracks)
|
||||
launch, arrival = layout.launch, layout.arrival
|
||||
residues = tuple(y % modulus for y in launch)
|
||||
if tuple(y % modulus for y in arrival) != residues:
|
||||
return
|
||||
widths = tuple(track.net.width for track in tracks)
|
||||
windows = ((start, start), *(column.span for column in columns), (finish, finish))
|
||||
preferences = []
|
||||
for column in columns:
|
||||
share = (sum(column.span) / 2 - start) / (finish - start)
|
||||
preferences.append(tuple(a + share * (b - a) for a, b in zip(launch, arrival, strict=True)))
|
||||
columns, widths, windows, preferences = layout.columns, layout.widths, layout.spans, layout.preferences
|
||||
# The first pass uses moderate lateral shifts. Later passes relax the
|
||||
# spacing or try smaller or larger bends if a transition will not fit.
|
||||
limits = tuple(dict.fromkeys(min(2 * radius - 1, ceil(radius * frac)) for frac in (0.8, 0.4, 1.44)))
|
||||
|
|
@ -476,7 +459,29 @@ def column_seed_candidates(
|
|||
signature = rows, moves
|
||||
if signature not in seen:
|
||||
seen.add(signature)
|
||||
yield _seeds(tracks, windows, moves, radius)
|
||||
yield moves
|
||||
|
||||
|
||||
|
||||
def column_seed_candidates(
|
||||
problem: RoutingProblem,
|
||||
search: SearchOptions,
|
||||
weights: Mapping[str, float],
|
||||
*,
|
||||
weighted: bool,
|
||||
sbend_penalty: float = 0.0,
|
||||
) -> Iterator[dict[str, PathSeed]]:
|
||||
"""Yield candidate routes from configured circular bends and problem geometry."""
|
||||
_, physical_geometry = resolve_bend_geometry(search)
|
||||
bend = min(search.sbend_radii, default=0.0)
|
||||
if problem.initial_paths or physical_geometry != "arc" or not isfinite(bend) or bend <= 0 or bend != int(bend):
|
||||
return
|
||||
layout = _column_layout(problem)
|
||||
if layout is None:
|
||||
return
|
||||
radius = int(bend)
|
||||
for moves in _column_sbend_schedules(problem, layout, radius, weights, weighted=weighted, sbend_penalty=sbend_penalty):
|
||||
yield _seeds(layout.tracks, layout.spans, moves, radius)
|
||||
|
||||
|
||||
def _jog_pivots(
|
||||
|
|
@ -568,41 +573,44 @@ def _jog_seeds(
|
|||
return seeds
|
||||
|
||||
|
||||
def column_jog_candidates(problem: RoutingProblem, search: SearchOptions) -> Iterator[dict[str, PathSeed]]:
|
||||
"""Allocate tracks through ordered apertures using staggered 90-degree jogs."""
|
||||
if problem.initial_paths:
|
||||
return
|
||||
tracks = _ordered_tracks(problem)
|
||||
if len(tracks) < 3 or any(not isfinite(track.net.width) or track.net.width <= 0 for track in tracks):
|
||||
return
|
||||
radii = sorted({int(r) for r in search.bend_radii if isfinite(r) and r > 0 and r == int(r)})
|
||||
if not radii:
|
||||
return
|
||||
start, finish = max(int(track.left.x) for track in tracks), min(int(track.right.x) for track in tracks)
|
||||
columns = _columns(problem, start, finish)
|
||||
if not columns or any(comb(len(tracks) + len(column.openings) - 1, len(column.openings) - 1) > 4096 for column in columns):
|
||||
return
|
||||
launch = tuple(int(track.left.y) for track in tracks)
|
||||
arrival = tuple(int(track.right.y) for track in tracks)
|
||||
widths = tuple(track.net.width for track in tracks)
|
||||
spans = ((start, start), *(column.span for column in columns), (finish, finish))
|
||||
windows = tuple((after, before) for (_, after), (before, _) in pairwise(spans))
|
||||
preferences = []
|
||||
for column in columns:
|
||||
fraction = (sum(column.span) / 2 - start) / (finish - start)
|
||||
preferences.append(tuple(a + fraction * (b - a) for a, b in zip(launch, arrival, strict=True)))
|
||||
def _column_jog_schedules(
|
||||
problem: RoutingProblem,
|
||||
layout: _ColumnLayout,
|
||||
radii: list[int],
|
||||
minimum: float,
|
||||
) -> Iterator[tuple[int, tuple[Profile, ...], tuple[JogPivots, ...]]]:
|
||||
columns, widths, preferences = layout.columns, layout.widths, layout.preferences
|
||||
launch, arrival = layout.launch, layout.arrival
|
||||
windows = tuple((after, before) for (_, after), (before, _) in pairwise(layout.spans))
|
||||
for slack in (1, 0):
|
||||
# Modulus one allows any integer track level, independent of the
|
||||
# set of exact S-bend spans.
|
||||
interior = [
|
||||
_column_profiles(problem, column, widths, (0,) * len(tracks), desired, slack, slack, 1, combinations_limit=8192)
|
||||
_column_profiles(problem, column, widths, (0,) * len(widths), desired, slack, slack, 1, combinations_limit=8192)
|
||||
for column, desired in zip(columns, preferences, strict=True)
|
||||
]
|
||||
for radius in radii:
|
||||
schedules = _jog_schedules(
|
||||
[[launch], *interior, [arrival]], windows, widths, problem.clearance, radius, search.min_straight_length
|
||||
[[launch], *interior, [arrival]], windows, widths, problem.clearance, radius, minimum
|
||||
)
|
||||
for _, rows, moves in schedules:
|
||||
seeds = _jog_seeds(tracks, rows, moves, radius, search)
|
||||
if seeds is not None:
|
||||
yield seeds
|
||||
yield radius, rows, moves
|
||||
|
||||
|
||||
|
||||
def column_jog_candidates(problem: RoutingProblem, search: SearchOptions) -> Iterator[dict[str, PathSeed]]:
|
||||
"""Allocate tracks through ordered apertures using staggered 90-degree jogs."""
|
||||
if problem.initial_paths:
|
||||
return
|
||||
radii = sorted({int(r) for r in search.bend_radii if isfinite(r) and r > 0 and r == int(r)})
|
||||
if not radii:
|
||||
return
|
||||
layout = _column_layout(problem)
|
||||
if layout is None or any(
|
||||
comb(len(layout.tracks) + len(column.openings) - 1, len(column.openings) - 1) > 4096 for column in layout.columns
|
||||
):
|
||||
return
|
||||
for radius, rows, moves in _column_jog_schedules(problem, layout, radii, search.min_straight_length):
|
||||
seeds = _jog_seeds(layout.tracks, rows, moves, radius, search)
|
||||
if seeds is not None:
|
||||
yield seeds
|
||||
|
|
|
|||
Loading…
Reference in a new issue