inire/examples/09_unroutable_best_effort.py

59 lines
2 KiB
Python
Raw Normal View History

2026-03-10 21:55:54 -07:00
from inire.geometry.collision import CollisionEngine
from inire.geometry.primitives import Port
from inire.router.astar import AStarRouter
from inire.router.cost import CostEvaluator
from inire.router.danger_map import DangerMap
2026-03-11 09:37:54 -07:00
from inire.router.pathfinder import PathFinder
2026-03-10 21:55:54 -07:00
from inire.utils.visualization import plot_routing_results
from shapely.geometry import box
def main() -> None:
2026-03-11 09:37:54 -07:00
print("Running Example 09: Best-Effort (Unroutable Net)...")
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
# 1. Setup Environment
2026-03-10 21:55:54 -07:00
bounds = (0, 0, 100, 100)
engine = CollisionEngine(clearance=2.0)
2026-03-11 09:37:54 -07:00
# Create a 'cage' that completely blocks the target
cage = [
box(70, 30, 75, 70), # Left wall
box(70, 70, 95, 75), # Top wall
box(70, 25, 95, 30), # Bottom wall
]
for obs in cage:
engine.add_static_obstacle(obs)
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
danger_map = DangerMap(bounds=bounds)
danger_map.precompute(cage)
2026-03-21 12:05:06 -07:00
evaluator = CostEvaluator(engine, danger_map, bend_penalty=50.0, sbend_penalty=150.0)
2026-03-11 09:37:54 -07:00
# Use a low node limit to fail faster
2026-03-21 12:05:06 -07:00
router = AStarRouter(evaluator, node_limit=2000, snap_size=1.0, bend_radii=[10.0])
2026-03-11 09:37:54 -07:00
# Enable partial path return
pf = PathFinder(router, evaluator)
# 2. Define Netlist: start outside, target inside the cage
netlist = {
"trapped_net": (Port(10, 50, 0), Port(85, 50, 0)),
}
net_widths = {"trapped_net": 2.0}
# 3. Route
print("Routing net into a cage (should fail and return partial)...")
results = pf.route_all(netlist, net_widths)
# 4. Check Results
res = results["trapped_net"]
if not res.is_valid:
print(f"Net failed to route as expected. Partial path length: {len(res.path)} segments.")
else:
print("Wait, it found a way in? Check the cage geometry!")
# 5. Visualize
fig, ax = plot_routing_results(results, cage, bounds, netlist=netlist)
2026-03-10 21:55:54 -07:00
fig.savefig("examples/09_unroutable_best_effort.png")
print("Saved plot to examples/09_unroutable_best_effort.png")
if __name__ == "__main__":
main()