inire/inire/router/_search.py

132 lines
4.3 KiB
Python

from __future__ import annotations
import heapq
from typing import TYPE_CHECKING
from inire.constants import TOLERANCE_LINEAR
from ._astar_admission import process_move
from ._astar_moves import expand_moves as _expand_moves
from ._astar_types import AStarContext, AStarMetrics, AStarNode as _AStarNode, CongestionCaches, SearchRunConfig
if TYPE_CHECKING:
from collections.abc import Sequence
from inire.geometry.components import ComponentResult
from inire.geometry.primitives import Port
from inire.results import RoutingResult
from inire.seeds import BendDirection, PathSegmentSeed
def incumbent_guidance(
result: RoutingResult | None,
bend_penalty: float,
) -> tuple[tuple[PathSegmentSeed, ...] | None, float]:
if result is None or not result.reached_target or not result.path:
return None, 0.0
return path_guidance(result.path, bend_penalty)
def path_guidance(path: Sequence[ComponentResult], bend_penalty: float) -> tuple[tuple[PathSegmentSeed, ...], float]:
return tuple(component.move_spec for component in path), max(10.0, bend_penalty * 0.25)
def _reconstruct_path(end_node: _AStarNode) -> list[ComponentResult]:
path = []
curr: _AStarNode | None = end_node
while curr and curr.component_result:
path.append(curr.component_result)
curr = curr.parent
return path[::-1]
def _distance_to_terminal(port: Port, target: Port) -> tuple[float, float]:
turn = (port.r - target.r) % 360
return abs(port.x - target.x) + abs(port.y - target.y), min(turn, 360 - turn)
def route_astar(
start: Port,
target: Port,
net_width: float,
context: AStarContext,
*,
metrics: AStarMetrics | None = None,
net_id: str = "default",
config: SearchRunConfig,
) -> list[ComponentResult] | None:
if metrics is None:
metrics = AStarMetrics()
metrics.reset_per_route()
context.ensure_static_caches_current()
context.cost_evaluator.set_target(target)
open_set: list[_AStarNode] = []
closed_set: dict[tuple[int, int, int], float] = {}
deferred_turns: list[tuple[_AStarNode, float, BendDirection]] = []
congestion = CongestionCaches()
start_node = _AStarNode(
start,
0.0,
config.estimate_remaining_cost(start, target, context),
seed_index=0 if config.guidance_seed else None,
)
heapq.heappush(open_set, start_node)
best_node = start_node
best_distance = _distance_to_terminal(start, target)
nodes_expanded = 0
while open_set or deferred_turns:
if nodes_expanded >= config.node_limit:
return _reconstruct_path(best_node) if config.return_partial else None
if not open_set:
# Recover turns away from the target after exhausting the preferred frontier.
for parent, radius, direction in deferred_turns:
process_move(
parent, target, net_width, net_id, open_set, closed_set, context, metrics, congestion, config, "bend90", (radius, direction)
)
deferred_turns.clear()
if not open_set:
break
current = heapq.heappop(open_set)
if config.max_cost is not None and current.fh_cost[0] > config.max_cost:
metrics.pruned_cost += 1
metrics.total_pruned_cost += 1
continue
state = current.port.as_tuple()
if state in closed_set and closed_set[state] <= current.g_cost + TOLERANCE_LINEAR:
continue
closed_set[state] = current.g_cost
if config.return_partial:
distance = _distance_to_terminal(current.port, target)
if distance < best_distance:
best_node, best_distance = current, distance
if config.store_expanded:
metrics.last_expanded_nodes.append(state)
nodes_expanded += 1
metrics.total_nodes_expanded += 1
metrics.nodes_expanded += 1
if current.port == target:
return _reconstruct_path(current)
_expand_moves(
current,
target,
net_width,
net_id,
open_set,
closed_set,
context,
metrics,
congestion,
config=config,
deferred_turns=deferred_turns,
)
return _reconstruct_path(best_node) if config.return_partial else None