inire/inire/router/_congestion_policy.py
Jan Petykiewicz 63acb94642 Fix routing correctness and bound optional routing work
Check physical keepout clearance, retain useful partial paths, and account for all pair repair searches. Verify complete seeds early and recover narrower corridors when shared guides are blocked.

Keep frontier diagnostics outside routing decisions and bound their work by the worker deadline. Correct benchmark scaling and scenario validation, and update the guides and release metadata.
2026-12-02 17:16:34 -08:00

108 lines
4.1 KiB
Python

"""Decisions taken between negotiated-congestion iterations."""
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING, TypeGuard
if TYPE_CHECKING:
from collections.abc import Iterable, Mapping, Sequence, Set
from inire.geometry.collision import PathVerificationDetail
from inire.model import NetOrder, NetSpec
from inire.results import RoutingResult
def ordered_nets(nets: Iterable[NetSpec], order: NetOrder) -> list[NetSpec]:
"""Apply the requested net order, retaining input order for ties."""
ordered = list(nets)
if order != "user":
ordered.sort(key=lambda net: abs(net.target.x - net.start.x) + abs(net.target.y - net.start.y), reverse=order == "longest")
return ordered
@dataclass(slots=True)
class IterationReview:
conflicting_nets: set[str]
conflict_edges: set[tuple[str, str]]
completed_net_ids: set[str]
total_dynamic_collisions: int
@property
def rank(self) -> tuple[int, int, int]:
"""Order verified layouts by completed nets, conflict edges, then collisions."""
return -len(self.completed_net_ids), len(self.conflict_edges), self.total_dynamic_collisions
def review_verified_results(
results: Mapping[str, RoutingResult],
details_by_net: Mapping[str, PathVerificationDetail],
) -> IterationReview:
conflict_edges: set[tuple[str, str]] = set()
conflicting_nets: set[str] = set()
completed_net_ids: set[str] = set()
total_dynamic_collisions = 0
for net_id, detail in details_by_net.items():
result = results[net_id]
if not result.reached_target:
continue
total_dynamic_collisions += detail.report.dynamic_collision_count
if result.is_valid:
completed_net_ids.add(net_id)
if detail.conflicting_net_ids:
conflicting_nets.add(net_id)
conflicting_nets.update(detail.conflicting_net_ids)
conflict_edges.update(
(net_id, peer) if net_id < peer else (peer, net_id)
for peer in detail.conflicting_net_ids
if peer != net_id
)
return IterationReview(conflicting_nets, conflict_edges, completed_net_ids, total_dynamic_collisions)
def all_results_reached_target(ordered_net_ids: Sequence[str], results: Mapping[str, RoutingResult]) -> bool:
return len(results) == len(ordered_net_ids) and all(result.reached_target for result in results.values())
def can_reserve(result: RoutingResult | None) -> TypeGuard[RoutingResult]:
return bool(result and result.path and result.reached_target and not result.report.intrinsic_collision_count)
def reroute_plan(
ordered_net_ids: Sequence[str],
reroute_net_ids: Set[str],
recent_attempt_work: Mapping[str, int],
*,
best_conflict_edges: int | None,
results: Mapping[str, RoutingResult],
) -> tuple[list[str], set[str]]:
routed_net_ids = [net_id for net_id in ordered_net_ids if net_id in reroute_net_ids]
if len(reroute_net_ids) >= len(ordered_net_ids) or not recent_attempt_work:
return routed_net_ids, set()
order_index = {net_id: idx for idx, net_id in enumerate(ordered_net_ids)}
routed_net_ids.sort(key=lambda net_id: (recent_attempt_work.get(net_id, 0), order_index[net_id]))
if (
len(routed_net_ids) != 4
or best_conflict_edges is None
or best_conflict_edges > 2
or not all_results_reached_target(ordered_net_ids, results)
):
return routed_net_ids, set()
heaviest = sorted(routed_net_ids, key=lambda net_id: (-recent_attempt_work.get(net_id, 0), order_index[net_id]))[:2]
return routed_net_ids, {
net_id for net_id in heaviest if recent_attempt_work.get(net_id, 0) >= 200 and not results[net_id].report.intrinsic_collision_count
}
def next_reroute_net_ids(
ordered_net_ids: Sequence[str],
review: IterationReview,
*,
all_reached_target: bool,
has_corridors: bool,
) -> set[str]:
if has_corridors or (all_reached_target and 0 < len(review.conflict_edges) <= 3):
return set(ordered_net_ids).difference(review.completed_net_ids)
return set(ordered_net_ids)