Validate direct benchmark inputs and preserve fallback headings

This commit is contained in:
Jan Petykiewicz 2026-12-01 23:05:48 -08:00
commit f247cff54a
3 changed files with 57 additions and 20 deletions

View file

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from dataclasses import dataclass, replace from dataclasses import dataclass, replace
from math import pi from math import pi
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -29,6 +30,7 @@ class BenchmarkCase:
def fixed_headings_problem(self) -> RoutingProblem: def fixed_headings_problem(self) -> RoutingProblem:
if self.options.topology.infer_terminal_headings: if self.options.topology.infer_terminal_headings:
with suppress(ValueError):
return ordered_bundle_terminal_headings(self.problem) return ordered_bundle_terminal_headings(self.problem)
return self.problem return self.problem

View file

@ -194,6 +194,25 @@ def test_retry_experiment_preserves_public_input_handling() -> None:
assert baseline["stages"] == experiment["stages"] 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: def test_seed_order_locked_rejects_touching_routes_at_zero_clearance() -> None:
problem = RoutingProblem( problem = RoutingProblem(
bounds=(0, 0, 100, 40), bounds=(0, 0, 100, 40),
@ -319,17 +338,21 @@ def test_bundle_first_reports_effective_settings_and_capacity_fallback(tmp_path,
assert not bundle["refinement"]["enabled"] assert not bundle["refinement"]["enabled"]
assert len(success["valid_costs"]) == len(original.problem.nets) assert len(success["valid_costs"]) == len(original.problem.nets)
case = replace(original, problem=replace(original.problem, clearance=7)) options = replace(original.options, search=replace(original.options.search, node_limit=20))
options = replace(case.options, search=replace(case.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") fallback = _measure(case, options, "direct-probe-near-port")
sample = _measure(case, options, "bundle-first") sample = _measure(case, options, "bundle-first")
assert sample.bundle_rejection is not None assert sample.bundle_rejection is not None
assert "insufficient track capacity" in sample.bundle_rejection assert rejection in sample.bundle_rejection
assert sample.bundle_duration_s is not None assert sample.bundle_duration_s is not None
assert sample.stages == fallback.stages assert sample.stages == fallback.stages
assert sample.net_outcomes == fallback.net_outcomes 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.valid_costs == fallback.valid_costs
assert sample.attempt_options[0].topology.mode == "bundle" assert sample.attempt_options[0].topology.mode == "bundle"
assert sample.attempt_options[1:] == fallback.attempt_options assert sample.attempt_options[1:] == fallback.attempt_options

View file

@ -35,7 +35,7 @@ from inire.tests.routing_corpus import (
) )
if TYPE_CHECKING: 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.router._routing_policy import RouteCall, Trial
from inire.tests.routing_benchmark_case import BenchmarkCase 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] = [] attempt_options: list[RoutingOptions] = []
run_route = partial(_route, run_router=partial(_dispatch_route, attempt_callback=attempt_options.append)) run_route = partial(_route, run_router=partial(_dispatch_route, attempt_callback=attempt_options.append))
run_worker = partial(route_once, 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() started = perf_counter()
bundle_rejection = None bundle_rejection = None
bundle_duration = None bundle_duration = None
@ -159,23 +172,22 @@ def _measure(case: BenchmarkCase, options: RoutingOptions, strategy: str) -> Run
except ValueError as exc: except ValueError as exc:
bundle_rejection = str(exc) bundle_rejection = str(exc)
bundle_duration = perf_counter() - started 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: else:
bundle_duration = perf_counter() - started bundle_duration = perf_counter() - started
stage = RoutingStage("bundle", (), run.metrics.nodes_expanded, options.search.node_limit, kind="bundle") stage = RoutingStage("bundle", (), run.metrics.nodes_expanded, options.search.node_limit, kind="bundle")
trials = [_trial(run, stage)] trials = [_trial(run, stage)]
elif strategy in DIRECT_POLICIES: elif strategy in DIRECT_POLICIES:
problem = case.fixed_headings_problem() run, trials = measure_direct(case.fixed_headings_problem(), strategy)
trials = _direct_trials(problem, board, options, strategy, run_route=run_worker)
elif strategy in ORDER_POLICIES: elif strategy in ORDER_POLICIES:
problem = case.fixed_headings_problem() problem = case.fixed_headings_problem()
trials = _order_trials(problem, options, strategy, run_route=run_route) trials = _order_trials(problem, options, strategy, run_route=run_route)
run = _best_trial(trials).run
else: else:
run = run_route(case.fixed_headings_problem(), options=options) run = run_route(case.fixed_headings_problem(), options=options)
trials = [_trial(run, RoutingStage(strategy, (), run.metrics.nodes_expanded, options.search.node_limit))] trials = [_trial(run, RoutingStage(strategy, (), run.metrics.nodes_expanded, options.search.node_limit))]
best = _best_trial(trials) best = _best_trial(trials)
duration = perf_counter() - started duration = perf_counter() - started
run = best.run
return RunSample( return RunSample(
duration_s=duration, duration_s=duration,
net_outcomes={net_id: result.outcome for net_id, result in run.results_by_net.items()}, net_outcomes={net_id: result.outcome for net_id, result in run.results_by_net.items()},