Sweep active rectangles for automatic capacity bounds
This commit is contained in:
parent
e0efd04181
commit
1f48f60d93
3 changed files with 58 additions and 16 deletions
|
|
@ -1,9 +1,11 @@
|
|||
"""Intervals left open by solid spans."""
|
||||
|
||||
from collections.abc import Iterable
|
||||
|
||||
Gap = tuple[float, float]
|
||||
|
||||
|
||||
def open_intervals(solid: list[Gap], ymin: float, ymax: float) -> tuple[Gap, ...]:
|
||||
def open_intervals(solid: Iterable[Gap], ymin: float, ymax: float) -> tuple[Gap, ...]:
|
||||
openings = []
|
||||
cursor = ymin
|
||||
for bottom, top in sorted(solid):
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from bisect import bisect_left, bisect_right
|
||||
from itertools import pairwise
|
||||
from math import floor, isfinite
|
||||
from typing import TYPE_CHECKING
|
||||
|
|
@ -16,12 +17,14 @@ from inire.model import resolve_bend_geometry
|
|||
from inire.results import CapacityLimit
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from inire.geometry.intervals import Gap
|
||||
from inire.model import RoutingOptions, RoutingProblem
|
||||
|
||||
|
||||
def _rectangular_obstacle_bounds(problem: RoutingProblem) -> tuple[tuple[float, float, float, float], ...] | None:
|
||||
bounds = tuple(tuple(shape.bounds) for shape in problem._blocking_polygons)
|
||||
if any(shape.is_empty or not shape.equals(box(*extent)) for shape, extent in zip(problem._blocking_polygons, bounds, strict=True)):
|
||||
polygons = problem._blocking_polygons
|
||||
bounds = tuple(tuple(shape.bounds) for shape in polygons)
|
||||
if any(shape.is_empty or not shape.equals(box(*extent)) for shape, extent in zip(polygons, bounds, strict=True)):
|
||||
return None
|
||||
return bounds
|
||||
|
||||
|
|
@ -29,32 +32,57 @@ def _rectangular_obstacle_bounds(problem: RoutingProblem) -> tuple[tuple[float,
|
|||
def cut_capacity(problem: RoutingProblem, options: RoutingOptions) -> CapacityLimit | None:
|
||||
"""Bound routes across vertical cuts through truly rectangular obstacles."""
|
||||
_, physical_geometry = resolve_bend_geometry(options.search)
|
||||
if physical_geometry != "arc" or not problem.nets:
|
||||
return None
|
||||
obstacles = _rectangular_obstacle_bounds(problem)
|
||||
if physical_geometry != "arc" or not obstacles or not problem.nets:
|
||||
if not obstacles:
|
||||
return None
|
||||
# The narrowest guide gives an optimistic upper bound for mixed widths.
|
||||
width = min(net.width for net in problem.nets)
|
||||
if not isfinite(width) or width <= 0 or problem.clearance < 0:
|
||||
return None
|
||||
pitch = width + problem.clearance
|
||||
clearance = problem.clearance
|
||||
half_width = width / 2
|
||||
pitch = max(TOLERANCE_LINEAR, width + clearance - TOLERANCE_LINEAR)
|
||||
ymin, ymax = problem.bounds[1], problem.bounds[3]
|
||||
net_intervals = []
|
||||
for net in problem.nets:
|
||||
left = min(net.start.x, net.target.x) + half_width
|
||||
right = max(net.start.x, net.target.x) - half_width
|
||||
if left < right:
|
||||
net_intervals.append((left, right))
|
||||
net_starts = sorted(left for left, _ in net_intervals)
|
||||
net_ends = sorted(right for _, right in net_intervals)
|
||||
|
||||
starts = sorted((left, index, (bottom, top)) for index, (left, bottom, _, top) in enumerate(obstacles))
|
||||
ends = sorted((right, index) for index, (_, _, right, _) in enumerate(obstacles))
|
||||
edges = sorted({left for left, _, _ in starts} | {right for right, _ in ends})
|
||||
active: dict[int, Gap] = {}
|
||||
start = end = 0
|
||||
limit = None
|
||||
edges = sorted({x for left, _, right, _ in obstacles for x in (left, right)})
|
||||
for left, right in pairwise(edges):
|
||||
x = (left + right) / 2
|
||||
crossing = sum(min(net.start.x, net.target.x) + width / 2 < x < max(net.start.x, net.target.x) - width / 2 for net in problem.nets)
|
||||
# Match the strict containment test at each cut, including a midpoint rounded to an edge.
|
||||
while start < len(starts) and starts[start][0] < x:
|
||||
_, index, span = starts[start]
|
||||
active[index] = span
|
||||
start += 1
|
||||
while end < len(ends) and ends[end][0] <= x:
|
||||
_, index = ends[end]
|
||||
active.pop(index, None)
|
||||
end += 1
|
||||
if not active:
|
||||
continue
|
||||
crossing = bisect_left(net_starts, x) - bisect_right(net_ends, x)
|
||||
if not crossing:
|
||||
continue
|
||||
solid = [(bottom, top) for start, bottom, end, top in obstacles if start < x < end]
|
||||
if not solid:
|
||||
continue
|
||||
ymin, ymax = problem.bounds[1], problem.bounds[3]
|
||||
tracks = 0
|
||||
for low, high in open_intervals(solid, ymin, ymax):
|
||||
bottom = low + width / 2 + (problem.clearance if low != ymin else 0)
|
||||
top = high - width / 2 - (problem.clearance if high != ymax else 0)
|
||||
for low, high in open_intervals(active.values(), ymin, ymax):
|
||||
bottom = low + half_width + (clearance if low != ymin else 0)
|
||||
top = high - half_width - (clearance if high != ymax else 0)
|
||||
room = top - bottom + 2 * TOLERANCE_LINEAR
|
||||
if room >= 0:
|
||||
tracks += 1 + floor(room / max(TOLERANCE_LINEAR, pitch - TOLERANCE_LINEAR))
|
||||
tracks += 1 + floor(room / pitch)
|
||||
max_routed = len(problem.nets) - crossing + tracks
|
||||
if tracks < crossing and (limit is None or max_routed < limit.max_routed):
|
||||
limit = CapacityLimit(x, crossing, tracks, max_routed)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
from dataclasses import replace
|
||||
|
||||
import pytest
|
||||
from shapely.geometry import Polygon
|
||||
from shapely.geometry import Polygon, box
|
||||
|
||||
from inire import CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, route
|
||||
from inire.results import CapacityLimit
|
||||
from inire.router._capacity import cut_capacity, port_conflicts
|
||||
from inire.tests.routing_corpus import CASE_GROUPS
|
||||
|
||||
|
|
@ -19,6 +20,17 @@ def test_cut_capacity_requires_rectangular_obstacles() -> None:
|
|||
assert cut_capacity(changed, case.options) is None
|
||||
|
||||
|
||||
def test_cut_capacity_counts_overlapping_rectangles_and_strict_crossings() -> None:
|
||||
through = tuple(NetSpec(str(y), Port(5, y, 0), Port(95, y, 0)) for y in (15, 40, 75))
|
||||
arriving = NetSpec("arriving", Port(5, 45, 0), Port(46, 45, 0))
|
||||
departing = NetSpec("departing", Port(44, 50, 0), Port(95, 50, 0))
|
||||
vertical = NetSpec("vertical", Port(45, 10, 90), Port(45, 90, 90))
|
||||
obstacles = (box(20, 0, 50, 30), box(20, 5, 50, 15), box(40, 34, 60, 100))
|
||||
problem = RoutingProblem((0, 0, 100, 100), (*through, arriving, departing, vertical), obstacles, clearance=1)
|
||||
|
||||
assert cut_capacity(problem, RoutingOptions()) == CapacityLimit(45, 3, 1, 4)
|
||||
|
||||
|
||||
def test_fixed_port_conflicts_are_disjoint_and_depend_on_clearance() -> None:
|
||||
(case,) = CASE_GROUPS["lidar-clements-8x8"]
|
||||
pairs = port_conflicts(case.problem, case.options)
|
||||
|
|
|
|||
Loading…
Reference in a new issue