Reuse installed geometry for the best final iteration

This commit is contained in:
Jan Petykiewicz 2026-11-27 00:27:16 -08:00
commit 1cad709c63
2 changed files with 35 additions and 1 deletions

View file

@ -60,6 +60,7 @@ class _RoutingState:
initial_paths: dict[str, tuple[ComponentResult, ...]] | None
accumulated_expanded_nodes: list[tuple[int, int, int]]
best_results: dict[str, RoutingResult]
best_is_installed: bool
best_completed_nets: int
best_conflict_edges: int
best_dynamic_collisions: int
@ -332,6 +333,7 @@ class PathFinder:
initial_paths=initial_paths,
accumulated_expanded_nodes=[],
best_results={},
best_is_installed=False,
best_completed_nets=-1,
best_conflict_edges=10**9,
best_dynamic_collisions=10**9,
@ -1088,6 +1090,7 @@ class PathFinder:
if review is None:
return True
improved = self._update_best_iteration(state, review)
state.best_is_installed = improved
if not any(
result.outcome in {"colliding", "partial", "unroutable"}
for result in state.results.values()
@ -1185,7 +1188,9 @@ class PathFinder:
state = self._prepare_state(corridors)
timed_out = self._run_iterations(state, iteration_callback)
self.accumulated_expanded_nodes = list(state.accumulated_expanded_nodes)
self._restore_best_iteration(state)
# A partial iteration or a callback can leave the index out of sync with the saved results.
if timed_out or iteration_callback is not None or not state.best_is_installed:
self._restore_best_iteration(state)
diagnostics = self.context.options.diagnostics
if not timed_out or diagnostics.capture_conflict_trace or diagnostics.capture_pre_pair_frontier_trace:

View file

@ -234,6 +234,32 @@ def test_iteration_callback_observes_reverified_conflicts() -> None:
assert results["vertical"].outcome == "colliding"
@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)
def test_capture_conflict_trace_preserves_route_outputs() -> None:
problem = RoutingProblem(
bounds=(0, 0, 100, 100),
@ -737,6 +763,7 @@ def test_route_all_restores_best_iteration_snapshot(monkeypatch: pytest.MonkeyPa
assert results["netA"].outcome == "completed"
assert results["netB"].outcome == "unroutable"
assert evaluator.collision_engine.has_dynamic_paths()
def test_route_all_restores_best_iteration_snapshot_on_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
@ -771,6 +798,7 @@ def test_route_all_restores_best_iteration_snapshot_on_timeout(monkeypatch: pyte
total_dynamic_collisions=0,
),
)
state.best_is_installed = True
state.results = {"netA": worse_result}
return True
@ -780,6 +808,7 @@ def test_route_all_restores_best_iteration_snapshot_on_timeout(monkeypatch: pyte
results = pathfinder.route_all()
assert results["netA"].outcome == "completed"
assert evaluator.collision_engine.has_dynamic_paths()
def test_capture_conflict_trace_records_restored_best_stage(monkeypatch: pytest.MonkeyPatch) -> None: