2026-11-26 11:57:43 -08:00
|
|
|
"""Automatic planner selection and verified residual routing."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from contextlib import contextmanager, suppress
|
2026-11-26 19:58:46 -08:00
|
|
|
from dataclasses import replace
|
2026-11-26 11:57:43 -08:00
|
|
|
from functools import partial
|
2026-11-26 19:58:46 -08:00
|
|
|
from itertools import chain, islice, pairwise, product
|
2026-11-26 11:57:43 -08:00
|
|
|
from time import perf_counter
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
import shapely
|
|
|
|
|
from shapely.geometry import box
|
|
|
|
|
|
2026-11-30 09:25:27 -08:00
|
|
|
from inire.geometry.collision import RoutingWorld, _violates_net_clearance
|
2026-11-29 00:24:00 -08:00
|
|
|
from inire.model import FixedRoute, resolve_bend_geometry
|
2026-11-29 00:18:30 -08:00
|
|
|
from inire.results import AutomaticRoutingReport, RoutingStage, RoutingTiming, _planner_for_mode
|
2026-11-26 11:57:43 -08:00
|
|
|
from inire.router._bundle import ordered_bundle_terminal_headings, plan_ordered_bundle
|
2026-11-26 19:58:46 -08:00
|
|
|
from inire.router._capacity import cut_capacity, port_conflicts
|
|
|
|
|
from inire.router._columns import column_jog_candidates, column_seed_candidates
|
2026-11-26 15:23:24 -08:00
|
|
|
from inire.router._compact import compact_port_seed_set, short_shift_seed_set
|
2026-11-27 00:30:40 -08:00
|
|
|
from inire.router._congestion_policy import ordered_nets
|
2026-11-26 15:23:24 -08:00
|
|
|
from inire.router._perimeter import (
|
|
|
|
|
bundle_turnaround_candidates,
|
|
|
|
|
perimeter_detour_candidates,
|
|
|
|
|
residual_dogleg_candidates,
|
|
|
|
|
terminal_link_seed,
|
|
|
|
|
)
|
2026-11-27 15:21:37 -08:00
|
|
|
from inire.router._route_traces import complete_final_conflict_trace
|
2026-11-26 15:23:24 -08:00
|
|
|
from inire.router._routing_policy import (
|
|
|
|
|
LockPlan,
|
|
|
|
|
_best_trial,
|
|
|
|
|
_direct_trials,
|
|
|
|
|
_disconnected_nets,
|
|
|
|
|
_is_short_shift,
|
|
|
|
|
_residual_problem,
|
2026-11-27 00:38:00 -08:00
|
|
|
_route_prepared_residual,
|
2026-11-27 00:53:58 -08:00
|
|
|
_route_residual,
|
2026-11-26 15:23:24 -08:00
|
|
|
_trial,
|
|
|
|
|
)
|
2026-11-29 00:16:13 -08:00
|
|
|
from inire.router._seed_materialization import materialize_path_seed, seed_preparation_error
|
2026-11-29 00:18:30 -08:00
|
|
|
from inire.router._stack import RoutingAttempts
|
2026-11-27 21:14:09 -08:00
|
|
|
from inire.router._topology import constrained_triangulation_available, plan_corridors
|
2026-11-26 23:00:23 -08:00
|
|
|
from inire.router.cost import build_native_cost_evaluator, weighted_path_length
|
2026-11-26 11:57:43 -08:00
|
|
|
from inire.seeds import SBendSeed
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from collections.abc import Callable, Iterable, Iterator
|
2026-11-26 19:58:46 -08:00
|
|
|
from typing import Literal
|
2026-11-26 11:57:43 -08:00
|
|
|
|
|
|
|
|
from shapely.geometry.base import BaseGeometry
|
|
|
|
|
|
|
|
|
|
from inire.model import NetSpec, RoutingOptions, RoutingProblem
|
2026-11-29 00:18:30 -08:00
|
|
|
from inire.results import RoutingOperation, RoutingResult, RoutingRunResult
|
2026-11-26 11:57:43 -08:00
|
|
|
from inire.router._routing_policy import Trial
|
2026-11-26 15:23:24 -08:00
|
|
|
from inire.router._topology import Corridor
|
2026-11-26 23:00:23 -08:00
|
|
|
from inire.router.cost import CostEvaluator
|
2026-11-26 11:57:43 -08:00
|
|
|
from inire.seeds import PathSeed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_INITIAL_NODE_LIMIT = 500
|
|
|
|
|
_NEAR_PORT_NODE_LIMIT = 100
|
2026-11-26 19:59:00 -08:00
|
|
|
_NEAR_PORT_ITERATION_LIMIT = 2
|
2026-11-26 11:57:43 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _has_fragmented_shifts(trial: Trial) -> bool:
|
|
|
|
|
for net_id in trial.valid_costs:
|
|
|
|
|
path = trial.run.results_by_net[net_id].path
|
|
|
|
|
# A chain of S-bends with a short offset is a sign that the span
|
|
|
|
|
# planner is piecing together a shift across a single window.
|
|
|
|
|
for a, b in pairwise(path):
|
|
|
|
|
if a.move_type == b.move_type == "sbend" and any(
|
|
|
|
|
isinstance(move.move_spec, SBendSeed) and abs(move.move_spec.offset) <= move.move_spec.radius / 2 for move in (a, b)
|
|
|
|
|
):
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _AutomaticRun:
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
problem: RoutingProblem,
|
|
|
|
|
options: RoutingOptions,
|
|
|
|
|
iteration_callback: Callable[[int, dict[str, RoutingResult]], None] | None,
|
|
|
|
|
) -> None:
|
2026-11-28 20:29:40 -08:00
|
|
|
if any(not seed.segments for seed in problem.initial_paths.values()):
|
|
|
|
|
problem = replace(problem, initial_paths={net_id: seed for net_id, seed in problem.initial_paths.items() if seed.segments})
|
2026-11-26 11:57:43 -08:00
|
|
|
self.raw_problem = problem
|
|
|
|
|
self.problem = problem
|
|
|
|
|
self.options = options
|
|
|
|
|
self.board = box(*problem.bounds)
|
2026-11-29 00:18:30 -08:00
|
|
|
self.attempts = RoutingAttempts(iteration_callback)
|
2026-11-26 11:57:43 -08:00
|
|
|
self.timings: list[RoutingTiming] = []
|
|
|
|
|
self.cost_pass_performed = False
|
|
|
|
|
self.evaluator: CostEvaluator | None = None
|
|
|
|
|
weights = options.topology.automatic_length_weights
|
|
|
|
|
self.length_weights = dict(zip(("straight", "bend90", "sbend"), weights, strict=True)) if weights is not None else None
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
def _time(
|
|
|
|
|
self,
|
|
|
|
|
operation: RoutingOperation,
|
|
|
|
|
*,
|
|
|
|
|
planner: str | None = None,
|
|
|
|
|
mode: str | None = None,
|
|
|
|
|
policy: str | None = None,
|
|
|
|
|
) -> Iterator[None]:
|
|
|
|
|
index = len(self.timings)
|
|
|
|
|
timing = RoutingTiming(operation, 0.0, planner, mode, policy)
|
|
|
|
|
self.timings.append(timing)
|
|
|
|
|
started = perf_counter()
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
elapsed = perf_counter() - started
|
|
|
|
|
# Nested verification and search are labeled independently of candidate generation.
|
|
|
|
|
child_time = sum(child.duration_s for child in self.timings[index + 1 :])
|
|
|
|
|
self.timings[index] = replace(timing, duration_s=max(0.0, elapsed - child_time))
|
|
|
|
|
|
|
|
|
|
def _run(
|
|
|
|
|
self,
|
|
|
|
|
problem: RoutingProblem,
|
|
|
|
|
*,
|
|
|
|
|
options: RoutingOptions,
|
|
|
|
|
operation: RoutingOperation = "search",
|
|
|
|
|
planner: str | None = None,
|
|
|
|
|
policy: str | None = None,
|
2026-11-26 15:23:24 -08:00
|
|
|
corridors: dict[str, tuple[Corridor, ...]] | None = None,
|
2026-11-26 11:57:43 -08:00
|
|
|
) -> RoutingRunResult:
|
|
|
|
|
mode = options.topology.mode
|
|
|
|
|
time_operation = partial(self._time, planner=planner or _planner_for_mode(mode), mode=mode, policy=policy)
|
|
|
|
|
with time_operation(operation):
|
2026-11-29 00:18:30 -08:00
|
|
|
return self.attempts.run(
|
2026-11-26 11:57:43 -08:00
|
|
|
problem,
|
|
|
|
|
options=options,
|
|
|
|
|
phase_timer=time_operation,
|
2026-11-26 15:23:24 -08:00
|
|
|
corridors=corridors,
|
2026-11-26 11:57:43 -08:00
|
|
|
)
|
|
|
|
|
|
2026-11-26 19:58:46 -08:00
|
|
|
def _selection_cost(self, result: RoutingResult) -> float:
|
2026-11-28 20:06:06 -08:00
|
|
|
if not self.options.search.node_limit:
|
|
|
|
|
# Verification-only runs have a single candidate to report.
|
|
|
|
|
return 0.0
|
2026-11-26 11:57:43 -08:00
|
|
|
if self.length_weights is not None:
|
2026-11-26 19:58:46 -08:00
|
|
|
return weighted_path_length(result.path, self.length_weights)
|
2026-11-26 11:57:43 -08:00
|
|
|
if self.evaluator is None:
|
2026-11-28 20:42:25 -08:00
|
|
|
self.evaluator = build_native_cost_evaluator(
|
|
|
|
|
self.problem, self.options.objective, proximity_range=self.options.search.proximity_range,
|
|
|
|
|
)
|
2026-11-26 11:57:43 -08:00
|
|
|
return self.evaluator.path_cost(result.path[0].start_port, result.path)
|
|
|
|
|
|
|
|
|
|
def _trial(self, run: RoutingRunResult, stage: RoutingStage) -> Trial:
|
|
|
|
|
with self._time("verify", planner=stage.planner, mode=stage.mode, policy=stage.policy):
|
2026-11-27 14:12:40 -08:00
|
|
|
return _trial(run, stage, score=self._selection_cost)
|
2026-11-26 11:57:43 -08:00
|
|
|
|
2026-11-28 10:57:01 -08:00
|
|
|
def _seeded_problem(self, trial: Trial, *, include_unresolved: bool = False) -> RoutingProblem:
|
|
|
|
|
seeds = {
|
|
|
|
|
net_id: result.as_seed() for net_id, result in trial.run.results_by_net.items()
|
|
|
|
|
if result.path and (include_unresolved or net_id in trial.valid_costs)
|
|
|
|
|
}
|
2026-11-28 20:29:40 -08:00
|
|
|
nets = []
|
|
|
|
|
for net in self.problem.nets:
|
|
|
|
|
if net.net_id not in seeds:
|
|
|
|
|
continue
|
|
|
|
|
result = trial.run.results_by_net[net.net_id]
|
|
|
|
|
nets.append(replace(
|
|
|
|
|
net, start=result.path[0].start_port,
|
|
|
|
|
target=result.path[-1].end_port if result.reached_target else net.target,
|
|
|
|
|
))
|
|
|
|
|
return replace(self.problem, nets=tuple(nets), initial_paths=seeds)
|
2026-11-26 19:58:46 -08:00
|
|
|
|
|
|
|
|
def _seeded_attempt(
|
|
|
|
|
self,
|
|
|
|
|
problem: RoutingProblem,
|
|
|
|
|
planner: str,
|
|
|
|
|
*,
|
|
|
|
|
kind: str,
|
|
|
|
|
policy: str | None,
|
|
|
|
|
operation: Literal["verify", "refine"],
|
2026-11-28 10:57:01 -08:00
|
|
|
node_limit: int = 1,
|
2026-11-26 19:58:46 -08:00
|
|
|
) -> tuple[RoutingRunResult, RoutingStage]:
|
2026-11-26 11:57:43 -08:00
|
|
|
options = self.options
|
2026-11-26 19:58:46 -08:00
|
|
|
refinement = options.refinement
|
|
|
|
|
if operation == "verify":
|
|
|
|
|
refinement = replace(refinement, enabled=False)
|
2026-11-28 20:29:40 -08:00
|
|
|
node_limit = 0
|
2026-11-26 19:58:46 -08:00
|
|
|
worker = replace(
|
2026-11-26 11:57:43 -08:00
|
|
|
options,
|
2026-11-28 10:57:01 -08:00
|
|
|
search=replace(options.search, node_limit=node_limit),
|
2026-11-26 11:57:43 -08:00
|
|
|
topology=replace(options.topology, mode="off"),
|
|
|
|
|
congestion=replace(options.congestion, max_iterations=1),
|
2026-11-26 19:58:46 -08:00
|
|
|
refinement=refinement,
|
2026-11-26 11:57:43 -08:00
|
|
|
)
|
2026-11-26 19:58:46 -08:00
|
|
|
run = self._run(problem, options=worker, operation=operation, planner=planner, policy=policy)
|
|
|
|
|
stage = RoutingStage(
|
|
|
|
|
worker.topology.mode,
|
|
|
|
|
(),
|
|
|
|
|
run.metrics.nodes_expanded,
|
|
|
|
|
worker.search.node_limit,
|
|
|
|
|
kind=kind,
|
|
|
|
|
planner=planner,
|
|
|
|
|
operation=operation,
|
|
|
|
|
policy=policy,
|
|
|
|
|
)
|
|
|
|
|
return run, stage
|
|
|
|
|
|
2026-11-28 20:29:40 -08:00
|
|
|
def _verify(
|
|
|
|
|
self, problem: RoutingProblem, planner: str, *,
|
|
|
|
|
kind: str | None = None, policy: str | None = None, preserve_hints: bool = False,
|
|
|
|
|
) -> Trial:
|
|
|
|
|
def verify(candidate: RoutingProblem) -> Trial:
|
|
|
|
|
run, stage = self._seeded_attempt(candidate, planner, kind=kind or planner, policy=policy, operation="verify")
|
|
|
|
|
return self._trial(run, stage)
|
|
|
|
|
|
|
|
|
|
if preserve_hints and self.problem.initial_paths:
|
|
|
|
|
active_ids = {net.net_id for net in problem.nets}
|
|
|
|
|
hints = {net_id: seed for net_id, seed in self.problem.initial_paths.items() if net_id in active_ids}
|
|
|
|
|
merged = {**problem.initial_paths, **hints}
|
|
|
|
|
if merged != problem.initial_paths:
|
|
|
|
|
incumbent = verify(replace(problem, initial_paths=merged))
|
|
|
|
|
if len(incumbent.valid_costs) == len(problem.nets):
|
|
|
|
|
return incumbent
|
|
|
|
|
candidate = verify(problem)
|
|
|
|
|
return candidate if candidate.improves_on(incumbent) else incumbent
|
|
|
|
|
return verify(problem)
|
2026-11-26 11:57:43 -08:00
|
|
|
|
|
|
|
|
def _constructive_trials(self) -> tuple[list[Trial], tuple[str, ...]]:
|
2026-11-28 20:29:40 -08:00
|
|
|
options = self.options
|
|
|
|
|
problem = replace(self.problem, initial_paths={}) if self.problem.initial_paths else self.problem
|
|
|
|
|
verify = partial(self._verify, preserve_hints=True)
|
2026-11-26 11:57:43 -08:00
|
|
|
trials: list[Trial] = []
|
|
|
|
|
rejections = []
|
|
|
|
|
try:
|
|
|
|
|
with self._time("plan", planner="bundle"):
|
2026-11-28 20:29:40 -08:00
|
|
|
raw = replace(self.raw_problem, initial_paths={}) if self.raw_problem.initial_paths else self.raw_problem
|
|
|
|
|
bundle = plan_ordered_bundle(raw, options.search, infer_terminal_headings=True)
|
2026-11-26 11:57:43 -08:00
|
|
|
except ValueError as exc:
|
|
|
|
|
rejections.append(str(exc))
|
|
|
|
|
else:
|
|
|
|
|
if bundle.nets == problem.nets:
|
2026-11-28 20:29:40 -08:00
|
|
|
trials.append(verify(bundle, "bundle"))
|
2026-11-26 11:57:43 -08:00
|
|
|
if len(trials[-1].valid_costs) == len(problem.nets):
|
|
|
|
|
return trials, ()
|
|
|
|
|
rejections.append("Bundle candidate failed geometric verification")
|
|
|
|
|
elif not options.topology.infer_terminal_headings:
|
|
|
|
|
with self._time("plan", planner="bundle-turnaround"):
|
|
|
|
|
for candidate in bundle_turnaround_candidates(problem, bundle, options.search):
|
2026-11-28 20:29:40 -08:00
|
|
|
trials.append(verify(candidate, "bundle-turnaround"))
|
2026-11-26 11:57:43 -08:00
|
|
|
if len(trials[-1].valid_costs) == len(problem.nets):
|
|
|
|
|
return trials, ()
|
|
|
|
|
rejections.append("No geometrically verified turnaround matched the terminal headings")
|
|
|
|
|
|
|
|
|
|
with self._time("plan", planner="compact-port-jogs"):
|
|
|
|
|
compact = compact_port_seed_set(problem, options.search)
|
|
|
|
|
if compact is not None:
|
2026-11-28 20:29:40 -08:00
|
|
|
trials.append(verify(replace(problem, initial_paths=compact), "compact-port-jogs"))
|
2026-11-26 11:57:43 -08:00
|
|
|
if len(trials[-1].valid_costs) == len(problem.nets):
|
|
|
|
|
return trials, tuple(rejections)
|
|
|
|
|
|
2026-11-28 20:29:40 -08:00
|
|
|
span_trial = self._column_trial(False, trials, problem=problem)
|
2026-11-26 11:57:43 -08:00
|
|
|
if span_trial is None:
|
|
|
|
|
rejections.append("Span scoring found no verified column layout")
|
|
|
|
|
policy = options.topology.automatic_cost
|
|
|
|
|
if policy == "always" or (policy == "selective" and span_trial is not None and _has_fragmented_shifts(span_trial)):
|
|
|
|
|
self.cost_pass_performed = True
|
2026-11-28 20:29:40 -08:00
|
|
|
if self._column_trial(True, trials, problem=problem) is None:
|
2026-11-26 11:57:43 -08:00
|
|
|
rejections.append("Cost scoring found no verified column layout")
|
2026-11-26 15:23:24 -08:00
|
|
|
if not trials or len(_best_trial(trials).valid_costs) < len(problem.nets):
|
|
|
|
|
planner = "local-sbend"
|
|
|
|
|
with self._time("plan", planner=planner):
|
2026-11-26 19:58:46 -08:00
|
|
|
shifts = short_shift_seed_set(problem, options.search, self._selection_cost)
|
2026-11-26 15:23:24 -08:00
|
|
|
if shifts is not None:
|
2026-11-28 20:29:40 -08:00
|
|
|
trial = verify(replace(problem, initial_paths=shifts), planner)
|
2026-11-26 15:23:24 -08:00
|
|
|
complete = len(trial.valid_costs) == len(problem.nets)
|
2026-11-26 19:58:46 -08:00
|
|
|
trials.append(trial if complete else replace(trial, selection="incomplete-layout"))
|
2026-11-26 15:23:24 -08:00
|
|
|
if not complete:
|
|
|
|
|
rejections.append("Local S-bend layout failed full verification")
|
2026-11-26 11:57:43 -08:00
|
|
|
return trials, tuple(rejections)
|
|
|
|
|
|
2026-11-28 20:29:40 -08:00
|
|
|
def _column_trial(self, weighted: bool, trials: list[Trial], *, problem: RoutingProblem) -> Trial | None:
|
2026-11-26 11:57:43 -08:00
|
|
|
objective = self.options.objective
|
|
|
|
|
weights = self.length_weights or dict.fromkeys(("straight", "bend90", "sbend"), objective.unit_length_cost)
|
|
|
|
|
penalty = 0.0 if self.length_weights is not None else objective.sbend_penalty
|
|
|
|
|
policy = "weighted" if weighted else "span"
|
|
|
|
|
kind = "column-cost" if weighted else "column-span"
|
|
|
|
|
with self._time("plan", planner="column", policy=policy):
|
2026-11-28 20:29:40 -08:00
|
|
|
candidates = column_seed_candidates(problem, self.options.search, weights, weighted=weighted, sbend_penalty=penalty)
|
2026-11-26 11:57:43 -08:00
|
|
|
for seeds in islice(candidates, 8):
|
2026-11-28 20:29:40 -08:00
|
|
|
trial = self._verify(replace(problem, initial_paths=seeds), "column", kind=kind, policy=policy, preserve_hints=True)
|
2026-11-26 11:57:43 -08:00
|
|
|
trials.append(trial)
|
|
|
|
|
if len(trial.valid_costs) == len(self.problem.nets):
|
|
|
|
|
return trial
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def _paired_perimeter(self, trials: list[Trial]) -> None:
|
|
|
|
|
problem, options = self.problem, self.options
|
|
|
|
|
if len(problem.nets) != 2:
|
|
|
|
|
return
|
2026-11-28 20:42:25 -08:00
|
|
|
world = RoutingWorld.from_problem(problem, board=self.board)
|
2026-11-26 11:57:43 -08:00
|
|
|
|
|
|
|
|
def footprints(net: NetSpec, seeds: Iterable[PathSeed]) -> list[tuple[PathSeed, BaseGeometry]]:
|
|
|
|
|
choices = []
|
|
|
|
|
for seed in seeds:
|
2026-11-29 00:16:13 -08:00
|
|
|
path = materialize_path_seed(
|
|
|
|
|
seed, start=net.start, net_width=net.width, search=options.search, clearance=problem.clearance, bounds=problem.bounds,
|
|
|
|
|
)
|
2026-11-26 11:57:43 -08:00
|
|
|
if not path or path[-1].end_port != net.target or not world.verify_path_report(net.net_id, path).is_valid:
|
|
|
|
|
continue
|
|
|
|
|
footprint = shapely.union_all([shape for component in path for shape in component.physical_geometry])
|
2026-11-27 14:12:40 -08:00
|
|
|
choices.append((seed, footprint))
|
2026-11-26 11:57:43 -08:00
|
|
|
return choices
|
|
|
|
|
|
|
|
|
|
candidates: list[list[tuple[PathSeed, BaseGeometry]]] = []
|
|
|
|
|
links: list[list[tuple[PathSeed, BaseGeometry]]] = []
|
|
|
|
|
for net in problem.nets:
|
|
|
|
|
supplied = problem.initial_paths.get(net.net_id)
|
|
|
|
|
simple = supplied if supplied is not None else terminal_link_seed(net, options.search)
|
|
|
|
|
links.append(footprints(net, (simple,)) if simple is not None else [])
|
|
|
|
|
single = replace(problem, nets=(net,), initial_paths={})
|
|
|
|
|
detours = (seed for _, seed in islice(perimeter_detour_candidates(single, options.search), 32))
|
|
|
|
|
candidates.append(links[-1] if supplied is not None else [*links[-1], *footprints(net, detours)])
|
|
|
|
|
|
|
|
|
|
pairs: Iterable[tuple[tuple[PathSeed, BaseGeometry], tuple[PathSeed, BaseGeometry]]] = product(*candidates)
|
|
|
|
|
first, second = problem.nets
|
|
|
|
|
for index, net in enumerate(problem.nets):
|
|
|
|
|
if net.net_id in problem.initial_paths or not links[1 - index]:
|
|
|
|
|
continue
|
|
|
|
|
peer_link = links[1 - index][0]
|
2026-11-29 00:24:00 -08:00
|
|
|
peer = problem.nets[1 - index]
|
|
|
|
|
fixed = FixedRoute(peer.net_id, tuple(shapely.get_parts(peer_link[1])))
|
|
|
|
|
reserved = replace(problem, nets=(net,), initial_paths={}, fixed_routes=(*problem.fixed_routes, fixed))
|
2026-11-26 11:57:43 -08:00
|
|
|
detours = footprints(net, (seed for _, seed in islice(perimeter_detour_candidates(reserved, options.search), 16)))
|
|
|
|
|
reserved_pairs = product(detours, (peer_link,)) if index == 0 else product((peer_link,), detours)
|
|
|
|
|
pairs = chain(pairs, reserved_pairs)
|
|
|
|
|
|
|
|
|
|
for (first_seed, first_shape), (second_seed, second_shape) in pairs:
|
2026-11-30 09:25:27 -08:00
|
|
|
if _violates_net_clearance(first_shape, second_shape, problem.clearance):
|
2026-11-26 11:57:43 -08:00
|
|
|
continue
|
|
|
|
|
plan = replace(problem, initial_paths={first.net_id: first_seed, second.net_id: second_seed})
|
|
|
|
|
trial = self._verify(plan, "paired-perimeter")
|
|
|
|
|
trials.append(trial)
|
|
|
|
|
if len(trial.valid_costs) == len(problem.nets):
|
|
|
|
|
return
|
2026-11-26 19:58:46 -08:00
|
|
|
trials[-1] = replace(trial, selection="incomplete-layout")
|
2026-11-26 11:57:43 -08:00
|
|
|
|
|
|
|
|
def _perimeter_trials(self, options: RoutingOptions) -> list[Trial]:
|
|
|
|
|
problem = self.problem
|
|
|
|
|
trials = []
|
|
|
|
|
if len(problem.nets) == 2:
|
|
|
|
|
with self._time("plan", planner="paired-perimeter"):
|
|
|
|
|
self._paired_perimeter(trials)
|
|
|
|
|
if trials and len(trials[-1].valid_costs) == len(problem.nets):
|
|
|
|
|
return trials
|
|
|
|
|
for net, seed in islice(perimeter_detour_candidates(problem, options.search), 32):
|
|
|
|
|
detour_problem = replace(problem, nets=(net,), initial_paths={net.net_id: seed})
|
|
|
|
|
candidate = self._verify(detour_problem, "perimeter", kind="perimeter-check")
|
|
|
|
|
if len(problem.nets) == 1 and candidate.valid_costs:
|
|
|
|
|
trials.append(replace(candidate, stage=replace(candidate.stage, kind="perimeter")))
|
|
|
|
|
break
|
2026-11-26 19:58:46 -08:00
|
|
|
trials.append(replace(candidate, selection="local-only"))
|
2026-11-26 11:57:43 -08:00
|
|
|
if not candidate.valid_costs:
|
|
|
|
|
continue
|
|
|
|
|
chosen = candidate.run.results_by_net[net.net_id]
|
2026-11-27 00:53:58 -08:00
|
|
|
policy = "perimeter-residual"
|
|
|
|
|
guide = replace(options, congestion=replace(options.congestion, max_iterations=1))
|
|
|
|
|
run, stage = _route_residual(
|
2026-11-26 11:57:43 -08:00
|
|
|
problem,
|
2026-11-27 00:53:58 -08:00
|
|
|
guide,
|
|
|
|
|
LockPlan({net.net_id: chosen}),
|
|
|
|
|
residual_mode="guide",
|
|
|
|
|
run_route=partial(self._run, policy=policy),
|
2026-11-26 11:57:43 -08:00
|
|
|
)
|
2026-11-27 00:53:58 -08:00
|
|
|
stage = replace(stage, kind=policy, policy=policy, planner="perimeter")
|
|
|
|
|
trials.append(self._trial(run, stage))
|
2026-11-26 11:57:43 -08:00
|
|
|
if len(trials[-1].valid_costs) == len(problem.nets):
|
2026-11-26 19:58:46 -08:00
|
|
|
trials[-1] = replace(trials[-1], selection="requires-layout-check")
|
2026-11-27 00:53:58 -08:00
|
|
|
seeds = {net_id: result.as_seed() for net_id, result in run.results_by_net.items()}
|
2026-11-26 11:57:43 -08:00
|
|
|
trials.append(self._verify(replace(problem, initial_paths=seeds), "perimeter"))
|
|
|
|
|
if len(trials[-1].valid_costs) == len(problem.nets):
|
|
|
|
|
break
|
|
|
|
|
return trials
|
|
|
|
|
|
2026-11-26 20:17:53 -08:00
|
|
|
def _incumbent_trials(self, options: RoutingOptions, best: Trial, target: int) -> list[Trial]:
|
2026-11-26 11:57:43 -08:00
|
|
|
problem = self.problem
|
|
|
|
|
retained = {net_id: best.run.results_by_net[net_id] for net_id in best.valid_costs}
|
|
|
|
|
with self._time("plan", policy="incumbent-retry"):
|
|
|
|
|
blocked = _disconnected_nets(problem, self.board, options, retained)
|
|
|
|
|
if blocked:
|
|
|
|
|
seeds = {net_id: result.as_seed() for net_id, result in retained.items()}
|
|
|
|
|
run = self._run(replace(problem, initial_paths=seeds), options=options, policy="incumbent-retry")
|
|
|
|
|
stage = RoutingStage(
|
|
|
|
|
"seed",
|
|
|
|
|
(),
|
|
|
|
|
run.metrics.nodes_expanded,
|
|
|
|
|
options.search.node_limit,
|
|
|
|
|
kind="incumbent-retry",
|
|
|
|
|
policy="incumbent-retry",
|
|
|
|
|
incumbent_net_ids=tuple(retained),
|
|
|
|
|
retry_reason=f"Incumbents disconnect {', '.join(sorted(blocked))} when locked",
|
|
|
|
|
)
|
|
|
|
|
return [self._trial(run, stage)]
|
|
|
|
|
|
|
|
|
|
policy = "incumbent-residual"
|
2026-11-26 15:23:24 -08:00
|
|
|
locks = LockPlan(retained)
|
2026-11-27 00:38:00 -08:00
|
|
|
residual = _residual_problem(problem, locks)
|
|
|
|
|
run, stage = _route_prepared_residual(residual, options, locks, residual_mode="seed", run_route=partial(self._run, policy=policy))
|
2026-11-26 11:57:43 -08:00
|
|
|
stage = replace(stage, kind=policy, policy=policy, retry_reason="Residual terminals remain connected with the incumbents locked")
|
|
|
|
|
trial = self._trial(run, stage)
|
2026-11-26 15:23:24 -08:00
|
|
|
if len(trial.valid_costs) == len(problem.nets):
|
2026-11-26 19:58:46 -08:00
|
|
|
return [replace(trial, selection="requires-layout-check"), self._verify_incumbent(trial)]
|
2026-11-26 15:23:24 -08:00
|
|
|
trials = [trial]
|
|
|
|
|
if len(trial.valid_costs) < target:
|
|
|
|
|
dogleg = self._residual_dogleg_trial(options, _best_trial([best, trial]))
|
|
|
|
|
if dogleg is not None:
|
|
|
|
|
return [trial, dogleg]
|
2026-11-27 00:38:00 -08:00
|
|
|
joint = self._joint_lane_trial(residual, options, locks, trial)
|
2026-11-26 15:23:24 -08:00
|
|
|
if joint is not None:
|
2026-11-26 19:58:46 -08:00
|
|
|
trials.append(replace(joint, selection="requires-layout-check"))
|
2026-11-26 15:23:24 -08:00
|
|
|
if joint.improves_on(_best_trial([best, trial])):
|
|
|
|
|
trials.append(self._verify_incumbent(joint))
|
|
|
|
|
return trials
|
|
|
|
|
|
|
|
|
|
def _verify_incumbent(self, trial: Trial) -> Trial:
|
2026-11-26 19:58:46 -08:00
|
|
|
problem = self._seeded_problem(trial)
|
|
|
|
|
# Check the seeded subproblem before restoring the unresolved results.
|
2026-11-26 15:23:24 -08:00
|
|
|
verified = self._verify(problem, trial.stage.planner, kind="incumbent-verified", policy=trial.stage.policy)
|
|
|
|
|
results = {**trial.run.results_by_net, **verified.run.results_by_net}
|
2026-11-26 19:58:46 -08:00
|
|
|
return replace(verified.as_replacement_for(trial), run=replace(verified.run, results_by_net=results))
|
2026-11-26 15:23:24 -08:00
|
|
|
|
|
|
|
|
def _residual_dogleg_trial(self, options: RoutingOptions, incumbent: Trial) -> Trial | None:
|
|
|
|
|
problem = self.problem
|
|
|
|
|
if len(incumbent.valid_costs) != len(problem.nets) - 1:
|
|
|
|
|
return None
|
|
|
|
|
retained = {net_id: incumbent.run.results_by_net[net_id] for net_id in incumbent.valid_costs}
|
|
|
|
|
residual = _residual_problem(problem, LockPlan(retained))
|
|
|
|
|
(net,) = residual.nets
|
|
|
|
|
seeds = {net_id: result.as_seed() for net_id, result in retained.items()}
|
|
|
|
|
planner = "residual-dogleg"
|
|
|
|
|
with self._time("plan", planner=planner, policy=planner):
|
|
|
|
|
for seed in islice(residual_dogleg_candidates(residual, options.search), 8):
|
|
|
|
|
candidate = replace(problem, initial_paths={**seeds, net.net_id: seed})
|
|
|
|
|
trial = self._verify(candidate, planner, policy=planner)
|
|
|
|
|
if len(trial.valid_costs) == len(problem.nets):
|
|
|
|
|
return trial
|
|
|
|
|
return None
|
2026-11-26 11:57:43 -08:00
|
|
|
|
|
|
|
|
def _direct(self, options: RoutingOptions, policy: str) -> list[Trial]:
|
|
|
|
|
with self._time("plan", planner="direct", policy=policy):
|
|
|
|
|
return _direct_trials(
|
|
|
|
|
self.problem,
|
|
|
|
|
self.board,
|
|
|
|
|
options,
|
|
|
|
|
policy,
|
|
|
|
|
run_route=partial(self._run, policy=policy),
|
|
|
|
|
check_trial=self._trial,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _early_guide(self, options: RoutingOptions) -> Trial | None:
|
|
|
|
|
problem = self.problem
|
|
|
|
|
if (
|
|
|
|
|
len(problem.nets) <= 2
|
|
|
|
|
or problem.initial_paths
|
|
|
|
|
or options.search.node_limit <= _NEAR_PORT_NODE_LIMIT
|
|
|
|
|
or options.congestion.shuffle_nets
|
|
|
|
|
):
|
|
|
|
|
return None
|
|
|
|
|
max_offset = 2 * max(options.search.sbend_radii, default=0)
|
|
|
|
|
if not all(_is_short_shift(net, max_offset) for net in problem.nets) or len({net.start.r % 180 for net in problem.nets}) < 2:
|
|
|
|
|
return None
|
|
|
|
|
policy = "early-guide"
|
|
|
|
|
guide = replace(options, topology=replace(options.topology, mode="guide"))
|
|
|
|
|
run = self._run(problem, options=guide, policy=policy)
|
|
|
|
|
return self._trial(run, RoutingStage("guide", (), run.metrics.nodes_expanded, guide.search.node_limit, policy=policy))
|
|
|
|
|
|
2026-11-26 15:26:34 -08:00
|
|
|
def _try_column_jogs(self, options: RoutingOptions, trials: list[Trial], target: int) -> bool:
|
|
|
|
|
problem = self.problem
|
|
|
|
|
if target != len(problem.nets) or problem.initial_paths:
|
|
|
|
|
return False
|
|
|
|
|
with self._time("plan", planner="column-jogs"):
|
|
|
|
|
for seeds in islice(column_jog_candidates(problem, options.search), 8):
|
|
|
|
|
trial = self._verify(replace(problem, initial_paths=seeds), "column-jogs")
|
|
|
|
|
if len(trial.valid_costs) == target:
|
|
|
|
|
trials.append(trial)
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2026-11-26 11:57:43 -08:00
|
|
|
def _search_trials(self, trials: list[Trial], target: int) -> None:
|
|
|
|
|
configured_search = self.options.search
|
|
|
|
|
first_limit = min(_INITIAL_NODE_LIMIT, configured_search.node_limit)
|
|
|
|
|
options = replace(
|
|
|
|
|
self.options,
|
|
|
|
|
search=replace(configured_search, node_limit=first_limit),
|
|
|
|
|
refinement=replace(self.options.refinement, enabled=False),
|
|
|
|
|
)
|
2026-11-26 19:58:46 -08:00
|
|
|
if self._initial_search_trials(options, trials, target):
|
2026-11-26 15:26:34 -08:00
|
|
|
return
|
2026-11-26 19:58:46 -08:00
|
|
|
self._seed_recovery_trials(options, trials, target)
|
2026-11-26 11:57:43 -08:00
|
|
|
if len(_best_trial(trials).valid_costs) < target:
|
|
|
|
|
self._lattice_fallback(trials, options, "Automatic planners left unresolved nets")
|
|
|
|
|
if first_limit < configured_search.node_limit and len(_best_trial(trials).valid_costs) < target:
|
|
|
|
|
self._lattice_fallback(
|
|
|
|
|
trials,
|
|
|
|
|
replace(options, search=configured_search),
|
|
|
|
|
"Initial pass fell short of the routing bound",
|
|
|
|
|
policy="budget-escalation",
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-26 19:58:46 -08:00
|
|
|
def _initial_search_trials(self, options: RoutingOptions, trials: list[Trial], target: int) -> bool:
|
|
|
|
|
problem = self.problem
|
|
|
|
|
early = self._early_guide(options)
|
|
|
|
|
if early is not None:
|
|
|
|
|
trials.append(early)
|
|
|
|
|
if len(_best_trial(trials).valid_costs) >= target:
|
|
|
|
|
return True
|
|
|
|
|
|
2026-11-27 15:21:37 -08:00
|
|
|
try_construction_early = not options.congestion.shuffle_nets
|
2026-11-26 19:58:46 -08:00
|
|
|
if try_construction_early and self._try_column_jogs(options, trials, target):
|
|
|
|
|
return True
|
2026-11-26 20:21:22 -08:00
|
|
|
probe_iterations = options.congestion.max_iterations
|
|
|
|
|
proxy, _ = resolve_bend_geometry(options.search)
|
|
|
|
|
# Clipped proxies can find cheaper routes in later probe iterations than in the wider retry.
|
|
|
|
|
if proxy == "arc":
|
|
|
|
|
probe_iterations = min(_NEAR_PORT_ITERATION_LIMIT, probe_iterations)
|
2026-11-26 19:59:00 -08:00
|
|
|
probe_options = replace(
|
|
|
|
|
options,
|
|
|
|
|
search=replace(options.search, node_limit=min(_NEAR_PORT_NODE_LIMIT, options.search.node_limit)),
|
2026-11-26 20:21:22 -08:00
|
|
|
congestion=replace(options.congestion, max_iterations=probe_iterations),
|
2026-11-26 19:59:00 -08:00
|
|
|
)
|
|
|
|
|
probe = self._direct(probe_options, "direct-probe-near-port")
|
2026-11-26 19:58:46 -08:00
|
|
|
trials.extend(probe)
|
|
|
|
|
if len(_best_trial(trials).valid_costs) >= target:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
if not try_construction_early and self._try_column_jogs(options, trials, target):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
if try_construction_early and target == len(problem.nets) and target > 2 and not problem.initial_paths:
|
|
|
|
|
dogleg = self._residual_dogleg_trial(options, _best_trial(probe))
|
|
|
|
|
if dogleg is not None:
|
|
|
|
|
trials.append(dogleg)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
if len(problem.nets) <= 2:
|
|
|
|
|
with self._time("plan", planner="perimeter"):
|
|
|
|
|
trials.extend(self._perimeter_trials(options))
|
|
|
|
|
if len(_best_trial(trials).valid_costs) >= target:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
# Every short shift has lateral displacement, so the direct retry cannot lock
|
|
|
|
|
# any straight paths. It would run the same guide with the same budget.
|
|
|
|
|
if early is None or early.run.metrics.timeout_events:
|
|
|
|
|
trials.extend(self._direct(options, "direct-lookahead-retry"))
|
|
|
|
|
return len(_best_trial(trials).valid_costs) >= target
|
|
|
|
|
|
|
|
|
|
def _seed_recovery_trials(self, options: RoutingOptions, trials: list[Trial], target: int) -> None:
|
|
|
|
|
problem = self.problem
|
|
|
|
|
best = _best_trial(trials)
|
|
|
|
|
seeded = replace(options, topology=replace(options.topology, mode="seed"))
|
|
|
|
|
if best.valid_costs and len(problem.nets) > 2 and not problem.initial_paths:
|
|
|
|
|
trials.extend(self._incumbent_trials(seeded, best, target))
|
|
|
|
|
if len(_best_trial(trials).valid_costs) < target:
|
|
|
|
|
run = self._run(problem, options=seeded, policy="seed-fallback")
|
|
|
|
|
stage = RoutingStage("seed", (), run.metrics.nodes_expanded, options.search.node_limit, policy="seed-fallback")
|
|
|
|
|
trials.append(self._trial(run, stage))
|
|
|
|
|
|
2026-11-27 00:38:00 -08:00
|
|
|
def _joint_lane_trial(self, residual: RoutingProblem, options: RoutingOptions, locks: LockPlan, previous: Trial) -> Trial | None:
|
2026-11-26 15:23:24 -08:00
|
|
|
if len(residual.nets) < 2:
|
|
|
|
|
return None
|
|
|
|
|
policy = "joint-portal-lanes"
|
|
|
|
|
unresolved = frozenset(net.net_id for net in residual.nets if net.net_id not in previous.valid_costs)
|
2026-11-27 00:30:40 -08:00
|
|
|
nets = ordered_nets(residual.nets, options.congestion.net_order)
|
2026-11-26 15:23:24 -08:00
|
|
|
with self._time("plan", planner="corridor-seed", mode="seed", policy=policy):
|
|
|
|
|
corridors = plan_corridors(
|
|
|
|
|
residual.bounds,
|
2026-11-29 00:12:59 -08:00
|
|
|
residual._blocking_polygons,
|
2026-11-26 15:23:24 -08:00
|
|
|
nets,
|
|
|
|
|
clearance=residual.clearance,
|
|
|
|
|
bend_radius=min(options.search.bend_radii, default=0.0),
|
|
|
|
|
limit=options.topology.max_corridors,
|
|
|
|
|
joint_net_ids=unresolved,
|
|
|
|
|
)
|
|
|
|
|
if not corridors:
|
|
|
|
|
return None
|
|
|
|
|
retry = replace(options, congestion=replace(options.congestion, max_iterations=min(3, options.congestion.max_iterations)))
|
2026-11-27 00:38:00 -08:00
|
|
|
run, stage = _route_prepared_residual(
|
|
|
|
|
residual, retry, locks, residual_mode="seed", run_route=partial(self._run, policy=policy, corridors=corridors)
|
2026-11-26 15:23:24 -08:00
|
|
|
)
|
|
|
|
|
stage = replace(
|
|
|
|
|
stage,
|
|
|
|
|
policy=policy,
|
|
|
|
|
incumbent_net_ids=tuple(locks.direct),
|
|
|
|
|
retry_reason="Unresolved nets share a portal with alternative lane assignments",
|
|
|
|
|
)
|
|
|
|
|
return self._trial(run, stage)
|
|
|
|
|
|
2026-11-26 11:57:43 -08:00
|
|
|
def _lattice_fallback(self, trials: list[Trial], options: RoutingOptions, reason: str, *, policy: str | None = None) -> None:
|
|
|
|
|
run = self._run(self.problem, options=replace(options, topology=replace(options.topology, mode="off")), policy=policy)
|
|
|
|
|
stage = RoutingStage(
|
|
|
|
|
"off",
|
|
|
|
|
(),
|
|
|
|
|
run.metrics.nodes_expanded,
|
|
|
|
|
options.search.node_limit,
|
|
|
|
|
kind="lattice-fallback",
|
|
|
|
|
policy=policy,
|
|
|
|
|
retry_reason=reason,
|
|
|
|
|
)
|
|
|
|
|
trials.append(self._trial(run, stage))
|
|
|
|
|
|
2026-11-28 20:06:06 -08:00
|
|
|
def _refine(self, trials: list[Trial]) -> Trial | None:
|
2026-11-26 11:57:43 -08:00
|
|
|
best = _best_trial(trials)
|
|
|
|
|
if not self.options.refinement.enabled or not best.valid_costs:
|
2026-11-28 20:06:06 -08:00
|
|
|
return None
|
2026-11-28 10:57:01 -08:00
|
|
|
has_unresolved_paths = any(result.path and not result.is_valid for result in best.run.results_by_net.values())
|
2026-11-26 19:58:46 -08:00
|
|
|
run, stage = self._seeded_attempt(
|
2026-11-28 10:57:01 -08:00
|
|
|
self._seeded_problem(best, include_unresolved=has_unresolved_paths),
|
2026-11-26 19:58:46 -08:00
|
|
|
best.stage.planner,
|
|
|
|
|
kind="refinement",
|
|
|
|
|
policy=best.stage.policy,
|
|
|
|
|
operation="refine",
|
2026-11-28 10:57:01 -08:00
|
|
|
node_limit=0 if has_unresolved_paths else 1,
|
2026-11-26 11:57:43 -08:00
|
|
|
)
|
2026-11-28 10:57:01 -08:00
|
|
|
# Any carried-over result without a replayed seed has no path to recheck.
|
2026-11-26 11:57:43 -08:00
|
|
|
results = {**best.run.results_by_net, **run.results_by_net}
|
2026-11-28 20:06:06 -08:00
|
|
|
refined = self._trial(replace(run, results_by_net=results), stage).as_replacement_for(best)
|
2026-11-26 11:57:43 -08:00
|
|
|
# Only a whole verified replacement of the incumbent can win.
|
2026-11-28 20:06:06 -08:00
|
|
|
trials.append(refined)
|
|
|
|
|
objective = self.options.refinement.objective
|
|
|
|
|
if objective is None:
|
|
|
|
|
return None
|
|
|
|
|
if not refined.selectable:
|
|
|
|
|
return best
|
2026-11-28 20:42:25 -08:00
|
|
|
evaluator = build_native_cost_evaluator(self.problem, objective, proximity_range=self.options.search.proximity_range)
|
2026-11-28 20:06:06 -08:00
|
|
|
|
|
|
|
|
def cost(trial: Trial) -> float:
|
|
|
|
|
paths = (trial.run.results_by_net[net_id].path for net_id in trial.valid_costs)
|
|
|
|
|
return sum(evaluator.path_cost(path[0].start_port, path) for path in paths)
|
|
|
|
|
|
|
|
|
|
return refined if cost(refined) < cost(best) else best
|
2026-11-26 11:57:43 -08:00
|
|
|
|
|
|
|
|
def execute(self) -> RoutingRunResult:
|
|
|
|
|
started = perf_counter()
|
|
|
|
|
options = self.options
|
|
|
|
|
proxy, physical = resolve_bend_geometry(options.search)
|
|
|
|
|
cut = None
|
|
|
|
|
conflicts = ()
|
|
|
|
|
rejections = ()
|
|
|
|
|
target = len(self.problem.nets)
|
|
|
|
|
trials: list[Trial] = []
|
2026-11-28 20:06:06 -08:00
|
|
|
refinement_choice = None
|
2026-11-26 11:57:43 -08:00
|
|
|
if not self.problem.nets or options.search.node_limit <= 0 or options.congestion.max_iterations <= 0:
|
|
|
|
|
self._lattice_fallback(trials, options, "No automatic search requested")
|
2026-11-28 20:03:41 -08:00
|
|
|
elif (
|
|
|
|
|
isinstance(proxy, shapely.Polygon) or physical != "arc"
|
|
|
|
|
or options.search.has_applicable_sbend_outline(net.width for net in self.problem.nets)
|
|
|
|
|
):
|
2026-11-26 11:57:43 -08:00
|
|
|
self._lattice_fallback(trials, options, "Custom bend geometry uses lattice routing")
|
|
|
|
|
else:
|
|
|
|
|
with self._time("plan"):
|
|
|
|
|
if options.topology.infer_terminal_headings and not self.problem.initial_paths:
|
|
|
|
|
with suppress(ValueError):
|
|
|
|
|
self.problem = ordered_bundle_terminal_headings(self.raw_problem)
|
2026-11-26 19:58:46 -08:00
|
|
|
cut = cut_capacity(self.problem, options)
|
|
|
|
|
conflicts = port_conflicts(self.problem, options)
|
2026-11-26 11:57:43 -08:00
|
|
|
target -= len(conflicts)
|
|
|
|
|
if cut:
|
|
|
|
|
target = min(target, cut.max_routed)
|
|
|
|
|
if not cut and not conflicts:
|
|
|
|
|
trials, rejections = self._constructive_trials()
|
2026-11-26 15:23:24 -08:00
|
|
|
if not any(trial.selectable for trial in trials) or len(_best_trial(trials).valid_costs) < target:
|
2026-11-27 21:14:09 -08:00
|
|
|
if constrained_triangulation_available():
|
|
|
|
|
self._search_trials(trials, target)
|
|
|
|
|
else:
|
|
|
|
|
worker = replace(options, refinement=replace(options.refinement, enabled=False))
|
|
|
|
|
self._lattice_fallback(trials, worker, "Constrained triangulation is unavailable")
|
2026-11-28 20:06:06 -08:00
|
|
|
refinement_choice = self._refine(trials)
|
|
|
|
|
best = refinement_choice or _best_trial(trials)
|
2026-11-29 00:18:30 -08:00
|
|
|
metrics = self.attempts.metrics()
|
2026-11-26 11:57:43 -08:00
|
|
|
report = AutomaticRoutingReport(
|
|
|
|
|
tuple(trial.stage for trial in trials),
|
|
|
|
|
next(i for i, trial in enumerate(trials) if trial is best),
|
|
|
|
|
target,
|
|
|
|
|
capacity_limit=cut,
|
|
|
|
|
port_conflicts=conflicts,
|
|
|
|
|
planner_rejections=rejections,
|
|
|
|
|
cost_pass_performed=self.cost_pass_performed,
|
|
|
|
|
timings=tuple(self.timings),
|
|
|
|
|
duration_s=perf_counter() - started,
|
|
|
|
|
)
|
2026-11-29 00:16:13 -08:00
|
|
|
results = best.run.results_by_net
|
|
|
|
|
if options.congestion.max_iterations and self.raw_problem.initial_paths:
|
|
|
|
|
results = dict(results)
|
|
|
|
|
for net in self.raw_problem.nets:
|
|
|
|
|
seed = self.raw_problem.initial_paths.get(net.net_id)
|
|
|
|
|
result = results[net.net_id]
|
|
|
|
|
if seed is None or result.report.seed_error is not None:
|
|
|
|
|
continue
|
|
|
|
|
error = seed_preparation_error(
|
|
|
|
|
seed, start=net.start, net_width=net.width, search=options.search, clearance=self.raw_problem.clearance,
|
|
|
|
|
)
|
|
|
|
|
if error:
|
|
|
|
|
report_with_error = replace(result.report, seed_error=f"Initial path for {net.net_id!r} {error}")
|
|
|
|
|
results[net.net_id] = replace(result, report=report_with_error)
|
|
|
|
|
return replace(complete_final_conflict_trace(replace(best.run, results_by_net=results)), metrics=metrics, automatic=report)
|
2026-11-26 11:57:43 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def route_automatic(
|
|
|
|
|
problem: RoutingProblem,
|
|
|
|
|
options: RoutingOptions,
|
|
|
|
|
iteration_callback: Callable[[int, dict[str, RoutingResult]], None] | None = None,
|
|
|
|
|
) -> RoutingRunResult:
|
|
|
|
|
return _AutomaticRun(problem, options, iteration_callback).execute()
|