Score refinement moves before materializing geometry
This commit is contained in:
parent
9ddef652e1
commit
d9f0e78330
4 changed files with 128 additions and 45 deletions
|
|
@ -16,6 +16,8 @@ from .primitives import Port, rotation_matrix2
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
|
||||||
|
from numpy.typing import NDArray
|
||||||
|
|
||||||
|
|
||||||
MoveKind = Literal["straight", "bend90", "sbend"]
|
MoveKind = Literal["straight", "bend90", "sbend"]
|
||||||
BendCollisionModelName = Literal["arc", "bbox", "clipped_bbox"]
|
BendCollisionModelName = Literal["arc", "bbox", "clipped_bbox"]
|
||||||
|
|
@ -27,6 +29,15 @@ def _normalize_length(value: float) -> float:
|
||||||
return float(value)
|
return float(value)
|
||||||
|
|
||||||
|
|
||||||
|
def _advance_port(start: Port, rotation: NDArray[numpy.int32], forward: float, lateral: float = 0.0, turn: int = 0) -> Port:
|
||||||
|
dx, dy = rotation @ numpy.array((forward, lateral))
|
||||||
|
return Port(start.x + dx, start.y + dy, start.r + turn)
|
||||||
|
|
||||||
|
|
||||||
|
def _bend90_length(radius: float) -> float:
|
||||||
|
return abs(radius) * numpy.pi / 2.0
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, slots=True)
|
@dataclass(frozen=True, slots=True)
|
||||||
class ComponentResult:
|
class ComponentResult:
|
||||||
start_port: Port
|
start_port: Port
|
||||||
|
|
@ -240,8 +251,7 @@ class Straight:
|
||||||
) -> ComponentResult:
|
) -> ComponentResult:
|
||||||
rot2 = rotation_matrix2(start_port.r)
|
rot2 = rotation_matrix2(start_port.r)
|
||||||
length_f = _normalize_length(length)
|
length_f = _normalize_length(length)
|
||||||
disp = rot2 @ numpy.array((length_f, 0.0))
|
end_port = _advance_port(start_port, rot2, length_f)
|
||||||
end_port = Port(start_port.x + disp[0], start_port.y + disp[1], start_port.r)
|
|
||||||
|
|
||||||
half_w = width / 2.0
|
half_w = width / 2.0
|
||||||
pts = numpy.array(((0.0, half_w), (length_f, half_w), (length_f, -half_w), (0.0, -half_w)))
|
pts = numpy.array(((0.0, half_w), (length_f, half_w), (length_f, -half_w), (0.0, -half_w)))
|
||||||
|
|
@ -293,10 +303,8 @@ class Bend90:
|
||||||
sign = 1 if direction == "CCW" else -1
|
sign = 1 if direction == "CCW" else -1
|
||||||
|
|
||||||
center_local = numpy.array((0.0, sign * radius))
|
center_local = numpy.array((0.0, sign * radius))
|
||||||
end_local = numpy.array((radius, sign * radius))
|
|
||||||
center_xy = (rot2 @ center_local) + numpy.array((start_port.x, start_port.y))
|
center_xy = (rot2 @ center_local) + numpy.array((start_port.x, start_port.y))
|
||||||
end_xy = (rot2 @ end_local) + numpy.array((start_port.x, start_port.y))
|
end_port = _advance_port(start_port, rot2, radius, sign * radius, sign * 90)
|
||||||
end_port = Port(end_xy[0], end_xy[1], start_port.r + sign * 90)
|
|
||||||
|
|
||||||
start_theta = start_port.r - sign * 90
|
start_theta = start_port.r - sign * 90
|
||||||
end_theta = start_port.r
|
end_theta = start_port.r
|
||||||
|
|
@ -353,7 +361,7 @@ class Bend90:
|
||||||
start_port=start_port,
|
start_port=start_port,
|
||||||
collision_geometry=collision_polys,
|
collision_geometry=collision_polys,
|
||||||
end_port=end_port,
|
end_port=end_port,
|
||||||
length=abs(radius) * numpy.pi / 2.0,
|
length=_bend90_length(radius),
|
||||||
move_type="bend90",
|
move_type="bend90",
|
||||||
move_spec=Bend90Seed(radius=radius, direction=direction),
|
move_spec=Bend90Seed(radius=radius, direction=direction),
|
||||||
physical_geometry=physical_geometry,
|
physical_geometry=physical_geometry,
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,21 @@ from inire.model import ObjectiveWeights
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
from typing import Protocol
|
||||||
|
|
||||||
from inire.geometry.collision import RoutingWorld
|
from inire.geometry.collision import RoutingWorld
|
||||||
from inire.geometry.components import ComponentResult, MoveKind
|
from inire.geometry.components import MoveKind
|
||||||
from inire.geometry.primitives import Port
|
from inire.geometry.primitives import Port
|
||||||
from inire.router.danger_map import DangerMap
|
from inire.router.danger_map import DangerMap
|
||||||
|
|
||||||
|
class ScoreableMove(Protocol):
|
||||||
|
@property
|
||||||
|
def end_port(self) -> Port: ...
|
||||||
|
@property
|
||||||
|
def length(self) -> float: ...
|
||||||
|
@property
|
||||||
|
def move_type(self) -> MoveKind: ...
|
||||||
|
|
||||||
|
|
||||||
class CostEvaluator:
|
class CostEvaluator:
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
|
|
@ -126,7 +136,7 @@ class CostEvaluator:
|
||||||
|
|
||||||
def score_component(
|
def score_component(
|
||||||
self,
|
self,
|
||||||
component: ComponentResult,
|
component: ScoreableMove,
|
||||||
*,
|
*,
|
||||||
start_port: Port | None = None,
|
start_port: Port | None = None,
|
||||||
weights: ObjectiveWeights | None = None,
|
weights: ObjectiveWeights | None = None,
|
||||||
|
|
@ -187,7 +197,7 @@ class CostEvaluator:
|
||||||
def path_cost(
|
def path_cost(
|
||||||
self,
|
self,
|
||||||
start_port: Port,
|
start_port: Port,
|
||||||
path: Sequence[ComponentResult],
|
path: Sequence[ScoreableMove],
|
||||||
*,
|
*,
|
||||||
weights: ObjectiveWeights | None = None,
|
weights: ObjectiveWeights | None = None,
|
||||||
) -> float:
|
) -> float:
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,31 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import math
|
import math
|
||||||
from typing import TYPE_CHECKING, Any, Literal
|
from dataclasses import dataclass
|
||||||
|
from itertools import chain
|
||||||
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from inire.geometry.component_overlap import components_overlap
|
from inire.geometry.component_overlap import components_overlap
|
||||||
from inire.geometry.components import Bend90, Straight
|
from inire.geometry.components import Bend90, Straight, _advance_port, _bend90_length
|
||||||
|
from inire.geometry.primitives import rotation_matrix2
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Iterator, Sequence
|
from collections.abc import Iterator, Sequence
|
||||||
|
|
||||||
from inire.geometry.collision import RoutingWorld
|
from inire.geometry.collision import RoutingWorld
|
||||||
from inire.geometry.components import ComponentResult
|
from inire.geometry.components import ComponentResult, MoveKind
|
||||||
from inire.geometry.primitives import Port
|
from inire.geometry.primitives import Port
|
||||||
from inire.router._astar_types import AStarContext
|
from inire.router._astar_types import AStarContext
|
||||||
|
from inire.router.cost import ScoreableMove
|
||||||
|
from inire.seeds import BendDirection
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class _DoglegMove:
|
||||||
|
end_port: Port
|
||||||
|
length: float
|
||||||
|
move_type: MoveKind
|
||||||
|
direction: BendDirection | None = None
|
||||||
|
|
||||||
|
|
||||||
def component_hits_ancestor_chain(component: ComponentResult, parent_node: Any) -> bool:
|
def component_hits_ancestor_chain(component: ComponentResult, parent_node: Any) -> bool:
|
||||||
|
|
@ -46,7 +59,7 @@ class PathRefiner:
|
||||||
actual_start = path[0].start_port if start is None else start
|
actual_start = path[0].start_port if start is None else start
|
||||||
return self.score_path(actual_start, path)
|
return self.score_path(actual_start, path)
|
||||||
|
|
||||||
def score_path(self, start: Port, path: Sequence[ComponentResult]) -> float:
|
def score_path(self, start: Port, path: Sequence[ScoreableMove]) -> float:
|
||||||
weights = self.context.options.refinement.objective or self.context.cost_evaluator.default_weights
|
weights = self.context.options.refinement.objective or self.context.cost_evaluator.default_weights
|
||||||
return self.context.cost_evaluator.path_cost(start, path, weights=weights)
|
return self.context.cost_evaluator.path_cost(start, path, weights=weights)
|
||||||
|
|
||||||
|
|
@ -171,14 +184,13 @@ class PathRefiner:
|
||||||
self.context.metrics.total_refinement_candidate_side_extents += len(direct_extents)
|
self.context.metrics.total_refinement_candidate_side_extents += len(direct_extents)
|
||||||
return sorted(direct_extents, key=lambda value: (abs(value), value))
|
return sorted(direct_extents, key=lambda value: (abs(value), value))
|
||||||
|
|
||||||
def _build_same_orientation_dogleg(
|
def _dogleg_moves(
|
||||||
self,
|
self,
|
||||||
start: Port,
|
start: Port,
|
||||||
target: Port,
|
target: Port,
|
||||||
net_width: float,
|
|
||||||
radius: float,
|
radius: float,
|
||||||
side_extent: float,
|
side_extent: float,
|
||||||
) -> list[ComponentResult] | None:
|
) -> list[_DoglegMove] | None:
|
||||||
local_dx, local_dy = self._to_local(start, target)
|
local_dx, local_dy = self._to_local(start, target)
|
||||||
if local_dx < 4.0 * radius - 0.01 or abs(side_extent) < 0.01:
|
if local_dx < 4.0 * radius - 0.01 or abs(side_extent) < 0.01:
|
||||||
return None
|
return None
|
||||||
|
|
@ -200,33 +212,55 @@ class PathRefiner:
|
||||||
if 0.01 < forward_length < min_straight - 0.01:
|
if 0.01 < forward_length < min_straight - 0.01:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
first_dir: Literal["CW", "CCW"] = "CCW" if side_extent > 0 else "CW"
|
first_sign = 1 if side_extent > 0 else -1
|
||||||
second_dir: Literal["CW", "CCW"] = "CW" if side_extent > 0 else "CCW"
|
bend_length = _bend90_length(radius)
|
||||||
dilation = self.collision_engine.clearance / 2.0
|
moves: list[_DoglegMove] = []
|
||||||
|
|
||||||
path: list[ComponentResult] = []
|
|
||||||
curr = start
|
curr = start
|
||||||
|
for sign, straight_len in (
|
||||||
for direction, straight_len in (
|
(first_sign, first_straight),
|
||||||
(first_dir, first_straight),
|
(-first_sign, forward_length),
|
||||||
(second_dir, forward_length),
|
(-first_sign, second_straight),
|
||||||
(second_dir, second_straight),
|
(first_sign, 0.0),
|
||||||
(first_dir, None),
|
|
||||||
):
|
):
|
||||||
bend = Bend90.generate(curr, radius, net_width, direction, dilation=dilation)
|
curr = _advance_port(curr, rotation_matrix2(curr.r), radius, sign * radius, sign * 90)
|
||||||
path.append(bend)
|
moves.append(_DoglegMove(curr, bend_length, "bend90", "CCW" if sign > 0 else "CW"))
|
||||||
curr = bend.end_port
|
|
||||||
if straight_len is None:
|
|
||||||
continue
|
|
||||||
if straight_len > 0.01:
|
if straight_len > 0.01:
|
||||||
straight = Straight.generate(curr, straight_len, net_width, dilation=dilation)
|
length = float(straight_len)
|
||||||
path.append(straight)
|
curr = _advance_port(curr, rotation_matrix2(curr.r), length)
|
||||||
curr = straight.end_port
|
moves.append(_DoglegMove(curr, length, "straight"))
|
||||||
|
return moves if curr == target else None
|
||||||
|
|
||||||
if curr != target:
|
def _build_dogleg(self, start: Port, width: float, radius: float, moves: list[_DoglegMove]) -> list[ComponentResult]:
|
||||||
return None
|
dilation = self.collision_engine.clearance / 2.0
|
||||||
|
path = []
|
||||||
|
port = start
|
||||||
|
for move in moves:
|
||||||
|
if move.direction is None:
|
||||||
|
component = Straight.generate(port, move.length, width, dilation=dilation)
|
||||||
|
else:
|
||||||
|
component = Bend90.generate(port, radius, width, move.direction, dilation=dilation)
|
||||||
|
path.append(component)
|
||||||
|
port = component.end_port
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
def _verify_changed_window(
|
||||||
|
self,
|
||||||
|
net_id: str,
|
||||||
|
replacement: list[ComponentResult],
|
||||||
|
prefix: list[ComponentResult],
|
||||||
|
suffix: list[ComponentResult],
|
||||||
|
) -> bool:
|
||||||
|
if not self.collision_engine.verify_path_report(net_id, replacement).is_valid:
|
||||||
|
return False
|
||||||
|
# The verified incumbent's prefix and suffix remain separated by
|
||||||
|
# several components. Only pairs involving a replacement can change.
|
||||||
|
for i, component in enumerate(replacement):
|
||||||
|
before = prefix if i > 0 else prefix[:-1]
|
||||||
|
after = suffix if i < len(replacement) - 1 else suffix[1:]
|
||||||
|
if any(components_overlap(component, other, prefer_actual=True) for other in chain(before, after)):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def _iter_refinement_windows(self, start: Port, path: Sequence[ComponentResult]) -> Iterator[tuple[int, int]]:
|
def _iter_refinement_windows(self, start: Port, path: Sequence[ComponentResult]) -> Iterator[tuple[int, int]]:
|
||||||
ports = self._path_ports(start, path)
|
ports = self._path_ports(start, path)
|
||||||
min_radius = min(self.context.options.search.bend_radii, default=0.0)
|
min_radius = min(self.context.options.search.bend_radii, default=0.0)
|
||||||
|
|
@ -256,11 +290,14 @@ class PathRefiner:
|
||||||
start_idx: int,
|
start_idx: int,
|
||||||
end_idx: int,
|
end_idx: int,
|
||||||
best_cost: float,
|
best_cost: float,
|
||||||
|
incumbent_valid: bool,
|
||||||
) -> tuple[list[ComponentResult], float] | None:
|
) -> tuple[list[ComponentResult], float] | None:
|
||||||
ports = self._path_ports(start, path)
|
ports = self._path_ports(start, path)
|
||||||
window_start = ports[start_idx]
|
window_start = ports[start_idx]
|
||||||
window_end = ports[end_idx]
|
window_end = ports[end_idx]
|
||||||
window_path = path[start_idx:end_idx]
|
window_path = path[start_idx:end_idx]
|
||||||
|
prefix, suffix = path[:start_idx], path[end_idx:]
|
||||||
|
scoring_start = prefix[0].start_port if prefix else window_start
|
||||||
|
|
||||||
best_path: list[ComponentResult] | None = None
|
best_path: list[ComponentResult] | None = None
|
||||||
best_candidate_cost = best_cost
|
best_candidate_cost = best_cost
|
||||||
|
|
@ -268,18 +305,22 @@ class PathRefiner:
|
||||||
for radius in self.context.options.search.bend_radii:
|
for radius in self.context.options.search.bend_radii:
|
||||||
side_extents = self._candidate_side_extents(window_start, window_end, window_path, net_width, radius)
|
side_extents = self._candidate_side_extents(window_start, window_end, window_path, net_width, radius)
|
||||||
for side_extent in side_extents:
|
for side_extent in side_extents:
|
||||||
replacement = self._build_same_orientation_dogleg(window_start, window_end, net_width, radius, side_extent)
|
moves = self._dogleg_moves(window_start, window_end, radius, side_extent)
|
||||||
if replacement is None:
|
if moves is None or not self.score_path(scoring_start, [*prefix, *moves, *suffix]) + 1e-6 < best_candidate_cost:
|
||||||
continue
|
continue
|
||||||
|
replacement = self._build_dogleg(window_start, net_width, radius, moves)
|
||||||
self.context.metrics.total_refinement_candidates_built += 1
|
self.context.metrics.total_refinement_candidates_built += 1
|
||||||
candidate_path = path[:start_idx] + replacement + path[end_idx:]
|
candidate_path = [*prefix, *replacement, *suffix]
|
||||||
# Only an improving candidate can replace the incumbent.
|
candidate_cost = self.score_path(scoring_start, candidate_path)
|
||||||
candidate_cost = self.path_cost(candidate_path)
|
|
||||||
if not candidate_cost + 1e-6 < best_candidate_cost:
|
if not candidate_cost + 1e-6 < best_candidate_cost:
|
||||||
continue
|
continue
|
||||||
self.context.metrics.total_refinement_candidates_verified += 1
|
self.context.metrics.total_refinement_candidates_verified += 1
|
||||||
report = self.collision_engine.verify_path_report(net_id, candidate_path)
|
valid = (
|
||||||
if not report.is_valid:
|
self._verify_changed_window(net_id, replacement, prefix, suffix)
|
||||||
|
if incumbent_valid
|
||||||
|
else self.collision_engine.verify_path_report(net_id, candidate_path).is_valid
|
||||||
|
)
|
||||||
|
if not valid:
|
||||||
continue
|
continue
|
||||||
self.context.metrics.total_refinement_candidates_accepted += 1
|
self.context.metrics.total_refinement_candidates_accepted += 1
|
||||||
best_candidate_cost = candidate_cost
|
best_candidate_cost = candidate_cost
|
||||||
|
|
@ -308,16 +349,20 @@ class PathRefiner:
|
||||||
best_path = path
|
best_path = path
|
||||||
best_cost = self.score_path(start, path)
|
best_cost = self.score_path(start, path)
|
||||||
remaining = self.context.options.refinement.max_windows_per_net
|
remaining = self.context.options.refinement.max_windows_per_net
|
||||||
|
incumbent_valid = None
|
||||||
|
|
||||||
for _ in range(3):
|
for _ in range(3):
|
||||||
improved = False
|
improved = False
|
||||||
for start_idx, end_idx in self._iter_refinement_windows(start, best_path):
|
for start_idx, end_idx in self._iter_refinement_windows(start, best_path):
|
||||||
|
if incumbent_valid is None:
|
||||||
|
incumbent_valid = self.collision_engine.verify_path_report(net_id, best_path).is_valid
|
||||||
if remaining is not None:
|
if remaining is not None:
|
||||||
remaining -= 1
|
remaining -= 1
|
||||||
self.context.metrics.total_refinement_windows_considered += 1
|
self.context.metrics.total_refinement_windows_considered += 1
|
||||||
refined = self._try_refine_window(net_id, start, net_width, best_path, start_idx, end_idx, best_cost)
|
refined = self._try_refine_window(net_id, start, net_width, best_path, start_idx, end_idx, best_cost, incumbent_valid)
|
||||||
if refined is not None:
|
if refined is not None:
|
||||||
best_path, best_cost = refined
|
best_path, best_cost = refined
|
||||||
|
incumbent_valid = True
|
||||||
improved = True
|
improved = True
|
||||||
break
|
break
|
||||||
if remaining == 0:
|
if remaining == 0:
|
||||||
|
|
|
||||||
|
|
@ -237,3 +237,23 @@ def test_refine_path_can_simplify_subpath_with_different_global_orientation() ->
|
||||||
assert sum(1 for comp in refined if comp.move_type == "bend90") == 5
|
assert sum(1 for comp in refined if comp.move_type == "bend90") == 5
|
||||||
assert refined[-1].end_port == target
|
assert refined[-1].end_port == target
|
||||||
assert pf.refiner.path_cost(refined) < pf.refiner.path_cost(path)
|
assert pf.refiner.path_cost(refined) < pf.refiner.path_cost(path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_changed_window_checks_for_collision_with_the_unchanged_prefix() -> None:
|
||||||
|
world = RoutingWorld(clearance=2.0)
|
||||||
|
start = Port(10, -10, 90)
|
||||||
|
prefix = _build_manual_path(
|
||||||
|
start,
|
||||||
|
2.0,
|
||||||
|
world.clearance,
|
||||||
|
[("S", 30), ("B", "CCW"), ("S", 20), ("B", "CCW"), ("S", 15), ("B", "CCW"), ("S", 15)],
|
||||||
|
)
|
||||||
|
window_start = prefix[-1].end_port
|
||||||
|
target = Port(20, 0, 0)
|
||||||
|
pf = _build_pathfinder(CostEvaluator(world), netlist={"net": (start, target)}, net_widths={"net": 2.0})
|
||||||
|
nearby = Straight.generate(window_start, 5, 2.0)
|
||||||
|
crossing = Straight.generate(window_start, 20, 2.0)
|
||||||
|
|
||||||
|
assert pf.refiner._verify_changed_window("net", [nearby], prefix, [])
|
||||||
|
assert world.verify_path_report("net", [*prefix, crossing]).self_collision_count == 1
|
||||||
|
assert not pf.refiner._verify_changed_window("net", [crossing], prefix, [])
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue