From d95eab6963df96ecec5316be27bd542058d1a2f1 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Fri, 27 Nov 2026 00:34:49 -0800 Subject: [PATCH] Simplify pair repair targets and avoid redundant index restores --- inire/router/_router.py | 33 +++++++-------------- inire/tests/test_api.py | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 22 deletions(-) diff --git a/inire/router/_router.py b/inire/router/_router.py index a8e1478..fc0b912 100644 --- a/inire/router/_router.py +++ b/inire/router/_router.py @@ -75,11 +75,6 @@ class _RoutingState: return len(self.best_review.conflict_edges) if self.best_review is not None else None -@dataclass(frozen=True, slots=True) -class _PairLocalTarget: - net_ids: tuple[str, str] - - @dataclass(frozen=True, slots=True) class _PrePairCandidate: iteration: int @@ -666,20 +661,17 @@ class PathFinder: return candidate_length < incumbent_length return False - def _pair_local_attempt_orders(self, target: _PairLocalTarget) -> tuple[tuple[str, str], tuple[str, str]]: - return target.net_ids, target.net_ids[::-1] - def _collect_pair_local_targets( self, state: _RoutingState, results: dict[str, RoutingResult], review: _IterationReview, - ) -> list[_PairLocalTarget]: + ) -> list[tuple[str, str]]: if not review.conflict_edges: return [] order_index = {net_id: idx for idx, net_id in enumerate(state.ordered_net_ids)} seen_net_ids: set[str] = set() - targets: list[_PairLocalTarget] = [] + targets: list[tuple[str, str]] = [] for left_net_id, right_net_id in sorted(review.conflict_edges): if left_net_id in seen_net_ids or right_net_id in seen_net_ids: return [] @@ -693,8 +685,8 @@ class PathFinder: ): continue seen_net_ids.update((left_net_id, right_net_id)) - targets.append(_PairLocalTarget(net_ids=(left_net_id, right_net_id))) - targets.sort(key=lambda target: min(order_index[target.net_ids[0]], order_index[target.net_ids[1]])) + targets.append((left_net_id, right_net_id)) + targets.sort(key=lambda pair: min(order_index[pair[0]], order_index[pair[1]])) return targets def _materialize_pre_pair_frontier_trace( @@ -772,10 +764,10 @@ class PathFinder: incumbent_review: _IterationReview, ) -> tuple[bool, _IterationReview]: self._replace_installed_paths(state, candidate_results) - candidate_results, _candidate_details_by_net, candidate_review = self._analyze_results( + candidate_results, _, candidate_review = self._analyze_results( state.ordered_net_ids, candidate_results, - capture_component_conflicts=True, + capture_component_conflicts=False, count_iteration_metrics=False, ) if self._whole_set_is_better( @@ -794,14 +786,13 @@ class PathFinder: def _run_pair_local_target( self, state: _RoutingState, - target: _PairLocalTarget, + target: tuple[str, str], review: _IterationReview, ) -> _IterationReview: - incumbent_results = dict(state.results) - incumbent_review = review + incumbent_results = state.results ordered_nets = tuple(state.net_specs[net_id] for net_id in state.ordered_net_ids) self.metrics.total_pair_local_search_pairs_considered += 1 - for pair_order in self._pair_local_attempt_orders(target): + for pair_order in (target, (target[1], target[0])): self.metrics.total_pair_local_search_attempts += 1 candidate = search_pair_candidate( self.context, @@ -819,14 +810,12 @@ class PathFinder: state, {**incumbent_results, **replacements}, incumbent_results, - incumbent_review, + review, ) if accepted: return next_review - state.results = incumbent_results - self._replace_installed_paths(state, incumbent_results) - return incumbent_review + return review def _run_pair_local_search(self, state: _RoutingState, review: _IterationReview) -> None: targets = self._collect_pair_local_targets(state, state.results, review) diff --git a/inire/tests/test_api.py b/inire/tests/test_api.py index dfd2142..74ee7d8 100644 --- a/inire/tests/test_api.py +++ b/inire/tests/test_api.py @@ -785,6 +785,71 @@ def test_first_iteration_and_pair_length_tie_break() -> None: assert finder._whole_set_is_better(results(10.0 - 2e-6), review, incumbent, review) +def test_pair_repair_limits_disjoint_targets_in_routing_order(monkeypatch: pytest.MonkeyPatch) -> None: + problem = RoutingProblem( + bounds=(0, 0, 100, 100), + nets=tuple(NetSpec(net_id, Port(10, i * 10, 0), Port(90, i * 10, 0)) for i, net_id in enumerate("cdbaef", 1)), + ) + options = RoutingOptions(congestion=CongestionOptions(warm_start_enabled=False), topology=TopologyOptions(mode="off")) + world = RoutingWorld(clearance=problem.clearance) + finder = PathFinder(AStarContext(CostEvaluator(world, DangerMap(bounds=problem.bounds)), problem, options)) + state = finder._prepare_state() + state.results = {net_id: RoutingResult(net_id, (), reached_target=True) for net_id in state.ordered_net_ids} + review = _IterationReview(set(state.ordered_net_ids), {("a", "b"), ("c", "d"), ("e", "f")}, set(), 6) + visited = [] + + def repair(_finder, _state, pair, current_review): + visited.append(pair) + return current_review + + monkeypatch.setattr(PathFinder, "_run_pair_local_target", repair) + finder._run_pair_local_search(state, review) + assert visited == [("c", "d"), ("a", "b")] + + visited.clear() + review.conflict_edges.add(("b", "c")) + finder._run_pair_local_search(state, review) + assert not visited + + +@pytest.mark.parametrize("candidate_reaches_live_check", [False, True]) +def test_rejected_pair_keeps_original_index_for_reverse_attempt(monkeypatch: pytest.MonkeyPatch, candidate_reaches_live_check: bool) -> None: + problem = RoutingProblem( + bounds=(0, 0, 100, 100), + nets=(NetSpec("a", Port(10, 20, 0), Port(90, 20, 0)), NetSpec("b", Port(10, 40, 0), Port(90, 40, 0))), + ) + options = RoutingOptions(congestion=CongestionOptions(warm_start_enabled=False), topology=TopologyOptions(mode="off")) + world = RoutingWorld(clearance=problem.clearance) + finder = PathFinder(AStarContext(CostEvaluator(world, DangerMap(bounds=problem.bounds)), problem, options)) + state = finder._prepare_state() + for net in problem.nets: + path = (Straight.generate(net.start, 80, net.width, dilation=1.0),) + finder._install_path(net.net_id, path) + state.results[net.net_id] = RoutingResult(net.net_id, path, reached_target=True, report=world.verify_path_report(net.net_id, path)) + initial_bounds = sorted(world.iter_dynamic_path_bounds(problem.bounds)) + incumbent = dict(state.results) + review = _IterationReview(set(), set(), {"a", "b"}, 0) + attempted = [] + + def search(_context, _ordered, _results, pair_order, **_kwargs): + attempted.append(pair_order) + assert sorted(world.iter_dynamic_path_bounds(problem.bounds)) == initial_bounds + if candidate_reaches_live_check and pair_order == ("a", "b"): + return {"a": RoutingResult("a", incumbent["b"].path, reached_target=True)}, 0 + return None + + monkeypatch.setattr(router_module, "search_pair_candidate", search) + assert finder._run_pair_local_target(state, ("a", "b"), review) is review + + assert attempted == [("a", "b"), ("b", "a")] + assert state.results == incumbent + assert sorted(world.iter_dynamic_path_bounds(problem.bounds)) == initial_bounds + assert all(world.verify_path_report(net_id, result.path) == result.report for net_id, result in incumbent.items()) + metrics = finder.metrics.snapshot() + assert metrics.dynamic_path_objects_added == 2 + 4 * candidate_reaches_live_check + assert metrics.dynamic_path_objects_removed == 4 * candidate_reaches_live_check + + def test_route_all_restores_best_iteration_snapshot_on_timeout(monkeypatch: pytest.MonkeyPatch) -> None: problem = RoutingProblem( bounds=(0, 0, 100, 100),