Initial buildout

This commit is contained in:
Jan Petykiewicz 2026-03-07 08:26:29 -08:00
commit f600b52f32

62
inire/router/cost.py Normal file
View file

@ -0,0 +1,62 @@
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from shapely.geometry import Polygon
from inire.geometry.collision import CollisionEngine
from inire.geometry.primitives import Port
from inire.router.danger_map import DangerMap
class CostEvaluator:
"""Calculates total cost f(n) = g(n) + h(n)."""
def __init__(self, collision_engine: CollisionEngine, danger_map: DangerMap) -> None:
self.collision_engine = collision_engine
self.danger_map = danger_map
# Cost weights
self.unit_length_cost = 1.0
self.bend_cost_multiplier = 10.0
self.greedy_h_weight = 1.1
self.congestion_penalty = 100.0 # Multiplier for overlaps
def g_proximity(self, x: float, y: float) -> float:
"""Get proximity cost from the Danger Map."""
return self.danger_map.get_cost(x, y)
def h_manhattan(self, current: Port, target: Port) -> float:
"""Heuristic: weighted Manhattan distance + orientation penalty."""
dist = abs(current.x - target.x) + abs(current.y - target.y)
# Orientation penalty if not aligned with target entry
penalty = 0.0
if current.orientation != target.orientation:
penalty += 50.0 # Arbitrary high cost for mismatch
return self.greedy_h_weight * (dist + penalty)
def evaluate_move(
self,
geometry: list[Polygon],
end_port: Port,
net_width: float,
net_id: str,
start_port: Port | None = None,
) -> float:
"""Calculate the cost of a single move (Straight, Bend, SBend)."""
total_cost = 0.0
# Strict collision check
for poly in geometry:
if self.collision_engine.is_collision(poly, net_width, start_port=start_port, end_port=end_port):
return 1e9 # Massive cost for hard collisions
# Negotiated Congestion Cost
overlaps = self.collision_engine.count_congestion(poly, net_id)
total_cost += overlaps * self.congestion_penalty
# Proximity cost from Danger Map
total_cost += self.g_proximity(end_port.x, end_port.y)
return total_cost