Query exact corner visibility on demand
Cache local queries at indexed corners and preserve exact distance limits. Keep the legacy counters available and report corner-index builds in the performance summary.
This commit is contained in:
parent
725f385ba3
commit
a6b0b9216a
8 changed files with 58 additions and 109 deletions
|
|
@ -24,6 +24,8 @@ Pair each counter with a question about the work. Use a repeated wall-time compa
|
|||
|
||||
`nets_routed` and `nets_reached_target` count attempts. Use `result.is_valid` or `verified_net_ids` to count final successes. Duration counters ending in `_total_ns` can vary between identical runs. For the full field list, see the [`RouteMetrics` definition](../inire/results.py).
|
||||
|
||||
Exact corner visibility uses cached local queries. Track them with `visibility_corner_queries_exact` and `visibility_point_cache_hits`. The legacy graph counters `visibility_builds` and `visibility_corner_pairs_checked` report zero, as do `ray_cast_calls_visibility_build` and `ray_cast_calls_visibility_query`.
|
||||
|
||||
Search bounds the two move caches by entries and polygon coordinates. It drops the corresponding static-admission keys when evicting positioned moves. An unusually large live frontier drops dominated nodes and retains the best 40,000 entries once it exceeds 50,000. Check `frontier_trim_events` and `frontier_nodes_discarded` when diagnosing a large failed attempt.
|
||||
|
||||
## Traces
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ For structured logging, `dataclasses.asdict(run)` preserves the per-net fields.
|
|||
| `bend_proxy_geometry` | `None` | Override the bend proxy. A polygon applies to 90-degree bends. A custom polygon proxy by itself keeps the standard physical arc and emits a warning. |
|
||||
| `bend_physical_geometry` | `None` | Override 90-degree bend geometry with `"arc"` or a polygon. A custom physical polygon also supplies the default proxy when the collision type is `"arc"`. Define the polygon relative to the arc center and include both tangent extensions for a fractional radius. |
|
||||
| `bend_clip_margin` | `None` | Optional legacy shrink margin for `"clipped_bbox"`. The default uses an eight-point proxy for each arc and extends it along any grid tangents. |
|
||||
| `visibility_guidance` | `"tangent_corner"` | Choose tangent candidates, exact corner visibility with `"exact_corner"`, or no visibility suggestions with `"off"`. |
|
||||
| `visibility_guidance` | `"tangent_corner"` | Choose tangent candidates, local visibility tests from indexed corners with `"exact_corner"`, or no visibility suggestions with `"off"`. |
|
||||
| `sbend_outlines` | `()` | Complete custom S-bend polygons, supplied as `SBendOutline(offset, radius, width, polygon)` entries. Each entry applies to its signed offset, radius, and net width. |
|
||||
| `proximity_range` | `10.0` | Obstacle proximity scoring range in layout units. Scale it with the coordinates when changing units. Large perimeters use an exact boundary index once the sample budget is reached. |
|
||||
| `bend_physical_length` | `None` | Optional authored centerline length for the shared custom 90-degree template, including tangent extensions. Search, refinement, and reports use it when supplied. |
|
||||
|
|
|
|||
|
|
@ -800,10 +800,6 @@ class RoutingWorld:
|
|||
self.metrics.total_ray_cast_calls_expand_snap += 1
|
||||
elif caller == "expand_forward":
|
||||
self.metrics.total_ray_cast_calls_expand_forward += 1
|
||||
elif caller == "visibility_build":
|
||||
self.metrics.total_ray_cast_calls_visibility_build += 1
|
||||
elif caller == "visibility_query":
|
||||
self.metrics.total_ray_cast_calls_visibility_query += 1
|
||||
elif caller == "visibility_tangent":
|
||||
self.metrics.total_ray_cast_calls_visibility_tangent += 1
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -9,19 +9,16 @@ if TYPE_CHECKING:
|
|||
from inire.geometry.collision import RoutingWorld
|
||||
from inire.geometry.primitives import Port
|
||||
|
||||
|
||||
class VisibilityManager:
|
||||
"""
|
||||
Manages corners of static obstacles for sparse A* / Visibility Graph jumps.
|
||||
"""
|
||||
"""Index static obstacle corners and query them for sparse A* moves."""
|
||||
__slots__ = (
|
||||
"collision_engine",
|
||||
"corners",
|
||||
"_exact_corners",
|
||||
"corner_index",
|
||||
"_corner_graph",
|
||||
"_point_visibility_cache",
|
||||
"_corner_index_version",
|
||||
"_corner_graph_version",
|
||||
)
|
||||
|
||||
def __init__(self, collision_engine: RoutingWorld) -> None:
|
||||
|
|
@ -29,10 +26,8 @@ class VisibilityManager:
|
|||
self.corners: list[tuple[float, float]] = []
|
||||
self._exact_corners: list[tuple[float, float]] = []
|
||||
self.corner_index = rtree.index.Index()
|
||||
self._corner_graph: dict[int, list[tuple[float, float, float]]] = {}
|
||||
self._point_visibility_cache: dict[tuple[int, int, int], list[tuple[float, float, float]]] = {}
|
||||
self._point_visibility_cache: dict[tuple[float, float, float], list[tuple[float, float, float]]] = {}
|
||||
self._corner_index_version = -1
|
||||
self._corner_graph_version = -1
|
||||
|
||||
def clear_cache(self) -> None:
|
||||
"""
|
||||
|
|
@ -41,30 +36,20 @@ class VisibilityManager:
|
|||
self.corners = []
|
||||
self._exact_corners = []
|
||||
self.corner_index = rtree.index.Index()
|
||||
self._corner_graph = {}
|
||||
self._point_visibility_cache = {}
|
||||
self._corner_index_version = -1
|
||||
self._corner_graph_version = -1
|
||||
|
||||
def ensure_corner_index_current(self) -> None:
|
||||
if self._corner_index_version != self.collision_engine.get_static_version():
|
||||
self._build_corner_index()
|
||||
|
||||
def ensure_corner_graph_current(self) -> None:
|
||||
self.ensure_corner_index_current()
|
||||
static_version = self.collision_engine.get_static_version()
|
||||
if self._corner_graph_version != static_version:
|
||||
self._build_corner_graph()
|
||||
|
||||
def _build_corner_index(self) -> None:
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_corner_index_builds += 1
|
||||
self.corners = []
|
||||
self._exact_corners = []
|
||||
self.corner_index = rtree.index.Index()
|
||||
self._corner_graph = {}
|
||||
self._point_visibility_cache = {}
|
||||
self._corner_graph_version = -1
|
||||
self._corner_index_version = self.collision_engine.get_static_version()
|
||||
raw_corners = []
|
||||
for poly in self.collision_engine.iter_static_dilated_geometries():
|
||||
|
|
@ -92,37 +77,6 @@ class VisibilityManager:
|
|||
for i, (x, y) in enumerate(self.corners):
|
||||
self.corner_index.insert(i, (x, y, x, y))
|
||||
|
||||
def _build_corner_graph(self) -> None:
|
||||
"""
|
||||
Pre-compute corner-to-corner visibility from the current corner index.
|
||||
"""
|
||||
self.ensure_corner_index_current()
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_builds += 1
|
||||
self._corner_graph = {}
|
||||
self._corner_graph_version = self.collision_engine.get_static_version()
|
||||
|
||||
if not self.corners:
|
||||
return
|
||||
|
||||
# Pre-compute visibility graph between corners
|
||||
num_corners = len(self.corners)
|
||||
if num_corners > 200:
|
||||
# Limit pre-computation if too many corners
|
||||
return
|
||||
|
||||
for i in range(num_corners):
|
||||
self._corner_graph[i] = []
|
||||
for j in range(num_corners):
|
||||
if i == j:
|
||||
continue
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_corner_pairs_checked += 1
|
||||
start, end = self._exact_corners[i], self._exact_corners[j]
|
||||
if self.collision_engine.corner_segment_visible(start, end):
|
||||
cx, cy = self.corners[j]
|
||||
self._corner_graph[i].append((cx, cy, hypot(end[0] - start[0], end[1] - start[1])))
|
||||
|
||||
def _corner_idx_at(self, origin: Port) -> int | None:
|
||||
self.ensure_corner_index_current()
|
||||
ox, oy = round(origin.x, 3), round(origin.y, 3)
|
||||
|
|
@ -176,42 +130,42 @@ class VisibilityManager:
|
|||
|
||||
return sorted(candidate_ids)
|
||||
|
||||
def get_point_visibility(self, origin: Port, max_dist: float = 1000.0) -> list[tuple[float, float, float]]:
|
||||
"""
|
||||
Find visible corners from an arbitrary point.
|
||||
This may test local corner segments against keepouts.
|
||||
"""
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_point_queries += 1
|
||||
self.ensure_corner_index_current()
|
||||
def get_corner_visibility(self, origin: Port, max_dist: float = 1000.0) -> list[tuple[float, float, float]]:
|
||||
"""Return visible neighbors when the origin is at an indexed corner."""
|
||||
metrics = self.collision_engine.metrics
|
||||
if metrics is not None:
|
||||
metrics.total_visibility_corner_queries_exact += 1
|
||||
if max_dist < 0:
|
||||
return []
|
||||
|
||||
corner_idx = self._corner_idx_at(origin)
|
||||
if corner_idx is not None:
|
||||
self.ensure_corner_graph_current()
|
||||
if corner_idx is not None and corner_idx in self._corner_graph:
|
||||
return [corner for corner in self._corner_graph[corner_idx] if corner[2] <= max_dist]
|
||||
if corner_idx is None:
|
||||
return []
|
||||
if metrics is not None:
|
||||
metrics.total_visibility_corner_hits_exact += 1
|
||||
metrics.total_visibility_point_queries += 1
|
||||
|
||||
ox, oy = round(origin.x, 3), round(origin.y, 3)
|
||||
cache_key = (int(ox * 1000), int(oy * 1000), int(round(max_dist * 1000)))
|
||||
start = self._exact_corners[corner_idx]
|
||||
cache_key = (*start, max_dist)
|
||||
if cache_key in self._point_visibility_cache:
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_point_cache_hits += 1
|
||||
if metrics is not None:
|
||||
metrics.total_visibility_point_cache_hits += 1
|
||||
return self._point_visibility_cache[cache_key]
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_point_cache_misses += 1
|
||||
if metrics is not None:
|
||||
metrics.total_visibility_point_cache_misses += 1
|
||||
|
||||
bounds = (origin.x - max_dist, origin.y - max_dist, origin.x + max_dist, origin.y + max_dist)
|
||||
# The index stores rounded corner coordinates. Filter by exact distance below.
|
||||
reach = max_dist + 0.001
|
||||
x, y = start
|
||||
bounds = (x - reach, y - reach, x + reach, y + reach)
|
||||
candidates = sorted(self.corner_index.intersection(bounds))
|
||||
|
||||
visible = []
|
||||
start = (origin.x, origin.y) if corner_idx is None else self._exact_corners[corner_idx]
|
||||
for i in candidates:
|
||||
end = self._exact_corners[i]
|
||||
dist = hypot(end[0] - start[0], end[1] - start[1])
|
||||
dist = hypot(end[0] - x, end[1] - y)
|
||||
|
||||
if dist > max_dist or dist < 1e-3:
|
||||
if dist == 0 or dist > max_dist:
|
||||
continue
|
||||
|
||||
if self.collision_engine.corner_segment_visible(start, end):
|
||||
|
|
@ -222,19 +176,3 @@ class VisibilityManager:
|
|||
self._point_visibility_cache.clear()
|
||||
self._point_visibility_cache[cache_key] = visible
|
||||
return visible
|
||||
|
||||
def get_corner_visibility(self, origin: Port, max_dist: float = 1000.0) -> list[tuple[float, float, float]]:
|
||||
"""
|
||||
Return visible neighbors when the origin is already at a known corner.
|
||||
"""
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_corner_queries_exact += 1
|
||||
if max_dist < 0:
|
||||
return []
|
||||
|
||||
corner_idx = self._corner_idx_at(origin)
|
||||
if corner_idx is None:
|
||||
return []
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_corner_hits_exact += 1
|
||||
return self.get_point_visibility(origin, max_dist)
|
||||
|
|
|
|||
|
|
@ -411,7 +411,7 @@ def test_route_astar_supports_all_visibility_guidance_modes(
|
|||
assert validation["connectivity_ok"]
|
||||
|
||||
|
||||
def test_tangent_corner_mode_avoids_exact_visibility_graph_builds(basic_evaluator: CostEvaluator) -> None:
|
||||
def test_tangent_corner_mode_uses_only_tangent_queries(basic_evaluator: CostEvaluator) -> None:
|
||||
obstacle = Polygon([(30, 10), (50, 10), (50, 40), (30, 40)])
|
||||
basic_evaluator.collision_engine.add_static_obstacle(obstacle)
|
||||
basic_evaluator.danger_map.precompute([obstacle])
|
||||
|
|
@ -427,9 +427,9 @@ def test_tangent_corner_mode_avoids_exact_visibility_graph_builds(basic_evaluato
|
|||
path = _route(context, Port(0, 0, 0), Port(80, 50, 0))
|
||||
|
||||
assert path is not None
|
||||
assert context.metrics.total_visibility_builds == 0
|
||||
assert context.metrics.total_visibility_corner_pairs_checked == 0
|
||||
assert context.metrics.total_ray_cast_calls_visibility_build == 0
|
||||
assert context.metrics.total_visibility_tangent_candidate_scans > 0
|
||||
assert context.metrics.total_visibility_corner_queries_exact == 0
|
||||
assert context.metrics.total_visibility_point_queries == 0
|
||||
|
||||
|
||||
def test_route_astar_repeated_searches_succeed_with_small_cache_limit(basic_evaluator: CostEvaluator) -> None:
|
||||
|
|
|
|||
|
|
@ -6,15 +6,16 @@ from inire.router._astar_types import AStarMetrics
|
|||
from inire.router.visibility import VisibilityManager
|
||||
|
||||
|
||||
def test_point_visibility_cache_respects_max_distance() -> None:
|
||||
def test_corner_visibility_cache_respects_max_distance() -> None:
|
||||
engine = RoutingWorld(clearance=0.0)
|
||||
engine.add_static_obstacle(box(-2, 0, 0, 2))
|
||||
engine.add_static_obstacle(box(10, 20, 20, 30))
|
||||
engine.add_static_obstacle(box(100, 20, 110, 30))
|
||||
visibility = VisibilityManager(engine)
|
||||
origin = Port(0, 0, 0)
|
||||
|
||||
near_corners = visibility.get_point_visibility(origin, max_dist=40.0)
|
||||
far_corners = visibility.get_point_visibility(origin, max_dist=200.0)
|
||||
near_corners = visibility.get_corner_visibility(origin, max_dist=40.0)
|
||||
far_corners = visibility.get_corner_visibility(origin, max_dist=200.0)
|
||||
|
||||
assert len(near_corners) == 3
|
||||
assert len(far_corners) > len(near_corners)
|
||||
|
|
@ -30,16 +31,14 @@ def test_visibility_manager_is_lazy_until_queried() -> None:
|
|||
|
||||
assert visibility.corners == []
|
||||
assert engine.metrics.total_visibility_corner_index_builds == 0
|
||||
assert engine.metrics.total_visibility_builds == 0
|
||||
|
||||
visibility.ensure_corner_index_current()
|
||||
|
||||
assert visibility.corners
|
||||
assert engine.metrics.total_visibility_corner_index_builds == 1
|
||||
assert engine.metrics.total_visibility_builds == 0
|
||||
|
||||
|
||||
def test_exact_corner_visibility_builds_graph_once_per_static_version() -> None:
|
||||
def test_exact_corner_visibility_caches_local_queries() -> None:
|
||||
engine = RoutingWorld(clearance=0.0)
|
||||
engine.metrics = AStarMetrics()
|
||||
engine.add_static_obstacle(box(10, 20, 20, 30))
|
||||
|
|
@ -51,7 +50,21 @@ def test_exact_corner_visibility_builds_graph_once_per_static_version() -> None:
|
|||
|
||||
assert second == first
|
||||
assert engine.metrics.total_visibility_corner_index_builds == 1
|
||||
assert engine.metrics.total_visibility_builds == 1
|
||||
assert engine.metrics.total_visibility_point_cache_misses == 1
|
||||
assert engine.metrics.total_visibility_point_cache_hits == 1
|
||||
assert engine.metrics.total_visibility_builds == 0
|
||||
assert engine.metrics.total_visibility_corner_pairs_checked == 0
|
||||
|
||||
|
||||
def test_exact_corner_visibility_respects_fractional_distance_limits() -> None:
|
||||
engine = RoutingWorld(clearance=0.0)
|
||||
engine.add_static_obstacle(box(0, 0, 1, 1))
|
||||
engine.add_static_obstacle(box(-11, 0, -9.9996, 1))
|
||||
visibility = VisibilityManager(engine)
|
||||
origin = Port(0, 0, 0)
|
||||
|
||||
assert visibility.get_corner_visibility(origin, max_dist=9.99955) == []
|
||||
assert visibility.get_corner_visibility(origin, max_dist=9.99965) == [(-10.0, 0.0, 9.9996)]
|
||||
|
||||
|
||||
def test_exact_corner_visibility_accepts_only_endpoint_contact_and_scales_to_large_boards() -> None:
|
||||
|
|
@ -82,13 +95,13 @@ def test_clear_cache_invalidates_without_rebuilding_and_static_change_rebuilds_l
|
|||
|
||||
visibility.get_corner_visibility(Port(10, 20, 0), max_dist=100.0)
|
||||
assert engine.metrics.total_visibility_corner_index_builds == 1
|
||||
assert engine.metrics.total_visibility_builds == 1
|
||||
assert engine.metrics.total_visibility_point_cache_misses == 1
|
||||
|
||||
visibility.clear_cache()
|
||||
|
||||
assert visibility.corners == []
|
||||
assert engine.metrics.total_visibility_corner_index_builds == 1
|
||||
assert engine.metrics.total_visibility_builds == 1
|
||||
assert engine.metrics.total_visibility_point_cache_misses == 1
|
||||
|
||||
engine.add_static_obstacle(box(40, 20, 50, 30))
|
||||
|
||||
|
|
@ -96,7 +109,7 @@ def test_clear_cache_invalidates_without_rebuilding_and_static_change_rebuilds_l
|
|||
|
||||
assert visible == []
|
||||
assert engine.metrics.total_visibility_corner_index_builds == 2
|
||||
assert engine.metrics.total_visibility_builds == 2
|
||||
assert engine.metrics.total_visibility_point_cache_misses == 2
|
||||
|
||||
|
||||
def test_tangent_corner_candidate_query_matches_bruteforce_filter() -> None:
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ Validate nonnegative integer budgets and enumerate allowed order names at constr
|
|||
|
||||
## 7. Make exact-corner visibility useful on real corner states — Medium
|
||||
|
||||
With clearance zero, put two keepouts at `box(10, 20, 20, 30)` and `box(-10, 10, 0, 15)`. The segment from `(10, 20)` to `(0, 15)` touches the two keepouts only at its endpoints. The visibility ray returns reach zero because it counts the source corner itself as a hit. The exact-corner query returns no visible corner. In a separate probe with 200 indexed corners, adding one distant rectangle dropped the same local query from five visible corners to zero. Above 200 corners the graph builder skips construction and the exact query has no local fallback. [Corner graph](../inire/router/visibility.py#L93) · [Ray intersection](../inire/geometry/collision.py#L812) · [Visibility option](../docs/user/api-reference.md#searchoptions)
|
||||
With clearance zero, put two keepouts at `box(10, 20, 20, 30)` and `box(-10, 10, 0, 15)`. The segment from `(10, 20)` to `(0, 15)` touches the two keepouts only at its endpoints. The visibility ray returns reach zero because it counts the source corner itself as a hit. The exact-corner query returns no visible corner. In a separate probe with 200 indexed corners, adding one distant rectangle dropped the same local query from five visible corners to zero. Above 200 corners the graph builder skips construction and the exact query has no local fallback. [Corner visibility](../inire/router/visibility.py) · [Ray intersection](../inire/geometry/collision.py#L812) · [Visibility option](../docs/user/api-reference.md#searchoptions)
|
||||
|
||||
Give visibility rays endpoint-aware semantics and leave move admission responsible for physical clearance. Compute or cache local exact neighbors on demand when global precomputation is skipped. Check an outward ray, a ray entering the source keepout, a blocker strictly between corners, and a local query before and after adding distant corners. Confirm the default tangent mode keeps its existing coverage and work.
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ def _render_markdown(payload: dict[str, object]) -> str:
|
|||
"Use `scripts/diff_performance_baseline.py` to compare a fresh run against that snapshot.",
|
||||
"",
|
||||
"| Scenario | Duration (s) | Total | Valid | Reached | Iter | Nets Routed | Nodes | Ray Casts | "
|
||||
"Moves Gen | Moves Added | Dyn Tree | Visibility Builds | Congestion Checks | Verify Calls |",
|
||||
"Moves Gen | Moves Added | Dyn Tree | Corner Index Builds | Congestion Checks | Verify Calls |",
|
||||
"| :-- | --: | --: | --: | --: | --: | --: | --: | --: | --: | --: | --: | --: | --: | --: |",
|
||||
]
|
||||
for row in rows:
|
||||
|
|
@ -53,7 +53,7 @@ def _render_markdown(payload: dict[str, object]) -> str:
|
|||
f"{metrics['moves_generated']} | "
|
||||
f"{metrics['moves_added']} | "
|
||||
f"{metrics['dynamic_tree_rebuilds']} | "
|
||||
f"{metrics['visibility_builds']} | "
|
||||
f"{metrics['visibility_corner_index_builds']} | "
|
||||
f"{metrics['congestion_check_calls']} | "
|
||||
f"{metrics['verify_path_report_calls']} |"
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue