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

@ -1,12 +1,12 @@
from __future__ import annotations
from dataclasses import dataclass
from dataclasses import dataclass, replace
from typing import TYPE_CHECKING
from shapely.geometry import box
from inire.geometry.collision import RoutingWorld
from inire.results import RoutingRunResult
from inire.results import RoutingRunResult, _sum_route_metrics
from inire.router._astar_types import AStarContext
from inire.router._router import PathFinder
from inire.router.cost import CostEvaluator
@ -103,3 +103,39 @@ def collect_worker_result(finder: PathFinder, results: dict[str, RoutingResult])
iteration_trace=tuple(finder.iteration_trace),
topology=finder.topology_report,
)
def route_explicit(
problem: RoutingProblem,
*,
options: RoutingOptions,
iteration_callback: Callable[[int, dict[str, RoutingResult]], None] | None = None,
) -> RoutingRunResult:
start_index = 0
def notify(_iteration: int, results: dict[str, RoutingResult]) -> None:
nonlocal start_index
assert iteration_callback is not None
iteration_callback(start_index, results)
start_index += 1
callback = notify if iteration_callback is not None else None
with_guidance = route_once(problem, options=options, iteration_callback=callback)
topology = options.topology
if (
topology.mode not in ("guide", "seed") or not topology.guide_strength
or not options.search.node_limit or options.congestion.max_iterations < 2
or with_guidance.metrics.timeout_events or not with_guidance.topology or not with_guidance.topology.corridor_counts
or all(result.is_valid for result in with_guidance.results_by_net.values())
):
return with_guidance
retry = replace(
options, topology=replace(topology, guide_strength=0.0),
congestion=replace(options.congestion, max_iterations=1),
)
without_guidance = route_once(problem, options=retry, iteration_callback=callback)
old_count = sum(result.is_valid for result in with_guidance.results_by_net.values())
new_count = sum(result.is_valid for result in without_guidance.results_by_net.values())
chosen = without_guidance if new_count > old_count else with_guidance
return replace(chosen, metrics=_sum_route_metrics((with_guidance.metrics, without_guidance.metrics)))