From f247cff54a6aaea9bfa478aadf4a9146df38bfbb Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Tue, 1 Dec 2026 23:05:48 -0800 Subject: [PATCH] Validate direct benchmark inputs and preserve fallback headings --- inire/tests/routing_benchmark_case.py | 4 ++- inire/tests/test_routing_benchmarks.py | 47 +++++++++++++++++++------- scripts/compare_topological_routing.py | 22 +++++++++--- 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/inire/tests/routing_benchmark_case.py b/inire/tests/routing_benchmark_case.py index 2390d4c..52c5781 100644 --- a/inire/tests/routing_benchmark_case.py +++ b/inire/tests/routing_benchmark_case.py @@ -2,6 +2,7 @@ from __future__ import annotations +from contextlib import suppress from dataclasses import dataclass, replace from math import pi from typing import TYPE_CHECKING @@ -29,7 +30,8 @@ class BenchmarkCase: def fixed_headings_problem(self) -> RoutingProblem: if self.options.topology.infer_terminal_headings: - return ordered_bundle_terminal_headings(self.problem) + with suppress(ValueError): + return ordered_bundle_terminal_headings(self.problem) return self.problem diff --git a/inire/tests/test_routing_benchmarks.py b/inire/tests/test_routing_benchmarks.py index bf54c90..4feeee2 100644 --- a/inire/tests/test_routing_benchmarks.py +++ b/inire/tests/test_routing_benchmarks.py @@ -194,6 +194,25 @@ def test_retry_experiment_preserves_public_input_handling() -> None: assert baseline["stages"] == experiment["stages"] +def test_direct_benchmarks_validate_inputs_before_locking_routes() -> None: + port = Port(10, 5, 0) + problem = RoutingProblem( + bounds=(0, 0, 100, 40), + nets=(NetSpec("coincident", port, port), NetSpec("straight", Port(10, 15, 0), Port(90, 15, 0))), + ) + options = RoutingOptions(search=SearchOptions(node_limit=20)) + case = BenchmarkCase("direct-inputs", "Coincident ports beside a straight route", problem, options) + bad_seed = replace(problem, initial_paths={"unknown": PathSeed((StraightSeed(20),))}) + + for strategy in ("direct-guide", "direct-probe-near-port", "bundle-first"): + with pytest.raises(ValueError, match="Initial path provided for unknown net: unknown"): + _measure(replace(case, problem=bad_seed), options, strategy) + + sample = _measure(case, options, strategy) + assert sample.net_outcomes == {"coincident": "unroutable", "straight": "completed"} + assert sample.stages[sample.selected_stage].direct_net_ids == ("straight",) + + def test_seed_order_locked_rejects_touching_routes_at_zero_clearance() -> None: problem = RoutingProblem( bounds=(0, 0, 100, 40), @@ -319,20 +338,24 @@ def test_bundle_first_reports_effective_settings_and_capacity_fallback(tmp_path, assert not bundle["refinement"]["enabled"] assert len(success["valid_costs"]) == len(original.problem.nets) - case = replace(original, problem=replace(original.problem, clearance=7)) - options = replace(case.options, search=replace(case.options.search, node_limit=20)) + options = replace(original.options, search=replace(original.options.search, node_limit=20)) + for clearance, rejection in ((7, "insufficient track capacity"), (15, "The short route must leave through the first gap")): + case = replace(original, problem=replace(original.problem, clearance=clearance)) + if clearance == 15: + assert case.fixed_headings_problem() == case.problem - fallback = _measure(case, options, "direct-probe-near-port") - sample = _measure(case, options, "bundle-first") + fallback = _measure(case, options, "direct-probe-near-port") + sample = _measure(case, options, "bundle-first") - assert sample.bundle_rejection is not None - assert "insufficient track capacity" in sample.bundle_rejection - assert sample.bundle_duration_s is not None - assert sample.stages == fallback.stages - assert sample.net_outcomes == fallback.net_outcomes - assert sample.valid_costs == fallback.valid_costs - assert sample.attempt_options[0].topology.mode == "bundle" - assert sample.attempt_options[1:] == fallback.attempt_options + assert sample.bundle_rejection is not None + assert rejection in sample.bundle_rejection + assert sample.bundle_duration_s is not None + assert sample.stages == fallback.stages + assert sample.net_outcomes == fallback.net_outcomes + assert sample.net_outcomes.keys() == {net.net_id for net in case.problem.nets} + assert sample.valid_costs == fallback.valid_costs + assert sample.attempt_options[0].topology.mode == "bundle" + assert sample.attempt_options[1:] == fallback.attempt_options def test_automatic_retry_releases_a_lock_that_disconnects_the_remaining_net() -> None: diff --git a/scripts/compare_topological_routing.py b/scripts/compare_topological_routing.py index 56ed169..ba035ff 100644 --- a/scripts/compare_topological_routing.py +++ b/scripts/compare_topological_routing.py @@ -35,7 +35,7 @@ from inire.tests.routing_corpus import ( ) if TYPE_CHECKING: - from inire import CapacityLimit, RoutingProblem, RoutingTiming, TopologyReport + from inire import CapacityLimit, RoutingProblem, RoutingRunResult, RoutingTiming, TopologyReport, _IterationCallback from inire.router._routing_policy import RouteCall, Trial from inire.tests.routing_benchmark_case import BenchmarkCase @@ -118,6 +118,19 @@ def _measure(case: BenchmarkCase, options: RoutingOptions, strategy: str) -> Run attempt_options: list[RoutingOptions] = [] run_route = partial(_route, run_router=partial(_dispatch_route, attempt_callback=attempt_options.append)) run_worker = partial(route_once, attempt_callback=attempt_options.append) + + def measure_direct(problem: RoutingProblem, policy: str) -> tuple[RoutingRunResult, list[Trial]]: + trials: list[Trial] = [] + + def run_direct( + prepared_problem: RoutingProblem, worker_options: RoutingOptions, _iteration_callback: _IterationCallback | None, + ) -> RoutingRunResult: + trials.extend(_direct_trials(prepared_problem, board, worker_options, policy, run_route=run_worker)) + return _best_trial(trials).run + + run = _route(problem, options=options, run_router=run_direct) + return run, trials + started = perf_counter() bundle_rejection = None bundle_duration = None @@ -159,23 +172,22 @@ def _measure(case: BenchmarkCase, options: RoutingOptions, strategy: str) -> Run except ValueError as exc: bundle_rejection = str(exc) bundle_duration = perf_counter() - started - trials = _direct_trials(case.fixed_headings_problem(), board, options, "direct-probe-near-port", run_route=run_worker) + run, trials = measure_direct(case.fixed_headings_problem(), "direct-probe-near-port") else: bundle_duration = perf_counter() - started stage = RoutingStage("bundle", (), run.metrics.nodes_expanded, options.search.node_limit, kind="bundle") trials = [_trial(run, stage)] elif strategy in DIRECT_POLICIES: - problem = case.fixed_headings_problem() - trials = _direct_trials(problem, board, options, strategy, run_route=run_worker) + run, trials = measure_direct(case.fixed_headings_problem(), strategy) elif strategy in ORDER_POLICIES: problem = case.fixed_headings_problem() trials = _order_trials(problem, options, strategy, run_route=run_route) + run = _best_trial(trials).run else: run = run_route(case.fixed_headings_problem(), options=options) trials = [_trial(run, RoutingStage(strategy, (), run.metrics.nodes_expanded, options.search.node_limit))] best = _best_trial(trials) duration = perf_counter() - started - run = best.run return RunSample( duration_s=duration, net_outcomes={net_id: result.outcome for net_id, result in run.results_by_net.items()},