292 lines
11 KiB
Python
292 lines
11 KiB
Python
from __future__ import annotations
|
|
|
|
import heapq
|
|
from typing import TYPE_CHECKING
|
|
|
|
from shapely.geometry import Polygon
|
|
|
|
from inire.constants import TOLERANCE_LINEAR
|
|
from inire.geometry.component_overlap import invalid_component_join
|
|
from inire.geometry.components import build_component, check_component_endpoint, path_within_board
|
|
from inire.geometry.primitives import Port
|
|
from inire.router.refiner import component_hits_ancestor_chain
|
|
|
|
from ._astar_types import AStarContext, AStarMetrics, AStarNode, SearchRunConfig
|
|
|
|
if TYPE_CHECKING:
|
|
from inire.geometry.components import ComponentResult, MoveKind
|
|
from ._astar_types import CongestionCaches
|
|
|
|
|
|
def process_move(
|
|
parent: AStarNode,
|
|
target: Port,
|
|
net_width: float,
|
|
net_id: str,
|
|
open_set: list[AStarNode],
|
|
closed_set: dict[tuple[int, int, int], float],
|
|
context: AStarContext,
|
|
metrics: AStarMetrics,
|
|
congestion: CongestionCaches,
|
|
config: SearchRunConfig,
|
|
move_class: MoveKind,
|
|
params: tuple,
|
|
) -> None:
|
|
cp = parent.port
|
|
coll_type = config.bend_collision_type
|
|
coll_key = id(coll_type) if isinstance(coll_type, Polygon) else coll_type
|
|
physical_type = config.bend_physical_geometry
|
|
physical_key = id(physical_type) if isinstance(physical_type, Polygon) else physical_type
|
|
search = context.options.search
|
|
template = search.sbend_template(params[0], params[1], net_width) if move_class == "sbend" and search.sbend_outlines else None
|
|
outline = template.polygon if template else None
|
|
self_dilation = context.cost_evaluator.collision_engine.clearance / 2.0
|
|
|
|
abs_key = (
|
|
cp.as_tuple(),
|
|
move_class,
|
|
params,
|
|
net_width,
|
|
coll_key,
|
|
physical_key,
|
|
id(outline) if outline is not None else None,
|
|
self_dilation,
|
|
)
|
|
if abs_key in context.move_cache_abs:
|
|
context.metrics.total_move_cache_abs_hits += 1
|
|
res = context.move_cache_abs[abs_key]
|
|
else:
|
|
context.metrics.total_move_cache_abs_misses += 1
|
|
base_port = Port(0, 0, cp.r)
|
|
rel_key = (
|
|
cp.r,
|
|
move_class,
|
|
params,
|
|
net_width,
|
|
coll_key,
|
|
physical_key,
|
|
id(outline) if outline is not None else None,
|
|
self_dilation,
|
|
)
|
|
if rel_key in context.move_cache_rel:
|
|
context.metrics.total_move_cache_rel_hits += 1
|
|
res_rel = context.move_cache_rel[rel_key]
|
|
else:
|
|
context.metrics.total_move_cache_rel_misses += 1
|
|
try:
|
|
board = context.cost_evaluator.collision_engine.board
|
|
# The terminal center must fit before we allocate a tessellated outline.
|
|
if board is not None and move_class != "straight":
|
|
check_component_endpoint(cp, move_class, params, board.bounds)
|
|
res_rel = build_component(
|
|
base_port,
|
|
net_width,
|
|
move_class,
|
|
params,
|
|
collision_type=coll_type,
|
|
physical_geometry_type=physical_type,
|
|
clip_margin=config.bend_clip_margin,
|
|
dilation=self_dilation,
|
|
sbend_outline=outline,
|
|
physical_length=search.bend_physical_length if move_class == "bend90" else (template.length if template else None),
|
|
)
|
|
except ValueError:
|
|
return
|
|
context.cache_move(rel_key, res_rel, relative=True)
|
|
res = res_rel.translate(cp.x, cp.y)
|
|
context.cache_move(abs_key, res, relative=False)
|
|
|
|
add_node(
|
|
parent,
|
|
res,
|
|
target,
|
|
net_id,
|
|
open_set,
|
|
closed_set,
|
|
context,
|
|
metrics,
|
|
congestion,
|
|
config,
|
|
move_class,
|
|
abs_key,
|
|
)
|
|
|
|
|
|
def add_node(
|
|
parent: AStarNode,
|
|
result: ComponentResult,
|
|
target: Port,
|
|
net_id: str,
|
|
open_set: list[AStarNode],
|
|
closed_set: dict[tuple[int, int, int], float],
|
|
context: AStarContext,
|
|
metrics: AStarMetrics,
|
|
congestion: CongestionCaches,
|
|
config: SearchRunConfig,
|
|
move_type: MoveKind,
|
|
cache_key: tuple,
|
|
) -> None:
|
|
frontier_trace = config.frontier_trace
|
|
metrics.moves_generated += 1
|
|
metrics.total_moves_generated += 1
|
|
state = result.end_port.as_tuple()
|
|
|
|
parent_p = parent.port
|
|
end_p = result.end_port
|
|
corridor = config.corridor
|
|
if (
|
|
corridor is not None
|
|
and config.restrict_to_corridor
|
|
and any(not corridor.footprint.covers(shape) for shape in result.physical_geometry)
|
|
):
|
|
return
|
|
|
|
if cache_key in context.hard_collision_set:
|
|
if frontier_trace is not None:
|
|
frontier_trace.record("hard_collision", move_type, parent.port.as_tuple(), state, result.total_dilated_bounds)
|
|
context.metrics.total_hard_collision_cache_hits += 1
|
|
metrics.pruned_hard_collision += 1
|
|
metrics.total_pruned_hard_collision += 1
|
|
return
|
|
|
|
is_static_safe = cache_key in context.static_safe_cache
|
|
if is_static_safe:
|
|
context.metrics.total_static_safe_cache_hits += 1
|
|
else:
|
|
ce = context.cost_evaluator.collision_engine
|
|
danger = context.cost_evaluator.danger_map
|
|
collision_found = (
|
|
(ce.board is not None and not path_within_board((result,), ce.board))
|
|
or (ce.board is None and danger is not None and not danger.is_within_bounds(end_p.x, end_p.y))
|
|
or ce.check_move_static(result, start_port=parent_p, end_port=end_p)
|
|
)
|
|
if collision_found:
|
|
if cache_key in context.move_cache_abs:
|
|
context.hard_collision_set.add(cache_key)
|
|
if frontier_trace is not None:
|
|
frontier_trace.record("hard_collision", move_type, parent.port.as_tuple(), state, result.total_dilated_bounds)
|
|
metrics.pruned_hard_collision += 1
|
|
metrics.total_pruned_hard_collision += 1
|
|
return
|
|
if cache_key in context.move_cache_abs:
|
|
context.static_safe_cache.add(cache_key)
|
|
|
|
predecessor = parent.component_result
|
|
self_clearance = context.cost_evaluator.collision_engine.self_clearance
|
|
if (
|
|
(not result.standard_faces and not all(result.physical_checks))
|
|
or ((config.self_collision_check or self_clearance) and component_hits_ancestor_chain(result, parent, self_clearance))
|
|
or (
|
|
not (config.self_collision_check or self_clearance) and predecessor is not None
|
|
and not (predecessor.standard_faces and result.standard_faces)
|
|
and invalid_component_join(predecessor, result)
|
|
)
|
|
):
|
|
if frontier_trace is not None:
|
|
frontier_trace.record("self_collision", move_type, parent.port.as_tuple(), state, result.total_dilated_bounds)
|
|
return
|
|
|
|
move_cost = context.cost_evaluator.score_component(
|
|
result,
|
|
start_port=parent_p,
|
|
)
|
|
next_seed_index = None
|
|
if (
|
|
config.guidance_seed is not None
|
|
and parent.seed_index is not None
|
|
and parent.seed_index < len(config.guidance_seed)
|
|
and result.move_spec == config.guidance_seed[parent.seed_index]
|
|
):
|
|
context.metrics.total_guidance_match_moves += 1
|
|
if result.move_type == "straight":
|
|
context.metrics.total_guidance_match_moves_straight += 1
|
|
applied_bonus = config.guidance_bonus
|
|
context.metrics.total_guidance_bonus_applied_straight += applied_bonus
|
|
elif result.move_type == "bend90":
|
|
context.metrics.total_guidance_match_moves_bend90 += 1
|
|
applied_bonus = config.guidance_bonus
|
|
context.metrics.total_guidance_bonus_applied_bend90 += applied_bonus
|
|
else:
|
|
context.metrics.total_guidance_match_moves_sbend += 1
|
|
applied_bonus = config.guidance_bonus
|
|
context.metrics.total_guidance_bonus_applied_sbend += applied_bonus
|
|
context.metrics.total_guidance_bonus_applied += applied_bonus
|
|
move_cost = max(0.001, move_cost - applied_bonus)
|
|
next_seed_index = parent.seed_index + 1
|
|
|
|
if corridor is not None and config.corridor_strength:
|
|
move_cost += result.length * config.corridor_strength * (corridor.distance(parent_p) + corridor.distance(end_p)) / 2
|
|
|
|
if config.max_cost is not None and parent.g_cost + move_cost > config.max_cost:
|
|
if frontier_trace is not None:
|
|
frontier_trace.record("cost", move_type, parent.port.as_tuple(), state, result.total_dilated_bounds)
|
|
metrics.pruned_cost += 1
|
|
metrics.total_pruned_cost += 1
|
|
return
|
|
if context.congestion_penalty >= 0 and state in closed_set and closed_set[state] <= parent.g_cost + move_cost + TOLERANCE_LINEAR:
|
|
if frontier_trace is not None:
|
|
frontier_trace.record("closed_set", move_type, parent.port.as_tuple(), state, result.total_dilated_bounds)
|
|
metrics.pruned_closed_set += 1
|
|
metrics.total_pruned_closed_set += 1
|
|
return
|
|
|
|
total_overlaps = 0
|
|
ce = context.cost_evaluator.collision_engine
|
|
if not config.skip_congestion and ce.has_dynamic_paths():
|
|
if config.hard_congestion or not ce.clearance:
|
|
if cache_key not in congestion.overlaps:
|
|
metrics.reroute_work += 1
|
|
congestion.overlaps[cache_key] = ce.check_physical_move_congestion(result, net_id).soft_overlap_count
|
|
total_overlaps = congestion.overlaps[cache_key]
|
|
elif ce.has_possible_move_congestion(result, net_id, congestion.presence):
|
|
if ce.has_candidate_move_congestion(
|
|
result,
|
|
net_id,
|
|
congestion.candidate_precheck,
|
|
congestion.net_envelopes,
|
|
congestion.grid_nets,
|
|
):
|
|
if cache_key in congestion.overlaps:
|
|
context.metrics.total_congestion_cache_hits += 1
|
|
total_overlaps = congestion.overlaps[cache_key]
|
|
else:
|
|
context.metrics.total_congestion_cache_misses += 1
|
|
metrics.reroute_work += 1
|
|
total_overlaps = ce.check_move_congestion(
|
|
result,
|
|
net_id,
|
|
net_envelope_cache=congestion.net_envelopes,
|
|
grid_net_cache=congestion.grid_nets,
|
|
broad_phase_cache=congestion.grid_spans,
|
|
)
|
|
congestion.overlaps[cache_key] = total_overlaps
|
|
else:
|
|
context.metrics.total_congestion_candidate_precheck_skips += 1
|
|
else:
|
|
context.metrics.total_congestion_presence_skips += 1
|
|
if total_overlaps and config.hard_congestion:
|
|
return
|
|
move_cost += total_overlaps * context.congestion_penalty
|
|
|
|
g_cost = parent.g_cost + move_cost
|
|
if state in closed_set and closed_set[state] <= g_cost + TOLERANCE_LINEAR:
|
|
if frontier_trace is not None:
|
|
frontier_trace.record("closed_set", move_type, parent.port.as_tuple(), state, result.total_dilated_bounds)
|
|
metrics.pruned_closed_set += 1
|
|
metrics.total_pruned_closed_set += 1
|
|
return
|
|
|
|
h_cost = config.estimate_remaining_cost(result.end_port, target, context)
|
|
heapq.heappush(
|
|
open_set,
|
|
AStarNode(
|
|
result.end_port,
|
|
g_cost,
|
|
h_cost,
|
|
parent,
|
|
result,
|
|
seed_index=next_seed_index,
|
|
),
|
|
)
|
|
metrics.moves_added += 1
|
|
metrics.total_moves_added += 1
|