Share the layout ranking used during congestion and repair
This commit is contained in:
parent
1cad709c63
commit
083f4365b4
4 changed files with 61 additions and 43 deletions
|
|
@ -19,6 +19,11 @@ class IterationReview:
|
||||||
completed_net_ids: set[str]
|
completed_net_ids: set[str]
|
||||||
total_dynamic_collisions: int
|
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(
|
def review_verified_results(
|
||||||
results: Mapping[str, RoutingResult],
|
results: Mapping[str, RoutingResult],
|
||||||
|
|
@ -55,7 +60,7 @@ def reroute_plan(
|
||||||
reroute_net_ids: Set[str],
|
reroute_net_ids: Set[str],
|
||||||
recent_attempt_work: Mapping[str, int],
|
recent_attempt_work: Mapping[str, int],
|
||||||
*,
|
*,
|
||||||
best_conflict_edges: int,
|
best_conflict_edges: int | None,
|
||||||
results: Mapping[str, RoutingResult],
|
results: Mapping[str, RoutingResult],
|
||||||
) -> tuple[list[str], set[str]]:
|
) -> tuple[list[str], set[str]]:
|
||||||
routed_net_ids = [net_id for net_id in ordered_net_ids if net_id in reroute_net_ids]
|
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)}
|
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]))
|
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()
|
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]
|
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)
|
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
|
# A non-improving iteration with at most two remaining conflicts is
|
||||||
# enough to hand the reached-target layout to bounded pair-local repair.
|
# 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:
|
def repeated_conflict_count(current_edges: int, previous_edges: int, previous_repeats: int) -> int:
|
||||||
|
|
|
||||||
|
|
@ -61,15 +61,17 @@ class _RoutingState:
|
||||||
accumulated_expanded_nodes: list[tuple[int, int, int]]
|
accumulated_expanded_nodes: list[tuple[int, int, int]]
|
||||||
best_results: dict[str, RoutingResult]
|
best_results: dict[str, RoutingResult]
|
||||||
best_is_installed: bool
|
best_is_installed: bool
|
||||||
best_completed_nets: int
|
best_review: _IterationReview | None
|
||||||
best_conflict_edges: int
|
|
||||||
best_dynamic_collisions: int
|
|
||||||
last_conflict_edge_count: int
|
last_conflict_edge_count: int
|
||||||
repeated_conflict_count: int
|
repeated_conflict_count: int
|
||||||
recent_attempt_work: dict[str, int]
|
recent_attempt_work: dict[str, int]
|
||||||
pre_pair_candidate: _PrePairCandidate | None
|
pre_pair_candidate: _PrePairCandidate | None
|
||||||
corridors: dict[str, tuple[Corridor, ...]] = field(default_factory=dict)
|
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)
|
@dataclass(frozen=True, slots=True)
|
||||||
class _PairLocalTarget:
|
class _PairLocalTarget:
|
||||||
|
|
@ -334,9 +336,7 @@ class PathFinder:
|
||||||
accumulated_expanded_nodes=[],
|
accumulated_expanded_nodes=[],
|
||||||
best_results={},
|
best_results={},
|
||||||
best_is_installed=False,
|
best_is_installed=False,
|
||||||
best_completed_nets=-1,
|
best_review=None,
|
||||||
best_conflict_edges=10**9,
|
|
||||||
best_dynamic_collisions=10**9,
|
|
||||||
last_conflict_edge_count=0,
|
last_conflict_edge_count=0,
|
||||||
repeated_conflict_count=0,
|
repeated_conflict_count=0,
|
||||||
recent_attempt_work={},
|
recent_attempt_work={},
|
||||||
|
|
@ -410,29 +410,11 @@ class PathFinder:
|
||||||
return review
|
return review
|
||||||
|
|
||||||
def _update_best_iteration(self, state: _RoutingState, review: _IterationReview) -> bool:
|
def _update_best_iteration(self, state: _RoutingState, review: _IterationReview) -> bool:
|
||||||
completed_nets = len(review.completed_net_ids)
|
if state.best_review is not None and review.rank >= state.best_review.rank:
|
||||||
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
|
return False
|
||||||
|
|
||||||
state.best_results = dict(state.results)
|
state.best_results = dict(state.results)
|
||||||
state.best_completed_nets = completed_nets
|
state.best_review = review
|
||||||
state.best_conflict_edges = conflict_edges
|
|
||||||
state.best_dynamic_collisions = dynamic_collisions
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _restore_best_iteration(self, state: _RoutingState) -> None:
|
def _restore_best_iteration(self, state: _RoutingState) -> None:
|
||||||
|
|
@ -681,18 +663,9 @@ class PathFinder:
|
||||||
incumbent_results: dict[str, RoutingResult],
|
incumbent_results: dict[str, RoutingResult],
|
||||||
incumbent_review: _IterationReview,
|
incumbent_review: _IterationReview,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
candidate_completed = len(candidate_review.completed_net_ids)
|
candidate_rank, incumbent_rank = candidate_review.rank, incumbent_review.rank
|
||||||
incumbent_completed = len(incumbent_review.completed_net_ids)
|
if candidate_rank != incumbent_rank:
|
||||||
if candidate_completed != incumbent_completed:
|
return candidate_rank < incumbent_rank
|
||||||
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(
|
candidate_length = sum(
|
||||||
result.report.total_length
|
result.report.total_length
|
||||||
|
|
|
||||||
|
|
@ -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)
|
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()
|
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}
|
state.recent_attempt_work = {"netA": 20, "netB": 40, "netC": 400, "netD": 220}
|
||||||
incumbents = dict(state.results)
|
incumbents = dict(state.results)
|
||||||
caps_by_net: dict[str, tuple[int | None, RoutingResult | None]] = {}
|
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()
|
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:
|
def test_route_all_restores_best_iteration_snapshot_on_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||||
problem = RoutingProblem(
|
problem = RoutingProblem(
|
||||||
bounds=(0, 0, 100, 100),
|
bounds=(0, 0, 100, 100),
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from itertools import pairwise
|
||||||
|
|
||||||
from inire.results import RoutingResult
|
from inire.results import RoutingResult
|
||||||
from inire.router._congestion_policy import (
|
from inire.router._congestion_policy import (
|
||||||
IterationReview,
|
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:
|
def test_reroute_plan_caps_only_heavy_nets_in_the_final_conflict_regime() -> None:
|
||||||
ordered = ("a", "b", "c", "d", "e")
|
ordered = ("a", "b", "c", "d", "e")
|
||||||
results = {net_id: RoutingResult(net_id, (), reached_target=True) for net_id in ordered}
|
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=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=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())
|
assert reroute_plan(ordered, set(ordered), last_work, best_conflict_edges=2, results=results) == (list(ordered), set())
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue