572 lines
23 KiB
Python
572 lines
23 KiB
Python
import numpy
|
|
import pytest
|
|
from shapely.geometry import box
|
|
|
|
import inire.geometry.collision as collision_module
|
|
from inire.geometry.collision import RoutingWorld
|
|
from inire.geometry.components import ComponentResult
|
|
from inire.geometry.components import Straight
|
|
from inire.geometry.primitives import Port
|
|
from inire.router._astar_types import AStarMetrics
|
|
from inire.seeds import StraightSeed
|
|
|
|
|
|
def _install_static_straight(
|
|
engine: RoutingWorld,
|
|
start: Port,
|
|
length: float,
|
|
*,
|
|
width: float,
|
|
dilation: float = 0.0,
|
|
) -> None:
|
|
obstacle = Straight.generate(start, length, width=width, dilation=dilation)
|
|
for polygon in obstacle.physical_geometry:
|
|
engine.add_static_obstacle(polygon)
|
|
|
|
|
|
def test_collision_detection() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
_install_static_straight(engine, Port(10, 15, 0), 10.0, width=10.0, dilation=1.0)
|
|
|
|
direct_hit = Straight.generate(Port(12, 12.5, 0), 1.0, width=1.0, dilation=1.0)
|
|
assert engine.check_move_static(direct_hit, start_port=direct_hit.start_port)
|
|
|
|
far_away = Straight.generate(Port(0, 2.5, 0), 5.0, width=5.0, dilation=1.0)
|
|
assert not engine.check_move_static(far_away, start_port=far_away.start_port)
|
|
|
|
near_hit = Straight.generate(Port(8, 12.5, 0), 1.0, width=5.0, dilation=1.0)
|
|
assert engine.check_move_static(near_hit, start_port=near_hit.start_port)
|
|
|
|
|
|
def test_safety_zone() -> None:
|
|
engine = RoutingWorld(clearance=0.0)
|
|
_install_static_straight(engine, Port(10, 15, 0), 10.0, width=10.0)
|
|
|
|
start_port = Port(10, 12, 0)
|
|
test_move = Straight.generate(start_port, 0.002, width=0.001)
|
|
assert not engine.check_move_static(test_move, start_port=start_port)
|
|
|
|
|
|
def test_ray_cast_width_clearance() -> None:
|
|
# Clearance = 2.0um, Width = 2.0um.
|
|
# Centerline to obstacle edge must be >= W/2 + C = 1.0 + 2.0 = 3.0um.
|
|
engine = RoutingWorld(clearance=2.0)
|
|
|
|
# Obstacle at x=10 to 20
|
|
_install_static_straight(engine, Port(10, 50, 0), 10.0, width=100.0)
|
|
|
|
# 1. Parallel move at x=6. Gap = 10 - 6 = 4.0. Clearly OK.
|
|
start_ok = Port(6, 50, 90)
|
|
reach_ok = engine.ray_cast(start_ok, 90, max_dist=10.0, net_width=2.0)
|
|
assert reach_ok >= 10.0
|
|
|
|
# 2. Parallel move at x=8. Gap = 10 - 8 = 2.0. COLLISION.
|
|
start_fail = Port(8, 50, 90)
|
|
reach_fail = engine.ray_cast(start_fail, 90, max_dist=10.0, net_width=2.0)
|
|
assert reach_fail < 10.0
|
|
|
|
|
|
def test_ray_cast_reuses_exact_queries_and_separates_parameters() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
engine.add_static_obstacle(box(10, -1, 20, 1))
|
|
origin = Port(0, 0, 0)
|
|
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(7.0)
|
|
assert engine.metrics.total_ray_cast_candidate_bounds == 1
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0, caller="expand_forward") == pytest.approx(7.0)
|
|
assert engine.metrics.total_ray_cast_calls == 2
|
|
assert engine.metrics.total_ray_cast_calls_expand_forward == 1
|
|
assert engine.metrics.total_ray_cast_candidate_bounds == 1
|
|
|
|
assert engine.ray_cast(origin, 0, 30, net_width=6.0) == pytest.approx(5.0)
|
|
assert engine.ray_cast(origin, 0, 30) == pytest.approx(9.0)
|
|
assert engine.ray_cast(origin, 0, 4, net_width=2.0) == 4.0
|
|
assert engine.ray_cast(origin, 90, 30, net_width=2.0) == 30.0
|
|
engine.clearance = 4.0
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(5.0)
|
|
|
|
|
|
def test_ray_cast_discards_results_after_obstacle_changes() -> None:
|
|
engine = RoutingWorld(clearance=0.0)
|
|
origin = Port(0, 0, 0)
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == 30.0
|
|
|
|
first = engine.add_static_obstacle(box(10, -1, 20, 1))
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(9.0)
|
|
second = engine.add_static_obstacle(box(4, -1, 6, 1))
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(3.0)
|
|
engine.remove_static_obstacle(second)
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(9.0)
|
|
engine.remove_static_obstacle(first)
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == 30.0
|
|
|
|
|
|
def test_ray_cast_preserves_scalar_precision_at_an_obstacle_boundary() -> None:
|
|
engine = RoutingWorld(clearance=0.0)
|
|
engine.add_static_obstacle(box(0, 10, 10, 20))
|
|
origin = Port(0, 0, 0)
|
|
|
|
assert engine.ray_cast(origin, 90.0, 30.0) == pytest.approx(10.0)
|
|
assert engine.ray_cast(origin, numpy.float32(90), 30.0) == 30.0
|
|
|
|
|
|
def test_ray_cast_cache_has_a_fixed_capacity(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(collision_module, "_RAY_CAST_CACHE_LIMIT", 2)
|
|
engine = RoutingWorld(clearance=0.0)
|
|
engine.add_static_obstacle(box(10, -1, 20, 1))
|
|
origin = Port(0, 0, 0)
|
|
for limit in (30, 40, 50):
|
|
assert engine.ray_cast(origin, 0, limit) == pytest.approx(10.0)
|
|
assert len(engine._static_obstacles.ray_cast_cache) <= 2
|
|
|
|
|
|
def test_check_move_static_clearance() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
_install_static_straight(engine, Port(10, 50, 0), 10.0, width=100.0, dilation=1.0)
|
|
|
|
# Straight move of length 10 at x=8 (Width 2.0)
|
|
# Gap = 10 - 8 = 2.0 < 3.0. COLLISION.
|
|
start = Port(8, 0, 90)
|
|
res = Straight.generate(start, 10.0, width=2.0, dilation=1.0) # dilation = C/2
|
|
|
|
assert engine.check_move_static(res, start_port=start)
|
|
|
|
# Move at x=7. Gap = 3.0 == minimum. OK.
|
|
start_ok = Port(7, 0, 90)
|
|
res_ok = Straight.generate(start_ok, 10.0, width=2.0, dilation=1.0)
|
|
assert not engine.check_move_static(res_ok, start_port=start_ok)
|
|
|
|
# 3. Same exact-boundary case.
|
|
start_exact = Port(7, 0, 90)
|
|
res_exact = Straight.generate(start_exact, 10.0, width=2.0, dilation=1.0)
|
|
assert not engine.check_move_static(res_exact, start_port=start_exact)
|
|
|
|
|
|
def test_verify_path_report_uses_physical_distance_at_a_mitred_corner() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
path = (Straight.generate(Port(0, 0, 0), 20.0, width=2.0),)
|
|
outside_radius = box(21.6, 2.6, 22.0, 3.0)
|
|
engine.add_static_obstacle(outside_radius)
|
|
|
|
assert engine.verify_path_report("net", path).is_valid
|
|
|
|
engine.add_static_obstacle(box(21.2, 2.2, 21.8, 2.8))
|
|
|
|
assert engine.verify_path_report("net", path).static_collision_count == 1
|
|
|
|
|
|
def test_verify_path_report_preserves_long_net_id() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
net_id = "net_abcdefghijklmnopqrstuvwxyz_0123456789"
|
|
path = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
geoms = [poly for component in path for poly in component.collision_geometry]
|
|
dilated = [poly for component in path for poly in component.dilated_collision_geometry]
|
|
|
|
engine.add_path(net_id, geoms, dilated_geometry=dilated)
|
|
report = engine.verify_path_report(net_id, path)
|
|
|
|
assert report.dynamic_collision_count == 0
|
|
assert engine.metrics.total_verify_dynamic_candidate_nets == 0
|
|
|
|
|
|
def test_verify_path_report_distinguishes_long_net_ids_with_shared_prefix() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
shared_prefix = "net_shared_prefix_abcdefghijklmnopqrstuvwxyz_"
|
|
net_a = f"{shared_prefix}A"
|
|
net_b = f"{shared_prefix}B"
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
net_a,
|
|
[poly for component in path_a for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_a for poly in component.dilated_collision_geometry],
|
|
)
|
|
engine.add_path(
|
|
net_b,
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
report = engine.verify_path_report(net_a, path_a)
|
|
|
|
assert report.dynamic_collision_count == 1
|
|
assert engine.metrics.total_verify_dynamic_candidate_nets == 1
|
|
|
|
|
|
def test_verify_path_report_uses_physical_bounds_before_exact_checks() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_far = [Straight.generate(Port(100, 100, 0), 20.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
"netB",
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
engine.add_path(
|
|
"netFar",
|
|
[poly for component in path_far for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_far for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
report = engine.verify_path_report("netA", path_a)
|
|
|
|
assert report.dynamic_collision_count == 1
|
|
assert engine.metrics.total_verify_dynamic_candidate_nets == 1
|
|
assert engine.metrics.total_verify_dynamic_exact_pair_checks == 1
|
|
|
|
|
|
@pytest.mark.parametrize(("clearance", "valid"), [(1.4, True), (1.5, False)])
|
|
def test_verify_path_report_uses_physical_distance_between_routes(clearance, valid) -> None:
|
|
engine = RoutingWorld(clearance=clearance)
|
|
existing = Straight.generate(Port(0, 0, 0), 10, width=2, dilation=clearance / 2)
|
|
query = Straight.generate(Port(12, 2, 90), 10, width=2, dilation=clearance / 2)
|
|
engine.install_path("existing", (existing,))
|
|
|
|
# The physical corners are one unit apart on each axis.
|
|
assert engine.verify_path_report("query", (query,)).is_valid is valid
|
|
engine.remove_path("existing")
|
|
assert engine.verify_path_report("query", (query,)).is_valid
|
|
|
|
|
|
def test_verify_path_details_returns_conflicting_net_ids() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
"netB",
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
detail = engine.verify_path_details("netA", path_a)
|
|
|
|
assert detail.report.dynamic_collision_count == 1
|
|
assert detail.conflicting_net_ids == ("netB",)
|
|
|
|
|
|
def test_verify_path_details_reports_component_conflicts() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [
|
|
Straight.generate(Port(100, 0, 0), 10.0, width=2.0, dilation=1.0),
|
|
Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0),
|
|
]
|
|
|
|
engine.add_path(
|
|
"netB",
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
component_indexes=[0] * len(path_b[0].collision_geometry) + [1] * len(path_b[1].collision_geometry),
|
|
)
|
|
|
|
detail = engine.verify_path_details("netA", path_a, capture_component_conflicts=True)
|
|
|
|
assert detail.conflicting_net_ids == ("netB",)
|
|
assert detail.component_conflicts == ((0, "netB", 1),)
|
|
|
|
|
|
def test_verify_path_details_deduplicates_component_conflicts() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
query_component = ComponentResult(
|
|
start_port=Port(0, 0, 0),
|
|
collision_geometry=[box(0, 0, 10, 10), box(12, 0, 22, 10)],
|
|
end_port=Port(22, 0, 0),
|
|
length=22.0,
|
|
move_type="straight",
|
|
move_spec=StraightSeed(22.0),
|
|
physical_geometry=[box(0, 0, 10, 10), box(12, 0, 22, 10)],
|
|
dilated_collision_geometry=[box(0, 0, 10, 10), box(12, 0, 22, 10)],
|
|
dilated_physical_geometry=[box(0, 0, 10, 10), box(12, 0, 22, 10)],
|
|
)
|
|
blocker_component = ComponentResult(
|
|
start_port=Port(0, 0, 0),
|
|
collision_geometry=[box(5, 0, 17, 10)],
|
|
end_port=Port(17, 0, 0),
|
|
length=12.0,
|
|
move_type="straight",
|
|
move_spec=StraightSeed(12.0),
|
|
physical_geometry=[box(5, 0, 17, 10)],
|
|
dilated_collision_geometry=[box(5, 0, 17, 10)],
|
|
dilated_physical_geometry=[box(5, 0, 17, 10)],
|
|
)
|
|
|
|
engine.add_path(
|
|
"netB",
|
|
blocker_component.collision_geometry,
|
|
dilated_geometry=blocker_component.dilated_collision_geometry,
|
|
component_indexes=[0],
|
|
)
|
|
|
|
detail = engine.verify_path_details("netA", [query_component], capture_component_conflicts=True)
|
|
|
|
assert detail.conflicting_net_ids == ("netB",)
|
|
assert detail.component_conflicts == ((0, "netB", 0),)
|
|
|
|
|
|
def test_remove_path_clears_dynamic_path() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
path = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
geoms = [poly for component in path for poly in component.collision_geometry]
|
|
dilated = [poly for component in path for poly in component.dilated_collision_geometry]
|
|
|
|
engine.add_path("netA", geoms, dilated_geometry=dilated)
|
|
assert {net_id for net_id, _ in engine._dynamic_paths.geometries.values()} == {"netA"}
|
|
|
|
engine.remove_path("netA")
|
|
assert list(engine._dynamic_paths.geometries.values()) == []
|
|
assert len(engine._static_obstacles.geometries) == 0
|
|
|
|
|
|
def test_dynamic_grid_updates_incrementally_on_add_and_remove() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 4, 0), 20.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
"netA",
|
|
[poly for component in path_a for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_a for poly in component.dilated_collision_geometry],
|
|
)
|
|
engine.add_path(
|
|
"netB",
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
dynamic_paths = engine._dynamic_paths
|
|
assert dynamic_paths.net_to_obj_ids["netA"]
|
|
assert dynamic_paths.net_to_obj_ids["netB"]
|
|
assert dynamic_paths.grid_net_obj_ids
|
|
assert engine.metrics.total_dynamic_grid_rebuilds == 0
|
|
|
|
engine.remove_path("netA")
|
|
|
|
assert "netA" not in dynamic_paths.net_to_obj_ids
|
|
assert "netB" in dynamic_paths.net_to_obj_ids
|
|
assert engine.metrics.total_dynamic_grid_rebuilds == 0
|
|
assert "netA" not in dynamic_paths.grid_net_obj_ids.get((0, -1), {})
|
|
assert "netB" in dynamic_paths.grid_net_obj_ids.get((0, 0), {})
|
|
|
|
|
|
def test_dynamic_net_envelopes_update_incrementally_on_add_and_remove() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 40, 0), 10.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
"netA",
|
|
[poly for component in path_a for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_a for poly in component.dilated_collision_geometry],
|
|
)
|
|
engine.add_path(
|
|
"netB",
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
dynamic_paths = engine._dynamic_paths
|
|
assert set(dynamic_paths.net_envelopes) == {"netA", "netB"}
|
|
assert dynamic_paths.net_envelopes["netA"] == (-1.0, -2.0, 21.0, 2.0)
|
|
assert dynamic_paths.net_envelopes["netB"] == (-1.0, 38.0, 11.0, 42.0)
|
|
assert engine.metrics.total_dynamic_tree_rebuilds == 0
|
|
|
|
net_b_envelope_obj_id = dynamic_paths.net_envelope_obj_ids["netB"]
|
|
assert list(dynamic_paths.net_envelope_index.intersection((-5.0, 35.0, 15.0, 45.0))) == [net_b_envelope_obj_id]
|
|
|
|
engine.remove_path("netA")
|
|
|
|
assert "netA" not in dynamic_paths.net_envelopes
|
|
assert "netA" not in dynamic_paths.net_envelope_obj_ids
|
|
assert "netB" in dynamic_paths.net_envelopes
|
|
assert engine.metrics.total_dynamic_tree_rebuilds == 0
|
|
|
|
|
|
|
|
def test_congestion_query_uses_per_polygon_bounds() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
|
|
blocker = Straight.generate(Port(40, 4, 90), 2.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
|
|
move = ComponentResult(
|
|
start_port=Port(0, 0, 0),
|
|
collision_geometry=[box(0, 0, 10, 10), box(90, 0, 100, 10)],
|
|
end_port=Port(100, 0, 0),
|
|
length=100.0,
|
|
move_type="straight",
|
|
move_spec=StraightSeed(100.0),
|
|
physical_geometry=[box(0, 0, 10, 10), box(90, 0, 100, 10)],
|
|
dilated_collision_geometry=[box(0, 0, 10, 10), box(90, 0, 100, 10)],
|
|
dilated_physical_geometry=[box(0, 0, 10, 10), box(90, 0, 100, 10)],
|
|
)
|
|
|
|
assert engine.check_move_congestion(move, "netA") == 0
|
|
assert engine.metrics.total_congestion_candidate_nets == 0
|
|
assert engine.metrics.total_congestion_candidate_ids == 0
|
|
assert engine.metrics.total_congestion_exact_pair_checks == 0
|
|
|
|
|
|
def test_congestion_touching_geometries_do_not_count_as_overlap() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
|
|
existing = Straight.generate(Port(0, 0, 0), 10.0, width=2.0, dilation=1.0)
|
|
touching = Straight.generate(Port(12, 0, 0), 10.0, width=2.0, dilation=1.0)
|
|
|
|
engine.add_path(
|
|
"netB",
|
|
list(existing.collision_geometry),
|
|
dilated_geometry=list(existing.dilated_collision_geometry),
|
|
)
|
|
|
|
assert engine.check_move_congestion(touching, "netA") == 0
|
|
|
|
|
|
def test_congestion_exact_checks_only_touch_relevant_move_polygons() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
|
|
blocker = Straight.generate(Port(0, 0, 0), 10.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
|
|
move = ComponentResult(
|
|
start_port=Port(0, 0, 0),
|
|
collision_geometry=[box(0, -2, 10, 2), box(90, -2, 100, 2)],
|
|
end_port=Port(100, 0, 0),
|
|
length=100.0,
|
|
move_type="straight",
|
|
move_spec=StraightSeed(100.0),
|
|
physical_geometry=[box(0, -2, 10, 2), box(90, -2, 100, 2)],
|
|
dilated_collision_geometry=[box(0, -2, 10, 2), box(90, -2, 100, 2)],
|
|
dilated_physical_geometry=[box(0, -2, 10, 2), box(90, -2, 100, 2)],
|
|
)
|
|
|
|
assert engine.check_move_congestion(move, "netA") == 1
|
|
assert engine.metrics.total_congestion_candidate_nets == 1
|
|
assert engine.metrics.total_congestion_candidate_ids == 1
|
|
assert engine.metrics.total_congestion_exact_pair_checks == 1
|
|
|
|
def test_congestion_grid_span_cache_reuses_broad_phase_candidates() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
net_envelope_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] = {}
|
|
grid_net_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] = {}
|
|
cache: dict[tuple[str, int, int, int, int], dict[str, tuple[int, ...]]] = {}
|
|
|
|
blocker = Straight.generate(Port(15, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
|
|
move_a = Straight.generate(Port(5, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
move_b = Straight.generate(Port(7, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
|
|
assert engine.check_move_congestion(
|
|
move_a,
|
|
"netA",
|
|
net_envelope_cache=net_envelope_cache,
|
|
grid_net_cache=grid_net_cache,
|
|
broad_phase_cache=cache,
|
|
) == 1
|
|
assert engine.check_move_congestion(
|
|
move_b,
|
|
"netA",
|
|
net_envelope_cache=net_envelope_cache,
|
|
grid_net_cache=grid_net_cache,
|
|
broad_phase_cache=cache,
|
|
) == 1
|
|
assert engine.metrics.total_congestion_candidate_nets == 2
|
|
assert engine.metrics.total_congestion_net_envelope_cache_misses == 1
|
|
assert engine.metrics.total_congestion_net_envelope_cache_hits == 1
|
|
assert engine.metrics.total_congestion_grid_net_cache_misses == 1
|
|
assert engine.metrics.total_congestion_grid_net_cache_hits == 1
|
|
assert engine.metrics.total_congestion_grid_span_cache_misses == 1
|
|
assert engine.metrics.total_congestion_grid_span_cache_hits == 1
|
|
|
|
|
|
def test_has_possible_move_congestion_uses_presence_cache_and_skips_empty_spans() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
presence_cache: dict[tuple[str, int, int, int, int], bool] = {}
|
|
|
|
blocker = Straight.generate(Port(10, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
empty_move = Straight.generate(Port(200, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
|
|
assert not engine.has_possible_move_congestion(empty_move, "netA", presence_cache)
|
|
assert not engine.has_possible_move_congestion(empty_move, "netA", presence_cache)
|
|
assert engine.metrics.total_congestion_presence_cache_misses == 1
|
|
assert engine.metrics.total_congestion_presence_cache_hits == 1
|
|
|
|
occupied_move = Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
|
|
assert engine.has_possible_move_congestion(occupied_move, "netA")
|
|
|
|
|
|
def test_has_candidate_move_congestion_uses_candidate_precheck_cache() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
candidate_precheck_cache: dict[tuple[str, int, int, int, int], bool] = {}
|
|
net_envelope_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] = {}
|
|
grid_net_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] = {}
|
|
|
|
blocker = Straight.generate(Port(10, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
empty_move = Straight.generate(Port(200, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
occupied_move = Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
|
|
assert not engine.has_candidate_move_congestion(
|
|
empty_move,
|
|
"netA",
|
|
candidate_precheck_cache,
|
|
net_envelope_cache,
|
|
grid_net_cache,
|
|
)
|
|
assert not engine.has_candidate_move_congestion(
|
|
empty_move,
|
|
"netA",
|
|
candidate_precheck_cache,
|
|
net_envelope_cache,
|
|
grid_net_cache,
|
|
)
|
|
assert engine.has_candidate_move_congestion(
|
|
occupied_move,
|
|
"netA",
|
|
candidate_precheck_cache,
|
|
net_envelope_cache,
|
|
grid_net_cache,
|
|
)
|
|
assert engine.metrics.total_congestion_candidate_precheck_misses >= 2
|
|
assert engine.metrics.total_congestion_candidate_precheck_hits >= 1
|