inire/scripts/route_complex_routing_example.py

52 lines
2.6 KiB
Python
Raw Normal View History

"""Route complex-routing example 1 and export verified waypoints."""
from argparse import ArgumentParser
from dataclasses import replace
from json import dumps
from pathlib import Path
from time import perf_counter
from shapely.geometry import box
from inire import TopologyOptions, route
from inire.router._waypoints import export_waypoints
from inire.router.cost import weighted_path_length
from inire.tests.terminal_bench_scenario import LENGTH_COST, terminal_bench_options, terminal_bench_problem
def main() -> None:
parser = ArgumentParser(description=__doc__)
parser.add_argument("--output", type=Path, help="Write the verified waypoint plan to this JSON file.")
parser.add_argument("--reference-lane-plan", action="store_true", help="Replay the manually supplied reference for comparison.")
parser.add_argument("--single-bottom-track", action="store_true", help="Replay the manual reference with one bottom track.")
args = parser.parse_args()
options = terminal_bench_options(max_iterations=1)
options = replace(options, refinement=replace(options.refinement, enabled=False))
started = perf_counter()
if args.reference_lane_plan or args.single_bottom_track:
from inire.tests.terminal_bench_lane_plan import terminal_bench_lane_problem # noqa: PLC0415
problem = terminal_bench_lane_problem(share_bottom_passage=not args.single_bottom_track)
else:
problem = terminal_bench_problem()
options = replace(options, topology=TopologyOptions(mode="bundle", infer_terminal_headings=True))
run = route(problem, options=options)
elapsed = perf_counter() - started
board = box(*problem.bounds)
if run.results_by_net.keys() != {net.net_id for net in problem.nets}:
parser.exit(1, "The route results do not cover the full waypoint plan\n")
for net_id, result in run.results_by_net.items():
if not result.is_valid or any(not board.covers(shape) for shape in result.locked_geometry):
parser.exit(1, f"Lane plan failed inire verification for {net_id}\n")
cost = weighted_path_length((component for result in run.results_by_net.values() for component in result.path), LENGTH_COST)
total = len(problem.nets)
print(f"{total}/{total} verified, benchmark-weighted length {cost:.2f}, {run.metrics.nodes_expanded} A* expansions, {elapsed:.3f} s")
if args.output is not None:
waypoints = {net_id: export_waypoints(result.path) for net_id, result in run.results_by_net.items()}
args.output.write_text(dumps({"nets": {net_id: {"waypoints": path} for net_id, path in waypoints.items()}}, indent=2) + "\n")
if __name__ == "__main__":
main()