inire/examples/07_large_scale_routing.py

120 lines
4.2 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 (
NetSpec,
Port,
RoutingProblem,
RoutingResult,
)
2026-03-30 21:22:20 -07:00
from inire.router._stack import build_routing_stack
2026-03-30 15:32:29 -07:00
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,
)
2026-03-30 21:22:20 -07:00
from inire import CongestionOptions, DiagnosticsOptions, ObjectiveWeights, RoutingOptions, SearchOptions
2026-03-30 15:32:29 -07:00
options = RoutingOptions(
search=SearchOptions(
node_limit=2_000_000,
bend_radii=(50.0,),
sbend_radii=(50.0,),
greedy_h_weight=1.5,
2026-03-30 21:22:20 -07:00
bend_clip_margin=10.0,
2026-03-30 15:32:29 -07:00
),
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,
2026-03-30 21:22:20 -07:00
net_order="shortest",
2026-03-30 15:32:29 -07:00
shuffle_nets=True,
seed=42,
),
diagnostics=DiagnosticsOptions(capture_expanded=True),
)
2026-03-30 21:22:20 -07:00
stack = build_routing_stack(problem, options)
evaluator = stack.evaluator
finder = stack.finder
metrics = finder.metrics
2026-03-30 15:32:29 -07:00
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())
2026-03-30 21:22:20 -07:00
total_nodes = metrics.nodes_expanded
2026-03-30 15:32:29 -07:00
print(f" Iteration {iteration} finished. Successes: {successes}/{len(netlist)}, Collisions: {total_collisions}")
2026-03-30 21:22:20 -07:00
new_greedy = max(1.1, 1.5 - ((iteration + 1) / 10.0) * 0.4)
evaluator.greedy_h_weight = new_greedy
print(f" Adaptive Greedy Weight for Next Iteration: {new_greedy:.3f}")
2026-03-30 15:32:29 -07:00
iteration_stats.append(
{
"Iteration": iteration,
"Success": successes,
"Congestion": total_collisions,
2026-03-30 21:22:20 -07:00
"Nodes": total_nodes,
2026-03-30 15:32:29 -07:00
}
)
2026-03-30 21:22:20 -07:00
metrics.reset_per_route()
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()
2026-03-30 21:22:20 -07:00
results = finder.route_all(iteration_callback=iteration_callback)
2026-03-30 15:32:29 -07:00
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 21:22:20 -07:00
print(f"{'Iter':<5} | {'Success':<8} | {'Congest':<8} | {'Nodes':<10}")
print("-" * 43)
2026-03-30 15:32:29 -07:00
for stats in iteration_stats:
2026-03-30 21:22:20 -07:00
print(f"{stats['Iteration']:<5} | {stats['Success']:<8} | {stats['Congestion']:<8} | {stats['Nodes']:<10}")
2026-03-18 00:06:41 -07:00
2026-03-30 21:22:20 -07:00
success_count = sum(1 for result in results.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 21:22:20 -07:00
for net_id, result in results.items():
2026-03-30 15:32:29 -07:00
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 21:22:20 -07:00
fig, ax = plot_routing_results(results, list(obstacles), bounds, netlist=netlist)
plot_expanded_nodes(list(finder.accumulated_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()