inire/inire/router/_congestion_policy.py

116 lines
4.5 KiB
Python

"""Decisions taken between negotiated-congestion iterations."""
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
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 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}
def next_reroute_net_ids(
ordered_net_ids: Sequence[str],
review: IterationReview,
*,
all_reached_target: bool,
has_corridors: bool,
) -> set[str]:
if all_reached_target and 0 < len(review.conflict_edges) <= 3:
return set(review.conflicting_nets)
if has_corridors:
return set(ordered_net_ids).difference(review.completed_net_ids)
return set(ordered_net_ids)
def reached_pair_local_plateau(*, improved: bool, all_reached_target: bool, best_conflict_edges: int | None) -> bool:
# A non-improving iteration with at most two remaining conflicts is
# enough to hand the reached-target layout to bounded pair-local repair.
return not improved and all_reached_target and best_conflict_edges is not None and best_conflict_edges <= 2
def repeated_conflict_count(current_edges: int, previous_edges: int, previous_repeats: int) -> int:
if current_edges and current_edges == previous_edges:
return previous_repeats + 1
return 0