"""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._congestion_policy import can_reserve 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], ) -> AStarContext: problem = context.problem 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, board=live_world.board) 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 not can_reserve(result): continue for component in result.path: for polygon in component.physical_geometry: world.add_static_obstacle(polygon) danger_map = DangerMap(bounds=problem.bounds) if context.options.objective.danger_weight: danger_map.precompute(list(static_obstacles)) evaluator = CostEvaluator.from_objective( world, danger_map, context.options.objective, greedy_h_weight=context.cost_evaluator.greedy_h_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], ) -> 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) 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