Improve automatic routing retries and reuse component geometry

Try verified perimeter construction early and reuse probe routes as movable hints during lookahead. Batch collision checks and cache component generation for repeated placement and refinement candidates.
This commit is contained in:
Jan Petykiewicz 2026-11-30 19:29:51 -08:00
commit ed52ea6ffd

View file

@ -44,6 +44,7 @@ from inire.router._seed_materialization import materialize_path_seed, seed_prepa
from inire.router._stack import RoutingAttempts
from inire.router._topology import constrained_triangulation_available, plan_corridors
from inire.router.cost import build_native_cost_evaluator, weighted_path_length
from inire.router.refiner import PathRefiner
from inire.seeds import SBendSeed
if TYPE_CHECKING:
@ -370,10 +371,10 @@ class _AutomaticRun:
return
trials[-1] = replace(trial, selection="incomplete-layout")
def _perimeter_trials(self, options: RoutingOptions) -> list[Trial]:
def _perimeter_trials(self, options: RoutingOptions, *, try_pair: bool = True) -> list[Trial]:
problem = self.problem
trials = []
if len(problem.nets) == 2:
if try_pair and 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):
@ -486,17 +487,51 @@ class _AutomaticRun:
return trial
return None
def _direct(self, options: RoutingOptions, policy: str) -> list[Trial]:
def _direct(self, options: RoutingOptions, policy: str, *, hints: dict[str, PathSeed] | None = None) -> list[Trial]:
def run(problem: RoutingProblem, *, options: RoutingOptions) -> RoutingRunResult:
if hints:
# Add internal hints after direct locks are chosen so search can move them.
active_hints = {net.net_id: hints[net.net_id] for net in problem.nets if net.net_id in hints}
if active_hints:
problem = replace(problem, initial_paths={**active_hints, **problem.initial_paths})
options = replace(options, congestion=replace(options.congestion, warm_start_enabled=False))
return self._run(problem, options=options, policy=policy)
with self._time("plan", planner="direct", policy=policy):
return _direct_trials(
self.problem,
self.board,
options,
policy,
run_route=partial(self._run, policy=policy),
run_route=run,
check_trial=self._trial,
)
def _try_hinted_lookahead(self, options: RoutingOptions, trials: list[Trial], incumbent: Trial, target: int) -> bool:
if (
self.problem.initial_paths or target != len(self.problem.nets)
or not 2 <= len(incumbent.valid_costs) < target - 1
or options.search.node_limit <= _NEAR_PORT_NODE_LIMIT
or options.congestion.max_iterations <= 1 or options.congestion.shuffle_nets or not options.congestion.warm_start_enabled
):
return False
retained = [incumbent.run.results_by_net[net_id] for net_id in incumbent.valid_costs]
if all(len(result.path) == 1 and result.path[0].move_type == "straight" for result in retained):
return False
hints = {result.net_id: result.as_seed() for result in retained}
attempts = self._direct(options, "direct-lookahead-hinted", hints=hints)
best = _best_trial(attempts)
if len(incumbent.valid_costs) < len(best.valid_costs) < target:
seeded = replace(options, topology=replace(options.topology, mode="seed"))
attempts.extend(self._incumbent_trials(seeded, best, target))
if len(_best_trial(attempts).valid_costs) >= target:
trials.extend(attempts)
return True
# The ordinary retry starts from its original incumbents if hints fall short.
trials.extend(replace(trial, selection="incomplete-layout") for trial in attempts)
return False
def _early_guide(self, options: RoutingOptions) -> Trial | None:
problem = self.problem
if (
@ -594,6 +629,27 @@ class _AutomaticRun:
if self._try_column_recovery(options, trials, groups):
return True
groups = ()
max_shift = 2 * max(options.search.sbend_radii, default=0)
early_perimeter = (
try_construction_early and target == len(problem.nets) <= 2 and not problem.initial_paths
and options.search.node_limit > 1 and options.search.bend_radii and problem._blocking_polygons
and all(net.start.r == net.target.r == 0 and not _is_short_shift(net, max_shift) for net in problem.nets)
and (target == 2 or problem.nets[0].start.x > problem.nets[0].target.x)
)
if early_perimeter:
# For a small, fixed-heading layout, try verified construction
# before searching. Defer the perimeter's residual A* to its usual turn.
with self._time("plan", planner="perimeter"):
perimeter = []
if target == 2:
with self._time("plan", planner="paired-perimeter"):
self._paired_perimeter(perimeter)
else:
perimeter = self._perimeter_trials(options)
trials.extend(perimeter)
if perimeter and len(perimeter[-1].valid_costs) == target:
return True
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.
@ -618,9 +674,9 @@ class _AutomaticRun:
trials.append(construction)
return True
if len(problem.nets) <= 2:
if len(problem.nets) <= 2 and (not early_perimeter or target == 2):
with self._time("plan", planner="perimeter"):
trials.extend(self._perimeter_trials(options))
trials.extend(self._perimeter_trials(options, try_pair=not early_perimeter))
if len(_best_trial(trials).valid_costs) >= target:
return True
@ -630,6 +686,8 @@ class _AutomaticRun:
# 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:
if self._try_hinted_lookahead(options, trials, _best_trial(probe), target):
return True
trials.extend(self._direct(options, "direct-lookahead-retry"))
return len(_best_trial(trials).valid_costs) >= target
@ -695,7 +753,9 @@ class _AutomaticRun:
def _refine(self, trials: list[Trial]) -> Trial | None:
best = _best_trial(trials)
if not self.options.refinement.enabled or not best.valid_costs:
if not self.options.refinement.enabled or not any(
PathRefiner.has_refinement_bends(best.run.results_by_net[net_id].path) for net_id in best.valid_costs
):
return None
has_unresolved_paths = any(result.path and not result.is_valid for result in best.run.results_by_net.values())
run, stage = self._seeded_attempt(