consistency and speed

This commit is contained in:
Jan Petykiewicz 2026-03-09 02:26:27 -07:00
commit c9bb8d6469

View file

@ -3,13 +3,14 @@ from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
import shapely
if TYPE_CHECKING:
from shapely.geometry import Polygon
class DangerMap:
"""A pre-computed grid for heuristic proximity costs."""
"""A pre-computed grid for heuristic proximity costs, vectorized for performance."""
def __init__(
self,
@ -28,47 +29,36 @@ class DangerMap:
self.width_cells = int(np.ceil((self.maxx - self.minx) / self.resolution))
self.height_cells = int(np.ceil((self.maxy - self.miny) / self.resolution))
# Use uint8 for memory efficiency if normalized, or float16/float32.
# Let's use float32 for simplicity and precision in the prototype.
# For a 1000x1000 grid, this is only 4MB.
# For 20000x20000, it's 1.6GB.
self.grid = np.zeros((self.width_cells, self.height_cells), dtype=np.float32)
def precompute(self, obstacles: list[Polygon]) -> None:
"""Pre-compute the proximity costs for the entire grid."""
# For each cell, find distance to nearest obstacle.
# This is a distance transform problem.
# For the prototype, we can use a simpler approach or scipy.ndimage.distance_transform_edt.
"""Pre-compute the proximity costs for the entire grid using vectorized operations."""
from scipy.ndimage import distance_transform_edt
# Create a binary mask of obstacles
# 1. Create a binary mask of obstacles
mask = np.ones((self.width_cells, self.height_cells), dtype=bool)
# Rasterize obstacles (simplified: mark cells whose center is inside an obstacle)
# This is slow for many obstacles; in a real engine, we'd use a faster rasterizer.
from shapely.geometry import Point
# Create coordinate grids
x_coords = np.linspace(self.minx + self.resolution/2, self.maxx - self.resolution/2, self.width_cells)
y_coords = np.linspace(self.miny + self.resolution/2, self.maxy - self.resolution/2, self.height_cells)
xv, yv = np.meshgrid(x_coords, y_coords, indexing='ij')
for poly in obstacles:
# Get bounding box in grid coordinates
p_minx, p_miny, p_maxx, p_maxy = poly.bounds
x_start = max(0, int((p_minx - self.minx) / self.resolution))
x_end = min(self.width_cells, int((p_maxx - self.minx) / self.resolution) + 1)
y_start = max(0, int((p_miny - self.miny) / self.resolution))
y_end = min(self.height_cells, int((p_maxy - self.miny) / self.resolution) + 1)
# Use shapely.contains_xy for fast vectorized point-in-polygon check
in_poly = shapely.contains_xy(poly, xv, yv)
mask[in_poly] = False
for ix in range(x_start, x_end):
cx = self.minx + (ix + 0.5) * self.resolution
for iy in range(y_start, y_end):
cy = self.miny + (iy + 0.5) * self.resolution
if poly.contains(Point(cx, cy)):
mask[ix, iy] = False
# Distance transform (mask=True for empty space)
# 2. Distance transform (mask=True for empty space)
distances = distance_transform_edt(mask) * self.resolution
# Proximity cost: k / d^2 if d < threshold, else 0
# To avoid division by zero, we cap distances at a small epsilon (e.g. 0.1um)
# 3. Proximity cost: k / d^2 if d < threshold, else 0
# Cap distances at a small epsilon (e.g. 0.1um) to avoid division by zero
safe_distances = np.maximum(distances, 0.1)
self.grid = np.where(distances < self.safety_threshold, self.k / (safe_distances**2), 0.0).astype(np.float32)
self.grid = np.where(
distances < self.safety_threshold,
self.k / (safe_distances**2),
0.0
).astype(np.float32)
def is_within_bounds(self, x: float, y: float) -> bool:
"""Check if a coordinate is within the design bounds."""