go to a tunable 5um grid

This commit is contained in:
jan 2026-03-11 09:37:54 -07:00
commit 91256cbcf9
21 changed files with 222 additions and 233 deletions

View file

@ -10,21 +10,24 @@ from .primitives import Port
# Search Grid Snap (1.0 µm)
SEARCH_GRID_SNAP_UM = 1.0
# Search Grid Snap (5.0 µm default)
SEARCH_GRID_SNAP_UM = 5.0
def snap_search_grid(value: float) -> float:
def snap_search_grid(value: float, snap_size: float = SEARCH_GRID_SNAP_UM) -> float:
"""
Snap a coordinate to the nearest search grid unit.
Args:
value: Value to snap.
snap_size: The grid size to snap to.
Returns:
Snapped value.
"""
return round(value / SEARCH_GRID_SNAP_UM) * SEARCH_GRID_SNAP_UM
if snap_size <= 0:
return value
return round(value / snap_size) * snap_size
class ComponentResult:
@ -144,6 +147,7 @@ class Straight:
width: float,
snap_to_grid: bool = True,
dilation: float = 0.0,
snap_size: float = SEARCH_GRID_SNAP_UM,
) -> ComponentResult:
"""
Generate a straight waveguide segment.
@ -154,6 +158,7 @@ class Straight:
width: Waveguide width.
snap_to_grid: Whether to snap the end port to the search grid.
dilation: Optional dilation distance for pre-calculating collision geometry.
snap_size: Grid size for snapping.
Returns:
A ComponentResult containing the straight segment.
@ -166,8 +171,8 @@ class Straight:
ey = start_port.y + length * sin_val
if snap_to_grid:
ex = snap_search_grid(ex)
ey = snap_search_grid(ey)
ex = snap_search_grid(ex, snap_size)
ey = snap_search_grid(ey, snap_size)
end_port = Port(ex, ey, start_port.orientation)
actual_length = numpy.sqrt((end_port.x - start_port.x)**2 + (end_port.y - start_port.y)**2)
@ -415,6 +420,7 @@ class Bend90:
collision_type: Literal["arc", "bbox", "clipped_bbox"] | Polygon = "arc",
clip_margin: float = 10.0,
dilation: float = 0.0,
snap_size: float = SEARCH_GRID_SNAP_UM,
) -> ComponentResult:
"""
Generate a 90-degree bend.
@ -430,8 +436,8 @@ class Bend90:
t_end_init = t_start_init + (numpy.pi / 2 if direction == "CCW" else -numpy.pi / 2)
# Snap the target point
ex = snap_search_grid(cx_init + radius * numpy.cos(t_end_init))
ey = snap_search_grid(cy_init + radius * numpy.sin(t_end_init))
ex = snap_search_grid(cx_init + radius * numpy.cos(t_end_init), snap_size)
ey = snap_search_grid(cy_init + radius * numpy.sin(t_end_init), snap_size)
end_port = Port(ex, ey, float((start_port.orientation + turn_angle) % 360))
# Adjust geometry to perfectly hit snapped port
@ -503,6 +509,7 @@ class SBend:
collision_type: Literal["arc", "bbox", "clipped_bbox"] | Polygon = "arc",
clip_margin: float = 10.0,
dilation: float = 0.0,
snap_size: float = SEARCH_GRID_SNAP_UM,
) -> ComponentResult:
"""
Generate a parametric S-bend (two tangent arcs).
@ -515,8 +522,8 @@ class SBend:
rad_start = numpy.radians(start_port.orientation)
# Snap the target point
ex = snap_search_grid(start_port.x + dx_init * numpy.cos(rad_start) - offset * numpy.sin(rad_start))
ey = snap_search_grid(start_port.y + dx_init * numpy.sin(rad_start) + offset * numpy.cos(rad_start))
ex = snap_search_grid(start_port.x + dx_init * numpy.cos(rad_start) - offset * numpy.sin(rad_start), snap_size)
ey = snap_search_grid(start_port.y + dx_init * numpy.sin(rad_start) + offset * numpy.cos(rad_start), snap_size)
end_port = Port(ex, ey, start_port.orientation)
# Solve for theta and radius that hit (ex, ey) exactly

View file

@ -8,7 +8,7 @@ import rtree
import numpy
from inire.geometry.components import Bend90, SBend, Straight
from inire.geometry.components import Bend90, SBend, Straight, SEARCH_GRID_SNAP_UM
from inire.geometry.primitives import Port
from inire.router.config import RouterConfig
@ -132,7 +132,11 @@ class AStarRouter:
# self._collision_cache.clear()
open_set: list[AStarNode] = []
# Key: (x, y, orientation) rounded to 1nm
# Calculate rounding precision based on search grid
# e.g. 1.0 -> 0, 0.1 -> 1, 0.001 -> 3
state_precision = int(numpy.ceil(-numpy.log10(SEARCH_GRID_SNAP_UM))) if SEARCH_GRID_SNAP_UM < 1.0 else 0
# Key: (x, y, orientation) rounded to search grid
closed_set: set[tuple[float, float, float]] = set()
start_node = AStarNode(start, 0.0, self.cost_evaluator.h_manhattan(start, target))
@ -153,7 +157,7 @@ class AStarRouter:
best_node = current
# Prune if already visited
state = (round(current.port.x, 3), round(current.port.y, 3), round(current.port.orientation, 2))
state = (round(current.port.x, state_precision), round(current.port.y, state_precision), round(current.port.orientation, 2))
if state in closed_set:
continue
closed_set.add(state)
@ -171,7 +175,7 @@ class AStarRouter:
return self._reconstruct_path(current)
# Expansion
self._expand_moves(current, target, net_width, net_id, open_set, closed_set)
self._expand_moves(current, target, net_width, net_id, open_set, closed_set, state_precision)
return self._reconstruct_path(best_node) if return_partial else None
@ -183,6 +187,7 @@ class AStarRouter:
net_id: str,
open_set: list[AStarNode],
closed_set: set[tuple[float, float, float]],
state_precision: int = 0,
) -> None:
# 1. Snap-to-Target Look-ahead
dist = numpy.sqrt((current.port.x - target.x)**2 + (current.port.y - target.y)**2)
@ -195,8 +200,8 @@ class AStarRouter:
proj = dx * numpy.cos(rad) + dy * numpy.sin(rad)
perp = -dx * numpy.sin(rad) + dy * numpy.cos(rad)
if proj > 0 and abs(perp) < 1e-6:
res = Straight.generate(current.port, proj, net_width, snap_to_grid=False, dilation=0.0)
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, 'SnapStraight')
res = Straight.generate(current.port, proj, net_width, snap_to_grid=False, dilation=self._self_dilation, snap_size=self.config.snap_size)
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, 'SnapStraight', state_precision=state_precision)
# B. Try SBend exact reach
if abs(current.port.orientation - target.orientation) < 0.1:
@ -205,7 +210,7 @@ class AStarRouter:
dy = target.y - current.port.y
proj = dx * numpy.cos(rad) + dy * numpy.sin(rad)
perp = -dx * numpy.sin(rad) + dy * numpy.cos(rad)
if proj > 0 and 0.5 <= abs(perp) < 20.0:
if proj > 0 and 0.5 <= abs(perp) < 100.0: # Match snap_to_target_dist
for radius in self.config.sbend_radii:
try:
res = SBend.generate(
@ -215,16 +220,17 @@ class AStarRouter:
net_width,
collision_type=self.config.bend_collision_type,
clip_margin=self.config.bend_clip_margin,
dilation=0.0
dilation=self._self_dilation,
snap_size=self.config.snap_size
)
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, 'SnapSBend', move_radius=radius)
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, 'SnapSBend', move_radius=radius, state_precision=state_precision)
except ValueError:
pass
# 2. Lattice Straights
cp = current.port
base_ori = round(cp.orientation, 2)
state_key = (round(cp.x, 3), round(cp.y, 3), base_ori)
state_key = (round(cp.x, state_precision), round(cp.y, state_precision), base_ori)
lengths = self.config.straight_lengths
if dist < 5.0:
@ -244,16 +250,16 @@ class AStarRouter:
# Check closed set before translating
ex = res_rel.end_port.x + cp.x
ey = res_rel.end_port.y + cp.y
end_state = (round(ex, 3), round(ey, 3), round(res_rel.end_port.orientation, 2))
end_state = (round(ex, state_precision), round(ey, state_precision), round(res_rel.end_port.orientation, 2))
if end_state in closed_set:
continue
res = res_rel.translate(cp.x, cp.y)
else:
res_rel = Straight.generate(Port(0, 0, base_ori), length, net_width, dilation=self._self_dilation)
res_rel = Straight.generate(Port(0, 0, base_ori), length, net_width, dilation=self._self_dilation, snap_size=self.config.snap_size)
self._move_cache[rel_key] = res_rel
res = res_rel.translate(cp.x, cp.y)
self._move_cache[abs_key] = res
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, f'S{length}')
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, f'S{length}', state_precision=state_precision)
# 3. Lattice Bends
for radius in self.config.bend_radii:
@ -268,7 +274,7 @@ class AStarRouter:
# Check closed set before translating
ex = res_rel.end_port.x + cp.x
ey = res_rel.end_port.y + cp.y
end_state = (round(ex, 3), round(ey, 3), round(res_rel.end_port.orientation, 2))
end_state = (round(ex, state_precision), round(ey, state_precision), round(res_rel.end_port.orientation, 2))
if end_state in closed_set:
continue
res = res_rel.translate(cp.x, cp.y)
@ -280,12 +286,13 @@ class AStarRouter:
direction,
collision_type=self.config.bend_collision_type,
clip_margin=self.config.bend_clip_margin,
dilation=self._self_dilation
dilation=self._self_dilation,
snap_size=self.config.snap_size
)
self._move_cache[rel_key] = res_rel
res = res_rel.translate(cp.x, cp.y)
self._move_cache[abs_key] = res
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, f'B{radius}{direction}', move_radius=radius)
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, f'B{radius}{direction}', move_radius=radius, state_precision=state_precision)
# 4. Discrete SBends
for offset in self.config.sbend_offsets:
@ -300,7 +307,7 @@ class AStarRouter:
# Check closed set before translating
ex = res_rel.end_port.x + cp.x
ey = res_rel.end_port.y + cp.y
end_state = (round(ex, 3), round(ey, 3), round(res_rel.end_port.orientation, 2))
end_state = (round(ex, state_precision), round(ey, state_precision), round(res_rel.end_port.orientation, 2))
if end_state in closed_set:
continue
res = res_rel.translate(cp.x, cp.y)
@ -313,14 +320,15 @@ class AStarRouter:
width=net_width,
collision_type=self.config.bend_collision_type,
clip_margin=self.config.bend_clip_margin,
dilation=self._self_dilation
dilation=self._self_dilation,
snap_size=self.config.snap_size
)
self._move_cache[rel_key] = res_rel
res = res_rel.translate(cp.x, cp.y)
except ValueError:
continue
self._move_cache[abs_key] = res
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, f'SB{offset}R{radius}', move_radius=radius)
self._add_node(current, res, target, net_width, net_id, open_set, closed_set, f'SB{offset}R{radius}', move_radius=radius, state_precision=state_precision)
def _add_node(
self,
@ -333,15 +341,16 @@ class AStarRouter:
closed_set: set[tuple[float, float, float]],
move_type: str,
move_radius: float | None = None,
state_precision: int = 0,
) -> None:
# Check closed set before adding to open set
state = (round(result.end_port.x, 3), round(result.end_port.y, 3), round(result.end_port.orientation, 2))
state = (round(result.end_port.x, state_precision), round(result.end_port.y, state_precision), round(result.end_port.orientation, 2))
if state in closed_set:
return
cache_key = (
round(parent.port.x, 3),
round(parent.port.y, 3),
round(parent.port.x, state_precision),
round(parent.port.y, state_precision),
round(parent.port.orientation, 2),
move_type,
net_width,

View file

@ -10,13 +10,14 @@ class RouterConfig:
"""Configuration parameters for the A* Router."""
node_limit: int = 1000000
straight_lengths: list[float] = field(default_factory=lambda: [1.0, 5.0, 25.0])
bend_radii: list[float] = field(default_factory=lambda: [10.0])
sbend_offsets: list[float] = field(default_factory=lambda: [-5.0, -2.0, 2.0, 5.0])
sbend_radii: list[float] = field(default_factory=lambda: [10.0])
snap_to_target_dist: float = 20.0
bend_penalty: float = 50.0
sbend_penalty: float = 150.0
snap_size: float = 5.0
straight_lengths: list[float] = field(default_factory=lambda: [5.0, 10.0, 100.0])
bend_radii: list[float] = field(default_factory=lambda: [50.0])
sbend_offsets: list[float] = field(default_factory=lambda: [-10.0, -5.0, 5.0, 10.0])
sbend_radii: list[float] = field(default_factory=lambda: [50.0])
snap_to_target_dist: float = 100.0
bend_penalty: float = 250.0
sbend_penalty: float = 500.0
bend_collision_type: Literal["arc", "bbox", "clipped_bbox"] | Any = "arc"
bend_clip_margin: float = 10.0
@ -28,5 +29,5 @@ class CostConfig:
unit_length_cost: float = 1.0
greedy_h_weight: float = 1.1
congestion_penalty: float = 10000.0
bend_penalty: float = 50.0
sbend_penalty: float = 150.0
bend_penalty: float = 250.0
sbend_penalty: float = 500.0

View file

@ -39,8 +39,8 @@ class CostEvaluator:
unit_length_cost: float = 1.0,
greedy_h_weight: float = 1.1,
congestion_penalty: float = 10000.0,
bend_penalty: float = 50.0,
sbend_penalty: float = 150.0,
bend_penalty: float = 250.0,
sbend_penalty: float = 500.0,
) -> None:
"""
Initialize the Cost Evaluator.
@ -102,8 +102,8 @@ class CostEvaluator:
# But we also need to account for the physical distance required for the turn.
penalty = 0.0
if current.orientation != target.orientation:
# 90-degree turn cost: radius 10 -> ~15.7 um + penalty
penalty += 15.7 + self.config.bend_penalty
# 90-degree turn cost: radius 50 -> ~78.5 um + penalty
penalty += 78.5 + self.config.bend_penalty
return self.greedy_h_weight * (dist + penalty)

