Recover bounded routes around bends and through narrow corridors

This commit is contained in:
Jan Petykiewicz 2026-11-28 20:29:40 -08:00
commit af3ddfbb09

View file

@ -84,6 +84,8 @@ class _AutomaticRun:
options: RoutingOptions,
iteration_callback: Callable[[int, dict[str, RoutingResult]], None] | None,
) -> None:
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})
self.raw_problem = problem
self.problem = problem
self.options = options
@ -165,8 +167,16 @@ class _AutomaticRun:
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)
}
nets = tuple(net for net in self.problem.nets if net.net_id in seeds)
return replace(self.problem, nets=nets, initial_paths=seeds)
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)
def _seeded_attempt(
self,
@ -182,6 +192,7 @@ class _AutomaticRun:
refinement = options.refinement
if operation == "verify":
refinement = replace(refinement, enabled=False)
node_limit = 0
worker = replace(
options,
search=replace(options.search, node_limit=node_limit),
@ -202,29 +213,48 @@ class _AutomaticRun:
)
return run, stage
def _verify(self, problem: RoutingProblem, planner: str, *, kind: str | None = None, policy: str | None = None) -> Trial:
run, stage = self._seeded_attempt(problem, planner, kind=kind or planner, policy=policy, operation="verify")
return self._trial(run, stage)
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)
def _constructive_trials(self) -> tuple[list[Trial], tuple[str, ...]]:
problem, options = self.problem, self.options
options = self.options
problem = replace(self.problem, initial_paths={}) if self.problem.initial_paths else self.problem
verify = partial(self._verify, preserve_hints=True)
trials: list[Trial] = []
rejections = []
try:
with self._time("plan", planner="bundle"):
bundle = plan_ordered_bundle(self.raw_problem, options.search, infer_terminal_headings=True)
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)
except ValueError as exc:
rejections.append(str(exc))
else:
if bundle.nets == problem.nets:
trials.append(self._verify(bundle, "bundle"))
trials.append(verify(bundle, "bundle"))
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):
trials.append(self._verify(candidate, "bundle-turnaround"))
trials.append(verify(candidate, "bundle-turnaround"))
if len(trials[-1].valid_costs) == len(problem.nets):
return trials, ()
rejections.append("No geometrically verified turnaround matched the terminal headings")
@ -232,40 +262,40 @@ class _AutomaticRun:
with self._time("plan", planner="compact-port-jogs"):
compact = compact_port_seed_set(problem, options.search)
if compact is not None:
trials.append(self._verify(replace(problem, initial_paths=compact), "compact-port-jogs"))
trials.append(verify(replace(problem, initial_paths=compact), "compact-port-jogs"))
if len(trials[-1].valid_costs) == len(problem.nets):
return trials, tuple(rejections)
span_trial = self._column_trial(False, trials)
span_trial = self._column_trial(False, trials, problem=problem)
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
if self._column_trial(True, trials) is None:
if self._column_trial(True, trials, problem=problem) is None:
rejections.append("Cost scoring found no verified column layout")
if not trials or len(_best_trial(trials).valid_costs) < len(problem.nets):
planner = "local-sbend"
with self._time("plan", planner=planner):
shifts = short_shift_seed_set(problem, options.search, self._selection_cost)
if shifts is not None:
trial = self._verify(replace(problem, initial_paths=shifts), planner)
trial = verify(replace(problem, initial_paths=shifts), planner)
complete = len(trial.valid_costs) == len(problem.nets)
trials.append(trial if complete else replace(trial, selection="incomplete-layout"))
if not complete:
rejections.append("Local S-bend layout failed full verification")
return trials, tuple(rejections)
def _column_trial(self, weighted: bool, trials: list[Trial]) -> Trial | None:
def _column_trial(self, weighted: bool, trials: list[Trial], *, problem: RoutingProblem) -> Trial | None:
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):
candidates = column_seed_candidates(self.problem, self.options.search, weights, weighted=weighted, sbend_penalty=penalty)
candidates = column_seed_candidates(problem, self.options.search, weights, weighted=weighted, sbend_penalty=penalty)
for seeds in islice(candidates, 8):
trial = self._verify(replace(self.problem, initial_paths=seeds), "column", kind=kind, policy=policy)
trial = self._verify(replace(problem, initial_paths=seeds), "column", kind=kind, policy=policy, preserve_hints=True)
trials.append(trial)
if len(trial.valid_costs) == len(self.problem.nets):
return trial