Share stable net ordering across routing planners

This commit is contained in:
Jan Petykiewicz 2026-11-27 00:30:40 -08:00
commit 5120ef52e2
5 changed files with 37 additions and 38 deletions

View file

@ -14,13 +14,13 @@ from shapely.geometry import box
from inire.constants import TOLERANCE_LINEAR from inire.constants import TOLERANCE_LINEAR
from inire.geometry.collision import RoutingWorld from inire.geometry.collision import RoutingWorld
from inire.geometry.primitives import port_local_coordinates
from inire.model import resolve_bend_geometry from inire.model import resolve_bend_geometry
from inire.results import AutomaticRoutingReport, RoutingStage, RoutingTiming, _planner_for_mode, _sum_route_metrics from inire.results import AutomaticRoutingReport, RoutingStage, RoutingTiming, _planner_for_mode, _sum_route_metrics
from inire.router._bundle import ordered_bundle_terminal_headings, plan_ordered_bundle from inire.router._bundle import ordered_bundle_terminal_headings, plan_ordered_bundle
from inire.router._capacity import cut_capacity, port_conflicts from inire.router._capacity import cut_capacity, port_conflicts
from inire.router._columns import column_jog_candidates, column_seed_candidates from inire.router._columns import column_jog_candidates, column_seed_candidates
from inire.router._compact import compact_port_seed_set, short_shift_seed_set from inire.router._compact import compact_port_seed_set, short_shift_seed_set
from inire.router._congestion_policy import ordered_nets
from inire.router._perimeter import ( from inire.router._perimeter import (
bundle_turnaround_candidates, bundle_turnaround_candidates,
perimeter_detour_candidates, perimeter_detour_candidates,
@ -546,14 +546,7 @@ class _AutomaticRun:
return None return None
policy = "joint-portal-lanes" policy = "joint-portal-lanes"
unresolved = frozenset(net.net_id for net in residual.nets if net.net_id not in previous.valid_costs) unresolved = frozenset(net.net_id for net in residual.nets if net.net_id not in previous.valid_costs)
order = options.congestion.net_order nets = ordered_nets(residual.nets, options.congestion.net_order)
nets = residual.nets
if order != "user":
nets = tuple(sorted(
nets,
key=lambda net: sum(map(abs, port_local_coordinates(net.start, net.target.x, net.target.y))),
reverse=order == "longest",
))
with self._time("plan", planner="corridor-seed", mode="seed", policy=policy): with self._time("plan", planner="corridor-seed", mode="seed", policy=policy):
corridors = plan_corridors( corridors = plan_corridors(
residual.bounds, residual.bounds,

View file

@ -6,12 +6,21 @@ from dataclasses import dataclass
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from collections.abc import Mapping, Sequence, Set from collections.abc import Iterable, Mapping, Sequence, Set
from inire.geometry.collision import PathVerificationDetail from inire.geometry.collision import PathVerificationDetail
from inire.model import NetOrder, NetSpec
from inire.results import RoutingResult from inire.results import RoutingResult
def ordered_nets(nets: Iterable[NetSpec], order: NetOrder) -> list[NetSpec]:
"""Apply the requested net order, retaining input order for ties."""
ordered = list(nets)
if order != "user":
ordered.sort(key=lambda net: abs(net.target.x - net.start.x) + abs(net.target.y - net.start.y), reverse=order == "longest")
return ordered
@dataclass(slots=True) @dataclass(slots=True)
class IterationReview: class IterationReview:
conflicting_nets: set[str] conflicting_nets: set[str]

View file

@ -6,7 +6,7 @@ from contextlib import contextmanager, nullcontext
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from inire.model import NetOrder, NetSpec, resolve_bend_geometry from inire.model import resolve_bend_geometry
from inire.results import ( from inire.results import (
ConflictTraceEntry, ConflictTraceEntry,
FrontierPruneSample, FrontierPruneSample,
@ -24,6 +24,7 @@ from inire.router._congestion_policy import (
IterationReview as _IterationReview, IterationReview as _IterationReview,
all_results_reached_target, all_results_reached_target,
next_reroute_net_ids, next_reroute_net_ids,
ordered_nets,
reached_pair_local_plateau, reached_pair_local_plateau,
repeated_conflict_count, repeated_conflict_count,
reroute_plan, reroute_plan,
@ -43,6 +44,7 @@ if TYPE_CHECKING:
from inire.geometry.collision import PathVerificationDetail from inire.geometry.collision import PathVerificationDetail
from inire.geometry.components import ComponentResult from inire.geometry.components import ComponentResult
from inire.model import NetSpec
from inire.results import ConflictTraceStage, RoutingOperation from inire.results import ConflictTraceStage, RoutingOperation
from inire.router._topology import Corridor from inire.router._topology import Corridor
@ -171,21 +173,6 @@ class PathFinder:
component_indexes=component_indexes, component_indexes=component_indexes,
) )
def _routing_order(
self,
net_specs: dict[str, NetSpec],
order: NetOrder,
) -> list[str]:
ordered_net_ids = list(net_specs.keys())
if order == "user":
return ordered_net_ids
ordered_net_ids.sort(
key=lambda net_id: abs(net_specs[net_id].target.x - net_specs[net_id].start.x)
+ abs(net_specs[net_id].target.y - net_specs[net_id].start.y),
reverse=(order == "longest"),
)
return ordered_net_ids
@contextmanager @contextmanager
def _temporary_path_reservations(self) -> Iterator[Callable[[Sequence[ComponentResult]], None]]: def _temporary_path_reservations(self) -> Iterator[Callable[[Sequence[ComponentResult]], None]]:
world = self.context.cost_evaluator.collision_engine world = self.context.cost_evaluator.collision_engine
@ -205,22 +192,20 @@ class PathFinder:
def _build_greedy_warm_start_paths( def _build_greedy_warm_start_paths(
self, self,
net_specs: dict[str, NetSpec], state: _RoutingState,
order: NetOrder,
corridors: dict[str, tuple[Corridor, ...]],
) -> dict[str, tuple[ComponentResult, ...]]: ) -> dict[str, tuple[ComponentResult, ...]]:
greedy_paths: dict[str, tuple[ComponentResult, ...]] = {} greedy_paths: dict[str, tuple[ComponentResult, ...]] = {}
greedy_node_limit = min(self.context.options.search.node_limit, 2000) greedy_node_limit = min(self.context.options.search.node_limit, 2000)
with self._temporary_path_reservations() as reserve: with self._temporary_path_reservations() as reserve:
for net_id in self._routing_order(net_specs, order): for net_id in state.ordered_net_ids:
net = net_specs[net_id] net = state.net_specs[net_id]
h_start = self.context.cost_evaluator.h_manhattan( h_start = self.context.cost_evaluator.h_manhattan(
net.start, net.start,
net.target, net.target,
min_bend_radius=self.context.min_bend_radius, min_bend_radius=self.context.min_bend_radius,
) )
max_cost_limit = max(h_start * 3.0, 2000.0) max_cost_limit = max(h_start * 3.0, 2000.0)
candidates = corridors.get(net_id, ()) candidates = state.corridors.get(net_id, ())
run_config = SearchRunConfig.from_options( run_config = SearchRunConfig.from_options(
self.context.options, self.context.options,
skip_congestion=True, skip_congestion=True,
@ -327,7 +312,7 @@ class PathFinder:
num_nets = len(net_specs) num_nets = len(net_specs)
state = _RoutingState( state = _RoutingState(
net_specs=net_specs, net_specs=net_specs,
ordered_net_ids=self._routing_order(net_specs, congestion.net_order), ordered_net_ids=[net.net_id for net in ordered_nets(net_specs.values(), congestion.net_order)],
results={}, results={},
needs_self_collision_check=set(), needs_self_collision_check=set(),
start_time=time.monotonic(), start_time=time.monotonic(),
@ -344,7 +329,7 @@ class PathFinder:
) )
self._prepare_topology(state, corridors) self._prepare_topology(state, corridors)
if state.initial_paths is None and congestion.warm_start_enabled: if state.initial_paths is None and congestion.warm_start_enabled:
state.initial_paths = self._build_greedy_warm_start_paths(net_specs, congestion.net_order, state.corridors) state.initial_paths = self._build_greedy_warm_start_paths(state)
if state.corridors and state.initial_paths and congestion.max_iterations > 0: if state.corridors and state.initial_paths and congestion.max_iterations > 0:
world = self.context.cost_evaluator.collision_engine world = self.context.cost_evaluator.collision_engine
for net_id, path in state.initial_paths.items(): for net_id, path in state.initial_paths.items():

View file

@ -14,6 +14,7 @@ from inire.geometry.components import Straight
from inire.geometry.primitives import port_local_coordinates from inire.geometry.primitives import port_local_coordinates
from inire.model import resolve_bend_geometry from inire.model import resolve_bend_geometry
from inire.results import RoutingResult, RoutingStage from inire.results import RoutingResult, RoutingStage
from inire.router._congestion_policy import ordered_nets
from inire.router._stack import route_once from inire.router._stack import route_once
if TYPE_CHECKING: if TYPE_CHECKING:
@ -85,10 +86,7 @@ def _direct_paths(
world = RoutingWorld(clearance=problem.clearance, safety_zone_radius=problem.safety_zone_radius) world = RoutingWorld(clearance=problem.clearance, safety_zone_radius=problem.safety_zone_radius)
for obstacle in problem.static_obstacles: for obstacle in problem.static_obstacles:
world.add_static_obstacle(obstacle) world.add_static_obstacle(obstacle)
ordered = list(problem.nets) ordered = ordered_nets(problem.nets, options.congestion.net_order)
order = options.congestion.net_order
if order != "user":
ordered.sort(key=lambda net: sum(map(abs, port_local_coordinates(net.start, net.target.x, net.target.y))), reverse=order == "longest")
if priority: if priority:
ordered.sort(key=lambda net: net.net_id not in priority) ordered.sort(key=lambda net: net.net_id not in priority)

View file

@ -1,15 +1,29 @@
from itertools import pairwise from itertools import pairwise
from inire import NetSpec, Port
from inire.results import RoutingResult from inire.results import RoutingResult
from inire.router._congestion_policy import ( from inire.router._congestion_policy import (
IterationReview, IterationReview,
next_reroute_net_ids, next_reroute_net_ids,
ordered_nets,
reached_pair_local_plateau, reached_pair_local_plateau,
repeated_conflict_count, repeated_conflict_count,
reroute_plan, reroute_plan,
) )
def test_net_order_keeps_equal_distances_stable_across_headings() -> None:
east = NetSpec("east", Port(0, 0, 0), Port(30, 0, 0))
west = NetSpec("west", Port(50, 30, 180), Port(30, 20, 270))
north = NetSpec("north", Port(0, 50, 90), Port(30, 70, 0))
south = NetSpec("south", Port(20, 120, 270), Port(20, 110, 270))
nets = (east, south, west, north)
assert ordered_nets(nets, "user") == list(nets)
assert ordered_nets(nets, "shortest") == [south, east, west, north]
assert ordered_nets(nets, "longest") == [north, east, west, south]
def test_layout_rank_prefers_coverage_then_edges_then_collisions() -> None: def test_layout_rank_prefers_coverage_then_edges_then_collisions() -> None:
one_edge = {("a", "b")} one_edge = {("a", "b")}
two_edges = {("a", "b"), ("b", "c")} two_edges = {("a", "b"), ("b", "c")}