inire/inire/tests/routing_corpus.py

136 lines
5.4 KiB
Python

"""Case selection and effective options shared by routing benchmarks and tests."""
from __future__ import annotations
from dataclasses import replace
from typing import TYPE_CHECKING
from inire import RoutingOptions
from inire.router.cost import build_native_cost_evaluator, weighted_path_length
from inire.tests.retry_holdout_benchmarks import RETRY_HOLDOUT_CASES
from inire.tests.routing_benchmarks import (
BENCHMARK_CASES,
COMPLEX_BENCHMARK_CASES,
CROSSING_BENCHMARK_CASES,
EXTERNAL_BENCHMARK_CASES,
FALLBACK_BENCHMARK_CASES,
VALIDATION_BENCHMARK_CASES,
)
from inire.tests.stress_routing_benchmarks import STRESS_BENCHMARK_CASES
from inire.tests.terminal_bench_scenario import LENGTH_COST
if TYPE_CHECKING:
from collections.abc import Iterable
from inire.results import RoutingResult
from inire.tests.routing_benchmark_case import BenchmarkCase
BROAD_CASES = (*VALIDATION_BENCHMARK_CASES, *FALLBACK_BENCHMARK_CASES, *EXTERNAL_BENCHMARK_CASES)
CORPUS_CASES = (*BENCHMARK_CASES, *BROAD_CASES, *CROSSING_BENCHMARK_CASES, *STRESS_BENCHMARK_CASES)
SNAPSHOT_CASES = tuple(
case for case in (*BENCHMARK_CASES, *FALLBACK_BENCHMARK_CASES, *EXTERNAL_BENCHMARK_CASES) if case.name != "terminal-bench"
)
EXAMPLE_NAMES = {
"terminal-bench": "complex-routing-example-1",
"terminal-bench-point-terminals": "complex-routing-example-1-point-terminals",
}
_NAMED_CASES = {case.name: (case,) for case in (*CORPUS_CASES, *RETRY_HOLDOUT_CASES)}
CASE_GROUPS = {
**_NAMED_CASES,
**{example_name: _NAMED_CASES[case_name] for case_name, example_name in EXAMPLE_NAMES.items()},
"all": BENCHMARK_CASES,
"complex": COMPLEX_BENCHMARK_CASES,
"holdout": VALIDATION_BENCHMARK_CASES,
"fallback": FALLBACK_BENCHMARK_CASES,
"external": EXTERNAL_BENCHMARK_CASES,
"broad": BROAD_CASES,
"crossings": CROSSING_BENCHMARK_CASES,
"stress": STRESS_BENCHMARK_CASES,
"retry-holdout": RETRY_HOLDOUT_CASES,
"corpus": CORPUS_CASES,
}
PROFILES = ("matched-budget", "public-defaults", "corpus-regression")
AUTOMATIC_COST_MODES = {"automatic": "selective", "automatic-span": "never", "automatic-weighted": "always"}
AUTOMATIC_POLICIES = tuple(AUTOMATIC_COST_MODES)
def select_cases(groups: Iterable[str]) -> tuple[BenchmarkCase, ...]:
return tuple({case.name: case for group in groups for case in CASE_GROUPS[group]}.values())
def _public_defaults(case: BenchmarkCase, defaults: RoutingOptions) -> RoutingOptions:
geometry = case.options.search
return replace(
defaults,
search=replace(
defaults.search,
bend_radii=geometry.bend_radii,
sbend_radii=geometry.sbend_radii,
sbend_offsets=geometry.sbend_offsets,
bend_collision_type=geometry.bend_collision_type,
bend_proxy_geometry=geometry.bend_proxy_geometry,
bend_physical_geometry=geometry.bend_physical_geometry,
bend_clip_margin=geometry.bend_clip_margin,
min_straight_length=geometry.min_straight_length,
max_straight_length=geometry.max_straight_length,
),
topology=replace(defaults.topology, infer_terminal_headings=case.options.topology.infer_terminal_headings),
)
def profile_options(case: BenchmarkCase, profile: str) -> RoutingOptions:
"""Resolve fixture geometry and the named budget before mode or CLI overrides."""
base = case.options
if profile == "fixture":
return base
if profile == "corpus-regression":
return replace(
base,
search=replace(base.search, node_limit=500),
congestion=replace(base.congestion, max_iterations=3),
topology=replace(base.topology, seed_node_limit=500),
)
defaults = RoutingOptions()
if profile == "public-defaults":
return _public_defaults(case, defaults)
if profile != "matched-budget":
raise ValueError(f"Unknown benchmark profile: {profile}")
return replace(
base,
search=replace(base.search, node_limit=500),
congestion=replace(
base.congestion,
max_iterations=2,
net_order=defaults.congestion.net_order,
warm_start_enabled=defaults.congestion.warm_start_enabled,
),
topology=replace(defaults.topology, infer_terminal_headings=base.topology.infer_terminal_headings),
)
def automatic_options(options: RoutingOptions, profile: str, strategy: str = "automatic") -> RoutingOptions:
topology = options.topology
weights = topology.automatic_length_weights
if profile in ("matched-budget", "corpus-regression"):
weights = (LENGTH_COST["straight"], LENGTH_COST["bend90"], LENGTH_COST["sbend"])
cost_policy = topology.automatic_cost if profile == "public-defaults" and strategy == "automatic" else AUTOMATIC_COST_MODES[strategy]
return replace(options, topology=replace(topology, mode="automatic", automatic_cost=cost_policy, automatic_length_weights=weights))
def benchmark_weighted_length(result: RoutingResult) -> float:
return weighted_path_length(result.path, LENGTH_COST)
def native_verified_costs(
case: BenchmarkCase,
options: RoutingOptions,
results: dict[str, RoutingResult],
verified_net_ids: Iterable[str],
) -> dict[str, float]:
scorer = build_native_cost_evaluator(case.problem, options.objective)
costs = {}
for net_id in verified_net_ids:
path = results[net_id].path
costs[net_id] = scorer.path_cost(path[0].start_port, path)
return costs