2026-03-30 19:51:37 -07:00
|
|
|
import importlib
|
|
|
|
|
|
|
|
|
|
import pytest
|
2026-11-26 11:57:43 -08:00
|
|
|
import shapely
|
2026-03-30 15:32:29 -07:00
|
|
|
from shapely.geometry import box
|
|
|
|
|
|
2026-11-26 19:58:46 -08:00
|
|
|
import inire
|
2026-04-02 18:57:34 -07:00
|
|
|
import inire.router._router as router_module
|
2026-03-30 15:32:29 -07:00
|
|
|
from inire import (
|
|
|
|
|
CongestionOptions,
|
|
|
|
|
DiagnosticsOptions,
|
|
|
|
|
NetSpec,
|
|
|
|
|
ObjectiveWeights,
|
|
|
|
|
Port,
|
|
|
|
|
RefinementOptions,
|
|
|
|
|
RoutingOptions,
|
|
|
|
|
RoutingProblem,
|
|
|
|
|
SearchOptions,
|
2026-11-26 11:57:43 -08:00
|
|
|
TopologyOptions,
|
2026-03-30 15:32:29 -07:00
|
|
|
route,
|
|
|
|
|
)
|
|
|
|
|
from inire.geometry.components import Straight
|
2026-04-01 21:29:23 -07:00
|
|
|
from inire.geometry.collision import RoutingWorld
|
|
|
|
|
from inire.results import RoutingReport, RoutingResult
|
|
|
|
|
from inire.router._astar_types import AStarContext
|
|
|
|
|
from inire.router._router import PathFinder, _IterationReview
|
|
|
|
|
from inire.router.cost import CostEvaluator
|
|
|
|
|
from inire.router.danger_map import DangerMap
|
2026-03-30 19:51:37 -07:00
|
|
|
def test_root_module_exports_only_stable_surface() -> None:
|
|
|
|
|
assert not hasattr(inire, "RoutingWorld")
|
|
|
|
|
assert not hasattr(inire, "AStarContext")
|
|
|
|
|
assert not hasattr(inire, "PathFinder")
|
|
|
|
|
assert not hasattr(inire, "CostEvaluator")
|
|
|
|
|
assert not hasattr(inire, "DangerMap")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_deep_raw_stack_imports_remain_accessible_but_unstable() -> None:
|
|
|
|
|
router_module = importlib.import_module("inire.router._router")
|
|
|
|
|
search_module = importlib.import_module("inire.router._search")
|
|
|
|
|
collision_module = importlib.import_module("inire.geometry.collision")
|
|
|
|
|
|
|
|
|
|
assert hasattr(router_module, "PathFinder")
|
|
|
|
|
assert hasattr(search_module, "route_astar")
|
|
|
|
|
assert hasattr(collision_module, "RoutingWorld")
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
def test_route_problem_smoke() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(NetSpec("net1", Port(10, 50, 0), Port(90, 50, 0), width=2.0),),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
run = route(problem)
|
|
|
|
|
|
|
|
|
|
assert set(run.results_by_net) == {"net1"}
|
|
|
|
|
assert run.results_by_net["net1"].is_valid
|
2026-11-26 11:57:43 -08:00
|
|
|
assert run.automatic is not None
|
|
|
|
|
assert run.automatic.verified_net_ids == ("net1",)
|
|
|
|
|
assert all(stage.node_limit <= 500 for stage in run.automatic.stages)
|
2026-04-02 14:39:39 -07:00
|
|
|
assert run.conflict_trace == ()
|
|
|
|
|
assert run.frontier_trace == ()
|
2026-04-02 18:57:34 -07:00
|
|
|
assert run.pre_pair_frontier_trace is None
|
2026-04-02 16:15:02 -07:00
|
|
|
assert run.iteration_trace == ()
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
|
2026-11-26 11:57:43 -08:00
|
|
|
def test_automatic_routes_report_boundary_violations_in_results_and_diagnostics() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("edge", Port(10, 0, 0), Port(90, 0, 0)),
|
|
|
|
|
NetSpec("flush", Port(0, 50, 0), Port(100, 50, 0)),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(search=SearchOptions(node_limit=100), diagnostics=DiagnosticsOptions(capture_conflict_trace=True))
|
|
|
|
|
|
|
|
|
|
run = route(problem, options=options)
|
|
|
|
|
|
|
|
|
|
assert run.automatic is not None
|
|
|
|
|
valid_ids = tuple(net_id for net_id, result in run.results_by_net.items() if result.is_valid)
|
|
|
|
|
assert valid_ids == run.automatic.verified_net_ids == ("flush",)
|
|
|
|
|
selected = run.automatic.stages[run.automatic.selected_stage]
|
|
|
|
|
assert selected.out_of_bounds_net_ids == ("edge",)
|
|
|
|
|
|
|
|
|
|
edge = run.results_by_net["edge"]
|
|
|
|
|
assert edge.reached_target
|
|
|
|
|
assert edge.outcome == "colliding"
|
|
|
|
|
assert edge.report.out_of_bounds
|
|
|
|
|
assert edge.collisions == 1
|
|
|
|
|
assert not edge.report.is_valid
|
|
|
|
|
|
|
|
|
|
final = run.conflict_trace[-1]
|
|
|
|
|
assert final.stage == "final"
|
|
|
|
|
assert "edge" not in final.completed_net_ids
|
|
|
|
|
traced_edge = next(net for net in final.nets if net.net_id == "edge")
|
|
|
|
|
assert traced_edge.outcome == edge.outcome
|
|
|
|
|
assert traced_edge.report == edge.report
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("node_limit", [40, 500, 700, None], ids=["tight", "initial", "higher", "default"])
|
|
|
|
|
def test_default_router_escalates_only_within_the_configured_node_limit(monkeypatch, node_limit) -> None:
|
|
|
|
|
problem = RoutingProblem((0, 0, 200, 200), (NetSpec("turn", Port(20, 20, 0), Port(180, 160, 90)),))
|
|
|
|
|
route_astar = router_module.route_astar
|
|
|
|
|
attempted_limits = []
|
|
|
|
|
|
|
|
|
|
def route_with_budget(*args, config, **kwargs):
|
|
|
|
|
attempted_limits.append(config.node_limit)
|
|
|
|
|
if config.node_limit <= 500:
|
|
|
|
|
return None
|
|
|
|
|
return route_astar(*args, config=config, **kwargs)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(router_module, "route_astar", route_with_budget)
|
|
|
|
|
options = None if node_limit is None else RoutingOptions(search=SearchOptions(node_limit=node_limit))
|
|
|
|
|
run = route(problem, options=options)
|
|
|
|
|
|
|
|
|
|
assert run.automatic is not None
|
|
|
|
|
ceiling = SearchOptions().node_limit if node_limit is None else node_limit
|
|
|
|
|
assert attempted_limits
|
|
|
|
|
assert max(attempted_limits) <= ceiling
|
|
|
|
|
limits = [stage.node_limit for stage in run.automatic.stages]
|
|
|
|
|
assert min(500, ceiling) in limits
|
|
|
|
|
assert [limit for limit in limits if limit > 500] == ([ceiling] if ceiling > 500 else [])
|
|
|
|
|
assert run.results_by_net["turn"].is_valid == (ceiling > 500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_default_router_uses_lattice_when_triangulation_is_unavailable(monkeypatch) -> None:
|
|
|
|
|
problem = RoutingProblem((0, 0, 200, 200), (NetSpec("turn", Port(20, 20, 0), Port(180, 160, 90)),))
|
|
|
|
|
monkeypatch.delattr(shapely, "constrained_delaunay_triangles", raising=False)
|
|
|
|
|
|
|
|
|
|
run = route(problem)
|
|
|
|
|
|
|
|
|
|
assert run.automatic is not None
|
|
|
|
|
assert run.automatic.selected_planner == "lattice"
|
|
|
|
|
assert run.automatic.stages[run.automatic.selected_stage].kind == "lattice-fallback"
|
|
|
|
|
assert run.automatic.verified_net_ids == ("turn",)
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
def test_route_problem_supports_configs_and_debug_data() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(NetSpec("net1", Port(10, 10, 0), Port(90, 90, 0), width=2.0),),
|
|
|
|
|
static_obstacles=(box(40, 0, 60, 70),),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
search=SearchOptions(
|
|
|
|
|
bend_radii=(10.0,),
|
|
|
|
|
node_limit=50000,
|
|
|
|
|
greedy_h_weight=1.2,
|
|
|
|
|
),
|
|
|
|
|
objective=ObjectiveWeights(
|
|
|
|
|
bend_penalty=50.0,
|
|
|
|
|
sbend_penalty=150.0,
|
|
|
|
|
),
|
2026-03-30 19:51:37 -07:00
|
|
|
congestion=CongestionOptions(warm_start_enabled=False),
|
2026-03-30 15:32:29 -07:00
|
|
|
refinement=RefinementOptions(enabled=True),
|
|
|
|
|
diagnostics=DiagnosticsOptions(capture_expanded=True),
|
2026-11-26 11:57:43 -08:00
|
|
|
topology=TopologyOptions(mode="off"),
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
run = route(problem, options=options)
|
|
|
|
|
|
|
|
|
|
assert run.results_by_net["net1"].reached_target
|
|
|
|
|
assert run.expanded_nodes
|
|
|
|
|
assert run.metrics.nodes_expanded > 0
|
2026-03-31 17:26:00 -07:00
|
|
|
assert run.metrics.route_iterations >= 1
|
2026-04-01 21:29:23 -07:00
|
|
|
assert run.metrics.iteration_reverify_calls >= 1
|
|
|
|
|
assert run.metrics.iteration_reverified_nets >= 0
|
|
|
|
|
assert run.metrics.iteration_conflicting_nets >= 0
|
|
|
|
|
assert run.metrics.iteration_conflict_edges >= 0
|
|
|
|
|
assert run.metrics.nets_carried_forward >= 0
|
2026-03-31 17:26:00 -07:00
|
|
|
assert run.metrics.nets_routed >= 1
|
|
|
|
|
assert run.metrics.move_cache_abs_misses >= 0
|
|
|
|
|
assert run.metrics.ray_cast_calls >= 0
|
|
|
|
|
assert run.metrics.dynamic_tree_rebuilds >= 0
|
2026-04-01 21:29:23 -07:00
|
|
|
assert run.metrics.visibility_corner_index_builds >= 0
|
2026-03-31 17:26:00 -07:00
|
|
|
assert run.metrics.visibility_builds >= 0
|
2026-04-02 14:39:39 -07:00
|
|
|
assert run.metrics.guidance_match_moves >= 0
|
|
|
|
|
assert run.metrics.guidance_match_moves_straight >= 0
|
|
|
|
|
assert run.metrics.guidance_match_moves_bend90 >= 0
|
|
|
|
|
assert run.metrics.guidance_match_moves_sbend >= 0
|
|
|
|
|
assert run.metrics.guidance_bonus_applied >= 0.0
|
|
|
|
|
assert run.metrics.guidance_bonus_applied_straight >= 0.0
|
|
|
|
|
assert run.metrics.guidance_bonus_applied_bend90 >= 0.0
|
|
|
|
|
assert run.metrics.guidance_bonus_applied_sbend >= 0.0
|
2026-04-01 21:29:23 -07:00
|
|
|
assert run.metrics.congestion_grid_span_cache_hits >= 0
|
|
|
|
|
assert run.metrics.congestion_grid_span_cache_misses >= 0
|
|
|
|
|
assert run.metrics.congestion_presence_cache_hits >= 0
|
|
|
|
|
assert run.metrics.congestion_presence_cache_misses >= 0
|
|
|
|
|
assert run.metrics.congestion_presence_skips >= 0
|
|
|
|
|
assert run.metrics.congestion_candidate_precheck_hits >= 0
|
|
|
|
|
assert run.metrics.congestion_candidate_precheck_misses >= 0
|
|
|
|
|
assert run.metrics.congestion_candidate_precheck_skips >= 0
|
|
|
|
|
assert run.metrics.congestion_candidate_nets >= 0
|
|
|
|
|
assert run.metrics.congestion_net_envelope_cache_hits >= 0
|
|
|
|
|
assert run.metrics.congestion_net_envelope_cache_misses >= 0
|
|
|
|
|
assert run.metrics.congestion_grid_net_cache_hits >= 0
|
|
|
|
|
assert run.metrics.congestion_grid_net_cache_misses >= 0
|
|
|
|
|
assert run.metrics.congestion_lazy_resolutions >= 0
|
|
|
|
|
assert run.metrics.congestion_lazy_requeues >= 0
|
|
|
|
|
assert run.metrics.congestion_candidate_ids >= 0
|
|
|
|
|
assert run.metrics.verify_dynamic_candidate_nets >= 0
|
2026-03-31 17:26:00 -07:00
|
|
|
assert run.metrics.verify_path_report_calls >= 0
|
2026-04-02 14:39:39 -07:00
|
|
|
assert run.metrics.pair_local_search_pairs_considered >= 0
|
|
|
|
|
assert run.metrics.pair_local_search_attempts >= 0
|
|
|
|
|
assert run.metrics.pair_local_search_accepts >= 0
|
|
|
|
|
assert run.metrics.pair_local_search_nodes_expanded >= 0
|
2026-04-02 18:57:34 -07:00
|
|
|
assert run.metrics.late_phase_capped_nets >= 0
|
|
|
|
|
assert run.metrics.late_phase_capped_fallbacks >= 0
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def test_iteration_callback_observes_reverified_conflicts() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("vertical", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
|
|
|
|
|
pathfinder = PathFinder(AStarContext(evaluator, problem, options))
|
|
|
|
|
snapshots: list[dict[str, str]] = []
|
|
|
|
|
|
|
|
|
|
def callback(iteration: int, current_results: dict[str, object]) -> None:
|
|
|
|
|
_ = iteration
|
|
|
|
|
snapshots.append({net_id: result.outcome for net_id, result in current_results.items()})
|
|
|
|
|
|
|
|
|
|
results = pathfinder.route_all(iteration_callback=callback)
|
|
|
|
|
|
|
|
|
|
assert snapshots == [{"horizontal": "colliding", "vertical": "colliding"}]
|
|
|
|
|
assert results["horizontal"].outcome == "colliding"
|
|
|
|
|
assert results["vertical"].outcome == "colliding"
|
|
|
|
|
|
|
|
|
|
|
2026-11-27 00:27:16 -08:00
|
|
|
@pytest.mark.parametrize("mutate_results", [False, True])
|
|
|
|
|
def test_installed_best_iteration_respects_callback_edits(mutate_results: bool) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(NetSpec("net", Port(10, 50, 0), Port(90, 50, 0)),),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
topology=TopologyOptions(mode="off"),
|
|
|
|
|
)
|
|
|
|
|
world = RoutingWorld(clearance=problem.clearance)
|
|
|
|
|
finder = PathFinder(AStarContext(CostEvaluator(world, DangerMap(bounds=problem.bounds)), problem, options))
|
|
|
|
|
|
|
|
|
|
def callback(_iteration, results):
|
|
|
|
|
assert results["net"].is_valid
|
|
|
|
|
results["net"] = RoutingResult("net", ())
|
|
|
|
|
|
|
|
|
|
results = finder.route_all(iteration_callback=callback if mutate_results else None)
|
|
|
|
|
|
|
|
|
|
assert results["net"].is_valid is not mutate_results
|
|
|
|
|
assert world.has_dynamic_paths() is not mutate_results
|
|
|
|
|
assert finder.metrics.snapshot().dynamic_path_objects_added == 1
|
|
|
|
|
assert finder.metrics.snapshot().dynamic_path_objects_removed == int(mutate_results)
|
2026-11-27 00:42:27 -08:00
|
|
|
if not mutate_results:
|
|
|
|
|
assert finder.metrics.snapshot().verify_path_report_calls == 3
|
2026-11-27 00:27:16 -08:00
|
|
|
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def test_capture_conflict_trace_preserves_route_outputs() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("vertical", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
base_options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
run_without_trace = route(problem, options=base_options)
|
|
|
|
|
run_with_trace = route(
|
|
|
|
|
problem,
|
|
|
|
|
options=RoutingOptions(
|
|
|
|
|
congestion=base_options.congestion,
|
|
|
|
|
refinement=base_options.refinement,
|
|
|
|
|
diagnostics=DiagnosticsOptions(capture_conflict_trace=True),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert {net_id: result.outcome for net_id, result in run_without_trace.results_by_net.items()} == {
|
|
|
|
|
net_id: result.outcome for net_id, result in run_with_trace.results_by_net.items()
|
|
|
|
|
}
|
|
|
|
|
assert [entry.stage for entry in run_with_trace.conflict_trace] == ["iteration", "restored_best", "final"]
|
|
|
|
|
|
|
|
|
|
|
2026-11-26 23:00:23 -08:00
|
|
|
def test_capture_iteration_trace_preserves_route_outputs(monkeypatch) -> None:
|
2026-04-02 16:15:02 -07:00
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("vertical", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
base_options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-26 23:00:23 -08:00
|
|
|
def unexpected_attempt_trace(*_args, **_kwargs):
|
|
|
|
|
pytest.fail("Routing built an attempt trace with diagnostics disabled")
|
|
|
|
|
|
|
|
|
|
with monkeypatch.context() as patch:
|
|
|
|
|
patch.setattr(router_module, "net_attempt_trace", unexpected_attempt_trace)
|
|
|
|
|
run_without_trace = route(problem, options=base_options)
|
2026-04-02 16:15:02 -07:00
|
|
|
run_with_trace = route(
|
|
|
|
|
problem,
|
|
|
|
|
options=RoutingOptions(
|
|
|
|
|
congestion=base_options.congestion,
|
|
|
|
|
refinement=base_options.refinement,
|
|
|
|
|
diagnostics=DiagnosticsOptions(capture_iteration_trace=True),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert {net_id: result.outcome for net_id, result in run_without_trace.results_by_net.items()} == {
|
|
|
|
|
net_id: result.outcome for net_id, result in run_with_trace.results_by_net.items()
|
|
|
|
|
}
|
|
|
|
|
assert len(run_with_trace.iteration_trace) == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_capture_iteration_trace_records_iteration_and_attempt_deltas() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("vertical", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
run = route(
|
|
|
|
|
problem,
|
|
|
|
|
options=RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
diagnostics=DiagnosticsOptions(capture_iteration_trace=True),
|
2026-11-26 11:57:43 -08:00
|
|
|
topology=TopologyOptions(mode="off"),
|
2026-04-02 16:15:02 -07:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
entry = run.iteration_trace[0]
|
|
|
|
|
assert entry.iteration == 0
|
|
|
|
|
assert entry.congestion_penalty == 100.0
|
|
|
|
|
assert entry.routed_net_ids == ("horizontal", "vertical")
|
|
|
|
|
assert entry.completed_nets == 0
|
|
|
|
|
assert entry.conflict_edges == 1
|
|
|
|
|
assert entry.total_dynamic_collisions >= 2
|
|
|
|
|
assert entry.nodes_expanded >= 0
|
|
|
|
|
assert entry.congestion_check_calls >= 0
|
|
|
|
|
assert entry.congestion_candidate_ids >= 0
|
|
|
|
|
assert entry.congestion_exact_pair_checks >= 0
|
|
|
|
|
assert len(entry.net_attempts) == 2
|
|
|
|
|
assert [attempt.net_id for attempt in entry.net_attempts] == ["horizontal", "vertical"]
|
|
|
|
|
assert all(attempt.nodes_expanded >= 0 for attempt in entry.net_attempts)
|
|
|
|
|
assert all(attempt.congestion_check_calls >= 0 for attempt in entry.net_attempts)
|
|
|
|
|
assert all(not attempt.guidance_seed_present for attempt in entry.net_attempts)
|
|
|
|
|
assert sum(attempt.nodes_expanded for attempt in entry.net_attempts) == entry.nodes_expanded
|
|
|
|
|
assert sum(attempt.congestion_check_calls for attempt in entry.net_attempts) == entry.congestion_check_calls
|
|
|
|
|
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def test_capture_conflict_trace_records_component_pairs() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("vertical", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
diagnostics=DiagnosticsOptions(capture_conflict_trace=True),
|
2026-11-26 11:57:43 -08:00
|
|
|
topology=TopologyOptions(mode="off"),
|
2026-04-02 14:39:39 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
run = route(problem, options=options)
|
|
|
|
|
final_entry = run.conflict_trace[-1]
|
|
|
|
|
trace_by_net = {net.net_id: net for net in final_entry.nets}
|
|
|
|
|
|
|
|
|
|
assert final_entry.stage == "final"
|
|
|
|
|
assert final_entry.conflict_edges == (("horizontal", "vertical"),)
|
|
|
|
|
assert trace_by_net["horizontal"].component_conflicts[0].other_net_id == "vertical"
|
|
|
|
|
assert trace_by_net["horizontal"].component_conflicts[0].self_component_index == 0
|
|
|
|
|
assert trace_by_net["horizontal"].component_conflicts[0].other_component_index == 0
|
|
|
|
|
assert trace_by_net["vertical"].component_conflicts[0].other_net_id == "horizontal"
|
|
|
|
|
assert trace_by_net["vertical"].component_conflicts[0].self_component_index == 0
|
|
|
|
|
assert trace_by_net["vertical"].component_conflicts[0].other_component_index == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_capture_frontier_trace_preserves_route_outputs() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("vertical", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
base_options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
2026-11-26 11:57:43 -08:00
|
|
|
topology=TopologyOptions(mode="off"),
|
2026-04-02 14:39:39 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
run_without_trace = route(problem, options=base_options)
|
|
|
|
|
run_with_trace = route(
|
|
|
|
|
problem,
|
|
|
|
|
options=RoutingOptions(
|
|
|
|
|
congestion=base_options.congestion,
|
|
|
|
|
refinement=base_options.refinement,
|
|
|
|
|
diagnostics=DiagnosticsOptions(capture_frontier_trace=True),
|
2026-11-26 11:57:43 -08:00
|
|
|
topology=base_options.topology,
|
2026-04-02 14:39:39 -07:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert {net_id: result.outcome for net_id, result in run_without_trace.results_by_net.items()} == {
|
|
|
|
|
net_id: result.outcome for net_id, result in run_with_trace.results_by_net.items()
|
|
|
|
|
}
|
|
|
|
|
assert {trace.net_id for trace in run_with_trace.frontier_trace} == {"horizontal", "vertical"}
|
2026-11-26 19:58:46 -08:00
|
|
|
assert run_with_trace.metrics.verify_path_report_calls == run_without_trace.metrics.verify_path_report_calls
|
2026-04-02 14:39:39 -07:00
|
|
|
|
|
|
|
|
|
2026-04-02 18:57:34 -07:00
|
|
|
def test_capture_pre_pair_frontier_trace_preserves_route_outputs() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("vertical", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
base_options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
run_without_trace = route(problem, options=base_options)
|
|
|
|
|
run_with_trace = route(
|
|
|
|
|
problem,
|
|
|
|
|
options=RoutingOptions(
|
|
|
|
|
congestion=base_options.congestion,
|
|
|
|
|
refinement=base_options.refinement,
|
|
|
|
|
diagnostics=DiagnosticsOptions(capture_pre_pair_frontier_trace=True),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert {net_id: result.outcome for net_id, result in run_without_trace.results_by_net.items()} == {
|
|
|
|
|
net_id: result.outcome for net_id, result in run_with_trace.results_by_net.items()
|
|
|
|
|
}
|
|
|
|
|
assert run_with_trace.pre_pair_frontier_trace is None
|
|
|
|
|
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def test_capture_frontier_trace_records_prune_reasons() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("vertical", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
run = route(
|
|
|
|
|
problem,
|
|
|
|
|
options=RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
diagnostics=DiagnosticsOptions(capture_frontier_trace=True),
|
2026-11-26 11:57:43 -08:00
|
|
|
topology=TopologyOptions(mode="off"),
|
2026-04-02 14:39:39 -07:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
trace_by_net = {entry.net_id: entry for entry in run.frontier_trace}
|
|
|
|
|
assert trace_by_net["horizontal"].hotspot_bounds
|
|
|
|
|
assert (
|
|
|
|
|
trace_by_net["horizontal"].pruned_closed_set
|
|
|
|
|
+ trace_by_net["horizontal"].pruned_hard_collision
|
|
|
|
|
+ trace_by_net["horizontal"].pruned_self_collision
|
|
|
|
|
+ trace_by_net["horizontal"].pruned_cost
|
|
|
|
|
) > 0
|
|
|
|
|
assert trace_by_net["horizontal"].samples
|
|
|
|
|
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def test_reverify_iterations_stop_early_on_stalled_conflict_graph() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("horizontal", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("vertical", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=10, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
2026-11-26 11:57:43 -08:00
|
|
|
topology=TopologyOptions(mode="off"),
|
2026-04-01 21:29:23 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
run = route(problem, options=options)
|
|
|
|
|
|
|
|
|
|
assert run.metrics.route_iterations < 10
|
|
|
|
|
|
|
|
|
|
|
2026-04-02 17:10:00 -07:00
|
|
|
def test_reverify_iterations_limit_late_reroutes_to_conflicting_nets(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("netA", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("netB", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
NetSpec("netC", Port(10, 20, 0), Port(90, 20, 0), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=10, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
|
|
|
|
|
pathfinder = PathFinder(AStarContext(evaluator, problem, options))
|
|
|
|
|
colliding_a = RoutingResult(
|
|
|
|
|
net_id="netA",
|
|
|
|
|
path=(Straight.generate(Port(10, 50, 0), 80.0, 2.0, dilation=1.0),),
|
|
|
|
|
reached_target=True,
|
|
|
|
|
report=RoutingReport(dynamic_collision_count=1, total_length=80.0),
|
|
|
|
|
)
|
|
|
|
|
colliding_b = RoutingResult(
|
|
|
|
|
net_id="netB",
|
|
|
|
|
path=(Straight.generate(Port(50, 10, 90), 80.0, 2.0, dilation=1.0),),
|
|
|
|
|
reached_target=True,
|
|
|
|
|
report=RoutingReport(dynamic_collision_count=1, total_length=80.0),
|
|
|
|
|
)
|
|
|
|
|
completed_c = RoutingResult(
|
|
|
|
|
net_id="netC",
|
|
|
|
|
path=(Straight.generate(Port(10, 20, 0), 80.0, 2.0, dilation=1.0),),
|
|
|
|
|
reached_target=True,
|
|
|
|
|
report=RoutingReport(total_length=80.0),
|
|
|
|
|
)
|
|
|
|
|
iterations_seen: list[int] = []
|
|
|
|
|
reroute_sets: list[set[str]] = []
|
|
|
|
|
|
|
|
|
|
def fake_run_iteration(self, state, iteration, reroute_net_ids, iteration_callback):
|
|
|
|
|
_ = self
|
|
|
|
|
_ = iteration_callback
|
|
|
|
|
iterations_seen.append(iteration)
|
|
|
|
|
reroute_sets.append(set(reroute_net_ids))
|
|
|
|
|
state.results = {"netA": colliding_a, "netB": colliding_b, "netC": completed_c}
|
|
|
|
|
return _IterationReview(
|
|
|
|
|
conflicting_nets={"netA", "netB"},
|
|
|
|
|
conflict_edges={("netA", "netB")},
|
|
|
|
|
completed_net_ids={"netC"},
|
|
|
|
|
total_dynamic_collisions=2,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_run_iteration", fake_run_iteration)
|
2026-11-26 19:58:46 -08:00
|
|
|
monkeypatch.setattr(PathFinder, "_verify_results", lambda self, state: (dict(state.results), {}))
|
2026-11-26 21:55:34 -08:00
|
|
|
monkeypatch.setattr(PathFinder, "_run_pair_local_search", lambda self, state, review: None)
|
2026-04-02 17:10:00 -07:00
|
|
|
|
|
|
|
|
results = pathfinder.route_all()
|
|
|
|
|
|
|
|
|
|
assert iterations_seen == [0, 1]
|
|
|
|
|
assert reroute_sets == [{"netA", "netB", "netC"}, {"netA", "netB"}]
|
|
|
|
|
assert results["netA"].outcome == "colliding"
|
|
|
|
|
assert results["netB"].outcome == "colliding"
|
2026-04-02 18:57:34 -07:00
|
|
|
assert results["netC"].reached_target
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_iteration_orders_subset_reroutes_by_recent_work(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("netA", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("netB", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
NetSpec("netC", Port(10, 20, 0), Port(90, 20, 0), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=2, warm_start_enabled=False, shuffle_nets=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
|
|
|
|
|
pathfinder = PathFinder(AStarContext(evaluator, problem, options))
|
|
|
|
|
state = pathfinder._prepare_state()
|
|
|
|
|
state.recent_attempt_work = {"netA": 200, "netB": 20}
|
|
|
|
|
route_order: list[str] = []
|
|
|
|
|
|
|
|
|
|
def fake_route_net_once(self, state, iteration, net_id, *, node_limit_override=None, incumbent_fallback=None):
|
|
|
|
|
_ = self
|
|
|
|
|
_ = state
|
|
|
|
|
_ = iteration
|
|
|
|
|
route_order.append(net_id)
|
|
|
|
|
assert node_limit_override is None
|
|
|
|
|
assert incumbent_fallback is None
|
|
|
|
|
return RoutingResult(net_id=net_id, path=(), reached_target=False), False
|
|
|
|
|
|
|
|
|
|
def fake_reverify(self, state):
|
|
|
|
|
_ = self
|
|
|
|
|
_ = state
|
|
|
|
|
return _IterationReview(
|
|
|
|
|
conflicting_nets={"netA", "netB"},
|
|
|
|
|
conflict_edges={("netA", "netB")},
|
|
|
|
|
completed_net_ids=set(),
|
|
|
|
|
total_dynamic_collisions=2,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_route_net_once", fake_route_net_once)
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_reverify_iteration_results", fake_reverify)
|
|
|
|
|
|
|
|
|
|
review = pathfinder._run_iteration(state, 1, {"netA", "netB"}, None)
|
|
|
|
|
|
|
|
|
|
assert review is not None
|
|
|
|
|
assert route_order == ["netB", "netA"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_iteration_caps_two_heaviest_late_phase_nets(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("netA", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("netB", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
NetSpec("netC", Port(10, 20, 0), Port(90, 20, 0), width=2.0),
|
|
|
|
|
NetSpec("netD", Port(10, 80, 0), Port(90, 80, 0), width=2.0),
|
|
|
|
|
NetSpec("netE", Port(10, 65, 0), Port(90, 65, 0), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
objective=ObjectiveWeights(bend_penalty=100.0),
|
|
|
|
|
congestion=CongestionOptions(max_iterations=2, warm_start_enabled=False, shuffle_nets=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
|
|
|
|
|
pathfinder = PathFinder(AStarContext(evaluator, problem, options))
|
|
|
|
|
state = pathfinder._prepare_state()
|
|
|
|
|
state.results = {
|
|
|
|
|
net_id: RoutingResult(net_id=net_id, path=(Straight.generate(spec.start, 80.0, 2.0, dilation=1.0),), reached_target=True)
|
|
|
|
|
for net_id, spec in state.net_specs.items()
|
|
|
|
|
}
|
2026-11-27 00:28:32 -08:00
|
|
|
state.best_review = _IterationReview({"netA", "netB", "netC", "netD"}, {("netA", "netB"), ("netC", "netD")}, {"netE"}, 4)
|
2026-04-02 18:57:34 -07:00
|
|
|
state.recent_attempt_work = {"netA": 20, "netB": 40, "netC": 400, "netD": 220}
|
|
|
|
|
incumbents = dict(state.results)
|
|
|
|
|
caps_by_net: dict[str, tuple[int | None, RoutingResult | None]] = {}
|
|
|
|
|
|
|
|
|
|
def fake_route_net_once(self, state, iteration, net_id, *, node_limit_override=None, incumbent_fallback=None):
|
|
|
|
|
_ = self
|
|
|
|
|
_ = state
|
|
|
|
|
_ = iteration
|
|
|
|
|
caps_by_net[net_id] = (node_limit_override, incumbent_fallback)
|
|
|
|
|
return RoutingResult(net_id=net_id, path=(), reached_target=False), False
|
|
|
|
|
|
|
|
|
|
def fake_reverify(self, state):
|
|
|
|
|
_ = self
|
|
|
|
|
_ = state
|
|
|
|
|
return _IterationReview(
|
|
|
|
|
conflicting_nets={"netA", "netB", "netC", "netD"},
|
|
|
|
|
conflict_edges={("netA", "netB"), ("netC", "netD")},
|
|
|
|
|
completed_net_ids=set(),
|
|
|
|
|
total_dynamic_collisions=2,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_route_net_once", fake_route_net_once)
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_reverify_iteration_results", fake_reverify)
|
|
|
|
|
|
|
|
|
|
review = pathfinder._run_iteration(state, 1, {"netA", "netB", "netC", "netD"}, None)
|
|
|
|
|
|
|
|
|
|
assert review is not None
|
|
|
|
|
assert caps_by_net["netA"] == (None, None)
|
|
|
|
|
assert caps_by_net["netB"] == (None, None)
|
|
|
|
|
assert caps_by_net["netC"][0] == 1
|
|
|
|
|
assert caps_by_net["netD"][0] == 1
|
|
|
|
|
assert caps_by_net["netC"][1] is incumbents["netC"]
|
|
|
|
|
assert caps_by_net["netD"][1] is incumbents["netD"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_net_once_skips_search_for_capped_incumbent_fallback(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(NetSpec("netA", Port(10, 50, 0), Port(90, 50, 0), width=2.0),),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
objective=ObjectiveWeights(bend_penalty=100.0),
|
|
|
|
|
congestion=CongestionOptions(max_iterations=2, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
|
|
|
|
|
pathfinder = PathFinder(AStarContext(evaluator, problem, options))
|
|
|
|
|
state = pathfinder._prepare_state()
|
|
|
|
|
incumbent = RoutingResult(
|
|
|
|
|
net_id="netA",
|
|
|
|
|
path=(Straight.generate(problem.nets[0].start, 80.0, 2.0, dilation=1.0),),
|
|
|
|
|
reached_target=True,
|
|
|
|
|
)
|
|
|
|
|
state.results["netA"] = incumbent
|
|
|
|
|
installed: list[tuple[str, tuple[object, ...]]] = []
|
|
|
|
|
|
2026-11-26 19:58:46 -08:00
|
|
|
def fail_route_astar(*_args, **_kwargs):
|
2026-04-02 18:57:34 -07:00
|
|
|
raise AssertionError("route_astar should not run for capped incumbent fallback")
|
|
|
|
|
|
|
|
|
|
def record_install(self, net_id, path):
|
|
|
|
|
_ = self
|
|
|
|
|
installed.append((net_id, tuple(path)))
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(router_module, "route_astar", fail_route_astar)
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_install_path", record_install)
|
|
|
|
|
|
|
|
|
|
result, guidance_seed_present = pathfinder._route_net_once(
|
|
|
|
|
state,
|
|
|
|
|
1,
|
|
|
|
|
"netA",
|
|
|
|
|
node_limit_override=1,
|
|
|
|
|
incumbent_fallback=incumbent,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result is incumbent
|
|
|
|
|
assert guidance_seed_present is True
|
|
|
|
|
assert installed == [("netA", incumbent.path)]
|
|
|
|
|
assert pathfinder.metrics.total_late_phase_capped_nets == 1
|
|
|
|
|
assert pathfinder.metrics.total_late_phase_capped_fallbacks == 1
|
2026-04-02 17:10:00 -07:00
|
|
|
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def test_route_all_restores_best_iteration_snapshot(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(
|
|
|
|
|
NetSpec("netA", Port(10, 50, 0), Port(90, 50, 0), width=2.0),
|
|
|
|
|
NetSpec("netB", Port(50, 10, 90), Port(50, 90, 90), width=2.0),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=2, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
|
|
|
|
|
pathfinder = PathFinder(AStarContext(evaluator, problem, options))
|
|
|
|
|
best_result = RoutingResult(
|
|
|
|
|
net_id="netA",
|
|
|
|
|
path=(Straight.generate(Port(10, 50, 0), 80.0, 2.0, dilation=1.0),),
|
|
|
|
|
reached_target=True,
|
|
|
|
|
report=RoutingReport(),
|
|
|
|
|
)
|
|
|
|
|
missing_result = RoutingResult(net_id="netA", path=(), reached_target=False)
|
|
|
|
|
unroutable_b = RoutingResult(net_id="netB", path=(), reached_target=False)
|
|
|
|
|
|
|
|
|
|
def fake_run_iteration(self, state, iteration, reroute_net_ids, iteration_callback):
|
|
|
|
|
_ = self
|
|
|
|
|
_ = reroute_net_ids
|
|
|
|
|
_ = iteration_callback
|
|
|
|
|
if iteration == 0:
|
|
|
|
|
state.results = {"netA": best_result, "netB": unroutable_b}
|
|
|
|
|
return _IterationReview(
|
|
|
|
|
conflicting_nets={"netA", "netB"},
|
|
|
|
|
conflict_edges={("netA", "netB")},
|
|
|
|
|
completed_net_ids={"netA"},
|
|
|
|
|
total_dynamic_collisions=1,
|
|
|
|
|
)
|
|
|
|
|
state.results = {"netA": missing_result, "netB": unroutable_b}
|
|
|
|
|
return _IterationReview(
|
|
|
|
|
conflicting_nets={"netA", "netB"},
|
|
|
|
|
conflict_edges={("netA", "netB")},
|
|
|
|
|
completed_net_ids=set(),
|
|
|
|
|
total_dynamic_collisions=2,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_run_iteration", fake_run_iteration)
|
2026-11-26 19:58:46 -08:00
|
|
|
monkeypatch.setattr(PathFinder, "_verify_results", lambda self, state: (dict(state.results), {}))
|
2026-04-01 21:29:23 -07:00
|
|
|
|
|
|
|
|
results = pathfinder.route_all()
|
|
|
|
|
|
|
|
|
|
assert results["netA"].outcome == "completed"
|
|
|
|
|
assert results["netB"].outcome == "unroutable"
|
2026-11-27 00:27:16 -08:00
|
|
|
assert evaluator.collision_engine.has_dynamic_paths()
|
2026-11-27 00:42:27 -08:00
|
|
|
assert pathfinder.metrics.snapshot().verify_path_report_calls == 1
|
2026-04-01 21:29:23 -07:00
|
|
|
|
|
|
|
|
|
2026-11-27 00:28:32 -08:00
|
|
|
def test_first_iteration_and_pair_length_tie_break() -> None:
|
|
|
|
|
problem = RoutingProblem(bounds=(0, 0, 100, 100))
|
|
|
|
|
options = RoutingOptions(congestion=CongestionOptions(warm_start_enabled=False), topology=TopologyOptions(mode="off"))
|
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
|
|
|
|
|
finder = PathFinder(AStarContext(evaluator, problem, options))
|
|
|
|
|
state = finder._prepare_state()
|
|
|
|
|
review = _IterationReview(set(), set(), set(), 0)
|
|
|
|
|
|
|
|
|
|
assert finder._update_best_iteration(state, review)
|
|
|
|
|
assert not finder._update_best_iteration(state, review)
|
|
|
|
|
|
|
|
|
|
def results(length):
|
|
|
|
|
return {"a": RoutingResult("a", (), reached_target=True, report=RoutingReport(total_length=length))}
|
|
|
|
|
|
|
|
|
|
incumbent = results(10.0)
|
|
|
|
|
assert not finder._whole_set_is_better(results(10.0 - 0.5e-6), review, incumbent, review)
|
|
|
|
|
assert finder._whole_set_is_better(results(10.0 - 2e-6), review, incumbent, review)
|
|
|
|
|
|
|
|
|
|
|
2026-11-27 00:34:49 -08:00
|
|
|
def test_pair_repair_limits_disjoint_targets_in_routing_order(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=tuple(NetSpec(net_id, Port(10, i * 10, 0), Port(90, i * 10, 0)) for i, net_id in enumerate("cdbaef", 1)),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(congestion=CongestionOptions(warm_start_enabled=False), topology=TopologyOptions(mode="off"))
|
|
|
|
|
world = RoutingWorld(clearance=problem.clearance)
|
|
|
|
|
finder = PathFinder(AStarContext(CostEvaluator(world, DangerMap(bounds=problem.bounds)), problem, options))
|
|
|
|
|
state = finder._prepare_state()
|
|
|
|
|
state.results = {net_id: RoutingResult(net_id, (), reached_target=True) for net_id in state.ordered_net_ids}
|
|
|
|
|
review = _IterationReview(set(state.ordered_net_ids), {("a", "b"), ("c", "d"), ("e", "f")}, set(), 6)
|
|
|
|
|
visited = []
|
|
|
|
|
|
|
|
|
|
def repair(_finder, _state, pair, current_review):
|
|
|
|
|
visited.append(pair)
|
|
|
|
|
return current_review
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_run_pair_local_target", repair)
|
|
|
|
|
finder._run_pair_local_search(state, review)
|
|
|
|
|
assert visited == [("c", "d"), ("a", "b")]
|
|
|
|
|
|
|
|
|
|
visited.clear()
|
|
|
|
|
review.conflict_edges.add(("b", "c"))
|
|
|
|
|
finder._run_pair_local_search(state, review)
|
|
|
|
|
assert not visited
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("candidate_reaches_live_check", [False, True])
|
|
|
|
|
def test_rejected_pair_keeps_original_index_for_reverse_attempt(monkeypatch: pytest.MonkeyPatch, candidate_reaches_live_check: bool) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
2026-11-27 00:40:44 -08:00
|
|
|
nets=tuple(NetSpec(net_id, Port(10, y, 0), Port(90, y, 0)) for net_id, y in (("a", 20), ("b", 40), ("c", 60))),
|
2026-11-27 00:34:49 -08:00
|
|
|
)
|
|
|
|
|
options = RoutingOptions(congestion=CongestionOptions(warm_start_enabled=False), topology=TopologyOptions(mode="off"))
|
|
|
|
|
world = RoutingWorld(clearance=problem.clearance)
|
|
|
|
|
finder = PathFinder(AStarContext(CostEvaluator(world, DangerMap(bounds=problem.bounds)), problem, options))
|
|
|
|
|
state = finder._prepare_state()
|
|
|
|
|
for net in problem.nets:
|
|
|
|
|
path = (Straight.generate(net.start, 80, net.width, dilation=1.0),)
|
|
|
|
|
finder._install_path(net.net_id, path)
|
|
|
|
|
state.results[net.net_id] = RoutingResult(net.net_id, path, reached_target=True, report=world.verify_path_report(net.net_id, path))
|
|
|
|
|
initial_bounds = sorted(world.iter_dynamic_path_bounds(problem.bounds))
|
|
|
|
|
incumbent = dict(state.results)
|
2026-11-27 00:40:44 -08:00
|
|
|
review = _IterationReview(set(), set(), set(incumbent), 0)
|
2026-11-27 00:34:49 -08:00
|
|
|
attempted = []
|
|
|
|
|
|
|
|
|
|
def search(_context, _ordered, _results, pair_order, **_kwargs):
|
|
|
|
|
attempted.append(pair_order)
|
|
|
|
|
assert sorted(world.iter_dynamic_path_bounds(problem.bounds)) == initial_bounds
|
|
|
|
|
if candidate_reaches_live_check and pair_order == ("a", "b"):
|
2026-11-27 00:40:44 -08:00
|
|
|
return {"a": RoutingResult("a", incumbent["b"].path, reached_target=True), "b": incumbent["b"]}, 0
|
2026-11-27 00:34:49 -08:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(router_module, "search_pair_candidate", search)
|
|
|
|
|
assert finder._run_pair_local_target(state, ("a", "b"), review) is review
|
|
|
|
|
|
|
|
|
|
assert attempted == [("a", "b"), ("b", "a")]
|
|
|
|
|
assert state.results == incumbent
|
|
|
|
|
assert sorted(world.iter_dynamic_path_bounds(problem.bounds)) == initial_bounds
|
|
|
|
|
assert all(world.verify_path_report(net_id, result.path) == result.report for net_id, result in incumbent.items())
|
|
|
|
|
metrics = finder.metrics.snapshot()
|
2026-11-27 00:40:44 -08:00
|
|
|
assert metrics.dynamic_path_objects_added == 3 + 4 * candidate_reaches_live_check
|
2026-11-27 00:34:49 -08:00
|
|
|
assert metrics.dynamic_path_objects_removed == 4 * candidate_reaches_live_check
|
|
|
|
|
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def test_route_all_restores_best_iteration_snapshot_on_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(NetSpec("netA", Port(10, 50, 0), Port(90, 50, 0), width=2.0),),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=2, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
|
|
|
|
|
pathfinder = PathFinder(AStarContext(evaluator, problem, options))
|
|
|
|
|
best_result = RoutingResult(
|
|
|
|
|
net_id="netA",
|
|
|
|
|
path=(Straight.generate(Port(10, 50, 0), 80.0, 2.0, dilation=1.0),),
|
|
|
|
|
reached_target=True,
|
|
|
|
|
report=RoutingReport(),
|
|
|
|
|
)
|
|
|
|
|
worse_result = RoutingResult(net_id="netA", path=(), reached_target=False)
|
|
|
|
|
|
|
|
|
|
def fake_run_iterations(self, state, iteration_callback):
|
|
|
|
|
_ = iteration_callback
|
|
|
|
|
_ = self
|
|
|
|
|
state.results = {"netA": best_result}
|
|
|
|
|
pathfinder._update_best_iteration(
|
|
|
|
|
state,
|
|
|
|
|
_IterationReview(
|
|
|
|
|
conflicting_nets=set(),
|
|
|
|
|
conflict_edges=set(),
|
|
|
|
|
completed_net_ids={"netA"},
|
|
|
|
|
total_dynamic_collisions=0,
|
|
|
|
|
),
|
|
|
|
|
)
|
2026-11-27 00:27:16 -08:00
|
|
|
state.best_is_installed = True
|
2026-04-01 21:29:23 -07:00
|
|
|
state.results = {"netA": worse_result}
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_run_iterations", fake_run_iterations)
|
2026-11-26 19:58:46 -08:00
|
|
|
monkeypatch.setattr(PathFinder, "_verify_results", lambda self, state: (dict(state.results), {}))
|
2026-04-01 21:29:23 -07:00
|
|
|
|
|
|
|
|
results = pathfinder.route_all()
|
|
|
|
|
|
|
|
|
|
assert results["netA"].outcome == "completed"
|
2026-11-27 00:27:16 -08:00
|
|
|
assert evaluator.collision_engine.has_dynamic_paths()
|
2026-04-01 21:29:23 -07:00
|
|
|
|
|
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def test_capture_conflict_trace_records_restored_best_stage(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(NetSpec("netA", Port(10, 50, 0), Port(90, 50, 0), width=2.0),),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
|
|
|
|
congestion=CongestionOptions(max_iterations=2, warm_start_enabled=False),
|
|
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
diagnostics=DiagnosticsOptions(capture_conflict_trace=True),
|
|
|
|
|
)
|
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=2.0), DangerMap(bounds=problem.bounds))
|
|
|
|
|
pathfinder = PathFinder(AStarContext(evaluator, problem, options))
|
|
|
|
|
best_result = RoutingResult(
|
|
|
|
|
net_id="netA",
|
|
|
|
|
path=(Straight.generate(Port(10, 50, 0), 80.0, 2.0, dilation=1.0),),
|
|
|
|
|
reached_target=True,
|
|
|
|
|
report=RoutingReport(),
|
|
|
|
|
)
|
|
|
|
|
worse_result = RoutingResult(net_id="netA", path=(), reached_target=False)
|
|
|
|
|
|
|
|
|
|
def fake_run_iteration(self, state, iteration, reroute_net_ids, iteration_callback):
|
|
|
|
|
_ = self
|
|
|
|
|
_ = reroute_net_ids
|
|
|
|
|
_ = iteration_callback
|
|
|
|
|
if iteration == 0:
|
|
|
|
|
state.results = {"netA": best_result}
|
|
|
|
|
return _IterationReview(
|
|
|
|
|
conflicting_nets=set(),
|
|
|
|
|
conflict_edges=set(),
|
|
|
|
|
completed_net_ids={"netA"},
|
|
|
|
|
total_dynamic_collisions=0,
|
|
|
|
|
)
|
|
|
|
|
state.results = {"netA": worse_result}
|
|
|
|
|
return _IterationReview(
|
|
|
|
|
conflicting_nets=set(),
|
|
|
|
|
conflict_edges=set(),
|
|
|
|
|
completed_net_ids=set(),
|
|
|
|
|
total_dynamic_collisions=0,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(PathFinder, "_run_iteration", fake_run_iteration)
|
|
|
|
|
|
|
|
|
|
pathfinder.route_all()
|
|
|
|
|
|
|
|
|
|
assert [entry.stage for entry in pathfinder.conflict_trace] == [
|
|
|
|
|
"restored_best",
|
|
|
|
|
"final",
|
|
|
|
|
]
|
|
|
|
|
restored_entry = pathfinder.conflict_trace[0]
|
|
|
|
|
assert restored_entry.nets[0].outcome == "completed"
|
2026-03-30 15:32:29 -07:00
|
|
|
def test_route_problem_locked_routes_become_static_obstacles() -> None:
|
|
|
|
|
locked = (Straight.generate(Port(10, 50, 0), 80.0, 2.0, dilation=1.0),)
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(NetSpec("crossing", Port(50, 10, 90), Port(50, 90, 90), width=2.0),),
|
2026-03-30 19:51:37 -07:00
|
|
|
static_obstacles=tuple(polygon for component in locked for polygon in component.physical_geometry),
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
|
|
|
|
options = RoutingOptions(
|
2026-03-30 19:51:37 -07:00
|
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
2026-03-30 15:32:29 -07:00
|
|
|
refinement=RefinementOptions(enabled=False),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
run = route(problem, options=options)
|
|
|
|
|
result = run.results_by_net["crossing"]
|
|
|
|
|
|
|
|
|
|
assert not result.is_valid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_locked_routes_enable_incremental_requests_without_sessions() -> None:
|
|
|
|
|
problem_a = RoutingProblem(
|
|
|
|
|
bounds=(0, -50, 100, 50),
|
|
|
|
|
nets=(NetSpec("netA", Port(10, 0, 0), Port(90, 0, 0), width=2.0),),
|
|
|
|
|
)
|
|
|
|
|
options = RoutingOptions(search=SearchOptions(bend_radii=(10.0,)))
|
|
|
|
|
results_a = route(problem_a, options=options)
|
|
|
|
|
assert results_a.results_by_net["netA"].is_valid
|
|
|
|
|
|
|
|
|
|
problem_b = RoutingProblem(
|
|
|
|
|
bounds=(0, -50, 100, 50),
|
|
|
|
|
nets=(NetSpec("netB", Port(50, -20, 90), Port(50, 20, 90), width=2.0),),
|
2026-03-30 19:51:37 -07:00
|
|
|
static_obstacles=results_a.results_by_net["netA"].locked_geometry,
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
|
|
|
|
results_b = route(problem_b, options=options)
|
|
|
|
|
|
|
|
|
|
assert results_b.results_by_net["netB"].is_valid
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 19:51:37 -07:00
|
|
|
def test_route_problem_rejects_untyped_initial_paths() -> None:
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
|
RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(NetSpec("net1", Port(10, 50, 0), Port(90, 50, 0), width=2.0),),
|
|
|
|
|
initial_paths={"net1": (object(),)}, # type: ignore[dict-item]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
def test_route_results_metrics_are_snapshots() -> None:
|
|
|
|
|
problem = RoutingProblem(
|
|
|
|
|
bounds=(0, 0, 100, 100),
|
|
|
|
|
nets=(NetSpec("net1", Port(10, 50, 0), Port(90, 50, 0), width=2.0),),
|
|
|
|
|
)
|
2026-11-26 11:57:43 -08:00
|
|
|
options = RoutingOptions(topology=TopologyOptions(mode="off"))
|
2026-03-30 15:32:29 -07:00
|
|
|
run1 = route(problem, options=options)
|
|
|
|
|
first_metrics = run1.metrics
|
|
|
|
|
run2 = route(problem, options=options)
|
|
|
|
|
|
|
|
|
|
assert first_metrics == run1.metrics
|
|
|
|
|
assert run1.metrics is not run2.metrics
|
|
|
|
|
assert first_metrics.nodes_expanded > 0
|