inire/inire/tests/test_perimeter.py

105 lines
4.6 KiB
Python

from dataclasses import replace
import pytest
from shapely.geometry import box
from inire import (
Bend90Seed, CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, StraightSeed, route,
)
from inire.router import _automatic
OPTIONS = RoutingOptions(
search=SearchOptions(node_limit=1, bend_radii=(10,), sbend_radii=()),
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
refinement=RefinementOptions(enabled=False),
)
def test_short_forward_perimeter_detour_does_not_abort_the_batch() -> None:
short = NetSpec("short", Port(30, 30, 0), Port(31, 70, 0))
peer = NetSpec("peer", Port(5, 90, 0), Port(95, 90, 0))
problem = RoutingProblem((0, 0, 100, 100), (short, peer), (box(30.3, 5, 30.6, 40),), clearance=0)
run = route(problem, options=OPTIONS)
assert run.results_by_net["short"].outcome == "unroutable"
assert run.results_by_net["peer"].is_valid
def test_forward_perimeter_detour_can_fit_at_exactly_four_radii() -> None:
net = NetSpec("edge", Port(20, 25, 0), Port(60, 25, 0))
obstacles = (box(42, 20, 45, 30), box(35, 20, 38, 30), box(75, 50, 85, 58))
problem = RoutingProblem((0, 0, 100, 100), (net,), obstacles, clearance=0)
run = route(problem, options=OPTIONS)
result = run.results_by_net[net.net_id]
assert result.is_valid
assert run.automatic.selected_planner == "perimeter"
assert sum(isinstance(move, Bend90Seed) for move in result.as_seed().segments) == 4
@pytest.mark.parametrize("smaller_radius", [10, 17.5])
def test_perimeter_detours_choose_a_radius_usable_by_the_planner(smaller_radius) -> None:
net = NetSpec("wide", Port(20, 70, 0), Port(220, 70, 0), width=30)
problem = RoutingProblem((0, 0, 250, 180), (net,), (box(100, 40, 130, 100),), clearance=0)
options = replace(OPTIONS, search=replace(OPTIONS.search, bend_radii=(smaller_radius, 20)))
run = route(problem, options=options)
result = run.results_by_net[net.net_id]
assert result.is_valid
assert run.automatic.selected_planner == "perimeter"
assert {move.radius for move in result.as_seed().segments if isinstance(move, Bend90Seed)} == {20}
def test_return_detours_snap_board_pivots_inward_before_generating_seeds() -> None:
net = NetSpec("return", Port(88, 50, 0), Port(12, 50, 0), width=3)
problem = RoutingProblem((0, 0, 100, 100), (net,), clearance=0)
run = route(problem, options=OPTIONS)
result = run.results_by_net[net.net_id]
assert result.is_valid
assert run.automatic.selected_planner == "perimeter"
assert result.path[-1].end_port == net.target
@pytest.mark.parametrize(("forward", "needs_short_straight"), [(43, True), (60, False)])
def test_residual_doglegs_prefer_the_lattice_minimum_but_can_relax_it(forward, needs_short_straight) -> None:
trunk = NetSpec("trunk", Port(20, 50, 0), Port(20 + forward, 50, 0))
north = NetSpec("north", Port(10, 60, 90), Port(10, 100, 90))
rail = NetSpec("rail", Port(85, 100, 0), Port(110, 100, 0))
problem = RoutingProblem((0, 0, 120, 120), (trunk, north, rail), (box(40, 47, 43, 53),), clearance=0)
run = route(problem, options=OPTIONS)
assert run.automatic.selected_planner == "residual-dogleg"
assert all(run.results_by_net[net.net_id].is_valid for net in problem.nets)
lengths = [move.length for move in run.results_by_net["trunk"].as_seed().segments if isinstance(move, StraightSeed)]
assert any(length < OPTIONS.search.min_straight_length for length in lengths) is needs_short_straight
@pytest.mark.parametrize("requires_reservation", [False, True])
def test_paired_perimeter_plans_peer_reserved_detours_only_as_a_fallback(monkeypatch, requires_reservation) -> None:
line = NetSpec("line", Port(5, 75, 0), Port(195, 75, 0))
returning = NetSpec("return", Port(130, 50, 0), Port(40, 50, 0))
obstacles = tuple(box(x, 100, x + 1, 104) for x in (*range(10, 29, 3), *range(142, 181, 4)))
problem = RoutingProblem((0, 0, 200, 130), (line, returning), obstacles, clearance=0)
original_detours = _automatic.perimeter_detour_candidates
calls = []
def detours(candidate_problem, search):
reserved = bool(candidate_problem.fixed_routes)
calls.append(reserved)
if reserved or not requires_reservation:
yield from original_detours(candidate_problem, search)
monkeypatch.setattr(_automatic, "perimeter_detour_candidates", detours)
run = route(problem, options=OPTIONS)
assert run.automatic.selected_planner == "paired-perimeter"
assert all(result.is_valid for result in run.results_by_net.values())
assert calls == ([False, False, True] if requires_reservation else [False, False])