inire/inire/router/_pair_local.py

119 lines
4.3 KiB
Python

"""Candidate search for a pair of conflicting routes in a local routing world."""
from __future__ import annotations
from typing import TYPE_CHECKING
from inire.geometry.collision import RoutingWorld
from inire.model import RoutingProblem
from inire.results import RoutingResult
from inire.router._astar_types import AStarContext, AStarMetrics, SearchRunConfig
from inire.router._search import incumbent_guidance, route_astar
from inire.router.cost import CostEvaluator
from inire.router.danger_map import DangerMap
if TYPE_CHECKING:
from collections.abc import Mapping, Set
from inire.model import NetSpec
def _build_pair_local_context(
context: AStarContext,
ordered_nets: tuple[NetSpec, ...],
incumbent_results: Mapping[str, RoutingResult],
pair_order: tuple[str, str],
*,
lock_partial_paths: bool,
) -> AStarContext:
problem = context.problem
objective = context.options.objective
live_world = context.cost_evaluator.collision_engine
static_obstacles = tuple(live_world.iter_static_geometries())
world = RoutingWorld(clearance=live_world.clearance, safety_zone_radius=live_world.safety_zone_radius)
for obstacle in static_obstacles:
world.add_static_obstacle(obstacle)
for net in ordered_nets:
if net.net_id in pair_order:
continue
result = incumbent_results.get(net.net_id)
if result is None or not result.path or (not lock_partial_paths and not result.reached_target):
continue
for component in result.path:
for polygon in component.physical_geometry:
world.add_static_obstacle(polygon)
danger_map = DangerMap(bounds=problem.bounds)
danger_map.precompute(list(static_obstacles))
evaluator = CostEvaluator(
world,
danger_map,
unit_length_cost=objective.unit_length_cost,
greedy_h_weight=context.cost_evaluator.greedy_h_weight,
bend_penalty=objective.bend_penalty,
sbend_penalty=objective.sbend_penalty,
danger_weight=objective.danger_weight,
)
return AStarContext(
evaluator,
RoutingProblem(
bounds=problem.bounds,
nets=ordered_nets,
static_obstacles=static_obstacles,
clearance=problem.clearance,
safety_zone_radius=problem.safety_zone_radius,
),
context.options,
metrics=AStarMetrics(),
)
def search_pair_candidate(
context: AStarContext,
ordered_nets: tuple[NetSpec, ...],
incumbent_results: Mapping[str, RoutingResult],
pair_order: tuple[str, str],
*,
needs_self_collision_check: Set[str],
lock_partial_paths: bool,
) -> tuple[dict[str, RoutingResult], int] | None:
"""Return both locally verified replacements and their search work."""
local = _build_pair_local_context(context, ordered_nets, incumbent_results, pair_order, lock_partial_paths=lock_partial_paths)
world = local.cost_evaluator.collision_engine
options = local.options
nets = {net.net_id: net for net in ordered_nets}
replacements: dict[str, RoutingResult] = {}
for net_id in pair_order:
net = nets[net_id]
guidance_seed, guidance_bonus = incumbent_guidance(incumbent_results.get(net_id), options.objective.bend_penalty)
config = SearchRunConfig.from_options(
options,
return_partial=False,
skip_congestion=True,
self_collision_check=net_id in needs_self_collision_check,
guidance_seed=guidance_seed,
guidance_bonus=guidance_bonus,
node_limit=options.search.node_limit,
)
path = route_astar(
net.start,
net.target,
net.width,
context=local,
metrics=local.metrics,
net_id=net_id,
config=config,
)
if not path or path[-1].end_port != net.target:
return None
report = world.verify_path_report(net_id, path)
if not report.is_valid:
return None
replacements[net_id] = RoutingResult(net_id, tuple(path), reached_target=True, report=report)
for component in path:
for polygon in component.physical_geometry:
world.add_static_obstacle(polygon)
local.clear_static_caches()
return replacements, local.metrics.total_nodes_expanded