warm start and some perf
This commit is contained in:
parent
7e6be50a86
commit
f505694523
3 changed files with 251 additions and 111 deletions
|
|
@ -96,7 +96,7 @@ class CollisionEngine:
|
|||
f" Congestion: {m['congestion_tree_queries']} checks\n"
|
||||
f" Safety Zone: {m['safety_zone_checks']} full intersections performed")
|
||||
|
||||
def add_static_obstacle(self, polygon: Polygon) -> None:
|
||||
def add_static_obstacle(self, polygon: Polygon) -> int:
|
||||
obj_id = self._static_id_counter
|
||||
self._static_id_counter += 1
|
||||
|
||||
|
|
@ -115,6 +115,26 @@ class CollisionEngine:
|
|||
b = dilated.bounds
|
||||
area = (b[2] - b[0]) * (b[3] - b[1])
|
||||
self.static_is_rect[obj_id] = (abs(dilated.area - area) < 1e-4)
|
||||
return obj_id
|
||||
|
||||
def remove_static_obstacle(self, obj_id: int) -> None:
|
||||
"""
|
||||
Remove a static obstacle by ID.
|
||||
"""
|
||||
if obj_id not in self.static_geometries:
|
||||
return
|
||||
|
||||
bounds = self.static_dilated[obj_id].bounds
|
||||
self.static_index.delete(obj_id, bounds)
|
||||
|
||||
del self.static_geometries[obj_id]
|
||||
del self.static_dilated[obj_id]
|
||||
del self.static_prepared[obj_id]
|
||||
del self.static_is_rect[obj_id]
|
||||
|
||||
self.static_tree = None
|
||||
self._static_raw_tree = None
|
||||
self.static_grid = {}
|
||||
|
||||
def _ensure_static_tree(self) -> None:
|
||||
if self.static_tree is None and self.static_dilated:
|
||||
|
|
@ -229,26 +249,43 @@ class CollisionEngine:
|
|||
tb = result.total_dilated_bounds
|
||||
if tb is None: return 0
|
||||
self._ensure_dynamic_grid()
|
||||
if not self.dynamic_grid: return 0
|
||||
dynamic_grid = self.dynamic_grid
|
||||
if not dynamic_grid: return 0
|
||||
|
||||
cs_inv = self._inv_grid_cell_size
|
||||
gx_min, gy_min = int(tb[0] * cs_inv), int(tb[1] * cs_inv)
|
||||
gx_max, gy_max = int(tb[2] * cs_inv), int(tb[3] * cs_inv)
|
||||
gx_min = int(tb[0] * cs_inv)
|
||||
gy_min = int(tb[1] * cs_inv)
|
||||
gx_max = int(tb[2] * cs_inv)
|
||||
gy_max = int(tb[3] * cs_inv)
|
||||
|
||||
any_possible = False
|
||||
dynamic_grid = self.dynamic_grid
|
||||
dynamic_geometries = self.dynamic_geometries
|
||||
|
||||
# Fast path for single cell
|
||||
if gx_min == gx_max and gy_min == gy_max:
|
||||
cell = (gx_min, gy_min)
|
||||
if cell in dynamic_grid:
|
||||
for obj_id in dynamic_grid[cell]:
|
||||
if dynamic_geometries[obj_id][0] != net_id:
|
||||
return self._check_real_congestion(result, net_id)
|
||||
return 0
|
||||
|
||||
# General case
|
||||
any_possible = False
|
||||
for gx in range(gx_min, gx_max + 1):
|
||||
for gy in range(gy_min, gy_max + 1):
|
||||
cell = (gx, gy)
|
||||
if cell in dynamic_grid:
|
||||
for obj_id in dynamic_grid[cell]:
|
||||
if dynamic_geometries[obj_id][0] != net_id:
|
||||
any_possible = True; break
|
||||
any_possible = True
|
||||
break
|
||||
if any_possible: break
|
||||
if any_possible: break
|
||||
|
||||
if not any_possible: return 0
|
||||
|
||||
return self._check_real_congestion(result, net_id)
|
||||
|
||||
def _check_real_congestion(self, result: ComponentResult, net_id: str) -> int:
|
||||
self.metrics['congestion_tree_queries'] += 1
|
||||
self._ensure_dynamic_tree()
|
||||
if self.dynamic_tree is None: return 0
|
||||
|
|
@ -259,28 +296,19 @@ class CollisionEngine:
|
|||
possible_total = (tb[0] < d_bounds[:, 2]) & (tb[2] > d_bounds[:, 0]) & \
|
||||
(tb[1] < d_bounds[:, 3]) & (tb[3] > d_bounds[:, 1])
|
||||
|
||||
# Filter by net_id (important for negotiated congestion)
|
||||
valid_hits = (self._dynamic_net_ids_array != net_id)
|
||||
if not numpy.any(possible_total & valid_hits):
|
||||
return 0
|
||||
|
||||
# 2. Per-polygon AABB check using query on geometries (LAZY triggering)
|
||||
# We only trigger evaluation if total bounds intersect with other nets.
|
||||
geoms_to_test = result.dilated_geometry if result.dilated_geometry else result.geometry
|
||||
res_indices, tree_indices = self.dynamic_tree.query(geoms_to_test, predicate='intersects')
|
||||
|
||||
if tree_indices.size == 0:
|
||||
return 0
|
||||
|
||||
# Filter out self-overlaps (from same net)
|
||||
hit_net_ids = numpy.take(self._dynamic_net_ids_array, tree_indices)
|
||||
valid_geoms_hits = (hit_net_ids != net_id)
|
||||
if not numpy.any(valid_geoms_hits):
|
||||
return 0
|
||||
|
||||
# 3. Real geometry check (Only if AABBs intersect with other nets)
|
||||
# We already have hits from STRtree which are accurate for polygons too.
|
||||
# But wait, query(..., predicate='intersects') ALREADY does real check!
|
||||
return int(numpy.sum(valid_geoms_hits))
|
||||
|
||||
def _is_in_safety_zone(self, geometry: Polygon, obj_id: int, start_port: Port | None, end_port: Port | None) -> bool:
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class ComponentResult:
|
|||
__slots__ = (
|
||||
'_geometry', '_dilated_geometry', '_proxy_geometry', '_actual_geometry', '_dilated_actual_geometry',
|
||||
'end_port', 'length', 'move_type', '_bounds', '_dilated_bounds',
|
||||
'_total_bounds', '_total_dilated_bounds', '_t_cache', '_total_geom_list', '_offsets', '_coords_cache',
|
||||
'_total_bounds', '_total_dilated_bounds', '_bounds_cached', '_total_geom_list', '_offsets', '_coords_cache',
|
||||
'_base_result', '_offset', '_lazy_evaluated', 'rel_gx', 'rel_gy', 'rel_go'
|
||||
)
|
||||
|
||||
|
|
@ -58,11 +58,11 @@ class ComponentResult:
|
|||
self.end_port = end_port
|
||||
self.length = length
|
||||
self.move_type = move_type
|
||||
self._t_cache = {}
|
||||
|
||||
self._base_result = _base_result
|
||||
self._offset = _offset
|
||||
self._lazy_evaluated = False
|
||||
self._bounds_cached = False
|
||||
|
||||
if rel_gx is not None:
|
||||
self.rel_gx = rel_gx
|
||||
|
|
@ -138,6 +138,7 @@ class ComponentResult:
|
|||
self._total_bounds = None
|
||||
self._dilated_bounds = None
|
||||
self._total_dilated_bounds = None
|
||||
self._bounds_cached = True
|
||||
|
||||
def _ensure_evaluated(self) -> None:
|
||||
if self._base_result is None or self._lazy_evaluated:
|
||||
|
|
@ -145,16 +146,11 @@ class ComponentResult:
|
|||
|
||||
# Perform Translation
|
||||
dx, dy = self._offset
|
||||
# Base uses its own coords cache
|
||||
base_coords = self._base_result._coords_cache
|
||||
if base_coords is None:
|
||||
self._lazy_evaluated = True
|
||||
return
|
||||
|
||||
new_coords = base_coords + [dx, dy]
|
||||
|
||||
# Translate ALL geometries at once
|
||||
new_total_arr = shapely.set_coordinates(list(self._base_result._total_geom_list), new_coords)
|
||||
# Use shapely.transform which is vectorized and doesn't modify in-place.
|
||||
# This is MUCH faster than cloning with copy.copy and then set_coordinates.
|
||||
import shapely
|
||||
new_total_arr = shapely.transform(self._base_result._total_geom_list, lambda x: x + [dx, dy])
|
||||
new_total = new_total_arr.tolist()
|
||||
|
||||
o = self._base_result._offsets
|
||||
|
|
@ -193,47 +189,53 @@ class ComponentResult:
|
|||
|
||||
@property
|
||||
def bounds(self) -> numpy.ndarray:
|
||||
if self._bounds is None:
|
||||
if self._base_result is not None:
|
||||
dx, dy = self._offset
|
||||
self._bounds = self._base_result.bounds + [dx, dy, dx, dy]
|
||||
if not self._bounds_cached:
|
||||
self._ensure_bounds_evaluated()
|
||||
return self._bounds
|
||||
|
||||
@property
|
||||
def total_bounds(self) -> numpy.ndarray:
|
||||
if self._total_bounds is None:
|
||||
if self._base_result is not None:
|
||||
dx, dy = self._offset
|
||||
self._total_bounds = self._base_result.total_bounds + [dx, dy, dx, dy]
|
||||
if not self._bounds_cached:
|
||||
self._ensure_bounds_evaluated()
|
||||
return self._total_bounds
|
||||
|
||||
@property
|
||||
def dilated_bounds(self) -> numpy.ndarray | None:
|
||||
if self._dilated_bounds is None:
|
||||
if self._base_result is not None and self._base_result.dilated_bounds is not None:
|
||||
dx, dy = self._offset
|
||||
self._dilated_bounds = self._base_result.dilated_bounds + [dx, dy, dx, dy]
|
||||
if not self._bounds_cached:
|
||||
self._ensure_bounds_evaluated()
|
||||
return self._dilated_bounds
|
||||
|
||||
@property
|
||||
def total_dilated_bounds(self) -> numpy.ndarray | None:
|
||||
if self._total_dilated_bounds is None:
|
||||
if self._base_result is not None and self._base_result.total_dilated_bounds is not None:
|
||||
dx, dy = self._offset
|
||||
self._total_dilated_bounds = self._base_result.total_dilated_bounds + [dx, dy, dx, dy]
|
||||
if not self._bounds_cached:
|
||||
self._ensure_bounds_evaluated()
|
||||
return self._total_dilated_bounds
|
||||
|
||||
def _ensure_bounds_evaluated(self) -> None:
|
||||
if self._bounds_cached: return
|
||||
base = self._base_result
|
||||
if base is not None:
|
||||
dx, dy = self._offset
|
||||
# Vectorized addition is faster if we avoid creating new lists/arrays in the loop
|
||||
if base._bounds is not None:
|
||||
self._bounds = base._bounds + [dx, dy, dx, dy]
|
||||
if base._total_bounds is not None:
|
||||
b = base._total_bounds
|
||||
self._total_bounds = numpy.array([b[0]+dx, b[1]+dy, b[2]+dx, b[3]+dy])
|
||||
if base._dilated_bounds is not None:
|
||||
self._dilated_bounds = base._dilated_bounds + [dx, dy, dx, dy]
|
||||
if base._total_dilated_bounds is not None:
|
||||
b = base._total_dilated_bounds
|
||||
self._total_dilated_bounds = numpy.array([b[0]+dx, b[1]+dy, b[2]+dx, b[3]+dy])
|
||||
self._bounds_cached = True
|
||||
|
||||
def translate(self, dx: float, dy: float, rel_gx: int | None = None, rel_gy: int | None = None, rel_go: int | None = None) -> ComponentResult:
|
||||
"""
|
||||
Create a new ComponentResult translated by (dx, dy).
|
||||
"""
|
||||
dxr, dyr = round(dx, 3), round(dy, 3)
|
||||
if (dxr, dyr) == (0.0, 0.0):
|
||||
return self
|
||||
if (dxr, dyr) in self._t_cache:
|
||||
return self._t_cache[(dxr, dyr)]
|
||||
|
||||
new_port = Port(self.end_port.x + dx, self.end_port.y + dy, self.end_port.orientation)
|
||||
# Optimized: no internal cache (already cached in router) and no rounding
|
||||
# Also skip snapping since parent and relative move are already snapped
|
||||
new_port = Port(self.end_port.x + dx, self.end_port.y + dy, self.end_port.orientation, snap=False)
|
||||
|
||||
# LAZY TRANSLATE
|
||||
if self._base_result:
|
||||
|
|
@ -244,7 +246,7 @@ class ComponentResult:
|
|||
base = self
|
||||
new_offset = [dx, dy]
|
||||
|
||||
res = ComponentResult(
|
||||
return ComponentResult(
|
||||
end_port=new_port,
|
||||
length=self.length,
|
||||
move_type=self.move_type,
|
||||
|
|
@ -254,9 +256,6 @@ class ComponentResult:
|
|||
rel_gy=rel_gy,
|
||||
rel_go=rel_go
|
||||
)
|
||||
|
||||
self._t_cache[(dxr, dyr)] = res
|
||||
return res
|
||||
|
||||
|
||||
class Straight:
|
||||
|
|
|
|||
|
|
@ -25,10 +25,20 @@ class Port:
|
|||
x: float,
|
||||
y: float,
|
||||
orientation: float,
|
||||
snap: bool = True
|
||||
) -> None:
|
||||
self.x = snap_nm(x)
|
||||
self.y = snap_nm(y)
|
||||
self.orientation = float(orientation % 360)
|
||||
if snap:
|
||||
self.x = round(x * 1000) / 1000
|
||||
self.y = round(y * 1000) / 1000
|
||||
# Faster orientation normalization for common cases
|
||||
if 0 <= orientation < 360:
|
||||
self.orientation = float(orientation)
|
||||
else:
|
||||
self.orientation = float(orientation % 360)
|
||||
else:
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.orientation = float(orientation)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f'Port(x={self.x}, y={self.y}, orientation={self.orientation})'
|
||||
|
|
|
|||
Loading…
Reference in a new issue