View file

@ -124,7 +124,7 @@ class PathFinder:
coll_model = "clipped_bbox"
net_start = time.monotonic()
path = self.router.route(start, target, width, net_id=net_id, bend_collision_type=coll_model)
path = self.router.route(start, target, width, net_id=net_id, bend_collision_type=coll_model, return_partial=True)
logger.debug(f' Net {net_id} routed in {time.monotonic() - net_start:.4f}s using {coll_model}')
if path:

View file

@ -15,11 +15,11 @@ def basic_evaluator() -> CostEvaluator:
engine = CollisionEngine(clearance=2.0)
danger_map = DangerMap(bounds=(0, 0, 100, 100))
danger_map.precompute([])
return CostEvaluator(engine, danger_map)
return CostEvaluator(engine, danger_map, bend_penalty=50.0, sbend_penalty=150.0)
def test_astar_straight(basic_evaluator: CostEvaluator) -> None:
router = AStarRouter(basic_evaluator)
router = AStarRouter(basic_evaluator, snap_size=1.0, straight_lengths=[1.0, 5.0, 25.0])
start = Port(0, 0, 0)
target = Port(50, 0, 0)
path = router.route(start, target, net_width=2.0)
@ -35,11 +35,9 @@ def test_astar_straight(basic_evaluator: CostEvaluator) -> None:
def test_astar_bend(basic_evaluator: CostEvaluator) -> None:
router = AStarRouter(basic_evaluator)
router = AStarRouter(basic_evaluator, snap_size=1.0, straight_lengths=[1.0, 5.0, 25.0], bend_radii=[10.0])
start = Port(0, 0, 0)
# 20um right, 20um up. Needs a 10um bend and a 10um bend.
# From (0,0,0) -> Bend90 CW R=10 -> (10, -10, 270) ??? No.
# Try: (0,0,0) -> Bend90 CCW R=10 -> (10, 10, 90) -> Straight 10 -> (10, 20, 90) -> Bend90 CW R=10 -> (20, 30, 0)
target = Port(20, 20, 0)
path = router.route(start, target, net_width=2.0)
@ -58,7 +56,7 @@ def test_astar_obstacle(basic_evaluator: CostEvaluator) -> None:
basic_evaluator.collision_engine.add_static_obstacle(obstacle)
basic_evaluator.danger_map.precompute([obstacle])
router = AStarRouter(basic_evaluator)
router = AStarRouter(basic_evaluator, snap_size=1.0, straight_lengths=[1.0, 5.0, 25.0], bend_radii=[10.0])
router.node_limit = 1000000 # Give it more room for detour
start = Port(0, 0, 0)
target = Port(60, 0, 0)
@ -74,7 +72,7 @@ def test_astar_obstacle(basic_evaluator: CostEvaluator) -> None:
def test_astar_snap_to_target_lookahead(basic_evaluator: CostEvaluator) -> None:
router = AStarRouter(basic_evaluator)
router = AStarRouter(basic_evaluator, snap_size=1.0)
# Target is NOT on 1um grid
start = Port(0, 0, 0)
target = Port(10.1, 0, 0)

