misc improvements?

This commit is contained in:
jan 2026-03-11 23:40:09 -07:00
commit c6116f88f3

View file

@ -84,25 +84,32 @@ class CostEvaluator:
def h_manhattan(self, current: Port, target: Port) -> float:
"""
Heuristic: weighted Manhattan distance + orientation penalty.
Args:
current: Current port state.
target: Target port state.
Returns:
Heuristic cost estimate.
Heuristic: weighted Manhattan distance + mandatory turn penalties.
"""
dx = abs(current.x - target.x)
dy = abs(current.y - target.y)
dist = dx + dy
# Orientation penalty if not aligned with target entry
# If we need to turn, the cost is at least min_bend_radius * pi/2
# But we also need to account for the physical distance required for the turn.
# Mandatory turn penalty:
# If we need to change Y and we are facing East/West (or change X and facing North/South),
# we MUST turn at least twice to reach the target with the same orientation.
penalty = 0.0
if current.orientation != target.orientation:
# 90-degree turn cost: radius 50 -> ~78.5 um + penalty
# Check if we need to change "transverse" coordinate
needs_transverse = False
if abs(current.orientation % 180) < 0.1: # Horizontal
if abs(dy) > 1e-3:
needs_transverse = True
else: # Vertical
if abs(dx) > 1e-3:
needs_transverse = True
if needs_transverse:
# At least 2 bends needed. Radius 50 -> 78.5 each.
# Plus bend_penalty (default 250 each).
penalty += 2 * (78.5 + self.config.bend_penalty)
elif abs(current.orientation - target.orientation) > 0.1:
# Needs at least 1 bend
penalty += 78.5 + self.config.bend_penalty
return self.greedy_h_weight * (dist + penalty)
@ -118,6 +125,7 @@ class CostEvaluator:
length: float = 0.0,
dilated_geometry: list[Polygon] | None = None,
skip_static: bool = False,
skip_congestion: bool = False,
penalty: float = 0.0,
) -> float:
"""
@ -131,8 +139,9 @@ class CostEvaluator:
start_port: Port at the start of the move.
length: Physical path length of the move.
dilated_geometry: Pre-calculated dilated polygons.
skip_static: If True, bypass static collision checks (e.g. if already done).
penalty: Fixed cost penalty for the move type (bend, sbend).
skip_static: If True, bypass static collision checks.
skip_congestion: If True, bypass congestion checks.
penalty: Fixed cost penalty for the move type.
Returns:
Total cost of the move, or 1e15 if invalid.
@ -156,11 +165,12 @@ class CostEvaluator:
return 1e15
# Soft Collision (Negotiated Congestion)
overlaps = self.collision_engine.check_collision(
poly, net_id, buffer_mode='congestion', dilated_geometry=dil_poly
)
if isinstance(overlaps, int) and overlaps > 0:
total_cost += overlaps * self.congestion_penalty
if not skip_congestion:
overlaps = self.collision_engine.check_collision(
poly, net_id, buffer_mode='congestion', dilated_geometry=dil_poly
)
if isinstance(overlaps, int) and overlaps > 0:
total_cost += overlaps * self.congestion_penalty
# 3. Proximity cost from Danger Map
total_cost += self.g_proximity(end_port.x, end_port.y)