inire/examples/07_large_scale_routing.py

111 lines
3.6 KiB
Python
Raw Normal View History

2026-03-12 23:50:25 -07:00
import time
2026-03-30 15:32:29 -07:00
2026-03-10 21:55:54 -07:00
from shapely.geometry import box
2026-03-30 15:32:29 -07:00
from inire import (
CongestionOptions,
DiagnosticsOptions,
NetSpec,
ObjectiveWeights,
Port,
RoutingOptions,
RoutingProblem,
RoutingResult,
SearchOptions,
route,
)
from inire.utils.visualization import plot_expanded_nodes, plot_routing_results
2026-03-10 21:55:54 -07:00
def main() -> None:
2026-03-29 01:26:22 -07:00
print("Running Example 07: Fan-Out (10 Nets, 50um Radius)...")
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
bounds = (0, 0, 1000, 1000)
2026-03-10 21:55:54 -07:00
obstacles = [
2026-03-11 09:37:54 -07:00
box(450, 0, 550, 400),
box(450, 600, 550, 1000),
2026-03-10 21:55:54 -07:00
]
num_nets = 10
2026-03-11 09:37:54 -07:00
start_x = 50
start_y_base = 500 - (num_nets * 10.0) / 2.0
end_x = 950
end_y_base = 100
end_y_pitch = 800.0 / (num_nets - 1)
2026-03-10 21:55:54 -07:00
2026-03-30 15:32:29 -07:00
netlist: dict[str, tuple[Port, Port]] = {}
for index in range(num_nets):
start_y = int(round(start_y_base + index * 10.0))
end_y = int(round(end_y_base + index * end_y_pitch))
netlist[f"net_{index:02d}"] = (Port(start_x, start_y, 0), Port(end_x, end_y, 0))
2026-03-10 21:55:54 -07:00
2026-03-30 15:32:29 -07:00
problem = RoutingProblem(
bounds=bounds,
nets=tuple(NetSpec(net_id, start, target, width=2.0) for net_id, (start, target) in netlist.items()),
static_obstacles=tuple(obstacles),
clearance=6.0,
)
options = RoutingOptions(
search=SearchOptions(
node_limit=2_000_000,
bend_radii=(50.0,),
sbend_radii=(50.0,),
greedy_h_weight=1.5,
),
objective=ObjectiveWeights(
unit_length_cost=0.1,
bend_penalty=100.0,
sbend_penalty=400.0,
),
congestion=CongestionOptions(
max_iterations=15,
base_penalty=100.0,
multiplier=1.4,
shuffle_nets=True,
seed=42,
),
diagnostics=DiagnosticsOptions(capture_expanded=True),
)
iteration_stats: list[dict[str, int]] = []
def iteration_callback(iteration: int, current_results: dict[str, RoutingResult]) -> None:
successes = sum(1 for result in current_results.values() if result.is_valid)
total_collisions = sum(result.collisions for result in current_results.values())
print(f" Iteration {iteration} finished. Successes: {successes}/{len(netlist)}, Collisions: {total_collisions}")
iteration_stats.append(
{
"Iteration": iteration,
"Success": successes,
"Congestion": total_collisions,
}
)
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
print(f"Routing {len(netlist)} nets through 200um bottleneck...")
2026-03-30 15:32:29 -07:00
start_time = time.perf_counter()
run = route(problem, options=options, iteration_callback=iteration_callback)
end_time = time.perf_counter()
2026-03-18 00:06:41 -07:00
2026-03-30 15:32:29 -07:00
print(f"Routing took {end_time - start_time:.4f}s")
2026-03-16 17:05:16 -07:00
print("\n--- Iteration Summary ---")
2026-03-30 15:32:29 -07:00
print(f"{'Iter':<5} | {'Success':<8} | {'Congest':<8}")
print("-" * 30)
for stats in iteration_stats:
print(f"{stats['Iteration']:<5} | {stats['Success']:<8} | {stats['Congestion']:<8}")
2026-03-18 00:06:41 -07:00
2026-03-30 15:32:29 -07:00
success_count = sum(1 for result in run.results_by_net.values() if result.is_valid)
2026-03-16 17:05:16 -07:00
print(f"\nFinal: Routed {success_count}/{len(netlist)} nets successfully.")
2026-03-30 15:32:29 -07:00
for net_id, result in run.results_by_net.items():
if not result.is_valid:
print(f" FAILED: {net_id}, collisions={result.collisions}")
2026-03-12 23:50:25 -07:00
else:
2026-03-30 15:32:29 -07:00
print(f" {net_id}: SUCCESS")
2026-03-18 00:06:41 -07:00
2026-03-30 15:32:29 -07:00
fig, ax = plot_routing_results(run.results_by_net, list(obstacles), bounds, netlist=netlist)
plot_expanded_nodes(list(run.expanded_nodes), ax=ax)
2026-03-10 21:55:54 -07:00
fig.savefig("examples/07_large_scale_routing.png")
print("Saved plot to examples/07_large_scale_routing.png")
2026-03-30 15:32:29 -07:00
2026-03-10 21:55:54 -07:00
if __name__ == "__main__":
main()