77)
88from PathPlanning .TimeBasedPathPlanning .BaseClasses import StartAndGoal
99from PathPlanning .TimeBasedPathPlanning .Node import NodePath
10+ from PathPlanning .TimeBasedPathPlanning .ConstraintTree import AgentId
1011
1112'''
1213Plot a single agent path.
@@ -50,7 +51,7 @@ def PlotNodePath(grid: Grid, start: Position, goal: Position, path: NodePath):
5051'''
5152Plot a series of agent paths.
5253'''
53- def PlotNodePaths (grid : Grid , start_and_goals : list [StartAndGoal ], paths : list [ NodePath ]):
54+ def PlotNodePaths (grid : Grid , start_and_goals : list [StartAndGoal ], paths : dict [ AgentId , NodePath ]):
5455 fig = plt .figure (figsize = (10 , 7 ))
5556
5657 ax = fig .add_subplot (
@@ -64,19 +65,21 @@ def PlotNodePaths(grid: Grid, start_and_goals: list[StartAndGoal], paths: list[N
6465 ax .set_yticks (np .arange (0 , grid .grid_size [1 ], 1 ))
6566
6667 # Plot start and goal positions for each agent
67- colors = [] # generated randomly in loop
68+ colors = {} # generated randomly in loop. Maps agent id to color
6869 markers = ['D' , 's' , '^' , 'o' , 'p' ] # Different markers for visual distinction
6970
7071 # Create plots for start and goal positions
7172 start_and_goal_plots = []
72- for i , path in enumerate (paths ):
73- marker_idx = i % len (markers )
74- agent_id = start_and_goals [i ].agent_id
75- start = start_and_goals [i ].start
76- goal = start_and_goals [i ].goal
73+ for agent_id , path in paths .items ():
74+ marker_idx = agent_id % len (markers )
75+ start_and_goal = next ((elem for elem in start_and_goals if elem .agent_id == agent_id ), None )
76+ if not start_and_goal :
77+ raise RuntimeError (f"Failed to get start and goal for agent { agent_id } " )
78+ start = start_and_goal .start
79+ goal = start_and_goal .goal
7780
7881 color = np .random .rand (3 ,)
79- colors . append ( color )
82+ colors [ agent_id ] = color
8083 sg_plot , = ax .plot ([], [], markers [marker_idx ], c = color , ms = 15 ,
8184 label = f"Agent { agent_id } Start/Goal" )
8285 sg_plot .set_data ([start .x , goal .x ], [start .y , goal .y ])
@@ -86,12 +89,11 @@ def PlotNodePaths(grid: Grid, start_and_goals: list[StartAndGoal], paths: list[N
8689 (obs_points ,) = ax .plot ([], [], "ro" , ms = 15 , label = "Obstacles" )
8790
8891 # Create plots for each agent's path
89- path_plots = []
90- for i , path in enumerate (paths ):
91- agent_id = start_and_goals [i ].agent_id
92- path_plot , = ax .plot ([], [], "o" , c = colors [i ], ms = 10 ,
92+ path_plots = {}
93+ for agent_id , path in paths .items ():
94+ path_plot , = ax .plot ([], [], "o" , c = colors [agent_id ], ms = 10 ,
9395 label = f"Agent { agent_id } Path" )
94- path_plots . append ( path_plot )
96+ path_plots [ agent_id ] = path_plot
9597
9698 ax .legend (bbox_to_anchor = (1.05 , 1 ))
9799
@@ -103,32 +105,32 @@ def PlotNodePaths(grid: Grid, start_and_goals: list[StartAndGoal], paths: list[N
103105 )
104106
105107 # Find the maximum time across all paths
106- max_time = max (path .goal_reached_time () for path in paths )
108+ max_time = max (path .goal_reached_time () for path in paths . values () )
107109
108110 # Animation loop
109- for i in range (0 , max_time + 1 ):
111+ for t in range (0 , max_time + 1 ):
110112 # Update obstacle positions
111- obs_positions = grid .get_obstacle_positions_at_time (i )
113+ obs_positions = grid .get_obstacle_positions_at_time (t )
112114 obs_points .set_data (obs_positions [0 ], obs_positions [1 ])
113115
114116 # Update each agent's position
115- for ( j , path ) in enumerate ( paths ):
117+ for agent_id , path in paths . items ( ):
116118 path_positions = []
117- if i <= path .goal_reached_time ():
118- res = path .get_position (i )
119+ if t <= path .goal_reached_time ():
120+ res = path .get_position (t )
119121 if not res :
120- print (path )
121- print (i )
122- path_position = path .get_position (i )
122+ print (f"Error getting position for agent { agent_id } at time { t } " )
123+ print (t )
124+ path_position = path .get_position (t )
123125 if not path_position :
124- raise Exception (f"Path position not found for time { i } ." )
126+ raise Exception (f"Path position not found for time { t } ." )
125127
126128 # Verify position is valid
127129 assert not path_position in obs_positions
128130 assert not path_position in path_positions
129131 path_positions .append (path_position )
130132
131- path_plots [j ].set_data ([path_position .x ], [path_position .y ])
133+ path_plots [agent_id ].set_data ([path_position .x ], [path_position .y ])
132134
133135 plt .pause (0.2 )
134136
0 commit comments