2026-03-30 15:32:29 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
import time
|
2026-11-26 11:57:43 -08:00
|
|
|
from contextlib import nullcontext
|
|
|
|
|
from dataclasses import dataclass, field
|
2026-03-30 15:32:29 -07:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
from inire.geometry.collision import RoutingWorld
|
|
|
|
|
from inire.model import NetOrder, NetSpec, RoutingProblem, resolve_bend_geometry
|
|
|
|
|
from inire.results import (
|
|
|
|
|
ComponentConflictTrace,
|
|
|
|
|
ConflictTraceEntry,
|
|
|
|
|
FrontierPruneSample,
|
2026-04-02 16:15:02 -07:00
|
|
|
IterationNetAttemptTrace,
|
|
|
|
|
IterationTraceEntry,
|
2026-04-02 14:39:39 -07:00
|
|
|
NetConflictTrace,
|
|
|
|
|
NetFrontierTrace,
|
2026-04-02 18:57:34 -07:00
|
|
|
PrePairFrontierTraceEntry,
|
|
|
|
|
PrePairNetTrace,
|
2026-04-02 14:39:39 -07:00
|
|
|
RoutingOutcome,
|
|
|
|
|
RoutingReport,
|
|
|
|
|
RoutingResult,
|
2026-11-26 11:57:43 -08:00
|
|
|
TopologyReport,
|
2026-04-02 14:39:39 -07:00
|
|
|
)
|
|
|
|
|
from inire.router._astar_types import AStarContext, AStarMetrics, FrontierTraceCollector, SearchRunConfig
|
2026-03-30 15:32:29 -07:00
|
|
|
from inire.router._search import route_astar
|
2026-03-30 19:51:37 -07:00
|
|
|
from inire.router._seed_materialization import materialize_path_seed
|
2026-04-02 14:39:39 -07:00
|
|
|
from inire.router.cost import CostEvaluator
|
|
|
|
|
from inire.router.danger_map import DangerMap
|
2026-03-30 15:32:29 -07:00
|
|
|
from inire.router.refiner import PathRefiner
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from collections.abc import Callable, Sequence
|
2026-11-26 11:57:43 -08:00
|
|
|
from contextlib import AbstractContextManager
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-03-30 23:54:30 -07:00
|
|
|
from shapely.geometry import Polygon
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
from inire.geometry.collision import PathVerificationDetail
|
2026-03-30 15:32:29 -07:00
|
|
|
from inire.geometry.components import ComponentResult
|
2026-11-26 11:57:43 -08:00
|
|
|
from inire.results import RoutingOperation
|
|
|
|
|
from inire.router._topology import Corridor
|
|
|
|
|
|
|
|
|
|
PhaseTimer = Callable[[RoutingOperation], AbstractContextManager[None]]
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(slots=True)
|
|
|
|
|
class _RoutingState:
|
|
|
|
|
net_specs: dict[str, NetSpec]
|
|
|
|
|
ordered_net_ids: list[str]
|
|
|
|
|
results: dict[str, RoutingResult]
|
|
|
|
|
needs_self_collision_check: set[str]
|
|
|
|
|
start_time: float
|
|
|
|
|
timeout_s: float
|
|
|
|
|
initial_paths: dict[str, tuple[ComponentResult, ...]] | None
|
|
|
|
|
accumulated_expanded_nodes: list[tuple[int, int, int]]
|
2026-04-01 21:29:23 -07:00
|
|
|
best_results: dict[str, RoutingResult]
|
|
|
|
|
best_completed_nets: int
|
|
|
|
|
best_conflict_edges: int
|
|
|
|
|
best_dynamic_collisions: int
|
|
|
|
|
last_conflict_signature: tuple[tuple[str, str], ...]
|
|
|
|
|
last_conflict_edge_count: int
|
|
|
|
|
repeated_conflict_count: int
|
2026-04-02 17:10:00 -07:00
|
|
|
pair_local_plateau_count: int
|
2026-04-02 18:57:34 -07:00
|
|
|
recent_attempt_work: dict[str, int]
|
|
|
|
|
pre_pair_candidate: _PrePairCandidate | None
|
2026-11-26 11:57:43 -08:00
|
|
|
corridors: dict[str, tuple[Corridor, ...]] = field(default_factory=dict)
|
2026-04-01 21:29:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(slots=True)
|
|
|
|
|
class _IterationReview:
|
|
|
|
|
conflicting_nets: set[str]
|
|
|
|
|
conflict_edges: set[tuple[str, str]]
|
|
|
|
|
completed_net_ids: set[str]
|
|
|
|
|
total_dynamic_collisions: int
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class _PairLocalTarget:
|
|
|
|
|
net_ids: tuple[str, str]
|
|
|
|
|
|
|
|
|
|
|
2026-04-02 18:57:34 -07:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class _PrePairCandidate:
|
|
|
|
|
iteration: int
|
|
|
|
|
routed_net_ids: tuple[str, ...]
|
|
|
|
|
conflict_edges: tuple[tuple[str, str], ...]
|
|
|
|
|
net_attempts: tuple[IterationNetAttemptTrace, ...]
|
|
|
|
|
|
|
|
|
|
|
2026-04-02 16:15:02 -07:00
|
|
|
_ITERATION_TRACE_TOTALS = (
|
|
|
|
|
"nodes_expanded",
|
|
|
|
|
"congestion_check_calls",
|
|
|
|
|
"congestion_candidate_ids",
|
|
|
|
|
"congestion_exact_pair_checks",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
_ATTEMPT_TRACE_TOTALS = (
|
|
|
|
|
"nodes_expanded",
|
|
|
|
|
"congestion_check_calls",
|
|
|
|
|
"pruned_closed_set",
|
|
|
|
|
"pruned_cost",
|
|
|
|
|
"pruned_hard_collision",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
class PathFinder:
|
|
|
|
|
__slots__ = (
|
|
|
|
|
"context",
|
|
|
|
|
"metrics",
|
|
|
|
|
"refiner",
|
|
|
|
|
"accumulated_expanded_nodes",
|
2026-04-02 14:39:39 -07:00
|
|
|
"conflict_trace",
|
|
|
|
|
"frontier_trace",
|
2026-04-02 18:57:34 -07:00
|
|
|
"pre_pair_frontier_trace",
|
2026-04-02 16:15:02 -07:00
|
|
|
"iteration_trace",
|
2026-11-26 11:57:43 -08:00
|
|
|
"topology_report",
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
context: AStarContext,
|
|
|
|
|
metrics: AStarMetrics | None = None,
|
|
|
|
|
) -> None:
|
|
|
|
|
self.context = context
|
2026-03-31 17:26:00 -07:00
|
|
|
self.metrics = self.context.metrics if metrics is None else metrics
|
|
|
|
|
self.context.metrics = self.metrics
|
|
|
|
|
self.context.cost_evaluator.collision_engine.metrics = self.metrics
|
2026-03-31 17:41:15 -07:00
|
|
|
if self.context.cost_evaluator.danger_map is not None:
|
|
|
|
|
self.context.cost_evaluator.danger_map.metrics = self.metrics
|
2026-03-30 15:32:29 -07:00
|
|
|
self.refiner = PathRefiner(self.context)
|
|
|
|
|
self.accumulated_expanded_nodes: list[tuple[int, int, int]] = []
|
2026-04-02 14:39:39 -07:00
|
|
|
self.conflict_trace: list[ConflictTraceEntry] = []
|
|
|
|
|
self.frontier_trace: list[NetFrontierTrace] = []
|
2026-04-02 18:57:34 -07:00
|
|
|
self.pre_pair_frontier_trace: PrePairFrontierTraceEntry | None = None
|
2026-04-02 16:15:02 -07:00
|
|
|
self.iteration_trace: list[IterationTraceEntry] = []
|
2026-11-26 11:57:43 -08:00
|
|
|
self.topology_report: TopologyReport | None = None
|
2026-04-02 16:15:02 -07:00
|
|
|
|
|
|
|
|
def _metric_total(self, metric_name: str) -> int:
|
|
|
|
|
return int(getattr(self.metrics, f"total_{metric_name}"))
|
|
|
|
|
|
|
|
|
|
def _capture_metric_totals(self, metric_names: tuple[str, ...]) -> dict[str, int]:
|
|
|
|
|
return {metric_name: self._metric_total(metric_name) for metric_name in metric_names}
|
|
|
|
|
|
|
|
|
|
def _metric_deltas(self, before: dict[str, int], after: dict[str, int]) -> dict[str, int]:
|
|
|
|
|
return {metric_name: after[metric_name] - before[metric_name] for metric_name in before}
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-04-02 20:17:03 -07:00
|
|
|
def _results_all_reached_target(self, state: _RoutingState) -> bool:
|
|
|
|
|
return (
|
|
|
|
|
len(state.results) == len(state.ordered_net_ids)
|
|
|
|
|
and all(result.reached_target for result in state.results.values())
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _has_incumbent_fallback(self, result: RoutingResult | None) -> bool:
|
|
|
|
|
return bool(result and result.reached_target and result.path)
|
|
|
|
|
|
|
|
|
|
def _restore_incumbent_fallback(
|
|
|
|
|
self,
|
|
|
|
|
net_id: str,
|
|
|
|
|
result: RoutingResult,
|
|
|
|
|
guidance_seed_present: bool,
|
|
|
|
|
) -> tuple[RoutingResult, bool]:
|
|
|
|
|
self.metrics.total_late_phase_capped_fallbacks += 1
|
|
|
|
|
self._install_path(net_id, result.path)
|
|
|
|
|
return result, guidance_seed_present
|
|
|
|
|
|
|
|
|
|
def _guidance_for_result(
|
|
|
|
|
self,
|
|
|
|
|
result: RoutingResult | None,
|
|
|
|
|
) -> tuple[Sequence[ComponentResult] | None, float, bool]:
|
|
|
|
|
if result is None or not result.reached_target or not result.path:
|
|
|
|
|
return None, 0.0, False
|
|
|
|
|
return (
|
|
|
|
|
result.as_seed().segments,
|
|
|
|
|
max(10.0, self.context.options.objective.bend_penalty * 0.25),
|
|
|
|
|
True,
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-30 19:51:37 -07:00
|
|
|
def _install_path(self, net_id: str, path: Sequence[ComponentResult]) -> None:
|
2026-03-30 23:54:30 -07:00
|
|
|
all_geoms: list[Polygon] = []
|
|
|
|
|
all_dilated: list[Polygon] = []
|
2026-04-02 14:39:39 -07:00
|
|
|
component_indexes: list[int] = []
|
|
|
|
|
for component_index, result in enumerate(path):
|
2026-03-30 15:32:29 -07:00
|
|
|
all_geoms.extend(result.collision_geometry)
|
|
|
|
|
all_dilated.extend(result.dilated_collision_geometry)
|
2026-04-02 14:39:39 -07:00
|
|
|
component_indexes.extend([component_index] * len(result.collision_geometry))
|
|
|
|
|
self.context.cost_evaluator.collision_engine.add_path(
|
|
|
|
|
net_id,
|
|
|
|
|
all_geoms,
|
|
|
|
|
dilated_geometry=all_dilated,
|
|
|
|
|
component_indexes=component_indexes,
|
|
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
def _routing_order(
|
|
|
|
|
self,
|
|
|
|
|
net_specs: dict[str, NetSpec],
|
2026-03-30 19:51:37 -07:00
|
|
|
order: NetOrder,
|
2026-03-30 15:32:29 -07:00
|
|
|
) -> list[str]:
|
|
|
|
|
ordered_net_ids = list(net_specs.keys())
|
|
|
|
|
if order == "user":
|
|
|
|
|
return ordered_net_ids
|
|
|
|
|
ordered_net_ids.sort(
|
|
|
|
|
key=lambda net_id: abs(net_specs[net_id].target.x - net_specs[net_id].start.x)
|
|
|
|
|
+ abs(net_specs[net_id].target.y - net_specs[net_id].start.y),
|
|
|
|
|
reverse=(order == "longest"),
|
|
|
|
|
)
|
|
|
|
|
return ordered_net_ids
|
|
|
|
|
|
|
|
|
|
def _build_greedy_warm_start_paths(
|
|
|
|
|
self,
|
|
|
|
|
net_specs: dict[str, NetSpec],
|
2026-03-30 19:51:37 -07:00
|
|
|
order: NetOrder,
|
2026-11-26 11:57:43 -08:00
|
|
|
corridors: dict[str, tuple[Corridor, ...]],
|
2026-03-30 15:32:29 -07:00
|
|
|
) -> dict[str, tuple[ComponentResult, ...]]:
|
|
|
|
|
greedy_paths: dict[str, tuple[ComponentResult, ...]] = {}
|
|
|
|
|
temp_obj_ids: list[int] = []
|
2026-03-30 19:51:37 -07:00
|
|
|
greedy_node_limit = min(self.context.options.search.node_limit, 2000)
|
2026-03-30 15:32:29 -07:00
|
|
|
for net_id in self._routing_order(net_specs, order):
|
|
|
|
|
net = net_specs[net_id]
|
2026-03-30 19:51:37 -07:00
|
|
|
h_start = self.context.cost_evaluator.h_manhattan(
|
|
|
|
|
net.start,
|
|
|
|
|
net.target,
|
|
|
|
|
min_bend_radius=self.context.min_bend_radius,
|
|
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
max_cost_limit = max(h_start * 3.0, 2000.0)
|
2026-11-26 11:57:43 -08:00
|
|
|
candidates = corridors.get(net_id, ())
|
2026-03-30 19:51:37 -07:00
|
|
|
run_config = SearchRunConfig.from_options(
|
|
|
|
|
self.context.options,
|
|
|
|
|
skip_congestion=True,
|
|
|
|
|
max_cost=max_cost_limit,
|
|
|
|
|
self_collision_check=True,
|
|
|
|
|
node_limit=greedy_node_limit,
|
2026-11-26 11:57:43 -08:00
|
|
|
corridor=candidates[0] if candidates else None,
|
|
|
|
|
corridor_strength=self.context.options.topology.guide_strength,
|
2026-03-30 19:51:37 -07:00
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
path = route_astar(
|
|
|
|
|
net.start,
|
|
|
|
|
net.target,
|
|
|
|
|
net.width,
|
|
|
|
|
context=self.context,
|
|
|
|
|
metrics=self.metrics,
|
|
|
|
|
net_id=net_id,
|
2026-03-30 19:51:37 -07:00
|
|
|
config=run_config,
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
|
|
|
|
if not path:
|
|
|
|
|
continue
|
2026-03-31 17:26:00 -07:00
|
|
|
self.metrics.total_warm_start_paths_built += 1
|
2026-03-30 15:32:29 -07:00
|
|
|
greedy_paths[net_id] = tuple(path)
|
2026-03-30 19:51:37 -07:00
|
|
|
for result in path:
|
|
|
|
|
for polygon in result.physical_geometry:
|
|
|
|
|
temp_obj_ids.append(self.context.cost_evaluator.collision_engine.add_static_obstacle(polygon))
|
2026-03-30 15:32:29 -07:00
|
|
|
self.context.clear_static_caches()
|
|
|
|
|
|
2026-03-30 19:51:37 -07:00
|
|
|
for obj_id in temp_obj_ids:
|
|
|
|
|
self.context.cost_evaluator.collision_engine.remove_static_obstacle(obj_id)
|
2026-03-30 15:32:29 -07:00
|
|
|
return greedy_paths
|
|
|
|
|
|
2026-11-26 11:57:43 -08:00
|
|
|
def _build_corridor_seeds(self, state: _RoutingState) -> dict[str, tuple[ComponentResult, ...]]:
|
|
|
|
|
topology = self.context.options.topology
|
|
|
|
|
world = self.context.cost_evaluator.collision_engine
|
|
|
|
|
existing = state.initial_paths or {}
|
|
|
|
|
seeds: dict[str, tuple[ComponentResult, ...]] = {}
|
|
|
|
|
temporary: list[int] = []
|
|
|
|
|
|
|
|
|
|
def reserve(path: Sequence[ComponentResult]) -> None:
|
|
|
|
|
for component in path:
|
|
|
|
|
for shape in component.physical_geometry:
|
|
|
|
|
temporary.append(world.add_static_obstacle(shape))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
for net_id, path in existing.items():
|
|
|
|
|
if path and path[-1].end_port == state.net_specs[net_id].target and world.verify_path_report(net_id, path).is_valid:
|
|
|
|
|
reserve(path)
|
|
|
|
|
for net_id in state.ordered_net_ids:
|
|
|
|
|
if net_id in existing:
|
|
|
|
|
continue
|
|
|
|
|
net = state.net_specs[net_id]
|
|
|
|
|
candidates = state.corridors.get(net_id, ())
|
|
|
|
|
remaining = min(topology.seed_node_limit, self.context.options.search.node_limit)
|
|
|
|
|
for index, corridor in enumerate(candidates):
|
|
|
|
|
if remaining <= 0:
|
|
|
|
|
break
|
|
|
|
|
searches_left = len(candidates) - index
|
|
|
|
|
budget = max(1, remaining // searches_left)
|
|
|
|
|
if index == 0 and searches_left > 1:
|
|
|
|
|
budget = max(budget, (remaining + 1) // 2)
|
|
|
|
|
config = SearchRunConfig.from_options(
|
|
|
|
|
self.context.options,
|
|
|
|
|
node_limit=budget,
|
|
|
|
|
skip_congestion=True,
|
|
|
|
|
self_collision_check=True,
|
|
|
|
|
corridor=corridor,
|
|
|
|
|
restrict_to_corridor=True,
|
|
|
|
|
)
|
|
|
|
|
path = route_astar(net.start, net.target, net.width, self.context, metrics=self.metrics, net_id=net_id, config=config)
|
|
|
|
|
remaining -= self.metrics.nodes_expanded
|
|
|
|
|
if path and path[-1].end_port == net.target and world.verify_path_report(net_id, path).is_valid:
|
|
|
|
|
seeds[net_id] = tuple(path)
|
|
|
|
|
self.metrics.total_warm_start_paths_built += 1
|
|
|
|
|
reserve(path)
|
|
|
|
|
break
|
|
|
|
|
finally:
|
|
|
|
|
for obstacle in temporary:
|
|
|
|
|
world.remove_static_obstacle(obstacle)
|
|
|
|
|
self.context.clear_static_caches()
|
|
|
|
|
return seeds
|
|
|
|
|
|
|
|
|
|
def _prepare_topology(self, state: _RoutingState) -> None:
|
|
|
|
|
topology = self.context.options.topology
|
|
|
|
|
if topology.mode == "bundle":
|
|
|
|
|
self.topology_report = TopologyReport(seeded_net_ids=tuple(self.context.problem.initial_paths))
|
|
|
|
|
return
|
|
|
|
|
if topology.mode not in ("guide", "seed"):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
from inire.router._topology import plan_corridors # noqa: PLC0415
|
|
|
|
|
|
|
|
|
|
world = self.context.cost_evaluator.collision_engine
|
|
|
|
|
for net_id in state.ordered_net_ids:
|
|
|
|
|
world.remove_path(net_id)
|
|
|
|
|
state.corridors = plan_corridors(
|
|
|
|
|
self.context.problem.bounds,
|
|
|
|
|
world._static_obstacles.geometries.values(),
|
|
|
|
|
[state.net_specs[net_id] for net_id in state.ordered_net_ids],
|
|
|
|
|
clearance=world.clearance,
|
|
|
|
|
bend_radius=min(self.context.options.search.bend_radii, default=0.0),
|
|
|
|
|
limit=topology.max_corridors,
|
|
|
|
|
)
|
|
|
|
|
before = self.metrics.total_nodes_expanded
|
|
|
|
|
seeds = self._build_corridor_seeds(state) if topology.mode == "seed" and state.corridors else {}
|
|
|
|
|
if seeds:
|
|
|
|
|
state.initial_paths = {**(state.initial_paths or {}), **seeds}
|
|
|
|
|
self.topology_report = TopologyReport(
|
|
|
|
|
corridor_counts=tuple((net_id, len(candidates)) for net_id, candidates in state.corridors.items()),
|
|
|
|
|
seeded_net_ids=tuple(seeds),
|
|
|
|
|
seed_nodes_expanded=self.metrics.total_nodes_expanded - before,
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
def _prepare_state(self) -> _RoutingState:
|
2026-03-30 19:51:37 -07:00
|
|
|
problem = self.context.problem
|
|
|
|
|
congestion = self.context.options.congestion
|
|
|
|
|
initial_paths = self._materialize_problem_initial_paths()
|
2026-03-30 15:32:29 -07:00
|
|
|
net_specs = {net.net_id: net for net in problem.nets}
|
|
|
|
|
num_nets = len(net_specs)
|
|
|
|
|
state = _RoutingState(
|
|
|
|
|
net_specs=net_specs,
|
2026-11-26 11:57:43 -08:00
|
|
|
ordered_net_ids=self._routing_order(net_specs, congestion.net_order),
|
2026-03-30 15:32:29 -07:00
|
|
|
results={},
|
|
|
|
|
needs_self_collision_check=set(),
|
|
|
|
|
start_time=time.monotonic(),
|
|
|
|
|
timeout_s=max(60.0, 10.0 * num_nets * congestion.max_iterations),
|
|
|
|
|
initial_paths=initial_paths,
|
|
|
|
|
accumulated_expanded_nodes=[],
|
2026-04-01 21:29:23 -07:00
|
|
|
best_results={},
|
|
|
|
|
best_completed_nets=-1,
|
|
|
|
|
best_conflict_edges=10**9,
|
|
|
|
|
best_dynamic_collisions=10**9,
|
|
|
|
|
last_conflict_signature=(),
|
|
|
|
|
last_conflict_edge_count=0,
|
|
|
|
|
repeated_conflict_count=0,
|
2026-04-02 17:10:00 -07:00
|
|
|
pair_local_plateau_count=0,
|
2026-04-02 18:57:34 -07:00
|
|
|
recent_attempt_work={},
|
|
|
|
|
pre_pair_candidate=None,
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
2026-11-26 11:57:43 -08:00
|
|
|
self._prepare_topology(state)
|
2026-03-30 19:51:37 -07:00
|
|
|
if state.initial_paths is None and congestion.warm_start_enabled:
|
2026-11-26 11:57:43 -08:00
|
|
|
state.initial_paths = self._build_greedy_warm_start_paths(net_specs, congestion.net_order, state.corridors)
|
2026-03-30 19:51:37 -07:00
|
|
|
self.context.clear_static_caches()
|
2026-11-26 11:57:43 -08:00
|
|
|
if state.corridors and state.initial_paths and congestion.max_iterations > 0:
|
|
|
|
|
world = self.context.cost_evaluator.collision_engine
|
|
|
|
|
for net_id, path in state.initial_paths.items():
|
|
|
|
|
if not path or path[-1].end_port != net_specs[net_id].target:
|
|
|
|
|
continue
|
|
|
|
|
report = world.verify_path_report(net_id, path)
|
|
|
|
|
if report.is_valid:
|
|
|
|
|
self._install_path(net_id, path)
|
|
|
|
|
state.results[net_id] = RoutingResult(net_id, path, reached_target=True, report=report)
|
2026-03-30 15:32:29 -07:00
|
|
|
return state
|
|
|
|
|
|
2026-03-30 19:51:37 -07:00
|
|
|
def _materialize_problem_initial_paths(self) -> dict[str, tuple[ComponentResult, ...]] | None:
|
|
|
|
|
if not self.context.problem.initial_paths:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
search = self.context.options.search
|
|
|
|
|
net_specs = {net.net_id: net for net in self.context.problem.nets}
|
|
|
|
|
initial_paths: dict[str, tuple[ComponentResult, ...]] = {}
|
|
|
|
|
for net_id, seed in self.context.problem.initial_paths.items():
|
|
|
|
|
if net_id not in net_specs:
|
|
|
|
|
raise ValueError(f"Initial path provided for unknown net: {net_id}")
|
|
|
|
|
net = net_specs[net_id]
|
|
|
|
|
initial_paths[net_id] = materialize_path_seed(
|
|
|
|
|
seed,
|
|
|
|
|
start=net.start,
|
|
|
|
|
net_width=net.width,
|
|
|
|
|
search=search,
|
|
|
|
|
clearance=self.context.cost_evaluator.collision_engine.clearance,
|
2026-04-01 21:29:23 -07:00
|
|
|
)
|
2026-03-30 19:51:37 -07:00
|
|
|
return initial_paths
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def _replace_installed_paths(self, state: _RoutingState, results: dict[str, RoutingResult]) -> None:
|
|
|
|
|
for net_id in state.ordered_net_ids:
|
|
|
|
|
self.context.cost_evaluator.collision_engine.remove_path(net_id)
|
|
|
|
|
for net_id in state.ordered_net_ids:
|
|
|
|
|
result = results.get(net_id)
|
2026-11-26 11:57:43 -08:00
|
|
|
if result and result.path and (result.reached_target or not state.corridors):
|
2026-04-01 21:29:23 -07:00
|
|
|
self._install_path(net_id, result.path)
|
|
|
|
|
|
2026-04-02 20:17:03 -07:00
|
|
|
def _analyze_restored_best(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
) -> tuple[dict[str, PathVerificationDetail], _IterationReview]:
|
|
|
|
|
capture_component_conflicts = (
|
|
|
|
|
self.context.options.diagnostics.capture_conflict_trace
|
|
|
|
|
or self.context.options.diagnostics.capture_pre_pair_frontier_trace
|
|
|
|
|
)
|
|
|
|
|
state.results, details_by_net, review = self._analyze_results(
|
|
|
|
|
state.ordered_net_ids,
|
|
|
|
|
state.results,
|
|
|
|
|
capture_component_conflicts=capture_component_conflicts,
|
|
|
|
|
count_iteration_metrics=False,
|
|
|
|
|
)
|
|
|
|
|
if self.context.options.diagnostics.capture_conflict_trace:
|
|
|
|
|
self._capture_conflict_trace_entry(
|
|
|
|
|
state,
|
|
|
|
|
stage="restored_best",
|
|
|
|
|
iteration=None,
|
|
|
|
|
results=state.results,
|
|
|
|
|
details_by_net=details_by_net,
|
|
|
|
|
review=review,
|
|
|
|
|
)
|
|
|
|
|
if self.context.options.diagnostics.capture_pre_pair_frontier_trace:
|
|
|
|
|
self.pre_pair_frontier_trace = self._materialize_pre_pair_frontier_trace(
|
|
|
|
|
state,
|
|
|
|
|
state.results,
|
|
|
|
|
details_by_net,
|
|
|
|
|
review,
|
|
|
|
|
)
|
|
|
|
|
return details_by_net, review
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def _update_best_iteration(self, state: _RoutingState, review: _IterationReview) -> bool:
|
|
|
|
|
completed_nets = len(review.completed_net_ids)
|
|
|
|
|
conflict_edges = len(review.conflict_edges)
|
|
|
|
|
dynamic_collisions = review.total_dynamic_collisions
|
|
|
|
|
is_better = (
|
|
|
|
|
completed_nets > state.best_completed_nets
|
|
|
|
|
or (
|
|
|
|
|
completed_nets == state.best_completed_nets
|
|
|
|
|
and (
|
|
|
|
|
conflict_edges < state.best_conflict_edges
|
|
|
|
|
or (
|
|
|
|
|
conflict_edges == state.best_conflict_edges
|
|
|
|
|
and dynamic_collisions < state.best_dynamic_collisions
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
if not is_better:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
state.best_results = dict(state.results)
|
|
|
|
|
state.best_completed_nets = completed_nets
|
|
|
|
|
state.best_conflict_edges = conflict_edges
|
|
|
|
|
state.best_dynamic_collisions = dynamic_collisions
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def _restore_best_iteration(self, state: _RoutingState) -> None:
|
|
|
|
|
if not state.best_results:
|
|
|
|
|
return
|
|
|
|
|
state.results = dict(state.best_results)
|
|
|
|
|
self._replace_installed_paths(state, state.results)
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def _capture_conflict_trace_entry(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
*,
|
|
|
|
|
stage: str,
|
|
|
|
|
iteration: int | None,
|
|
|
|
|
results: dict[str, RoutingResult],
|
|
|
|
|
details_by_net: dict[str, PathVerificationDetail],
|
|
|
|
|
review: _IterationReview,
|
|
|
|
|
) -> None:
|
|
|
|
|
if not self.context.options.diagnostics.capture_conflict_trace:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
nets = []
|
|
|
|
|
for net_id in state.ordered_net_ids:
|
|
|
|
|
result = results.get(net_id)
|
|
|
|
|
if result is None:
|
|
|
|
|
result = RoutingResult(net_id=net_id, path=(), reached_target=False)
|
|
|
|
|
detail = details_by_net.get(net_id)
|
|
|
|
|
component_conflicts = ()
|
|
|
|
|
conflicting_net_ids = ()
|
|
|
|
|
if detail is not None:
|
|
|
|
|
conflicting_net_ids = detail.conflicting_net_ids
|
|
|
|
|
component_conflicts = tuple(
|
|
|
|
|
ComponentConflictTrace(
|
|
|
|
|
other_net_id=other_net_id,
|
|
|
|
|
self_component_index=self_component_index,
|
|
|
|
|
other_component_index=other_component_index,
|
|
|
|
|
)
|
|
|
|
|
for self_component_index, other_net_id, other_component_index in detail.component_conflicts
|
|
|
|
|
)
|
|
|
|
|
nets.append(
|
|
|
|
|
NetConflictTrace(
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
outcome=result.outcome,
|
|
|
|
|
reached_target=result.reached_target,
|
|
|
|
|
report=result.report,
|
|
|
|
|
conflicting_net_ids=tuple(conflicting_net_ids),
|
|
|
|
|
component_conflicts=component_conflicts,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.conflict_trace.append(
|
|
|
|
|
ConflictTraceEntry(
|
|
|
|
|
stage=stage, # type: ignore[arg-type]
|
|
|
|
|
iteration=iteration,
|
|
|
|
|
completed_net_ids=tuple(sorted(review.completed_net_ids)),
|
|
|
|
|
conflict_edges=tuple(sorted(review.conflict_edges)),
|
|
|
|
|
nets=tuple(nets),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _build_frontier_hotspot_bounds(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
net_id: str,
|
|
|
|
|
details_by_net: dict[str, PathVerificationDetail],
|
|
|
|
|
) -> tuple[tuple[float, float, float, float], ...]:
|
|
|
|
|
result = state.results.get(net_id)
|
|
|
|
|
detail = details_by_net.get(net_id)
|
|
|
|
|
if result is None or detail is None or not result.path:
|
|
|
|
|
return ()
|
|
|
|
|
|
|
|
|
|
hotspot_bounds: list[tuple[float, float, float, float]] = []
|
|
|
|
|
seen: set[tuple[float, float, float, float]] = set()
|
|
|
|
|
margin = max(5.0, self.context.cost_evaluator.collision_engine.clearance * 2.0)
|
|
|
|
|
|
|
|
|
|
for self_component_index, other_net_id, other_component_index in detail.component_conflicts:
|
|
|
|
|
other_result = state.results.get(other_net_id)
|
|
|
|
|
if other_result is None or not other_result.path:
|
|
|
|
|
continue
|
|
|
|
|
if self_component_index >= len(result.path) or other_component_index >= len(other_result.path):
|
|
|
|
|
continue
|
|
|
|
|
left_component = result.path[self_component_index]
|
|
|
|
|
right_component = other_result.path[other_component_index]
|
|
|
|
|
overlap_found = False
|
|
|
|
|
for left_poly in left_component.dilated_physical_geometry:
|
|
|
|
|
for right_poly in right_component.dilated_physical_geometry:
|
|
|
|
|
if not left_poly.intersects(right_poly) or left_poly.touches(right_poly):
|
|
|
|
|
continue
|
|
|
|
|
overlap = left_poly.intersection(right_poly)
|
|
|
|
|
if overlap.is_empty:
|
|
|
|
|
continue
|
|
|
|
|
buffered = overlap.buffer(margin, join_style="mitre").bounds
|
|
|
|
|
if buffered not in seen:
|
|
|
|
|
seen.add(buffered)
|
|
|
|
|
hotspot_bounds.append(buffered)
|
|
|
|
|
overlap_found = True
|
|
|
|
|
if overlap_found:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
left_bounds = left_component.total_dilated_bounds
|
|
|
|
|
right_bounds = right_component.total_dilated_bounds
|
|
|
|
|
if (
|
|
|
|
|
left_bounds[0] < right_bounds[2]
|
|
|
|
|
and left_bounds[2] > right_bounds[0]
|
|
|
|
|
and left_bounds[1] < right_bounds[3]
|
|
|
|
|
and left_bounds[3] > right_bounds[1]
|
|
|
|
|
):
|
|
|
|
|
buffered = (
|
|
|
|
|
max(left_bounds[0], right_bounds[0]) - margin,
|
|
|
|
|
max(left_bounds[1], right_bounds[1]) - margin,
|
|
|
|
|
min(left_bounds[2], right_bounds[2]) + margin,
|
|
|
|
|
min(left_bounds[3], right_bounds[3]) + margin,
|
|
|
|
|
)
|
|
|
|
|
if buffered not in seen:
|
|
|
|
|
seen.add(buffered)
|
|
|
|
|
hotspot_bounds.append(buffered)
|
|
|
|
|
|
|
|
|
|
return tuple(hotspot_bounds)
|
|
|
|
|
|
2026-04-02 18:57:34 -07:00
|
|
|
def _capture_single_frontier_trace(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
net_id: str,
|
|
|
|
|
result: RoutingResult,
|
|
|
|
|
hotspot_bounds: tuple[tuple[float, float, float, float], ...],
|
|
|
|
|
) -> NetFrontierTrace:
|
|
|
|
|
if not hotspot_bounds:
|
|
|
|
|
return NetFrontierTrace(
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
hotspot_bounds=(),
|
|
|
|
|
pruned_closed_set=0,
|
|
|
|
|
pruned_hard_collision=0,
|
|
|
|
|
pruned_self_collision=0,
|
|
|
|
|
pruned_cost=0,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
original_metrics = self.metrics
|
|
|
|
|
original_context_metrics = self.context.metrics
|
|
|
|
|
original_engine_metrics = self.context.cost_evaluator.collision_engine.metrics
|
|
|
|
|
original_danger_metrics = None
|
|
|
|
|
if self.context.cost_evaluator.danger_map is not None:
|
|
|
|
|
original_danger_metrics = self.context.cost_evaluator.danger_map.metrics
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
scratch_metrics = AStarMetrics()
|
|
|
|
|
self.context.metrics = scratch_metrics
|
|
|
|
|
self.context.cost_evaluator.collision_engine.metrics = scratch_metrics
|
|
|
|
|
if self.context.cost_evaluator.danger_map is not None:
|
|
|
|
|
self.context.cost_evaluator.danger_map.metrics = scratch_metrics
|
|
|
|
|
|
|
|
|
|
guidance_seed = result.as_seed().segments if result.path else None
|
|
|
|
|
guidance_bonus = 0.0
|
|
|
|
|
if guidance_seed:
|
|
|
|
|
guidance_bonus = max(10.0, self.context.options.objective.bend_penalty * 0.25)
|
|
|
|
|
collector = FrontierTraceCollector(hotspot_bounds=hotspot_bounds)
|
|
|
|
|
run_config = SearchRunConfig.from_options(
|
|
|
|
|
self.context.options,
|
|
|
|
|
return_partial=True,
|
|
|
|
|
store_expanded=False,
|
|
|
|
|
guidance_seed=guidance_seed,
|
|
|
|
|
guidance_bonus=guidance_bonus,
|
|
|
|
|
frontier_trace=collector,
|
|
|
|
|
self_collision_check=(net_id in state.needs_self_collision_check),
|
|
|
|
|
node_limit=self.context.options.search.node_limit,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.context.cost_evaluator.collision_engine.remove_path(net_id)
|
|
|
|
|
try:
|
|
|
|
|
route_astar(
|
|
|
|
|
state.net_specs[net_id].start,
|
|
|
|
|
state.net_specs[net_id].target,
|
|
|
|
|
state.net_specs[net_id].width,
|
|
|
|
|
context=self.context,
|
|
|
|
|
metrics=scratch_metrics,
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
config=run_config,
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
if result.path:
|
|
|
|
|
self._install_path(net_id, result.path)
|
|
|
|
|
|
|
|
|
|
return NetFrontierTrace(
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
hotspot_bounds=hotspot_bounds,
|
|
|
|
|
pruned_closed_set=collector.pruned_closed_set,
|
|
|
|
|
pruned_hard_collision=collector.pruned_hard_collision,
|
|
|
|
|
pruned_self_collision=collector.pruned_self_collision,
|
|
|
|
|
pruned_cost=collector.pruned_cost,
|
|
|
|
|
samples=tuple(
|
|
|
|
|
FrontierPruneSample(
|
|
|
|
|
reason=reason, # type: ignore[arg-type]
|
|
|
|
|
move_type=move_type,
|
|
|
|
|
hotspot_index=hotspot_index,
|
|
|
|
|
parent_state=parent_state,
|
|
|
|
|
end_state=end_state,
|
|
|
|
|
)
|
|
|
|
|
for reason, move_type, hotspot_index, parent_state, end_state in collector.samples
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
self.metrics = original_metrics
|
|
|
|
|
self.context.metrics = original_context_metrics
|
|
|
|
|
self.context.cost_evaluator.collision_engine.metrics = original_engine_metrics
|
|
|
|
|
if self.context.cost_evaluator.danger_map is not None:
|
|
|
|
|
self.context.cost_evaluator.danger_map.metrics = original_danger_metrics
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def _analyze_results(
|
|
|
|
|
self,
|
|
|
|
|
ordered_net_ids: Sequence[str],
|
|
|
|
|
results: dict[str, RoutingResult],
|
|
|
|
|
*,
|
|
|
|
|
capture_component_conflicts: bool,
|
|
|
|
|
count_iteration_metrics: bool,
|
|
|
|
|
) -> tuple[dict[str, RoutingResult], dict[str, PathVerificationDetail], _IterationReview]:
|
|
|
|
|
if count_iteration_metrics:
|
|
|
|
|
self.metrics.total_iteration_reverify_calls += 1
|
|
|
|
|
conflict_edges: set[tuple[str, str]] = set()
|
|
|
|
|
conflicting_nets: set[str] = set()
|
|
|
|
|
completed_net_ids: set[str] = set()
|
|
|
|
|
total_dynamic_collisions = 0
|
|
|
|
|
analyzed_results = dict(results)
|
|
|
|
|
details_by_net: dict[str, PathVerificationDetail] = {}
|
|
|
|
|
|
|
|
|
|
for net_id in ordered_net_ids:
|
|
|
|
|
result = results.get(net_id)
|
|
|
|
|
if not result or not result.path or not result.reached_target:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if count_iteration_metrics:
|
|
|
|
|
self.metrics.total_iteration_reverified_nets += 1
|
|
|
|
|
detail = self.context.cost_evaluator.collision_engine.verify_path_details(
|
|
|
|
|
net_id,
|
|
|
|
|
result.path,
|
|
|
|
|
capture_component_conflicts=capture_component_conflicts,
|
|
|
|
|
)
|
|
|
|
|
details_by_net[net_id] = detail
|
|
|
|
|
analyzed_results[net_id] = RoutingResult(
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
path=result.path,
|
|
|
|
|
reached_target=result.reached_target,
|
|
|
|
|
report=detail.report,
|
|
|
|
|
)
|
|
|
|
|
total_dynamic_collisions += detail.report.dynamic_collision_count
|
|
|
|
|
if analyzed_results[net_id].outcome == "completed":
|
|
|
|
|
completed_net_ids.add(net_id)
|
|
|
|
|
if not detail.conflicting_net_ids:
|
|
|
|
|
continue
|
|
|
|
|
conflicting_nets.add(net_id)
|
|
|
|
|
for other_net_id in detail.conflicting_net_ids:
|
|
|
|
|
conflicting_nets.add(other_net_id)
|
|
|
|
|
if other_net_id == net_id:
|
|
|
|
|
continue
|
|
|
|
|
conflict_edges.add(tuple(sorted((net_id, other_net_id))))
|
|
|
|
|
|
|
|
|
|
if count_iteration_metrics:
|
|
|
|
|
self.metrics.total_iteration_conflicting_nets += len(conflicting_nets)
|
|
|
|
|
self.metrics.total_iteration_conflict_edges += len(conflict_edges)
|
|
|
|
|
return (
|
|
|
|
|
analyzed_results,
|
|
|
|
|
details_by_net,
|
|
|
|
|
_IterationReview(
|
|
|
|
|
conflicting_nets=conflicting_nets,
|
|
|
|
|
conflict_edges=conflict_edges,
|
|
|
|
|
completed_net_ids=completed_net_ids,
|
|
|
|
|
total_dynamic_collisions=total_dynamic_collisions,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _capture_frontier_trace(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
final_results: dict[str, RoutingResult],
|
|
|
|
|
) -> None:
|
|
|
|
|
if not self.context.options.diagnostics.capture_frontier_trace:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.frontier_trace = []
|
|
|
|
|
state.results = dict(final_results)
|
|
|
|
|
state.results, details_by_net, _ = self._analyze_results(
|
|
|
|
|
state.ordered_net_ids,
|
|
|
|
|
state.results,
|
|
|
|
|
capture_component_conflicts=True,
|
|
|
|
|
count_iteration_metrics=False,
|
|
|
|
|
)
|
2026-04-02 18:57:34 -07:00
|
|
|
for net_id in state.ordered_net_ids:
|
|
|
|
|
result = state.results.get(net_id)
|
|
|
|
|
detail = details_by_net.get(net_id)
|
|
|
|
|
if result is None or detail is None or not result.reached_target:
|
|
|
|
|
continue
|
|
|
|
|
if detail.report.dynamic_collision_count == 0 or not detail.component_conflicts:
|
|
|
|
|
continue
|
2026-04-02 14:39:39 -07:00
|
|
|
|
2026-04-02 18:57:34 -07:00
|
|
|
hotspot_bounds = self._build_frontier_hotspot_bounds(state, net_id, details_by_net)
|
|
|
|
|
if not hotspot_bounds:
|
|
|
|
|
continue
|
2026-04-02 14:39:39 -07:00
|
|
|
|
2026-04-02 18:57:34 -07:00
|
|
|
self.frontier_trace.append(
|
|
|
|
|
self._capture_single_frontier_trace(
|
|
|
|
|
state,
|
|
|
|
|
net_id,
|
|
|
|
|
result,
|
|
|
|
|
hotspot_bounds,
|
2026-04-02 14:39:39 -07:00
|
|
|
)
|
2026-04-02 18:57:34 -07:00
|
|
|
)
|
2026-04-02 14:39:39 -07:00
|
|
|
|
|
|
|
|
def _whole_set_is_better(
|
|
|
|
|
self,
|
|
|
|
|
candidate_results: dict[str, RoutingResult],
|
|
|
|
|
candidate_review: _IterationReview,
|
|
|
|
|
incumbent_results: dict[str, RoutingResult],
|
|
|
|
|
incumbent_review: _IterationReview,
|
|
|
|
|
) -> bool:
|
|
|
|
|
candidate_completed = len(candidate_review.completed_net_ids)
|
|
|
|
|
incumbent_completed = len(incumbent_review.completed_net_ids)
|
|
|
|
|
if candidate_completed != incumbent_completed:
|
|
|
|
|
return candidate_completed > incumbent_completed
|
|
|
|
|
|
|
|
|
|
candidate_edges = len(candidate_review.conflict_edges)
|
|
|
|
|
incumbent_edges = len(incumbent_review.conflict_edges)
|
|
|
|
|
if candidate_edges != incumbent_edges:
|
|
|
|
|
return candidate_edges < incumbent_edges
|
|
|
|
|
|
|
|
|
|
if candidate_review.total_dynamic_collisions != incumbent_review.total_dynamic_collisions:
|
|
|
|
|
return candidate_review.total_dynamic_collisions < incumbent_review.total_dynamic_collisions
|
|
|
|
|
|
|
|
|
|
candidate_length = sum(
|
|
|
|
|
result.report.total_length
|
|
|
|
|
for result in candidate_results.values()
|
|
|
|
|
if result.reached_target
|
|
|
|
|
)
|
|
|
|
|
incumbent_length = sum(
|
|
|
|
|
result.report.total_length
|
|
|
|
|
for result in incumbent_results.values()
|
|
|
|
|
if result.reached_target
|
|
|
|
|
)
|
|
|
|
|
if abs(candidate_length - incumbent_length) > 1e-6:
|
|
|
|
|
return candidate_length < incumbent_length
|
|
|
|
|
return False
|
|
|
|
|
|
2026-04-02 20:17:03 -07:00
|
|
|
def _pair_local_attempt_orders(self, target: _PairLocalTarget) -> tuple[tuple[str, str], tuple[str, str]]:
|
|
|
|
|
return target.net_ids, target.net_ids[::-1]
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def _collect_pair_local_targets(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
results: dict[str, RoutingResult],
|
|
|
|
|
review: _IterationReview,
|
|
|
|
|
) -> list[_PairLocalTarget]:
|
|
|
|
|
if not review.conflict_edges:
|
|
|
|
|
return []
|
|
|
|
|
order_index = {net_id: idx for idx, net_id in enumerate(state.ordered_net_ids)}
|
|
|
|
|
seen_net_ids: set[str] = set()
|
|
|
|
|
targets: list[_PairLocalTarget] = []
|
|
|
|
|
for left_net_id, right_net_id in sorted(review.conflict_edges):
|
|
|
|
|
if left_net_id in seen_net_ids or right_net_id in seen_net_ids:
|
|
|
|
|
return []
|
|
|
|
|
left_result = results.get(left_net_id)
|
|
|
|
|
right_result = results.get(right_net_id)
|
|
|
|
|
if (
|
|
|
|
|
left_result is None
|
|
|
|
|
or right_result is None
|
|
|
|
|
or not left_result.reached_target
|
|
|
|
|
or not right_result.reached_target
|
|
|
|
|
):
|
|
|
|
|
continue
|
|
|
|
|
seen_net_ids.update((left_net_id, right_net_id))
|
|
|
|
|
targets.append(_PairLocalTarget(net_ids=(left_net_id, right_net_id)))
|
|
|
|
|
targets.sort(key=lambda target: min(order_index[target.net_ids[0]], order_index[target.net_ids[1]]))
|
|
|
|
|
return targets
|
|
|
|
|
|
|
|
|
|
def _build_pair_local_context(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
incumbent_results: dict[str, RoutingResult],
|
|
|
|
|
pair_net_ids: tuple[str, str],
|
|
|
|
|
) -> AStarContext:
|
|
|
|
|
problem = self.context.problem
|
|
|
|
|
objective = self.context.options.objective
|
|
|
|
|
static_obstacles = tuple(self.context.cost_evaluator.collision_engine._static_obstacles.geometries.values())
|
|
|
|
|
engine = RoutingWorld(
|
|
|
|
|
clearance=self.context.cost_evaluator.collision_engine.clearance,
|
|
|
|
|
safety_zone_radius=self.context.cost_evaluator.collision_engine.safety_zone_radius,
|
|
|
|
|
)
|
|
|
|
|
for obstacle in static_obstacles:
|
|
|
|
|
engine.add_static_obstacle(obstacle)
|
|
|
|
|
for net_id in state.ordered_net_ids:
|
|
|
|
|
if net_id in pair_net_ids:
|
|
|
|
|
continue
|
|
|
|
|
result = incumbent_results.get(net_id)
|
2026-11-26 11:57:43 -08:00
|
|
|
if result is None or not result.path or (state.corridors and not result.reached_target):
|
2026-04-02 14:39:39 -07:00
|
|
|
continue
|
|
|
|
|
for component in result.path:
|
|
|
|
|
for polygon in component.physical_geometry:
|
|
|
|
|
engine.add_static_obstacle(polygon)
|
|
|
|
|
|
|
|
|
|
danger_map = DangerMap(bounds=problem.bounds)
|
|
|
|
|
danger_map.precompute(list(static_obstacles))
|
|
|
|
|
evaluator = CostEvaluator(
|
|
|
|
|
engine,
|
|
|
|
|
danger_map,
|
|
|
|
|
unit_length_cost=objective.unit_length_cost,
|
|
|
|
|
greedy_h_weight=self.context.cost_evaluator.greedy_h_weight,
|
|
|
|
|
bend_penalty=objective.bend_penalty,
|
|
|
|
|
sbend_penalty=objective.sbend_penalty,
|
|
|
|
|
danger_weight=objective.danger_weight,
|
|
|
|
|
)
|
|
|
|
|
return AStarContext(
|
|
|
|
|
evaluator,
|
|
|
|
|
RoutingProblem(
|
|
|
|
|
bounds=problem.bounds,
|
|
|
|
|
nets=tuple(state.net_specs[net_id] for net_id in state.ordered_net_ids),
|
|
|
|
|
static_obstacles=static_obstacles,
|
|
|
|
|
clearance=problem.clearance,
|
|
|
|
|
safety_zone_radius=problem.safety_zone_radius,
|
|
|
|
|
),
|
|
|
|
|
self.context.options,
|
|
|
|
|
metrics=AStarMetrics(),
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-02 18:57:34 -07:00
|
|
|
def _materialize_pre_pair_frontier_trace(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
results: dict[str, RoutingResult],
|
|
|
|
|
details_by_net: dict[str, PathVerificationDetail],
|
|
|
|
|
review: _IterationReview,
|
|
|
|
|
) -> PrePairFrontierTraceEntry | None:
|
|
|
|
|
candidate = state.pre_pair_candidate
|
|
|
|
|
if candidate is None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
result_by_net = dict(results)
|
|
|
|
|
detail_by_net = dict(details_by_net)
|
|
|
|
|
nets: list[PrePairNetTrace] = []
|
|
|
|
|
attempt_by_net = {attempt.net_id: attempt for attempt in candidate.net_attempts}
|
|
|
|
|
for net_id in candidate.routed_net_ids:
|
|
|
|
|
attempt = attempt_by_net.get(net_id)
|
|
|
|
|
result = result_by_net.get(net_id)
|
|
|
|
|
detail = detail_by_net.get(net_id)
|
|
|
|
|
if attempt is None or result is None or detail is None or not result.reached_target:
|
|
|
|
|
continue
|
|
|
|
|
hotspot_bounds = self._build_frontier_hotspot_bounds(state, net_id, detail_by_net)
|
|
|
|
|
nets.append(
|
|
|
|
|
PrePairNetTrace(
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
nodes_expanded=attempt.nodes_expanded,
|
|
|
|
|
congestion_check_calls=attempt.congestion_check_calls,
|
|
|
|
|
pruned_closed_set=attempt.pruned_closed_set,
|
|
|
|
|
pruned_cost=attempt.pruned_cost,
|
|
|
|
|
pruned_hard_collision=attempt.pruned_hard_collision,
|
|
|
|
|
guidance_seed_present=attempt.guidance_seed_present,
|
|
|
|
|
frontier=self._capture_single_frontier_trace(state, net_id, result, hotspot_bounds),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not nets:
|
|
|
|
|
return None
|
|
|
|
|
return PrePairFrontierTraceEntry(
|
|
|
|
|
iteration=candidate.iteration,
|
|
|
|
|
routed_net_ids=candidate.routed_net_ids,
|
|
|
|
|
conflict_edges=candidate.conflict_edges,
|
|
|
|
|
nets=tuple(nets),
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-02 20:17:03 -07:00
|
|
|
def _build_iteration_reroute_plan(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
reroute_net_ids: set[str],
|
|
|
|
|
) -> tuple[list[str], set[str]]:
|
|
|
|
|
routed_net_ids = [net_id for net_id in state.ordered_net_ids if net_id in reroute_net_ids]
|
|
|
|
|
capped_net_ids: set[str] = set()
|
|
|
|
|
if len(reroute_net_ids) >= len(state.ordered_net_ids) or not state.recent_attempt_work:
|
|
|
|
|
return routed_net_ids, capped_net_ids
|
|
|
|
|
|
|
|
|
|
order_index = {net_id: idx for idx, net_id in enumerate(state.ordered_net_ids)}
|
|
|
|
|
routed_net_ids.sort(key=lambda net_id: (state.recent_attempt_work.get(net_id, 0), order_index[net_id]))
|
|
|
|
|
if (
|
|
|
|
|
len(routed_net_ids) == 4
|
|
|
|
|
and state.best_conflict_edges <= 2
|
|
|
|
|
and self._results_all_reached_target(state)
|
|
|
|
|
):
|
|
|
|
|
heavy_net_ids = sorted(
|
|
|
|
|
routed_net_ids,
|
|
|
|
|
key=lambda net_id: (-state.recent_attempt_work.get(net_id, 0), order_index[net_id]),
|
|
|
|
|
)[:2]
|
|
|
|
|
capped_net_ids = {
|
|
|
|
|
net_id for net_id in heavy_net_ids if state.recent_attempt_work.get(net_id, 0) >= 200
|
|
|
|
|
}
|
|
|
|
|
return routed_net_ids, capped_net_ids
|
|
|
|
|
|
|
|
|
|
def _update_pre_pair_candidate(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
*,
|
|
|
|
|
iteration: int,
|
|
|
|
|
reroute_net_ids: set[str],
|
|
|
|
|
routed_net_ids: list[str],
|
|
|
|
|
attempt_traces: list[IterationNetAttemptTrace],
|
|
|
|
|
review: _IterationReview,
|
|
|
|
|
) -> None:
|
|
|
|
|
if self._results_all_reached_target(state) and len(reroute_net_ids) < len(state.ordered_net_ids) and review.conflict_edges:
|
|
|
|
|
state.pre_pair_candidate = _PrePairCandidate(
|
|
|
|
|
iteration=iteration,
|
|
|
|
|
routed_net_ids=tuple(routed_net_ids),
|
|
|
|
|
conflict_edges=tuple(sorted(review.conflict_edges)),
|
|
|
|
|
net_attempts=tuple(attempt_traces),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
state.pre_pair_candidate = None
|
|
|
|
|
|
|
|
|
|
def _next_reroute_net_ids(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
review: _IterationReview,
|
|
|
|
|
) -> set[str]:
|
|
|
|
|
if self._results_all_reached_target(state) and 0 < len(review.conflict_edges) <= 3:
|
|
|
|
|
return set(review.conflicting_nets)
|
2026-11-26 11:57:43 -08:00
|
|
|
if state.corridors:
|
|
|
|
|
return set(state.ordered_net_ids).difference(review.completed_net_ids)
|
2026-04-02 20:17:03 -07:00
|
|
|
return set(state.ordered_net_ids)
|
|
|
|
|
|
|
|
|
|
def _should_stop_for_pair_local_plateau(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
*,
|
|
|
|
|
improved: bool,
|
|
|
|
|
) -> bool:
|
|
|
|
|
if improved:
|
|
|
|
|
state.pair_local_plateau_count = 0
|
|
|
|
|
return False
|
|
|
|
|
if self._results_all_reached_target(state) and state.best_conflict_edges <= 2:
|
|
|
|
|
# Once the run is fully reached-target and already in the final <=2-edge
|
|
|
|
|
# basin, another non-improving negotiated iteration is just churn before
|
|
|
|
|
# the bounded pair-local repair.
|
|
|
|
|
state.pair_local_plateau_count += 1
|
|
|
|
|
return state.pair_local_plateau_count >= 1
|
|
|
|
|
state.pair_local_plateau_count = 0
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def _update_repeated_conflict_state(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
review: _IterationReview,
|
|
|
|
|
) -> bool:
|
|
|
|
|
current_signature = tuple(sorted(review.conflict_edges))
|
|
|
|
|
repeated = (
|
|
|
|
|
bool(current_signature)
|
|
|
|
|
and (
|
|
|
|
|
current_signature == state.last_conflict_signature
|
|
|
|
|
or len(current_signature) == state.last_conflict_edge_count
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
state.repeated_conflict_count = state.repeated_conflict_count + 1 if repeated else 0
|
|
|
|
|
state.last_conflict_signature = current_signature
|
|
|
|
|
state.last_conflict_edge_count = len(current_signature)
|
|
|
|
|
return state.repeated_conflict_count >= 2
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def _run_pair_local_attempt(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
incumbent_results: dict[str, RoutingResult],
|
|
|
|
|
pair_order: tuple[str, str],
|
|
|
|
|
) -> tuple[dict[str, RoutingResult], int] | None:
|
|
|
|
|
local_context = self._build_pair_local_context(state, incumbent_results, pair_order)
|
|
|
|
|
local_results = dict(incumbent_results)
|
|
|
|
|
|
|
|
|
|
for net_id in pair_order:
|
|
|
|
|
net = state.net_specs[net_id]
|
2026-04-02 20:17:03 -07:00
|
|
|
guidance_seed, guidance_bonus, _ = self._guidance_for_result(incumbent_results.get(net_id))
|
2026-04-02 14:39:39 -07:00
|
|
|
|
|
|
|
|
run_config = SearchRunConfig.from_options(
|
|
|
|
|
self.context.options,
|
|
|
|
|
return_partial=False,
|
|
|
|
|
skip_congestion=True,
|
|
|
|
|
self_collision_check=(net_id in state.needs_self_collision_check),
|
|
|
|
|
guidance_seed=guidance_seed,
|
|
|
|
|
guidance_bonus=guidance_bonus,
|
|
|
|
|
node_limit=self.context.options.search.node_limit,
|
|
|
|
|
)
|
|
|
|
|
path = route_astar(
|
|
|
|
|
net.start,
|
|
|
|
|
net.target,
|
|
|
|
|
net.width,
|
|
|
|
|
context=local_context,
|
|
|
|
|
metrics=local_context.metrics,
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
config=run_config,
|
|
|
|
|
)
|
|
|
|
|
if not path or path[-1].end_port != net.target:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
report = local_context.cost_evaluator.collision_engine.verify_path_report(net_id, path)
|
|
|
|
|
if not report.is_valid:
|
|
|
|
|
return None
|
|
|
|
|
local_results[net_id] = RoutingResult(
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
path=tuple(path),
|
|
|
|
|
reached_target=True,
|
|
|
|
|
report=report,
|
|
|
|
|
)
|
|
|
|
|
for component in path:
|
|
|
|
|
for polygon in component.physical_geometry:
|
|
|
|
|
local_context.cost_evaluator.collision_engine.add_static_obstacle(polygon)
|
|
|
|
|
local_context.clear_static_caches()
|
|
|
|
|
|
|
|
|
|
return local_results, local_context.metrics.total_nodes_expanded
|
|
|
|
|
|
2026-04-02 20:17:03 -07:00
|
|
|
def _apply_pair_local_candidate(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
candidate_results: dict[str, RoutingResult],
|
|
|
|
|
incumbent_results: dict[str, RoutingResult],
|
|
|
|
|
incumbent_review: _IterationReview,
|
|
|
|
|
) -> tuple[bool, _IterationReview]:
|
|
|
|
|
self._replace_installed_paths(state, candidate_results)
|
|
|
|
|
candidate_results, _candidate_details_by_net, candidate_review = self._analyze_results(
|
|
|
|
|
state.ordered_net_ids,
|
|
|
|
|
candidate_results,
|
|
|
|
|
capture_component_conflicts=True,
|
|
|
|
|
count_iteration_metrics=False,
|
|
|
|
|
)
|
|
|
|
|
if self._whole_set_is_better(
|
|
|
|
|
candidate_results,
|
|
|
|
|
candidate_review,
|
|
|
|
|
incumbent_results,
|
|
|
|
|
incumbent_review,
|
|
|
|
|
):
|
|
|
|
|
self.metrics.total_pair_local_search_accepts += 1
|
|
|
|
|
state.results = candidate_results
|
|
|
|
|
return True, candidate_review
|
|
|
|
|
|
|
|
|
|
self._replace_installed_paths(state, incumbent_results)
|
|
|
|
|
return False, incumbent_review
|
|
|
|
|
|
|
|
|
|
def _run_pair_local_target(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
target: _PairLocalTarget,
|
|
|
|
|
review: _IterationReview,
|
|
|
|
|
) -> _IterationReview:
|
|
|
|
|
incumbent_results = dict(state.results)
|
|
|
|
|
incumbent_review = review
|
|
|
|
|
self.metrics.total_pair_local_search_pairs_considered += 1
|
|
|
|
|
for pair_order in self._pair_local_attempt_orders(target):
|
|
|
|
|
self.metrics.total_pair_local_search_attempts += 1
|
|
|
|
|
candidate = self._run_pair_local_attempt(state, incumbent_results, pair_order)
|
|
|
|
|
if candidate is None:
|
|
|
|
|
continue
|
|
|
|
|
candidate_results, nodes_expanded = candidate
|
|
|
|
|
self.metrics.total_pair_local_search_nodes_expanded += nodes_expanded
|
|
|
|
|
accepted, next_review = self._apply_pair_local_candidate(
|
|
|
|
|
state,
|
|
|
|
|
candidate_results,
|
|
|
|
|
incumbent_results,
|
|
|
|
|
incumbent_review,
|
|
|
|
|
)
|
|
|
|
|
if accepted:
|
|
|
|
|
return next_review
|
|
|
|
|
|
|
|
|
|
state.results = incumbent_results
|
|
|
|
|
self._replace_installed_paths(state, incumbent_results)
|
|
|
|
|
return incumbent_review
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def _run_pair_local_search(self, state: _RoutingState) -> None:
|
|
|
|
|
state.results, _details_by_net, review = self._analyze_results(
|
|
|
|
|
state.ordered_net_ids,
|
|
|
|
|
state.results,
|
|
|
|
|
capture_component_conflicts=True,
|
|
|
|
|
count_iteration_metrics=False,
|
|
|
|
|
)
|
|
|
|
|
targets = self._collect_pair_local_targets(state, state.results, review)
|
|
|
|
|
if not targets:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for target in targets[:2]:
|
2026-04-02 20:17:03 -07:00
|
|
|
review = self._run_pair_local_target(state, target, review)
|
2026-04-02 14:39:39 -07:00
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
def _route_net_once(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
iteration: int,
|
|
|
|
|
net_id: str,
|
2026-04-02 18:57:34 -07:00
|
|
|
*,
|
|
|
|
|
node_limit_override: int | None = None,
|
|
|
|
|
incumbent_fallback: RoutingResult | None = None,
|
2026-04-02 16:15:02 -07:00
|
|
|
) -> tuple[RoutingResult, bool]:
|
2026-03-30 19:51:37 -07:00
|
|
|
search = self.context.options.search
|
|
|
|
|
congestion = self.context.options.congestion
|
|
|
|
|
diagnostics = self.context.options.diagnostics
|
2026-03-30 15:32:29 -07:00
|
|
|
net = state.net_specs[net_id]
|
2026-03-31 17:26:00 -07:00
|
|
|
self.metrics.total_nets_routed += 1
|
2026-04-02 18:57:34 -07:00
|
|
|
if node_limit_override is not None:
|
|
|
|
|
self.metrics.total_late_phase_capped_nets += 1
|
2026-03-30 19:51:37 -07:00
|
|
|
self.context.cost_evaluator.collision_engine.remove_path(net_id)
|
2026-04-02 16:15:02 -07:00
|
|
|
guidance_seed_present = False
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
if iteration == 0 and state.initial_paths and net_id in state.initial_paths:
|
2026-03-31 17:26:00 -07:00
|
|
|
self.metrics.total_warm_start_paths_used += 1
|
2026-03-30 15:32:29 -07:00
|
|
|
path: Sequence[ComponentResult] | None = state.initial_paths[net_id]
|
|
|
|
|
else:
|
2026-03-30 23:40:29 -07:00
|
|
|
coll_model, _ = resolve_bend_geometry(search)
|
2026-03-30 15:32:29 -07:00
|
|
|
skip_congestion = False
|
2026-04-02 20:17:03 -07:00
|
|
|
guidance_seed, guidance_bonus, guidance_seed_present = (None, 0.0, False)
|
2026-11-26 11:57:43 -08:00
|
|
|
if congestion.use_tiered_strategy and iteration == 0 and not state.corridors:
|
2026-03-30 15:32:29 -07:00
|
|
|
skip_congestion = True
|
|
|
|
|
if coll_model == "arc":
|
|
|
|
|
coll_model = "clipped_bbox"
|
2026-04-02 14:39:39 -07:00
|
|
|
elif iteration > 0:
|
2026-04-02 20:17:03 -07:00
|
|
|
guidance_seed, guidance_bonus, guidance_seed_present = self._guidance_for_result(
|
|
|
|
|
state.results.get(net_id)
|
|
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-04-02 20:17:03 -07:00
|
|
|
if node_limit_override is not None and self._has_incumbent_fallback(incumbent_fallback):
|
|
|
|
|
return self._restore_incumbent_fallback(net_id, incumbent_fallback, guidance_seed_present)
|
2026-04-02 18:57:34 -07:00
|
|
|
|
2026-11-26 11:57:43 -08:00
|
|
|
corridors = state.corridors.get(net_id, ())
|
2026-03-30 19:51:37 -07:00
|
|
|
run_config = SearchRunConfig.from_options(
|
|
|
|
|
self.context.options,
|
2026-03-30 15:32:29 -07:00
|
|
|
bend_collision_type=coll_model,
|
|
|
|
|
return_partial=True,
|
|
|
|
|
store_expanded=diagnostics.capture_expanded,
|
2026-04-02 14:39:39 -07:00
|
|
|
guidance_seed=guidance_seed,
|
|
|
|
|
guidance_bonus=guidance_bonus,
|
2026-03-30 15:32:29 -07:00
|
|
|
skip_congestion=skip_congestion,
|
|
|
|
|
self_collision_check=(net_id in state.needs_self_collision_check),
|
2026-04-02 18:57:34 -07:00
|
|
|
node_limit=search.node_limit if node_limit_override is None else node_limit_override,
|
2026-11-26 11:57:43 -08:00
|
|
|
corridor=corridors[iteration] if iteration < len(corridors) else None,
|
|
|
|
|
corridor_strength=self.context.options.topology.guide_strength / (iteration + 1),
|
|
|
|
|
hard_congestion=bool(state.corridors) and iteration == 0,
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
2026-03-30 19:51:37 -07:00
|
|
|
path = route_astar(
|
|
|
|
|
net.start,
|
|
|
|
|
net.target,
|
|
|
|
|
net.width,
|
|
|
|
|
context=self.context,
|
|
|
|
|
metrics=self.metrics,
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
config=run_config,
|
|
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
if diagnostics.capture_expanded and self.metrics.last_expanded_nodes:
|
|
|
|
|
state.accumulated_expanded_nodes.extend(self.metrics.last_expanded_nodes)
|
|
|
|
|
|
|
|
|
|
if not path:
|
2026-04-02 20:17:03 -07:00
|
|
|
if self._has_incumbent_fallback(incumbent_fallback):
|
|
|
|
|
return self._restore_incumbent_fallback(net_id, incumbent_fallback, guidance_seed_present)
|
2026-04-02 16:15:02 -07:00
|
|
|
return RoutingResult(net_id=net_id, path=(), reached_target=False), guidance_seed_present
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
reached_target = path[-1].end_port == net.target
|
2026-04-02 20:17:03 -07:00
|
|
|
if not reached_target and self._has_incumbent_fallback(incumbent_fallback):
|
|
|
|
|
return self._restore_incumbent_fallback(net_id, incumbent_fallback, guidance_seed_present)
|
2026-03-31 17:26:00 -07:00
|
|
|
if reached_target:
|
|
|
|
|
self.metrics.total_nets_reached_target += 1
|
2026-03-30 15:32:29 -07:00
|
|
|
report = None
|
2026-11-26 11:57:43 -08:00
|
|
|
if reached_target or not state.corridors:
|
|
|
|
|
self._install_path(net_id, path)
|
2026-03-30 15:32:29 -07:00
|
|
|
if reached_target:
|
2026-03-30 19:51:37 -07:00
|
|
|
report = self.context.cost_evaluator.collision_engine.verify_path_report(net_id, path)
|
2026-03-30 15:32:29 -07:00
|
|
|
if report.self_collision_count > 0:
|
|
|
|
|
state.needs_self_collision_check.add(net_id)
|
|
|
|
|
|
2026-04-02 16:15:02 -07:00
|
|
|
return (
|
|
|
|
|
RoutingResult(
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
path=tuple(path),
|
|
|
|
|
reached_target=reached_target,
|
|
|
|
|
report=RoutingReport() if report is None else report,
|
|
|
|
|
),
|
|
|
|
|
guidance_seed_present,
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _run_iteration(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
iteration: int,
|
2026-04-01 21:29:23 -07:00
|
|
|
reroute_net_ids: set[str],
|
2026-03-30 15:32:29 -07:00
|
|
|
iteration_callback: Callable[[int, dict[str, RoutingResult]], None] | None,
|
2026-04-01 21:29:23 -07:00
|
|
|
) -> _IterationReview | None:
|
2026-03-30 19:51:37 -07:00
|
|
|
congestion = self.context.options.congestion
|
2026-04-02 16:15:02 -07:00
|
|
|
diagnostics = self.context.options.diagnostics
|
2026-03-31 17:26:00 -07:00
|
|
|
self.metrics.total_route_iterations += 1
|
2026-03-30 15:32:29 -07:00
|
|
|
self.metrics.reset_per_route()
|
|
|
|
|
if congestion.shuffle_nets and (iteration > 0 or state.initial_paths is None):
|
|
|
|
|
iteration_seed = (congestion.seed + iteration) if congestion.seed is not None else None
|
|
|
|
|
random.Random(iteration_seed).shuffle(state.ordered_net_ids)
|
|
|
|
|
|
2026-04-02 16:15:02 -07:00
|
|
|
iteration_penalty = self.context.congestion_penalty
|
2026-04-02 20:17:03 -07:00
|
|
|
routed_net_ids, capped_net_ids = self._build_iteration_reroute_plan(state, reroute_net_ids)
|
2026-04-01 21:29:23 -07:00
|
|
|
self.metrics.total_nets_carried_forward += len(state.ordered_net_ids) - len(routed_net_ids)
|
2026-04-02 16:15:02 -07:00
|
|
|
iteration_before = {}
|
|
|
|
|
attempt_traces: list[IterationNetAttemptTrace] = []
|
2026-04-02 18:57:34 -07:00
|
|
|
attempt_work: dict[str, int] = {}
|
2026-04-02 16:15:02 -07:00
|
|
|
if diagnostics.capture_iteration_trace:
|
|
|
|
|
iteration_before = self._capture_metric_totals(_ITERATION_TRACE_TOTALS)
|
2026-04-01 21:29:23 -07:00
|
|
|
|
|
|
|
|
for net_id in routed_net_ids:
|
2026-03-30 15:32:29 -07:00
|
|
|
if time.monotonic() - state.start_time > state.timeout_s:
|
2026-03-31 17:26:00 -07:00
|
|
|
self.metrics.total_timeout_events += 1
|
2026-03-30 15:32:29 -07:00
|
|
|
return None
|
|
|
|
|
|
2026-04-02 18:57:34 -07:00
|
|
|
attempt_before = self._capture_metric_totals(_ATTEMPT_TRACE_TOTALS)
|
|
|
|
|
node_limit_override = None
|
|
|
|
|
incumbent_fallback = None
|
|
|
|
|
if net_id in capped_net_ids:
|
|
|
|
|
node_limit_override = min(self.context.options.search.node_limit, 1)
|
|
|
|
|
incumbent_fallback = state.results.get(net_id)
|
|
|
|
|
result, guidance_seed_present = self._route_net_once(
|
|
|
|
|
state,
|
|
|
|
|
iteration,
|
|
|
|
|
net_id,
|
|
|
|
|
node_limit_override=node_limit_override,
|
|
|
|
|
incumbent_fallback=incumbent_fallback,
|
|
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
state.results[net_id] = result
|
2026-04-02 18:57:34 -07:00
|
|
|
attempt_after = self._capture_metric_totals(_ATTEMPT_TRACE_TOTALS)
|
|
|
|
|
deltas = self._metric_deltas(attempt_before, attempt_after)
|
|
|
|
|
attempt_work[net_id] = deltas["nodes_expanded"] + deltas["congestion_check_calls"]
|
|
|
|
|
attempt_traces.append(
|
|
|
|
|
IterationNetAttemptTrace(
|
|
|
|
|
net_id=net_id,
|
|
|
|
|
reached_target=result.reached_target,
|
|
|
|
|
nodes_expanded=deltas["nodes_expanded"],
|
|
|
|
|
congestion_check_calls=deltas["congestion_check_calls"],
|
|
|
|
|
pruned_closed_set=deltas["pruned_closed_set"],
|
|
|
|
|
pruned_cost=deltas["pruned_cost"],
|
|
|
|
|
pruned_hard_collision=deltas["pruned_hard_collision"],
|
|
|
|
|
guidance_seed_present=guidance_seed_present,
|
2026-04-02 16:15:02 -07:00
|
|
|
)
|
2026-04-02 18:57:34 -07:00
|
|
|
)
|
2026-04-01 21:29:23 -07:00
|
|
|
|
2026-04-02 18:57:34 -07:00
|
|
|
state.recent_attempt_work = attempt_work
|
2026-04-01 21:29:23 -07:00
|
|
|
review = self._reverify_iteration_results(state)
|
2026-04-02 20:17:03 -07:00
|
|
|
self._update_pre_pair_candidate(
|
|
|
|
|
state,
|
|
|
|
|
iteration=iteration,
|
|
|
|
|
reroute_net_ids=reroute_net_ids,
|
|
|
|
|
routed_net_ids=routed_net_ids,
|
|
|
|
|
attempt_traces=attempt_traces,
|
|
|
|
|
review=review,
|
2026-04-02 18:57:34 -07:00
|
|
|
)
|
2026-04-02 16:15:02 -07:00
|
|
|
if diagnostics.capture_iteration_trace:
|
|
|
|
|
iteration_after = self._capture_metric_totals(_ITERATION_TRACE_TOTALS)
|
|
|
|
|
deltas = self._metric_deltas(iteration_before, iteration_after)
|
|
|
|
|
self.iteration_trace.append(
|
|
|
|
|
IterationTraceEntry(
|
|
|
|
|
iteration=iteration,
|
|
|
|
|
congestion_penalty=iteration_penalty,
|
|
|
|
|
routed_net_ids=tuple(routed_net_ids),
|
|
|
|
|
completed_nets=len(review.completed_net_ids),
|
|
|
|
|
conflict_edges=len(review.conflict_edges),
|
|
|
|
|
total_dynamic_collisions=review.total_dynamic_collisions,
|
|
|
|
|
nodes_expanded=deltas["nodes_expanded"],
|
|
|
|
|
congestion_check_calls=deltas["congestion_check_calls"],
|
|
|
|
|
congestion_candidate_ids=deltas["congestion_candidate_ids"],
|
|
|
|
|
congestion_exact_pair_checks=deltas["congestion_exact_pair_checks"],
|
|
|
|
|
net_attempts=tuple(attempt_traces),
|
|
|
|
|
)
|
|
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
if iteration_callback:
|
|
|
|
|
iteration_callback(iteration, state.results)
|
2026-04-01 21:29:23 -07:00
|
|
|
return review
|
|
|
|
|
|
|
|
|
|
def _reverify_iteration_results(self, state: _RoutingState) -> _IterationReview:
|
2026-04-02 14:39:39 -07:00
|
|
|
state.results, details_by_net, review = self._analyze_results(
|
|
|
|
|
state.ordered_net_ids,
|
|
|
|
|
state.results,
|
|
|
|
|
capture_component_conflicts=self.context.options.diagnostics.capture_conflict_trace,
|
|
|
|
|
count_iteration_metrics=True,
|
2026-04-01 21:29:23 -07:00
|
|
|
)
|
2026-04-02 14:39:39 -07:00
|
|
|
self._capture_conflict_trace_entry(
|
|
|
|
|
state,
|
|
|
|
|
stage="iteration",
|
|
|
|
|
iteration=self.metrics.total_route_iterations - 1,
|
|
|
|
|
results=state.results,
|
|
|
|
|
details_by_net=details_by_net,
|
|
|
|
|
review=review,
|
|
|
|
|
)
|
|
|
|
|
return review
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
def _run_iterations(
|
|
|
|
|
self,
|
|
|
|
|
state: _RoutingState,
|
|
|
|
|
iteration_callback: Callable[[int, dict[str, RoutingResult]], None] | None,
|
|
|
|
|
) -> bool:
|
2026-03-30 19:51:37 -07:00
|
|
|
congestion = self.context.options.congestion
|
2026-04-02 17:10:00 -07:00
|
|
|
reroute_net_ids = set(state.ordered_net_ids)
|
2026-03-30 15:32:29 -07:00
|
|
|
for iteration in range(congestion.max_iterations):
|
2026-04-01 21:29:23 -07:00
|
|
|
review = self._run_iteration(
|
|
|
|
|
state,
|
|
|
|
|
iteration,
|
2026-04-02 17:10:00 -07:00
|
|
|
reroute_net_ids,
|
2026-04-01 21:29:23 -07:00
|
|
|
iteration_callback,
|
|
|
|
|
)
|
|
|
|
|
if review is None:
|
2026-03-30 15:32:29 -07:00
|
|
|
return True
|
2026-04-02 17:10:00 -07:00
|
|
|
improved = self._update_best_iteration(state, review)
|
2026-04-01 21:29:23 -07:00
|
|
|
if not any(
|
|
|
|
|
result.outcome in {"colliding", "partial", "unroutable"}
|
|
|
|
|
for result in state.results.values()
|
|
|
|
|
):
|
|
|
|
|
return False
|
|
|
|
|
|
2026-04-02 20:17:03 -07:00
|
|
|
reroute_net_ids = self._next_reroute_net_ids(state, review)
|
|
|
|
|
if self._should_stop_for_pair_local_plateau(state, improved=improved):
|
|
|
|
|
return False
|
2026-04-02 17:10:00 -07:00
|
|
|
|
2026-04-02 20:17:03 -07:00
|
|
|
if self._update_repeated_conflict_state(state, review):
|
2026-03-30 15:32:29 -07:00
|
|
|
return False
|
2026-03-30 19:51:37 -07:00
|
|
|
self.context.congestion_penalty *= congestion.multiplier
|
2026-03-30 15:32:29 -07:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def _refine_results(self, state: _RoutingState) -> None:
|
2026-03-30 19:51:37 -07:00
|
|
|
if not self.context.options.refinement.enabled or not state.results:
|
2026-03-30 15:32:29 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for net_id in state.ordered_net_ids:
|
|
|
|
|
result = state.results.get(net_id)
|
2026-03-30 19:51:37 -07:00
|
|
|
if not result or not result.path or result.outcome in {"colliding", "partial", "unroutable"}:
|
2026-03-30 15:32:29 -07:00
|
|
|
continue
|
|
|
|
|
net = state.net_specs[net_id]
|
2026-03-31 17:26:00 -07:00
|
|
|
self.metrics.total_refine_path_calls += 1
|
2026-03-30 19:51:37 -07:00
|
|
|
self.context.cost_evaluator.collision_engine.remove_path(net_id)
|
|
|
|
|
refined_path = self.refiner.refine_path(net_id, net.start, net.width, result.path)
|
2026-03-30 15:32:29 -07:00
|
|
|
self._install_path(net_id, refined_path)
|
2026-04-01 21:29:23 -07:00
|
|
|
# Defer full verification until _verify_results() so we do not
|
|
|
|
|
# verify the same refined path twice in one route_all() call.
|
2026-03-30 19:51:37 -07:00
|
|
|
state.results[net_id] = RoutingResult(
|
2026-03-30 15:32:29 -07:00
|
|
|
net_id=net_id,
|
2026-03-30 23:54:30 -07:00
|
|
|
path=tuple(refined_path),
|
2026-03-30 15:32:29 -07:00
|
|
|
reached_target=result.reached_target,
|
2026-04-01 21:29:23 -07:00
|
|
|
report=result.report,
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _verify_results(self, state: _RoutingState) -> dict[str, RoutingResult]:
|
|
|
|
|
final_results: dict[str, RoutingResult] = {}
|
2026-04-02 14:39:39 -07:00
|
|
|
details_by_net: dict[str, PathVerificationDetail] = {}
|
2026-03-30 19:51:37 -07:00
|
|
|
for net in self.context.problem.nets:
|
2026-03-30 15:32:29 -07:00
|
|
|
result = state.results.get(net.net_id)
|
|
|
|
|
if not result or not result.path:
|
2026-03-30 19:51:37 -07:00
|
|
|
final_results[net.net_id] = RoutingResult(net_id=net.net_id, path=(), reached_target=False)
|
2026-03-30 15:32:29 -07:00
|
|
|
continue
|
2026-04-02 14:39:39 -07:00
|
|
|
detail = self.context.cost_evaluator.collision_engine.verify_path_details(
|
|
|
|
|
net.net_id,
|
|
|
|
|
result.path,
|
|
|
|
|
capture_component_conflicts=self.context.options.diagnostics.capture_conflict_trace,
|
|
|
|
|
)
|
|
|
|
|
details_by_net[net.net_id] = detail
|
2026-03-30 19:51:37 -07:00
|
|
|
final_results[net.net_id] = RoutingResult(
|
2026-03-30 15:32:29 -07:00
|
|
|
net_id=net.net_id,
|
|
|
|
|
path=result.path,
|
|
|
|
|
reached_target=result.reached_target,
|
2026-04-02 14:39:39 -07:00
|
|
|
report=detail.report,
|
|
|
|
|
)
|
|
|
|
|
if self.context.options.diagnostics.capture_conflict_trace:
|
|
|
|
|
_, _, review = self._analyze_results(
|
|
|
|
|
state.ordered_net_ids,
|
|
|
|
|
final_results,
|
|
|
|
|
capture_component_conflicts=True,
|
|
|
|
|
count_iteration_metrics=False,
|
|
|
|
|
)
|
|
|
|
|
self._capture_conflict_trace_entry(
|
|
|
|
|
state,
|
|
|
|
|
stage="final",
|
|
|
|
|
iteration=None,
|
|
|
|
|
results=final_results,
|
|
|
|
|
details_by_net=details_by_net,
|
|
|
|
|
review=review,
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
|
|
|
|
return final_results
|
|
|
|
|
|
|
|
|
|
def route_all(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
iteration_callback: Callable[[int, dict[str, RoutingResult]], None] | None = None,
|
2026-11-26 11:57:43 -08:00
|
|
|
phase_timer: PhaseTimer | None = None,
|
2026-03-30 15:32:29 -07:00
|
|
|
) -> dict[str, RoutingResult]:
|
2026-03-30 19:51:37 -07:00
|
|
|
self.context.congestion_penalty = self.context.options.congestion.base_penalty
|
2026-03-30 15:32:29 -07:00
|
|
|
self.accumulated_expanded_nodes = []
|
2026-04-02 14:39:39 -07:00
|
|
|
self.conflict_trace = []
|
|
|
|
|
self.frontier_trace = []
|
2026-04-02 18:57:34 -07:00
|
|
|
self.pre_pair_frontier_trace = None
|
2026-04-02 16:15:02 -07:00
|
|
|
self.iteration_trace = []
|
2026-11-26 11:57:43 -08:00
|
|
|
self.topology_report = None
|
2026-03-30 15:32:29 -07:00
|
|
|
self.metrics.reset_totals()
|
|
|
|
|
self.metrics.reset_per_route()
|
|
|
|
|
|
|
|
|
|
state = self._prepare_state()
|
|
|
|
|
timed_out = self._run_iterations(state, iteration_callback)
|
|
|
|
|
self.accumulated_expanded_nodes = list(state.accumulated_expanded_nodes)
|
2026-04-01 21:29:23 -07:00
|
|
|
self._restore_best_iteration(state)
|
2026-04-02 20:17:03 -07:00
|
|
|
self._analyze_restored_best(state)
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-11-26 11:57:43 -08:00
|
|
|
if not timed_out:
|
|
|
|
|
self._run_pair_local_search(state)
|
|
|
|
|
with phase_timer("refine") if phase_timer is not None and self.context.options.refinement.enabled else nullcontext():
|
|
|
|
|
self._refine_results(state)
|
|
|
|
|
with phase_timer("verify") if phase_timer is not None else nullcontext():
|
2026-04-02 14:39:39 -07:00
|
|
|
final_results = self._verify_results(state)
|
|
|
|
|
self._capture_frontier_trace(state, final_results)
|
|
|
|
|
return final_results
|