Share the layout ranking used during congestion and repair

This commit is contained in:
Jan Petykiewicz 2026-11-27 00:28:32 -08:00
commit 083f4365b4
4 changed files with 61 additions and 43 deletions

View file

@ -19,6 +19,11 @@ class IterationReview:
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],
@ -55,7 +60,7 @@ def reroute_plan(
reroute_net_ids: Set[str],
recent_attempt_work: Mapping[str, int],
*,
best_conflict_edges: 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]
@ -64,7 +69,12 @@ def reroute_plan(
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 > 2 or not all_results_reached_target(ordered_net_ids, results):
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]
@ -85,10 +95,10 @@ def next_reroute_net_ids(
return set(ordered_net_ids)
def reached_pair_local_plateau(*, improved: bool, all_reached_target: bool, best_conflict_edges: int) -> bool:
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 <= 2
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:

View file

@ -61,15 +61,17 @@ class _RoutingState:
accumulated_expanded_nodes: list[tuple[int, int, int]]
best_results: dict[str, RoutingResult]
best_is_installed: bool
best_completed_nets: int
best_conflict_edges: int
best_dynamic_collisions: int
best_review: _IterationReview | None
last_conflict_edge_count: int
repeated_conflict_count: int
recent_attempt_work: dict[str, int]
pre_pair_candidate: _PrePairCandidate | None
corridors: dict[str, tuple[Corridor, ...]] = field(default_factory=dict)
@property
def best_conflict_edges(self) -> int | None:
return len(self.best_review.conflict_edges) if self.best_review is not None else None
@dataclass(frozen=True, slots=True)
class _PairLocalTarget:
@ -334,9 +336,7 @@ class PathFinder:
accumulated_expanded_nodes=[],
best_results={},
best_is_installed=False,
best_completed_nets=-1,
best_conflict_edges=10**9,
best_dynamic_collisions=10**9,
best_review=None,
last_conflict_edge_count=0,
repeated_conflict_count=0,
recent_attempt_work={},
@ -410,29 +410,11 @@ class PathFinder:
return review
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:
if state.best_review is not None and review.rank >= state.best_review.rank:
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
state.best_review = review
return True
def _restore_best_iteration(self, state: _RoutingState) -> None:
@ -681,18 +663,9 @@ class PathFinder:
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_rank, incumbent_rank = candidate_review.rank, incumbent_review.rank
if candidate_rank != incumbent_rank:
return candidate_rank < incumbent_rank
candidate_length = sum(
result.report.total_length

View file

@ -631,7 +631,7 @@ def test_run_iteration_caps_two_heaviest_late_phase_nets(monkeypatch: pytest.Mon
net_id: RoutingResult(net_id=net_id, path=(Straight.generate(spec.start, 80.0, 2.0, dilation=1.0),), reached_target=True)
for net_id, spec in state.net_specs.items()
}
state.best_conflict_edges = 2
state.best_review = _IterationReview({"netA", "netB", "netC", "netD"}, {("netA", "netB"), ("netC", "netD")}, {"netE"}, 4)
state.recent_attempt_work = {"netA": 20, "netB": 40, "netC": 400, "netD": 220}
incumbents = dict(state.results)
caps_by_net: dict[str, tuple[int | None, RoutingResult | None]] = {}
@ -766,6 +766,25 @@ def test_route_all_restores_best_iteration_snapshot(monkeypatch: pytest.MonkeyPa
assert evaluator.collision_engine.has_dynamic_paths()
def test_first_iteration_and_pair_length_tie_break() -> None:
problem = RoutingProblem(bounds=(0, 0, 100, 100))
options = RoutingOptions(congestion=CongestionOptions(warm_start_enabled=False), topology=TopologyOptions(mode="off"))
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
finder = PathFinder(AStarContext(evaluator, problem, options))
state = finder._prepare_state()
review = _IterationReview(set(), set(), set(), 0)
assert finder._update_best_iteration(state, review)
assert not finder._update_best_iteration(state, review)
def results(length):
return {"a": RoutingResult("a", (), reached_target=True, report=RoutingReport(total_length=length))}
incumbent = results(10.0)
assert not finder._whole_set_is_better(results(10.0 - 0.5e-6), review, incumbent, review)
assert finder._whole_set_is_better(results(10.0 - 2e-6), review, incumbent, review)
def test_route_all_restores_best_iteration_snapshot_on_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
problem = RoutingProblem(
bounds=(0, 0, 100, 100),

View file

@ -1,3 +1,5 @@
from itertools import pairwise
from inire.results import RoutingResult
from inire.router._congestion_policy import (
IterationReview,
@ -8,6 +10,19 @@ from inire.router._congestion_policy import (
)
def test_layout_rank_prefers_coverage_then_edges_then_collisions() -> None:
one_edge = {("a", "b")}
two_edges = {("a", "b"), ("b", "c")}
ranked = [
IterationReview({"a", "b", "c"}, two_edges, {"d", "e"}, 6),
IterationReview({"a", "b"}, one_edge, {"d"}, 5),
IterationReview({"a", "b"}, one_edge, {"d"}, 8),
IterationReview({"a", "b", "c"}, two_edges, {"d"}, 4),
]
assert all(better.rank < worse.rank for better, worse in pairwise(ranked))
def test_reroute_plan_caps_only_heavy_nets_in_the_final_conflict_regime() -> None:
ordered = ("a", "b", "c", "d", "e")
results = {net_id: RoutingResult(net_id, (), reached_target=True) for net_id in ordered}
@ -16,6 +31,7 @@ def test_reroute_plan_caps_only_heavy_nets_in_the_final_conflict_regime() -> Non
assert reroute_plan(ordered, reroute, last_work, best_conflict_edges=2, results=results) == (["d", "b", "a", "c"], {"a", "c"})
assert reroute_plan(ordered, reroute, last_work, best_conflict_edges=3, results=results) == (["d", "b", "a", "c"], set())
assert reroute_plan(ordered, reroute, last_work, best_conflict_edges=None, results=results) == (["d", "b", "a", "c"], set())
assert reroute_plan(ordered, set(ordered), last_work, best_conflict_edges=2, results=results) == (list(ordered), set())