2026-03-30 19:51:37 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-11-26 19:58:46 -08:00
|
|
|
from dataclasses import dataclass, field, fields
|
2026-03-30 19:51:37 -07:00
|
|
|
from typing import TYPE_CHECKING, Literal
|
|
|
|
|
|
2026-11-29 00:31:51 -08:00
|
|
|
from inire._snapshot import snapshot
|
2026-03-30 19:51:37 -07:00
|
|
|
from inire.seeds import PathSeed
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2026-11-29 00:31:51 -08:00
|
|
|
from collections.abc import Iterable, Mapping
|
2026-11-26 19:58:46 -08:00
|
|
|
|
2026-03-30 19:51:37 -07:00
|
|
|
from shapely.geometry import Polygon
|
|
|
|
|
|
|
|
|
|
from inire.geometry.components import ComponentResult
|
2026-11-29 00:12:59 -08:00
|
|
|
from inire.model import NetSpec
|
2026-03-30 19:51:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
RoutingOutcome = Literal["completed", "colliding", "partial", "unroutable"]
|
2026-04-02 14:39:39 -07:00
|
|
|
ConflictTraceStage = Literal["iteration", "restored_best", "final"]
|
|
|
|
|
FrontierTraceReason = Literal["closed_set", "hard_collision", "self_collision", "cost"]
|
2026-03-30 19:51:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class RoutingReport:
|
|
|
|
|
static_collision_count: int = 0
|
|
|
|
|
dynamic_collision_count: int = 0
|
|
|
|
|
self_collision_count: int = 0
|
|
|
|
|
total_length: float = 0.0
|
2026-11-26 11:57:43 -08:00
|
|
|
out_of_bounds: bool = False
|
2026-11-29 23:12:28 -08:00
|
|
|
discontinuous: bool = False # Retained for reports created by older versions.
|
2026-11-27 15:21:37 -08:00
|
|
|
invalid_geometry: bool = False
|
2026-11-28 20:06:06 -08:00
|
|
|
seed_error: str | None = None
|
2026-11-28 20:29:40 -08:00
|
|
|
terminal_error: str | None = None
|
2026-11-29 23:56:47 -08:00
|
|
|
sbend_template_error: str | None = None
|
|
|
|
|
|
|
|
|
|
def __setstate__(self, state: list[object]) -> None:
|
|
|
|
|
# Reports pickled before the template diagnostic have one fewer field.
|
|
|
|
|
for index, item in enumerate(fields(self)):
|
|
|
|
|
object.__setattr__(self, item.name, state[index] if index < len(state) else item.default)
|
2026-03-30 19:51:37 -07:00
|
|
|
|
|
|
|
|
@property
|
2026-11-28 00:09:11 -08:00
|
|
|
def intrinsic_collision_count(self) -> int:
|
2026-11-27 15:21:37 -08:00
|
|
|
return (
|
2026-11-28 00:09:11 -08:00
|
|
|
self.static_collision_count + self.self_collision_count
|
2026-11-27 15:21:37 -08:00
|
|
|
+ int(self.out_of_bounds) + int(self.discontinuous) + int(self.invalid_geometry)
|
|
|
|
|
)
|
2026-03-30 19:51:37 -07:00
|
|
|
|
2026-11-28 00:09:11 -08:00
|
|
|
@property
|
|
|
|
|
def collision_count(self) -> int:
|
|
|
|
|
return self.intrinsic_collision_count + self.dynamic_collision_count
|
|
|
|
|
|
2026-03-30 19:51:37 -07:00
|
|
|
@property
|
|
|
|
|
def is_valid(self) -> bool:
|
2026-11-28 20:57:22 -08:00
|
|
|
return self.collision_count == 0 and self.terminal_error is None
|
2026-03-30 19:51:37 -07:00
|
|
|
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class ComponentConflictTrace:
|
|
|
|
|
other_net_id: str
|
|
|
|
|
self_component_index: int
|
|
|
|
|
other_component_index: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class NetConflictTrace:
|
|
|
|
|
net_id: str
|
|
|
|
|
outcome: RoutingOutcome
|
|
|
|
|
reached_target: bool
|
|
|
|
|
report: RoutingReport
|
|
|
|
|
conflicting_net_ids: tuple[str, ...] = ()
|
|
|
|
|
component_conflicts: tuple[ComponentConflictTrace, ...] = ()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class ConflictTraceEntry:
|
|
|
|
|
stage: ConflictTraceStage
|
|
|
|
|
iteration: int | None
|
|
|
|
|
completed_net_ids: tuple[str, ...]
|
|
|
|
|
conflict_edges: tuple[tuple[str, str], ...]
|
|
|
|
|
nets: tuple[NetConflictTrace, ...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class FrontierPruneSample:
|
|
|
|
|
reason: FrontierTraceReason
|
|
|
|
|
move_type: str
|
|
|
|
|
hotspot_index: int
|
|
|
|
|
parent_state: tuple[int, int, int]
|
|
|
|
|
end_state: tuple[int, int, int]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class NetFrontierTrace:
|
|
|
|
|
net_id: str
|
|
|
|
|
hotspot_bounds: tuple[tuple[float, float, float, float], ...]
|
|
|
|
|
pruned_closed_set: int
|
|
|
|
|
pruned_hard_collision: int
|
|
|
|
|
pruned_self_collision: int
|
|
|
|
|
pruned_cost: int
|
|
|
|
|
samples: tuple[FrontierPruneSample, ...] = ()
|
|
|
|
|
|
|
|
|
|
|
2026-04-02 18:57:34 -07:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class PrePairNetTrace:
|
|
|
|
|
net_id: str
|
|
|
|
|
nodes_expanded: int
|
|
|
|
|
congestion_check_calls: int
|
|
|
|
|
pruned_closed_set: int
|
|
|
|
|
pruned_cost: int
|
|
|
|
|
pruned_hard_collision: int
|
|
|
|
|
guidance_seed_present: bool
|
|
|
|
|
frontier: NetFrontierTrace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class PrePairFrontierTraceEntry:
|
|
|
|
|
iteration: int
|
|
|
|
|
routed_net_ids: tuple[str, ...]
|
|
|
|
|
conflict_edges: tuple[tuple[str, str], ...]
|
|
|
|
|
nets: tuple[PrePairNetTrace, ...]
|
|
|
|
|
|
|
|
|
|
|
2026-04-02 16:15:02 -07:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class IterationNetAttemptTrace:
|
|
|
|
|
net_id: str
|
|
|
|
|
reached_target: bool
|
|
|
|
|
nodes_expanded: int
|
|
|
|
|
congestion_check_calls: int
|
|
|
|
|
pruned_closed_set: int
|
|
|
|
|
pruned_cost: int
|
|
|
|
|
pruned_hard_collision: int
|
|
|
|
|
guidance_seed_present: bool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class IterationTraceEntry:
|
|
|
|
|
iteration: int
|
|
|
|
|
congestion_penalty: float
|
|
|
|
|
routed_net_ids: tuple[str, ...]
|
|
|
|
|
completed_nets: int
|
|
|
|
|
conflict_edges: int
|
|
|
|
|
total_dynamic_collisions: int
|
|
|
|
|
nodes_expanded: int
|
|
|
|
|
congestion_check_calls: int
|
|
|
|
|
congestion_candidate_ids: int
|
|
|
|
|
congestion_exact_pair_checks: int
|
|
|
|
|
net_attempts: tuple[IterationNetAttemptTrace, ...] = ()
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 19:51:37 -07:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class RouteMetrics:
|
|
|
|
|
nodes_expanded: int
|
|
|
|
|
moves_generated: int
|
|
|
|
|
moves_added: int
|
|
|
|
|
pruned_closed_set: int
|
|
|
|
|
pruned_hard_collision: int
|
|
|
|
|
pruned_cost: int
|
2026-03-31 17:26:00 -07:00
|
|
|
route_iterations: int
|
|
|
|
|
nets_routed: int
|
|
|
|
|
nets_reached_target: int
|
|
|
|
|
warm_start_paths_built: int
|
|
|
|
|
warm_start_paths_used: int
|
|
|
|
|
refine_path_calls: int
|
|
|
|
|
timeout_events: int
|
2026-11-28 20:42:25 -08:00
|
|
|
frontier_trim_events: int
|
|
|
|
|
frontier_nodes_discarded: int
|
2026-04-01 21:29:23 -07:00
|
|
|
iteration_reverify_calls: int
|
|
|
|
|
iteration_reverified_nets: int
|
|
|
|
|
iteration_conflicting_nets: int
|
|
|
|
|
iteration_conflict_edges: int
|
|
|
|
|
nets_carried_forward: int
|
2026-03-31 17:41:15 -07:00
|
|
|
score_component_calls: int
|
|
|
|
|
score_component_total_ns: int
|
|
|
|
|
path_cost_calls: int
|
|
|
|
|
danger_map_lookup_calls: int
|
|
|
|
|
danger_map_cache_hits: int
|
|
|
|
|
danger_map_cache_misses: int
|
|
|
|
|
danger_map_query_calls: int
|
|
|
|
|
danger_map_total_ns: int
|
2026-03-31 17:26:00 -07:00
|
|
|
move_cache_abs_hits: int
|
|
|
|
|
move_cache_abs_misses: int
|
|
|
|
|
move_cache_rel_hits: int
|
|
|
|
|
move_cache_rel_misses: int
|
2026-04-02 14:39:39 -07:00
|
|
|
guidance_match_moves: int
|
|
|
|
|
guidance_match_moves_straight: int
|
|
|
|
|
guidance_match_moves_bend90: int
|
|
|
|
|
guidance_match_moves_sbend: int
|
|
|
|
|
guidance_bonus_applied: float
|
|
|
|
|
guidance_bonus_applied_straight: float
|
|
|
|
|
guidance_bonus_applied_bend90: float
|
|
|
|
|
guidance_bonus_applied_sbend: float
|
2026-03-31 17:26:00 -07:00
|
|
|
static_safe_cache_hits: int
|
|
|
|
|
hard_collision_cache_hits: int
|
|
|
|
|
congestion_cache_hits: int
|
|
|
|
|
congestion_cache_misses: int
|
2026-04-01 21:29:23 -07:00
|
|
|
congestion_presence_cache_hits: int
|
|
|
|
|
congestion_presence_cache_misses: int
|
|
|
|
|
congestion_presence_skips: int
|
|
|
|
|
congestion_candidate_precheck_hits: int
|
|
|
|
|
congestion_candidate_precheck_misses: int
|
|
|
|
|
congestion_candidate_precheck_skips: int
|
|
|
|
|
congestion_grid_net_cache_hits: int
|
|
|
|
|
congestion_grid_net_cache_misses: int
|
|
|
|
|
congestion_grid_span_cache_hits: int
|
|
|
|
|
congestion_grid_span_cache_misses: int
|
|
|
|
|
congestion_candidate_nets: int
|
|
|
|
|
congestion_net_envelope_cache_hits: int
|
|
|
|
|
congestion_net_envelope_cache_misses: int
|
2026-03-31 17:26:00 -07:00
|
|
|
dynamic_path_objects_added: int
|
|
|
|
|
dynamic_path_objects_removed: int
|
|
|
|
|
dynamic_tree_rebuilds: int
|
|
|
|
|
dynamic_grid_rebuilds: int
|
|
|
|
|
static_tree_rebuilds: int
|
|
|
|
|
static_raw_tree_rebuilds: int
|
|
|
|
|
static_net_tree_rebuilds: int
|
2026-04-01 21:29:23 -07:00
|
|
|
visibility_corner_index_builds: int
|
2026-03-31 17:26:00 -07:00
|
|
|
visibility_builds: int
|
|
|
|
|
visibility_corner_pairs_checked: int
|
2026-03-31 17:41:15 -07:00
|
|
|
visibility_corner_queries_exact: int
|
|
|
|
|
visibility_corner_hits_exact: int
|
2026-03-31 17:26:00 -07:00
|
|
|
visibility_point_queries: int
|
|
|
|
|
visibility_point_cache_hits: int
|
|
|
|
|
visibility_point_cache_misses: int
|
2026-03-31 17:41:15 -07:00
|
|
|
visibility_tangent_candidate_scans: int
|
|
|
|
|
visibility_tangent_candidate_corner_checks: int
|
|
|
|
|
visibility_tangent_candidate_ray_tests: int
|
2026-03-31 17:26:00 -07:00
|
|
|
ray_cast_calls: int
|
2026-03-31 17:41:15 -07:00
|
|
|
ray_cast_calls_straight_static: int
|
|
|
|
|
ray_cast_calls_expand_snap: int
|
|
|
|
|
ray_cast_calls_expand_forward: int
|
|
|
|
|
ray_cast_calls_visibility_build: int
|
|
|
|
|
ray_cast_calls_visibility_query: int
|
|
|
|
|
ray_cast_calls_visibility_tangent: int
|
|
|
|
|
ray_cast_calls_other: int
|
2026-03-31 17:26:00 -07:00
|
|
|
ray_cast_candidate_bounds: int
|
|
|
|
|
ray_cast_exact_geometry_checks: int
|
|
|
|
|
congestion_check_calls: int
|
2026-04-01 21:29:23 -07:00
|
|
|
congestion_lazy_resolutions: int
|
|
|
|
|
congestion_lazy_requeues: int
|
|
|
|
|
congestion_candidate_ids: int
|
2026-03-31 17:26:00 -07:00
|
|
|
congestion_exact_pair_checks: int
|
|
|
|
|
verify_path_report_calls: int
|
|
|
|
|
verify_static_buffer_ops: int
|
2026-04-01 21:29:23 -07:00
|
|
|
verify_dynamic_candidate_nets: int
|
2026-03-31 17:26:00 -07:00
|
|
|
verify_dynamic_exact_pair_checks: int
|
2026-03-31 17:41:15 -07:00
|
|
|
refinement_windows_considered: int
|
|
|
|
|
refinement_static_bounds_checked: int
|
|
|
|
|
refinement_dynamic_bounds_checked: int
|
|
|
|
|
refinement_candidate_side_extents: int
|
|
|
|
|
refinement_candidates_built: int
|
|
|
|
|
refinement_candidates_verified: int
|
|
|
|
|
refinement_candidates_accepted: int
|
2026-04-02 14:39:39 -07:00
|
|
|
pair_local_search_pairs_considered: int
|
|
|
|
|
pair_local_search_attempts: int
|
|
|
|
|
pair_local_search_accepts: int
|
|
|
|
|
pair_local_search_nodes_expanded: int
|
2026-04-02 18:57:34 -07:00
|
|
|
late_phase_capped_nets: int
|
|
|
|
|
late_phase_capped_fallbacks: int
|
2026-03-30 19:51:37 -07:00
|
|
|
|
|
|
|
|
|
2026-11-26 19:58:46 -08:00
|
|
|
def _sum_route_metrics(samples: Iterable[RouteMetrics]) -> RouteMetrics:
|
|
|
|
|
runs = tuple(samples)
|
|
|
|
|
return RouteMetrics(*(sum(getattr(run, metric.name) for run in runs) for metric in fields(RouteMetrics)))
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 19:51:37 -07:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class RoutingResult:
|
|
|
|
|
net_id: str
|
|
|
|
|
path: tuple[ComponentResult, ...]
|
|
|
|
|
reached_target: bool = False
|
|
|
|
|
report: RoutingReport = field(default_factory=RoutingReport)
|
2026-11-29 00:12:59 -08:00
|
|
|
requested_net: NetSpec | None = field(default=None, kw_only=True, compare=False)
|
|
|
|
|
effective_net: NetSpec | None = field(default=None, kw_only=True, compare=False)
|
2026-03-30 19:51:37 -07:00
|
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
|
|
|
|
object.__setattr__(self, "path", tuple(self.path))
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def collisions(self) -> int:
|
|
|
|
|
return self.report.collision_count
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def outcome(self) -> RoutingOutcome:
|
|
|
|
|
if not self.path:
|
|
|
|
|
return "unroutable"
|
|
|
|
|
if not self.reached_target:
|
|
|
|
|
return "partial"
|
2026-11-28 20:57:22 -08:00
|
|
|
if not self.report.is_valid:
|
2026-03-30 19:51:37 -07:00
|
|
|
return "colliding"
|
|
|
|
|
return "completed"
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_valid(self) -> bool:
|
|
|
|
|
return self.outcome == "completed"
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def locked_geometry(self) -> tuple[Polygon, ...]:
|
2026-03-30 23:54:30 -07:00
|
|
|
polygons: list[Polygon] = []
|
2026-03-30 19:51:37 -07:00
|
|
|
for component in self.path:
|
|
|
|
|
polygons.extend(component.physical_geometry)
|
|
|
|
|
return tuple(polygons)
|
|
|
|
|
|
|
|
|
|
def as_seed(self) -> PathSeed:
|
|
|
|
|
return PathSeed(tuple(component.move_spec for component in self.path))
|
|
|
|
|
|
|
|
|
|
|
2026-11-26 11:57:43 -08:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class TopologyReport:
|
|
|
|
|
corridor_counts: tuple[tuple[str, int], ...] = ()
|
|
|
|
|
seeded_net_ids: tuple[str, ...] = ()
|
|
|
|
|
seed_nodes_expanded: int = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RoutingOperation = Literal["plan", "verify", "search", "refine"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _planner_for_mode(mode: str) -> str:
|
|
|
|
|
return {"off": "lattice", "guide": "portal-guide", "seed": "corridor-seed"}.get(mode, mode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class RoutingTiming:
|
|
|
|
|
"""Exclusive elapsed time for an operation, including unsuccessful work."""
|
|
|
|
|
|
|
|
|
|
operation: RoutingOperation
|
|
|
|
|
duration_s: float
|
|
|
|
|
planner: str | None = None
|
|
|
|
|
mode: str | None = None
|
|
|
|
|
policy: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class RoutingStage:
|
|
|
|
|
"""One routing or verification attempt in an automatic run."""
|
|
|
|
|
|
|
|
|
|
mode: str
|
|
|
|
|
direct_net_ids: tuple[str, ...]
|
|
|
|
|
nodes_expanded: int
|
|
|
|
|
node_limit: int
|
|
|
|
|
skipped_net_ids: tuple[str, ...] = ()
|
|
|
|
|
kind: str = "route"
|
|
|
|
|
net_order: str | None = None
|
|
|
|
|
incumbent_net_ids: tuple[str, ...] = ()
|
|
|
|
|
valid_net_ids: tuple[str, ...] = ()
|
|
|
|
|
policy: str | None = None
|
|
|
|
|
retry_reason: str | None = None
|
|
|
|
|
out_of_bounds_net_ids: tuple[str, ...] = ()
|
|
|
|
|
planner: str = ""
|
|
|
|
|
operation: RoutingOperation = "search"
|
|
|
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
|
|
|
|
if not self.planner:
|
|
|
|
|
object.__setattr__(self, "planner", _planner_for_mode(self.mode))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class CapacityLimit:
|
|
|
|
|
"""A vertical cut that bounds the number of routes through the board."""
|
|
|
|
|
|
|
|
|
|
cut_x: float
|
|
|
|
|
crossing_nets: int
|
|
|
|
|
tracks: int
|
|
|
|
|
max_routed: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class AutomaticRoutingReport:
|
|
|
|
|
"""Planner selection, verified coverage, and geometry-based upper bounds."""
|
|
|
|
|
|
|
|
|
|
stages: tuple[RoutingStage, ...]
|
|
|
|
|
selected_stage: int
|
|
|
|
|
max_routed: int
|
|
|
|
|
capacity_limit: CapacityLimit | None = None
|
|
|
|
|
port_conflicts: tuple[tuple[str, str], ...] = ()
|
|
|
|
|
planner_rejections: tuple[str, ...] = ()
|
|
|
|
|
cost_pass_performed: bool = False
|
|
|
|
|
timings: tuple[RoutingTiming, ...] = ()
|
|
|
|
|
duration_s: float = 0.0
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def selected_planner(self) -> str:
|
|
|
|
|
return self.stages[self.selected_stage].planner
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def verified_net_ids(self) -> tuple[str, ...]:
|
|
|
|
|
return self.stages[self.selected_stage].valid_net_ids
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 19:51:37 -07:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class RoutingRunResult:
|
2026-11-29 00:31:51 -08:00
|
|
|
results_by_net: Mapping[str, RoutingResult]
|
2026-03-30 19:51:37 -07:00
|
|
|
metrics: RouteMetrics
|
|
|
|
|
expanded_nodes: tuple[tuple[int, int, int], ...] = ()
|
2026-04-02 14:39:39 -07:00
|
|
|
conflict_trace: tuple[ConflictTraceEntry, ...] = ()
|
|
|
|
|
frontier_trace: tuple[NetFrontierTrace, ...] = ()
|
2026-04-02 18:57:34 -07:00
|
|
|
pre_pair_frontier_trace: PrePairFrontierTraceEntry | None = None
|
2026-04-02 16:15:02 -07:00
|
|
|
iteration_trace: tuple[IterationTraceEntry, ...] = ()
|
2026-11-26 11:57:43 -08:00
|
|
|
topology: TopologyReport | None = None
|
|
|
|
|
automatic: AutomaticRoutingReport | None = None
|
2026-11-29 00:31:51 -08:00
|
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
|
|
|
|
object.__setattr__(self, "results_by_net", snapshot(self.results_by_net))
|