109 lines
4.3 KiB
Python
109 lines
4.3 KiB
Python
"""Direct routing policies available through the benchmark CLI."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, replace
|
|
from functools import partial
|
|
from typing import TYPE_CHECKING
|
|
|
|
from inire.router._routing_policy import (
|
|
_alternate_locks,
|
|
_direct_trials,
|
|
_lattice_probe,
|
|
_plan_locks,
|
|
_prefer_connected_locks,
|
|
_route_residual,
|
|
_split_search_budget,
|
|
_trial,
|
|
)
|
|
from inire.router._stack import route_once
|
|
from inire.tests.routing_corpus import benchmark_weighted_length
|
|
|
|
if TYPE_CHECKING:
|
|
from typing import Literal
|
|
|
|
from shapely.geometry import Polygon
|
|
|
|
from inire import RoutingOptions, RoutingProblem, RoutingRunResult, RoutingStage
|
|
from inire.router._routing_policy import LockPlan, Trial
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class _DirectExperiment:
|
|
residual_mode: Literal["guide", "seed"] | None = None
|
|
check_connectivity: bool = False
|
|
reorder_disconnected: bool = False
|
|
split_budget: bool = False
|
|
probe: Literal["first", "after"] | None = None
|
|
retry_unresolved: bool = False
|
|
|
|
|
|
_LOOKAHEAD = _DirectExperiment(check_connectivity=True, reorder_disconnected=True)
|
|
# The two None entries use the shipped retry sequence.
|
|
_EXPERIMENTS = {
|
|
"direct-guide": _DirectExperiment(residual_mode="guide"),
|
|
"direct-seed": _DirectExperiment(residual_mode="seed"),
|
|
"direct-adaptive": _DirectExperiment(),
|
|
"direct-adaptive-retry": _DirectExperiment(retry_unresolved=True),
|
|
"direct-cull": _DirectExperiment(check_connectivity=True),
|
|
"direct-lookahead": _LOOKAHEAD,
|
|
"direct-budgeted-retry": _DirectExperiment(split_budget=True),
|
|
"direct-lookahead-retry": None,
|
|
"direct-probe-first": replace(_LOOKAHEAD, split_budget=True, probe="first"),
|
|
"direct-probe-near-port": None,
|
|
"direct-probe-after": replace(_LOOKAHEAD, split_budget=True, probe="after"),
|
|
}
|
|
DIRECT_POLICIES = tuple(_EXPERIMENTS)
|
|
|
|
|
|
def direct_trials(problem: RoutingProblem, board: Polygon, options: RoutingOptions, strategy: str) -> list[Trial]:
|
|
experiment = _EXPERIMENTS[strategy]
|
|
summarize = partial(_trial, score=benchmark_weighted_length)
|
|
if experiment is None:
|
|
return _direct_trials(problem, board, options, strategy, check_trial=summarize)
|
|
|
|
def check(run: RoutingRunResult, stage: RoutingStage) -> Trial:
|
|
return summarize(run, replace(stage, policy=strategy))
|
|
|
|
def run_residual(worker: RoutingOptions, locks: LockPlan) -> Trial:
|
|
return check(*_route_residual(problem, worker, locks, residual_mode=experiment.residual_mode))
|
|
|
|
plan = partial(_plan_locks, problem, board, options, check_connectivity=experiment.check_connectivity)
|
|
locks = plan()
|
|
if experiment.reorder_disconnected:
|
|
locks = _prefer_connected_locks(problem, board, options, locks)
|
|
|
|
first_options = options
|
|
retry_options = options
|
|
retry_locks = None
|
|
probe_limit = 0
|
|
if experiment.split_budget:
|
|
retry_locks = _alternate_locks(problem, board, options, locks, check_connectivity=experiment.check_connectivity)
|
|
if retry_locks is not None:
|
|
probe_limit = min(32, options.search.node_limit // 3) if experiment.probe else 0
|
|
first_options, retry_options = _split_search_budget(options, probe_limit)
|
|
|
|
trials = []
|
|
if probe_limit and experiment.probe == "first":
|
|
trials.append(_lattice_probe(problem, options, probe_limit, run_route=route_once, check_trial=check))
|
|
if len(trials[-1].valid_costs) == len(problem.nets):
|
|
return trials
|
|
|
|
first = run_residual(first_options, locks)
|
|
trials.append(first)
|
|
if len(first.valid_costs) == len(problem.nets):
|
|
return trials
|
|
|
|
if probe_limit and experiment.probe == "after":
|
|
trials.append(_lattice_probe(problem, options, probe_limit, run_route=route_once, check_trial=check))
|
|
if len(trials[-1].valid_costs) == len(problem.nets):
|
|
return trials
|
|
|
|
if experiment.retry_unresolved and 0 < len(locks.direct) < len(problem.nets):
|
|
unresolved = frozenset(first.run.results_by_net.keys() - first.valid_costs.keys())
|
|
candidate = plan(priority=unresolved)
|
|
if candidate.direct.keys() != locks.direct.keys():
|
|
retry_locks = candidate
|
|
if retry_locks is not None:
|
|
trials.append(run_residual(retry_options, retry_locks))
|
|
return trials
|