inire/inire/router/_pair_local.py

116 lines
4.3 KiB
Python
Raw Normal View History

"""Candidate search for a pair of conflicting routes in a local routing world."""
from __future__ import annotations
from dataclasses import replace
from typing import TYPE_CHECKING
from inire.geometry.collision import RoutingWorld
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, proximity_map
if TYPE_CHECKING:
from collections.abc import Callable, 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_entries = tuple(live_world.iter_static_entries())
world = RoutingWorld(
clearance=live_world.clearance, safety_zone_radius=live_world.safety_zone_radius, board=live_world.board,
self_clearance=live_world.self_clearance,
)
for obstacle, owner in static_entries:
world.add_static_obstacle(obstacle, owner=owner)
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, owner=net.net_id)
danger_map = proximity_map(problem, context.options.search.proximity_range)
if context.options.objective.danger_weight:
danger_map.precompute([obstacle for obstacle, _ in static_entries])
evaluator = CostEvaluator.from_objective(
world,
danger_map,
context.options.objective,
greedy_h_weight=context.cost_evaluator.greedy_h_weight,
distance_heuristic_weight=context.cost_evaluator.distance_heuristic_weight,
)
return AStarContext(
evaluator,
replace(
problem, nets=ordered_nets, initial_paths={},
static_obstacles=tuple(obstacle for obstacle, owner in static_entries if owner is None),
),
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],
expired: Callable[[], bool] | None = None,
) -> 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,
expired=expired,
)
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, owner=net_id)
local.clear_static_caches()
return replacements, local.metrics.total_nodes_expanded