Simplify routing verification and isolate benchmark policies

This commit is contained in:
Jan Petykiewicz 2026-11-26 21:55:34 -08:00
commit ec2768853e
2 changed files with 1874 additions and 1164 deletions

View file

@ -142,12 +142,6 @@ class RoutingWorld:
def _ensure_static_raw_tree(self) -> None:
self._static_obstacles.ensure_raw_tree()
def _ensure_dynamic_tree(self) -> None:
self._dynamic_paths.ensure_tree()
def _ensure_dynamic_grid(self) -> None:
self._dynamic_paths.ensure_grid()
def add_path(
self,
net_id: str,
@ -349,10 +343,6 @@ class RoutingWorld:
if not dynamic_paths.dilated:
return {}
self._ensure_dynamic_grid()
if not dynamic_paths.grid:
return {}
candidates_by_net: dict[str, dict[int, set[int]]] = {}
for test_geometry_index, test_bounds in enumerate(result.dilated_bounds):
if not self._has_possible_congestion_in_grid(test_bounds, net_id):
@ -407,16 +397,16 @@ class RoutingWorld:
net_id: str,
) -> bool:
gx_min, gy_min, gx_max, gy_max = grid_cell_span(bounds, self.grid_cell_size)
dynamic_paths = self._dynamic_paths
cells = self._dynamic_paths.grid_net_obj_ids
if gx_min == gx_max and gy_min == gy_max:
net_counts = dynamic_paths.grid_net_counts.get((gx_min, gy_min))
return bool(net_counts and (len(net_counts) > 1 or net_id not in net_counts))
members = cells.get((gx_min, gy_min))
return bool(members and (len(members) > 1 or net_id not in members))
for gx in range(gx_min, gx_max + 1):
for gy in range(gy_min, gy_max + 1):
net_counts = dynamic_paths.grid_net_counts.get((gx, gy))
if net_counts and (len(net_counts) > 1 or net_id not in net_counts):
members = cells.get((gx, gy))
if members and (len(members) > 1 or net_id not in members):
return True
return False
@ -430,10 +420,6 @@ class RoutingWorld:
if not dynamic_paths.dilated:
return False
self._ensure_dynamic_grid()
if not dynamic_paths.grid:
return False
for bounds in result.dilated_bounds:
gx_min, gy_min, gx_max, gy_max = grid_cell_span(bounds, self.grid_cell_size)
cache_key = (net_id, gx_min, gy_min, gx_max, gy_max)

View file