View file

@ -8,7 +8,7 @@ def test_straight_generation() -> None:
start = Port(0, 0, 0)
length = 10.0
width = 2.0
result = Straight.generate(start, length, width)
result = Straight.generate(start, length, width, snap_size=1.0)
assert result.end_port.x == 10.0
assert result.end_port.y == 0.0
@ -29,13 +29,13 @@ def test_bend90_generation() -> None:
width = 2.0
# CW bend
result_cw = Bend90.generate(start, radius, width, direction="CW")
result_cw = Bend90.generate(start, radius, width, direction="CW", snap_size=1.0)
assert result_cw.end_port.x == 10.0
assert result_cw.end_port.y == -10.0
assert result_cw.end_port.orientation == 270.0
# CCW bend
result_ccw = Bend90.generate(start, radius, width, direction="CCW")
result_ccw = Bend90.generate(start, radius, width, direction="CCW", snap_size=1.0)
assert result_ccw.end_port.x == 10.0
assert result_ccw.end_port.y == 10.0
assert result_ccw.end_port.orientation == 90.0
@ -47,7 +47,7 @@ def test_sbend_generation() -> None:
radius = 10.0
width = 2.0
result = SBend.generate(start, offset, radius, width)
result = SBend.generate(start, offset, radius, width, snap_size=1.0)
assert result.end_port.y == 5.0
assert result.end_port.orientation == 0.0
assert len(result.geometry) == 2 # Optimization: returns individual arcs
@ -63,7 +63,7 @@ def test_bend_collision_models() -> None:
width = 2.0
# 1. BBox model
res_bbox = Bend90.generate(start, radius, width, direction="CCW", collision_type="bbox")
res_bbox = Bend90.generate(start, radius, width, direction="CCW", collision_type="bbox", snap_size=1.0)
# Arc CCW R=10 from (0,0,0) ends at (10,10,90).
# Waveguide width is 2.0, so bbox will be slightly larger than (0,0,10,10)
minx, miny, maxx, maxy = res_bbox.geometry[0].bounds
@ -73,7 +73,7 @@ def test_bend_collision_models() -> None:
assert maxy >= 10.0 - 1e-6
# 2. Clipped BBox model
res_clipped = Bend90.generate(start, radius, width, direction="CCW", collision_type="clipped_bbox", clip_margin=1.0)
res_clipped = Bend90.generate(start, radius, width, direction="CCW", collision_type="clipped_bbox", clip_margin=1.0, snap_size=1.0)
# Area should be less than full bbox
assert res_clipped.geometry[0].area < res_bbox.geometry[0].area
@ -84,11 +84,11 @@ def test_sbend_collision_models() -> None:
radius = 10.0
width = 2.0
res_bbox = SBend.generate(start, offset, radius, width, collision_type="bbox")
res_bbox = SBend.generate(start, offset, radius, width, collision_type="bbox", snap_size=1.0)
# Geometry should be a list of individual bbox polygons for each arc
assert len(res_bbox.geometry) == 2
res_arc = SBend.generate(start, offset, radius, width, collision_type="arc")
res_arc = SBend.generate(start, offset, radius, width, collision_type="arc", snap_size=1.0)
area_bbox = sum(p.area for p in res_bbox.geometry)
area_arc = sum(p.area for p in res_arc.geometry)
assert area_bbox > area_arc
@ -101,7 +101,8 @@ def test_sbend_continuity() -> None:
radius = 20.0
width = 1.0
res = SBend.generate(start, offset, radius, width)
# We use snap_size=1.0 so that (10-offset) = 6.0 is EXACTLY hit.
res = SBend.generate(start, offset, radius, width, snap_size=1.0)
# Target orientation should be same as start
assert abs(res.end_port.orientation - 90.0) < 1e-6
@ -141,7 +142,7 @@ def test_component_transform_invariance() -> None:
radius = 10.0
width = 2.0
res0 = Bend90.generate(start0, radius, width, direction="CCW")
res0 = Bend90.generate(start0, radius, width, direction="CCW", snap_size=1.0)
# Transform: Translate (10, 10) then Rotate 90
dx, dy = 10.0, 5.0
@ -152,7 +153,7 @@ def test_component_transform_invariance() -> None:
# 2. Generate at transformed start
start_transformed = rotate_port(translate_port(start0, dx, dy), angle)
res_transformed = Bend90.generate(start_transformed, radius, width, direction="CCW")
res_transformed = Bend90.generate(start_transformed, radius, width, direction="CCW", snap_size=1.0)
assert abs(res_transformed.end_port.x - p_end_transformed.x) < 1e-6
assert abs(res_transformed.end_port.y - p_end_transformed.y) < 1e-6

