-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_runner.py
More file actions
442 lines (370 loc) · 15.1 KB
/
benchmark_runner.py
File metadata and controls
442 lines (370 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""
Path Planner Comparison Benchmark
Runs Dijkstra, A*, RRT, RRT*, and PRM across multiple obstacle scenarios,
collects metrics (path length, planning time, explored nodes), and produces
comparison visualizations and animations.
Usage:
python Compare_planner.py
python Compare_planner.py --show
python Compare_planner.py --animate
python Compare_planner.py --show-animation
"""
import argparse
import csv
import inspect
import os
import shutil
from contextlib import contextmanager, nullcontext
from matplotlib.animation import FuncAnimation, PillowWriter
import matplotlib.pyplot as plt
import numpy as np
from APF import APFPlanner
from Astar import AStarPlanner
from BSpline import BSplinePlanner
from Bezier import BezierPlanner
from common import PlannerConfig, build_scenario_obstacles, calc_mean_heading_change, calc_path_length, format_table
from Dijkstra import DijkstraPlanner
from Dubins import DubinsPlanner
from DWA import DWAPlanner
from Frenet import FrenetPlanner
from HybridAstar import HybridAStarPlanner
from Lattice import LatticePlanner
from PRM import PRMPlanner
from ReedsShepp import ReedsSheppPlanner
from RRT import RRTPlanner
from RRT_Star import RRTStarPlanner
COLORS = {
"Dijkstra": "tab:red",
"A*": "tab:blue",
"Hybrid A*": "tab:orange",
"RRT": "tab:green",
"RRT*": "tab:purple",
"PRM": "tab:brown",
"Bezier": "tab:pink",
"B-Spline": "tab:gray",
"Dubins": "goldenrod",
"Reeds-Shepp": "olive",
"Frenet": "teal",
"Lattice": "darkcyan",
"APF": "crimson",
"DWA": "navy",
}
PLANNERS = [
("Dijkstra", DijkstraPlanner),
("A*", AStarPlanner),
("Hybrid A*", HybridAStarPlanner),
("RRT", RRTPlanner),
("RRT*", RRTStarPlanner),
("PRM", PRMPlanner),
("Bezier", BezierPlanner),
("B-Spline", BSplinePlanner),
("Dubins", DubinsPlanner),
("Reeds-Shepp", ReedsSheppPlanner),
("Frenet", FrenetPlanner),
("Lattice", LatticePlanner),
("APF", APFPlanner),
("DWA", DWAPlanner),
]
def _slugify(name):
return name.lower().replace(" ", "_").replace("*", "_star")
def _prepare_output_dir(output_dir):
if os.path.isdir(output_dir):
shutil.rmtree(output_dir)
os.makedirs(output_dir, exist_ok=True)
@contextmanager
def _suppress_interactive_windows():
was_interactive = plt.isinteractive()
plt.ioff()
try:
yield
finally:
if was_interactive:
plt.ion()
def _scenario_label(scenario_name):
labels = {
"corridor": "Corridor",
"scattered": "Scattered",
"narrow_passage": "Narrow Passage",
}
return labels.get(scenario_name, scenario_name)
def _draw_scene(axis, scenario_data):
ox = scenario_data["ox"]
oy = scenario_data["oy"]
sx, sy = scenario_data["sx"], scenario_data["sy"]
gx, gy = scenario_data["gx"], scenario_data["gy"]
axis.plot(ox, oy, ".k", markersize=1.5, label="Obstacles")
axis.plot(sx, sy, "og", markersize=8, label="Start")
axis.plot(gx, gy, "sr", markersize=8, label="Goal")
axis.set_xlabel("X [m]")
axis.set_ylabel("Y [m]")
axis.set_aspect("equal")
axis.grid(True, alpha=0.3)
def _run_planner(planner, sx, sy, gx, gy, ox, oy, scenario_data):
signature = inspect.signature(planner.plan)
kwargs = {}
if "scenario" in signature.parameters:
kwargs["scenario"] = scenario_data
return planner.plan(sx, sy, gx, gy, ox, oy, **kwargs)
def plot_overlay_comparison(scenario_name, scenario_data, scenario_results, output_path, show=False):
context = nullcontext() if show else _suppress_interactive_windows()
with context:
figure, axis = plt.subplots(figsize=(8.2, 6.6))
_draw_scene(axis, scenario_data)
for planner_name, result in scenario_results:
color = COLORS.get(planner_name, "tab:gray")
if result.success:
axis.plot(result.path_x, result.path_y, color=color, linewidth=2.2, label=planner_name)
else:
axis.plot([], [], color=color, linewidth=2.2, label=f"{planner_name} (fail)")
axis.set_title(f"{_scenario_label(scenario_name)} - All Planners")
axis.legend(frameon=False, ncol=4, loc="best", fontsize=8)
figure.tight_layout()
figure.savefig(output_path, dpi=180, bbox_inches="tight")
if show:
return figure
plt.close(figure)
return None
def animate_overlay_comparison(
scenario_name,
scenario_data,
scenario_results,
output_path=None,
show=False,
fps=15,
):
context = nullcontext() if show else _suppress_interactive_windows()
with context:
figure, axis = plt.subplots(figsize=(8.2, 6.6))
max_points = max((len(result.path_x) for _, result in scenario_results if result.success), default=1)
frame_count = max(45, max_points * 2)
progress = np.linspace(0.0, 1.0, frame_count)
def _draw_frame(frame_no):
axis.clear()
_draw_scene(axis, scenario_data)
frac = float(progress[frame_no])
for planner_name, result in scenario_results:
color = COLORS.get(planner_name, "tab:gray")
if not result.success or not result.path_x:
axis.plot([], [], color=color, linewidth=2.0, label=f"{planner_name} (fail)")
continue
point_count = max(1, int(np.ceil(frac * len(result.path_x))))
point_count = min(point_count, len(result.path_x))
axis.plot(
result.path_x[:point_count],
result.path_y[:point_count],
color=color,
linewidth=2.2,
label=planner_name,
)
axis.scatter(
[result.path_x[point_count - 1]],
[result.path_y[point_count - 1]],
color=color,
s=22,
)
axis.set_title(
f"{_scenario_label(scenario_name)} - All Planners Live ({int(frac * 100):d}%)"
)
axis.legend(frameon=False, ncol=4, loc="best", fontsize=8)
animation = FuncAnimation(
figure,
_draw_frame,
frames=frame_count,
interval=max(40, int(1000 / fps)),
repeat=False,
)
if output_path:
animation.save(output_path, writer=PillowWriter(fps=fps))
if show:
return figure, animation
plt.close(figure)
return None, None
def run_benchmark(output_dir="outputs_planning", show=False, make_animation=False, show_animation=False):
_prepare_output_dir(output_dir)
scenarios = build_scenario_obstacles()
config = PlannerConfig(resolution=1.0, robot_radius=2.0)
all_results = []
scenario_result_map = {}
overlay_dir = os.path.join(output_dir, "overlays")
animation_dir = os.path.join(output_dir, "animations")
os.makedirs(overlay_dir, exist_ok=True)
if make_animation:
os.makedirs(animation_dir, exist_ok=True)
figures = []
animation_figures = []
for scenario_name, scenario_data in scenarios.items():
print(f"\n{'=' * 60}")
print(f"Scenario: {_scenario_label(scenario_name)}")
print(f"{'=' * 60}")
ox = scenario_data["ox"]
oy = scenario_data["oy"]
sx, sy = scenario_data["sx"], scenario_data["sy"]
gx, gy = scenario_data["gx"], scenario_data["gy"]
scenario_results = []
for planner_name, planner_cls in PLANNERS:
print(f" Running {planner_name}...", end=" ", flush=True)
planner = planner_cls(config=config)
result = _run_planner(planner, sx, sy, gx, gy, ox, oy, scenario_data)
if result.success and result.path_x:
result.path_length = calc_path_length(result.path_x, result.path_y)
result.smoothness = calc_mean_heading_change(result.path_x, result.path_y)
all_results.append((scenario_name, result))
scenario_results.append((planner_name, result))
status = "OK" if result.success else "FAIL"
print(
f"{status} | Length={result.path_length:.1f}m | "
f"Time={result.planning_time * 1000:.1f}ms | Nodes={result.explored_nodes}"
)
scenario_result_map[scenario_name] = scenario_results
overlay_path = os.path.join(output_dir, f"{_slugify(scenario_name)}_comparison.png")
overlay_figure = plot_overlay_comparison(
scenario_name,
scenario_data,
scenario_results,
overlay_path,
show=show,
)
if overlay_figure is not None:
figures.append(overlay_figure)
overlay_copy_path = os.path.join(overlay_dir, f"{_slugify(scenario_name)}_all_planners.png")
plot_overlay_comparison(
scenario_name,
scenario_data,
scenario_results,
overlay_copy_path,
show=False,
)
if make_animation or show_animation:
animation_path = None
if make_animation:
animation_path = os.path.join(animation_dir, f"{_slugify(scenario_name)}_all_planners.gif")
animation_figure, _ = animate_overlay_comparison(
scenario_name,
scenario_data,
scenario_results,
output_path=animation_path,
show=show_animation,
)
if animation_figure is not None:
animation_figures.append(animation_figure)
_plot_trajectory_overview(scenarios, scenario_result_map, output_dir, show)
_plot_summary(all_results, scenarios, output_dir, show)
print(f"\n{'=' * 60}")
print("Summary")
print(f"{'=' * 60}")
planning_results = [result for _, result in all_results]
print(format_table(planning_results))
_save_csv(all_results, output_dir)
if show and figures:
plt.show()
for figure in figures:
plt.close(figure)
if show_animation and animation_figures:
plt.show()
for figure in animation_figures:
plt.close(figure)
print(f"\nResults saved to {output_dir}/")
print(f" - Static comparisons: {len(scenarios)}")
print(f" - Overlay snapshots: {overlay_dir}")
if make_animation:
print(f" - Scenario GIFs: {animation_dir}")
def _plot_trajectory_overview(scenarios, scenario_result_map, output_dir, show):
context = nullcontext() if show else _suppress_interactive_windows()
with context:
figure, axes = plt.subplots(1, len(scenarios), figsize=(18, 5.6))
if len(scenarios) == 1:
axes = [axes]
for axis, (scenario_name, scenario_data) in zip(axes, scenarios.items()):
_draw_scene(axis, scenario_data)
for planner_name, result in scenario_result_map[scenario_name]:
color = COLORS.get(planner_name, "tab:gray")
if result.success:
axis.plot(result.path_x, result.path_y, color=color, linewidth=1.8, label=planner_name)
axis.set_title(_scenario_label(scenario_name))
axis.legend(frameon=False, ncol=2, fontsize=7, loc="best")
figure.suptitle("Trajectory Overview Across Scenarios", fontsize=15, y=1.02)
figure.tight_layout()
figure.savefig(os.path.join(output_dir, "trajectory_overview.png"), dpi=160, bbox_inches="tight")
if show:
plt.show()
plt.close(figure)
def _plot_summary(all_results, scenarios, output_dir, show):
scenario_names = list(scenarios.keys())
planner_names = [name for name, _ in PLANNERS]
metrics = {
"Path Length (m)": lambda result: result.path_length if result.success else 0.0,
"Mean Heading Change (rad)": lambda result: result.smoothness if result.success else 0.0,
"Planning Time (ms)": lambda result: result.planning_time * 1000.0,
"Explored Nodes": lambda result: result.explored_nodes,
}
for metric_name, metric_fn in metrics.items():
context = nullcontext() if show else _suppress_interactive_windows()
with context:
figure, axis = plt.subplots(figsize=(10, 6))
x_axis = np.arange(len(scenario_names))
width = max(0.05, 0.84 / max(len(planner_names), 1))
offsets = np.linspace(
-width * (len(planner_names) - 1) / 2.0,
width * (len(planner_names) - 1) / 2.0,
len(planner_names),
)
for i, planner_name in enumerate(planner_names):
values = []
for scenario_name in scenario_names:
for stored_scenario, result in all_results:
if stored_scenario == scenario_name and result.planner == planner_name:
values.append(metric_fn(result))
break
axis.bar(
x_axis + offsets[i],
values,
width,
label=planner_name,
color=COLORS.get(planner_name, "tab:gray"),
)
axis.set_xlabel("Scenario")
axis.set_ylabel(metric_name)
axis.set_title(f"Planner Comparison - {metric_name}")
axis.set_xticks(x_axis)
axis.set_xticklabels([_scenario_label(name) for name in scenario_names])
axis.legend(frameon=False, ncol=4, fontsize=8)
axis.grid(True, axis="y", alpha=0.3)
figure.tight_layout()
slug = metric_name.lower().replace(" ", "_").replace("(", "").replace(")", "")
figure.savefig(os.path.join(output_dir, f"summary_{slug}.png"), dpi=150, bbox_inches="tight")
if show:
plt.show()
plt.close(figure)
def _save_csv(all_results, output_dir):
path = os.path.join(output_dir, "results.csv")
fieldnames = [
"Scenario",
"Planner",
"Success",
"Path Length (m)",
"Mean Heading Change (rad)",
"Planning Time (ms)",
"Explored Nodes",
]
with open(path, "w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for scenario_name, result in all_results:
writer.writerow({"Scenario": _scenario_label(scenario_name), **result.to_dict()})
def build_compare_arg_parser():
parser = argparse.ArgumentParser(description="Path Planner Comparison Benchmark")
parser.add_argument("--output-dir", default="outputs_planning", help="Output directory")
parser.add_argument("--show", action="store_true", help="Display figures interactively")
parser.add_argument("--animate", action="store_true", help="Save GIF animations")
parser.add_argument("--show-animation", action="store_true", help="Display animations interactively")
return parser
def main():
args = build_compare_arg_parser().parse_args()
run_benchmark(
output_dir=args.output_dir,
show=args.show,
make_animation=args.animate,
show_animation=args.show_animation,
)
if __name__ == "__main__":
main()