Rank column S-bend profiles without enumerating full products
This commit is contained in:
parent
f4b16d3c55
commit
06dbfd4093
2 changed files with 79 additions and 28 deletions
|
|
@ -4,7 +4,8 @@ from __future__ import annotations
|
|||
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from functools import lru_cache
|
||||
from functools import cache, lru_cache
|
||||
from heapq import merge
|
||||
from itertools import combinations_with_replacement, islice, pairwise, product
|
||||
from math import acos, ceil, comb, cos, floor, gcd, isfinite, pi, sin
|
||||
from typing import TYPE_CHECKING
|
||||
|
|
@ -31,6 +32,7 @@ Window = tuple[int, int]
|
|||
JogPivots = tuple[int | None, ...]
|
||||
JogSchedule = tuple[float, tuple[Profile, ...], tuple[JogPivots, ...]]
|
||||
_MAX_COLUMN_PARTITIONS = 4096
|
||||
_MAX_COLUMN_PROFILES = 140
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
|
|
@ -221,36 +223,66 @@ def _column_profiles(
|
|||
*,
|
||||
combinations_limit: int | None = None,
|
||||
) -> list[Profile]:
|
||||
@cache
|
||||
def band_profiles(opening: Gap, left: int, right: int) -> tuple[Profile, ...]:
|
||||
return _band_profiles(
|
||||
problem, opening, widths[left:right], residues[left:right], desired[left:right], spacing_slack, wall_slack, modulus,
|
||||
tuple(candidate[left:right] for candidate in reachability),
|
||||
)
|
||||
|
||||
if combinations_limit is None:
|
||||
return _best_column_profiles(column.openings, desired, band_profiles)
|
||||
|
||||
profiles = set()
|
||||
n = len(widths)
|
||||
band_cache = {}
|
||||
for cuts in combinations_with_replacement(range(n + 1), len(column.openings) - 1):
|
||||
groups = []
|
||||
for opening, (left, right) in zip(column.openings, pairwise((0, *cuts, n)), strict=True):
|
||||
key = opening, left, right
|
||||
if key not in band_cache:
|
||||
band_cache[key] = _band_profiles(
|
||||
problem,
|
||||
opening,
|
||||
widths[left:right],
|
||||
residues[left:right],
|
||||
desired[left:right],
|
||||
spacing_slack,
|
||||
wall_slack,
|
||||
modulus,
|
||||
tuple(candidate[left:right] for candidate in reachability),
|
||||
)
|
||||
groups.append(band_cache[key])
|
||||
combinations = product(*groups)
|
||||
if combinations_limit is None:
|
||||
profiles.update(tuple(y for group in chosen for y in group) for chosen in combinations)
|
||||
else:
|
||||
for chosen in islice(combinations, combinations_limit):
|
||||
profiles.add(tuple(y for group in chosen for y in group))
|
||||
combinations_limit -= 1
|
||||
if combinations_limit == 0:
|
||||
break
|
||||
return sorted(profiles, key=lambda row: (sum(abs(a - b) for a, b in zip(row, desired, strict=True)), row))[:140]
|
||||
groups = [
|
||||
band_profiles(opening, left, right)
|
||||
for opening, (left, right) in zip(column.openings, pairwise((0, *cuts, n)), strict=True)
|
||||
]
|
||||
for chosen in islice(product(*groups), combinations_limit):
|
||||
profiles.add(tuple(y for group in chosen for y in group))
|
||||
combinations_limit -= 1
|
||||
if combinations_limit == 0:
|
||||
break
|
||||
return sorted(profiles, key=lambda row: (sum(abs(a - b) for a, b in zip(row, desired, strict=True)), row))[:_MAX_COLUMN_PROFILES]
|
||||
|
||||
|
||||
def _best_column_profiles(
|
||||
openings: tuple[Gap, ...],
|
||||
desired: tuple[float, ...],
|
||||
band_profiles: Callable[[Gap, int, int], tuple[Profile, ...]],
|
||||
) -> list[Profile]:
|
||||
# Sum deviations exactly so adding a band preserves the order of tied prefixes.
|
||||
ratios = tuple(y.as_integer_ratio() for y in desired)
|
||||
scale = max((denominator for _, denominator in ratios), default=1)
|
||||
scaled_targets = tuple(numerator * (scale // denominator) for numerator, denominator in ratios)
|
||||
|
||||
def extend(prefixes: list[tuple[int, Profile]], band: Profile, deviation: int) -> Iterator[tuple[int, Profile]]:
|
||||
return ((cost + deviation, prefix + band) for cost, prefix in prefixes)
|
||||
|
||||
# Prefixes that have placed the same number of tracks can use the same
|
||||
# remaining bands. Each extension preserves the order by deviation and row.
|
||||
# Disjoint bands make each row correspond to a unique track assignment.
|
||||
prefixes: dict[int, list[tuple[int, Profile]]] = {0: [(0, ())]}
|
||||
count = len(desired)
|
||||
for index, opening in enumerate(openings, start=1):
|
||||
next_prefixes = {}
|
||||
for right in ((count,) if index == len(openings) else range(count + 1)):
|
||||
streams = []
|
||||
for left, ranked in prefixes.items():
|
||||
if left > right:
|
||||
continue
|
||||
targets = scaled_targets[left:right]
|
||||
for band in band_profiles(opening, left, right):
|
||||
deviation = sum(abs(y * scale - target) for y, target in zip(band, targets, strict=True))
|
||||
streams.append(extend(ranked, band, deviation))
|
||||
if streams:
|
||||
next_prefixes[right] = list(islice(merge(*streams), _MAX_COLUMN_PROFILES))
|
||||
if not next_prefixes:
|
||||
return []
|
||||
prefixes = next_prefixes
|
||||
return [row for _, row in prefixes.get(count, ())]
|
||||
|
||||
|
||||
def _bend_sequences(
|
||||
|
|
|
|||
|
|
@ -95,6 +95,25 @@ def test_automatic_routing_skips_column_profiles_with_too_many_partitions(monkey
|
|||
assert all(results[net.net_id].is_valid for net in nets)
|
||||
|
||||
|
||||
def test_sbend_column_profiles_match_exhaustive_ranking_without_using_a_product(monkeypatch) -> None:
|
||||
problem = RoutingProblem((0, 0, 200, 300), clearance=0)
|
||||
column = _columns._Column((80, 100), ((0, 40), (45, 85), (90, 140), (145, 200), (205, 300)))
|
||||
widths = (2, 3, 2, 4, 2)
|
||||
desired = (20.25, 65.5, 110.75, 160.5, 250.25)
|
||||
residues = (0, 1, 2, 3, 0)
|
||||
reachable = (((16, 20, 24), (61, 65, 69), (106, 110, 114), (155, 159, 163), (244, 248, 252)),)
|
||||
args = (problem, column, widths, residues, desired, 2, 1, 4, reachable)
|
||||
exhaustive = _columns._column_profiles(*args, combinations_limit=1_000_000)
|
||||
|
||||
def unexpected_product(*_groups):
|
||||
raise AssertionError("S-bend profile ranking should avoid the Cartesian product")
|
||||
|
||||
monkeypatch.setattr(_columns, "product", unexpected_product)
|
||||
|
||||
assert len(exhaustive) == 140
|
||||
assert _columns._column_profiles(*args) == exhaustive
|
||||
|
||||
|
||||
def test_jog_transition_rejects_an_offset_that_cannot_fit_two_bends() -> None:
|
||||
assert _jog_pivots((10, 40), (17, 40), (0, 100), (2, 2), 2, 5, 1) is None
|
||||
assert _jog_pivots((10, 40), (25, 40), (0, 100), (2, 2), 2, 5, 1) == (8, None)
|
||||
|
|
|
|||
Loading…
Reference in a new issue