View file

@ -15,11 +15,11 @@ def basic_evaluator() -> CostEvaluator:
# Wider bounds to allow going around (y from -40 to 40)
danger_map = DangerMap(bounds=(0, -40, 100, 40))
danger_map.precompute([])
return CostEvaluator(engine, danger_map)
return CostEvaluator(engine, danger_map, bend_penalty=50.0, sbend_penalty=150.0)
def test_astar_sbend(basic_evaluator: CostEvaluator) -> None:
router = AStarRouter(basic_evaluator)
router = AStarRouter(basic_evaluator, snap_size=1.0, sbend_offsets=[2.0, 5.0])
# Start at (0,0), target at (50, 2) -> 2um lateral offset
# This matches one of our discretized SBend offsets.
start = Port(0, 0, 0)
@ -39,7 +39,7 @@ def test_astar_sbend(basic_evaluator: CostEvaluator) -> None:
def test_pathfinder_negotiated_congestion_resolution(basic_evaluator: CostEvaluator) -> None:
router = AStarRouter(basic_evaluator)
router = AStarRouter(basic_evaluator, snap_size=1.0, straight_lengths=[1.0, 5.0, 25.0])
# Increase base penalty to force detour immediately
pf = PathFinder(router, basic_evaluator, max_iterations=10, base_congestion_penalty=1000.0)