| """ |
| Demo 3: Visualization |
| |
| Purpose: Demonstrate all visualization capabilities |
| Difficulty: Beginner |
| Prerequisites: Understanding of demo 01_quickstart.py |
| |
| This demo shows how to create beautiful visualizations of your quantum pathfinding |
| results, including static plots, step-by-step animations, and custom images. |
| """ |
|
|
| import sys |
| from pathlib import Path |
| sys.path.append(str(Path(__file__).parent.parent)) |
|
|
| |
| import quantum.visualizer as visualizer |
| from quantum.pathFormulation import PathfindingProblem |
| from quantum.builder import GraphQUBO |
| from quantum.solvers import SolverFactory |
| import quantum.config.parser as config_parser |
| from quantum.utils.logger import set_verbose_level |
|
|
| def main(): |
| print("=" * 70) |
| print("Demo 3: Visualization - Creating Beautiful Plots") |
| print("=" * 70) |
| |
| |
| base_path = Path(__file__).parent.parent |
| |
| |
| print("\n[1/3] Solving a pathfinding problem...") |
| |
| config_path = base_path / "config/config.yaml" |
| penalty_sets = config_parser.load_config(str(config_path), sections=["penalty_sets"]) |
| penalties_conf = penalty_sets["penalty_sets"]["crash"] |
| |
| |
| set_verbose_level(1) |
|
|
| map_path = base_path / "maps/synthetic/3x3/obs3x3_standard" |
| |
| |
| materials_path = base_path / "config/materials.yaml" |
| materials_data = config_parser.load_config(str(materials_path))["materials"] |
| |
| problem = PathfindingProblem.from_map_config( |
| str(map_path), |
| problem_name="baseline", |
| materials_data=materials_data |
| ) |
| |
| print(f" β Map: {problem.grid.M}x{problem.grid.N}") |
| print(f" β Obstacles: {problem.grid.obstacles}") |
| |
| p_graph = problem.as_graph_only() |
| builder = GraphQUBO(p_graph, penalties=penalties_conf, name="viz_demo") |
| |
| solver = SolverFactory.create_solver(solver="dwave", normalize_scale=4, num_reads=10) |
|
|
| solution = solver.solve(builder) |
| |
| raw_path = solver.decode_path(solution["solution"], p_graph) |
| |
| |
| robot_paths = solver.get_robot_paths(raw_path) |
| path = list(robot_paths.values())[0] if robot_paths else [] |
| |
| print(f" β Path: {path}") |
| print(f" β Path found: {len(path)} steps") |
| |
| |
| print("\n[2/3] Creating visualizer...") |
| |
| grid_size = (problem.grid.M, problem.grid.N) |
| start = problem.robots["Lucia"].current_position |
| goal = problem.robots["Lucia"].goal |
| obstacles = problem.grid.obstacles |
| |
| |
| viz = visualizer.QuantumRoboticsVisualizer( |
| grid_size, |
| title="Quantum Pathfinding - 3x3 Grid" |
| |
| ) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| print(" β Visualizer created") |
| |
| |
| print("\n[3/3] Generating plots...") |
| |
| |
| print("\n Creating static plot (current position at step 3)...") |
| static_fig = viz.create_static_plot( |
| obstacles=obstacles, |
| path=path, |
| start=start, |
| goal=goal, |
| current_step=3, |
| problem=problem |
| ) |
| |
| |
| viz.write_html(static_fig, "visualization_static.html") |
| viz.show(static_fig) |
| print(" β Saved to: visualization_static.html") |
| |
| |
| try: |
| viz.write_image(static_fig, "output/static_path.png") |
| print(" β Saved to: output/static_path.png") |
| except Exception as e: |
| print(f" β Could not save PNG: {e}") |
| print(" Install kaleido: pip install kaleido") |
| |
| |
| print("\n Creating step-by-step plot (all timesteps)...") |
| step_fig = viz.create_step_by_step_plot( |
| obstacles=obstacles, |
| path=path, |
| start=start, |
| goal=goal, |
| problem=problem |
| ) |
| |
| |
| viz.write_html(step_fig, "output/step_by_step_path.html") |
| print(" β Saved to: output/step_by_step_path.html") |
| viz.show(step_fig) |
| |
| |
| try: |
| viz.write_image(step_fig, "output/step_by_step_path.png", width=1200, height=800) |
| print(" β Saved to: output/step_by_step_path.png") |
| except Exception as e: |
| print(f" β Could not save PNG: {e}") |
|
|
| |
| print("\n Creating animated plot (play/pause + time slider)...") |
| |
| |
| |
| anim_fig = viz.create_animated_plot( |
| obstacles=obstacles, |
| path=path, |
| start=start, |
| goal=goal, |
| problem=problem |
| ) |
| viz.write_html(anim_fig, "output/animated_path.html") |
| viz.show(anim_fig) |
| print(" β Saved to: output/animated_path.html") |
|
|
| |
| print("\n Exporting animation as GIF (requires kaleido + pillow)...") |
| viz.write_gif(anim_fig, "output/animated_path.gif", timestep_duration=600) |
| |
| |
| |
| |
| |
| print("\n" + "=" * 70) |
| print("β
Demo complete!") |
| print("\nVisualization files created:") |
| print(" - visualization_static.html (open in browser)") |
| print(" - output/step_by_step_path.html (open in browser)") |
| print(" - output/animated_path.html (play/pause + time slider)") |
| print(" - output/animated_path.gif (shareable animation)") |
| print("\nVisualization features:") |
| print(" β Interactive hover to see robot ID and timestep") |
| print(" β Custom images for robots and obstacles") |
| print(" β Terrain background rendering") |
| print(" β Step-by-step path evolution") |
| print(" β Animated timeline (smooth or discrete) + GIF export") |
| print("\nNext steps:") |
| print(" - Open the HTML files in your browser to explore") |
| print(" - Try demo 04_multi_robot.py for multi-agent visualization") |
| print(" - Customize with your own images!") |
| print("=" * 70) |
|
|
| if __name__ == "__main__": |
| |
| import os |
| os.makedirs("output", exist_ok=True) |
| |
| main() |
|
|