@ -1,13 +1,10 @@
from __future__ import annotations
from contextlib import suppress
from typing import TYPE_CHECKING
import numpy
import rtree
from shapely.strtree import STRtree
from inire.geometry.index_helpers import build_index_payload, iter_grid_cells
from inire.geometry.index_helpers import iter_grid_cells
if TYPE_CHECKING:
from collections.abc import Sequence
@ -29,16 +26,10 @@ class DynamicPathIndex:
"net_envelopes",
"net_envelope_obj_ids",
"net_envelope_obj_to_net",
"tree",
"obj_ids",
"grid",
"grid_net_obj_ids",
"grid_net_counts",
"obj_cells",
"net_to_obj_ids",
"id_counter",
"net_ids_array",
"bounds_array",
)
def __init__(self, engine: RoutingWorld) -> None:
@ -52,16 +43,10 @@ class DynamicPathIndex:
self.net_envelopes: dict[str, tuple[float, float, float, float]] = {}
self.net_envelope_obj_ids: dict[str, int] = {}
self.net_envelope_obj_to_net: dict[int, str] = {}
self.tree: STRtree | None = None
self.obj_ids: numpy.ndarray = numpy.array([], dtype=numpy.int32)
self.grid: dict[tuple[int, int], list[int]] = {}
self.grid_net_obj_ids: dict[tuple[int, int], dict[str, set[int]]] = {}
self.grid_net_counts: dict[tuple[int, int], dict[str, int]] = {}
self.obj_cells: dict[int, tuple[tuple[int, int], ...]] = {}
self.net_to_obj_ids: dict[str, set[int]] = {}
self.id_counter = 0
self.net_ids_array = numpy.array([], dtype=object)
self.bounds_array = numpy.array([], dtype=numpy.float64).reshape(0, 4)
def _combine_net_bounds(self, obj_ids: set[int]) -> tuple[float, float, float, float]:
first_obj_id = next(iter(obj_ids))
@ -104,34 +89,6 @@ class DynamicPathIndex:
return
self._set_net_envelope(net_id, self._combine_net_bounds(obj_ids))
def invalidate_queries(self) -> None:
self.tree = None
self.grid = {}
self.grid_net_obj_ids = {}
self.grid_net_counts = {}
self.obj_cells = {}
def ensure_tree(self) -> None:
if self.tree is None and self.dilated:
if self.engine.metrics is not None:
self.engine.metrics.total_dynamic_tree_rebuilds += 1
ids, geometries, bounds_array = build_index_payload(self.dilated)
self.tree = STRtree(geometries)
self.obj_ids = numpy.array(ids, dtype=numpy.int32)
self.bounds_array = bounds_array
net_ids = [self.geometries[obj_id][0] for obj_id in self.obj_ids]
self.net_ids_array = numpy.array(net_ids, dtype=object)
def ensure_grid(self) -> None:
if self.grid or not self.dilated:
return
if self.engine.metrics is not None:
self.engine.metrics.total_dynamic_grid_rebuilds += 1
cell_size = self.engine.grid_cell_size
for obj_id, polygon in self.dilated.items():
self._register_grid_membership(obj_id, self.geometries[obj_id][0], polygon.bounds, cell_size=cell_size)
def _register_grid_membership(
self,
obj_id: int,
@ -143,39 +100,17 @@ class DynamicPathIndex:
cells = tuple(iter_grid_cells(bounds, cell_size))
self.obj_cells[obj_id] = cells
for cell in cells:
self.grid.setdefault(cell, []).append(obj_id)
net_obj_ids = self.grid_net_obj_ids.setdefault(cell, {})
net_obj_ids.setdefault(net_id, set()).add(obj_id)
net_counts = self.grid_net_counts.setdefault(cell, {})
net_counts[net_id] = net_counts.get(net_id, 0) + 1
self.grid_net_obj_ids.setdefault(cell, {}).setdefault(net_id, set()).add(obj_id)
def _unregister_grid_membership(self, obj_id: int, net_id: str) -> None:
cells = self.obj_cells.pop(obj_id, ())
for cell in cells:
obj_ids = self.grid.get(cell)
if obj_ids is not None:
with suppress(ValueError):
obj_ids.remove(obj_id)
if not obj_ids:
self.grid.pop(cell, None)
net_obj_ids = self.grid_net_obj_ids.get(cell)
if net_obj_ids is not None:
member_ids = net_obj_ids.get(net_id)
if member_ids is not None:
member_ids.discard(obj_id)
if not member_ids:
net_obj_ids.pop(net_id, None)
if not net_obj_ids:
self.grid_net_obj_ids.pop(cell, None)
net_counts = self.grid_net_counts.get(cell)
if net_counts is not None:
remaining = net_counts.get(net_id, 0) - 1
if remaining > 0:
net_counts[net_id] = remaining
else:
net_counts.pop(net_id, None)
if not net_counts:
self.grid_net_counts.pop(cell, None)
for cell in self.obj_cells.pop(obj_id):
net_obj_ids = self.grid_net_obj_ids[cell]
member_ids = net_obj_ids[net_id]
member_ids.remove(obj_id)
if not member_ids:
del net_obj_ids[net_id]
if not net_obj_ids:
del self.grid_net_obj_ids[cell]
def add_path(
self,
@ -200,7 +135,6 @@ class DynamicPathIndex:
self.net_to_obj_ids.setdefault(net_id, set()).add(obj_id)
self._register_grid_membership(obj_id, net_id, dilated_bounds, cell_size=cell_size)
self._refresh_net_envelope(net_id)
self.tree = None
def remove_path(self, net_id: str) -> None:
to_remove = list(self.net_to_obj_ids.get(net_id, ()))
@ -229,4 +163,3 @@ class DynamicPathIndex:
self.net_to_obj_ids.pop(net_id, None)
for net_id in affected_nets:
self._refresh_net_envelope(net_id)
self.tree = None