From 64e103b59e54774a38cb97de744650e50cdf7986 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Thu, 26 Nov 2026 19:59:00 -0800 Subject: [PATCH] Cap the automatic near-port probe at two iterations --- inire/router/_automatic.py | 9 +++++++-- inire/tests/test_routing_benchmarks.py | 23 +++++++++++++++++++++++ scripts/benchmark_retry_order.py | 14 +++++++++----- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/inire/router/_automatic.py b/inire/router/_automatic.py index 2734766..016a508 100644 --- a/inire/router/_automatic.py +++ b/inire/router/_automatic.py @@ -59,6 +59,7 @@ if TYPE_CHECKING: _INITIAL_NODE_LIMIT = 500 _NEAR_PORT_NODE_LIMIT = 100 +_NEAR_PORT_ITERATION_LIMIT = 2 def _has_fragmented_shifts(trial: Trial) -> bool: @@ -506,8 +507,12 @@ class _AutomaticRun: try_construction_early = self.iteration_callback is None and not options.congestion.shuffle_nets if try_construction_early and self._try_column_jogs(options, trials, target): return True - small = replace(options, search=replace(options.search, node_limit=min(_NEAR_PORT_NODE_LIMIT, options.search.node_limit))) - probe = self._direct(small, "direct-probe-near-port") + probe_options = replace( + options, + search=replace(options.search, node_limit=min(_NEAR_PORT_NODE_LIMIT, options.search.node_limit)), + congestion=replace(options.congestion, max_iterations=min(_NEAR_PORT_ITERATION_LIMIT, options.congestion.max_iterations)), + ) + probe = self._direct(probe_options, "direct-probe-near-port") trials.extend(probe) if len(_best_trial(trials).valid_costs) >= target: return True diff --git a/inire/tests/test_routing_benchmarks.py b/inire/tests/test_routing_benchmarks.py index cd8bf5f..a50ea5c 100644 --- a/inire/tests/test_routing_benchmarks.py +++ b/inire/tests/test_routing_benchmarks.py @@ -155,6 +155,29 @@ def test_opt_in_near_port_recovery_finishes_before_the_direct_retry() -> None: assert report.stages[report.selected_stage].kind == "incumbent-verified" +@pytest.mark.parametrize(("iterations", "probe_limit"), [(1, 1), (3, 2)]) +def test_automatic_probe_caps_iterations_and_preserves_callback_order(monkeypatch, iterations, probe_limit) -> None: + (case,) = CASE_GROUPS["pic-splitter-tree-1x8-ew"] + profile = "corpus-regression" + options = automatic_options(profile_options(case, profile), profile) + options = replace(options, congestion=replace(options.congestion, max_iterations=iterations)) + run_worker = _AutomaticRun._run + search_budgets = {} + callback_indices = [] + + def record_budget(self, problem, *, options, **kwargs): + if kwargs.get("policy") in {"direct-probe-near-port", "direct-lookahead-retry"}: + search_budgets[kwargs["policy"]] = options.congestion.max_iterations + return run_worker(self, problem, options=options, **kwargs) + + monkeypatch.setattr(_AutomaticRun, "_run", record_budget) + route(case.problem, options=options, iteration_callback=lambda index, _results: callback_indices.append(index)) + + assert search_budgets == {"direct-probe-near-port": probe_limit, "direct-lookahead-retry": iterations} + assert len(callback_indices) > probe_limit + assert callback_indices == list(range(len(callback_indices))) + + def test_automatic_routes_an_unseeded_grid_between_preplaced_crossing_ports() -> None: case = next(case for case in CROSSING_BENCHMARK_CASES if case.name == "crossing-aligned-grid") assert not case.problem.initial_paths diff --git a/scripts/benchmark_retry_order.py b/scripts/benchmark_retry_order.py index 64441e3..ada1520 100644 --- a/scripts/benchmark_retry_order.py +++ b/scripts/benchmark_retry_order.py @@ -13,7 +13,7 @@ from time import perf_counter from typing import TYPE_CHECKING from inire import route -from inire.router._automatic import _AutomaticRun, _INITIAL_NODE_LIMIT +from inire.router._automatic import _AutomaticRun, _INITIAL_NODE_LIMIT, _NEAR_PORT_ITERATION_LIMIT from inire.router._routing_policy import LockPlan, _best_trial, _direct_trials, _disconnected_nets, _route_residual from inire.tests.routing_corpus import ( CASE_GROUPS, @@ -37,7 +37,7 @@ class _RetryPolicy: lattice_limit: int | None = None recovery_stage: str | None = None recovery_remaining: int = 1 - probe_iterations: int | None = None + probe_iterations: int | None = _NEAR_PORT_ITERATION_LIMIT residual_limit: int | None = None residual_after_fallback: bool = False @@ -51,6 +51,7 @@ _POLICIES = { lattice_limit=1500, recovery_stage="direct-probe-near-port", recovery_remaining=2 ), **{f"probe-iterations-{count}": _RetryPolicy(probe_iterations=count) for count in (1, 2, 3)}, + "probe-full-budget": _RetryPolicy(probe_iterations=None), **{f"residual-{limit}": _RetryPolicy(residual_limit=limit) for limit in (1500, 3000)}, "late-residual-3000": _RetryPolicy(residual_limit=3000, residual_after_fallback=True), } @@ -119,9 +120,12 @@ class _RetryExperiment(_AutomaticRun): return [replace(candidate, selection="incomplete-layout") for candidate in recovered] def _direct(self, options: RoutingOptions, policy: str) -> list[Trial]: - if policy == "direct-probe-near-port" and self.retry_policy.probe_iterations is not None: - iterations = min(options.congestion.max_iterations, self.retry_policy.probe_iterations) - options = replace(options, congestion=replace(options.congestion, max_iterations=iterations)) + if policy == "direct-probe-near-port": + cap = self.retry_policy.probe_iterations + configured = self.options.congestion.max_iterations + iterations = configured if cap is None else min(configured, cap) + if iterations != options.congestion.max_iterations: + options = replace(options, congestion=replace(options.congestion, max_iterations=iterations)) if policy != self.retry_policy.recovery_stage: return super()._direct(options, policy) with self._time("plan", planner="direct", policy=policy):