20 import matplotlib.pylab
as plt
23 from tabulate
import tabulate
26 def getPaths(results):
28 for result
in results:
30 paths.append(path.path)
34 def getTimes(results):
36 for result
in results:
38 times.append(time.planning_time.nanosec / 1e09 + time.planning_time.sec)
42 def getMapCoordsFromPaths(paths, resolution):
47 for pose
in path.poses:
48 x.append(pose.pose.position.x / resolution)
49 y.append(pose.pose.position.y / resolution)
55 def getPathLength(path):
57 x_prev = path.poses[0].pose.position.x
58 y_prev = path.poses[0].pose.position.y
59 for i
in range(1, len(path.poses)):
60 x_curr = path.poses[i].pose.position.x
61 y_curr = path.poses[i].pose.position.y
62 path_length = path_length + math.sqrt(
63 (x_curr - x_prev) ** 2 + (y_curr - y_prev) ** 2
70 def plotResults(costmap, paths):
71 coords = getMapCoordsFromPaths(paths, costmap.metadata.resolution)
72 data = np.asarray(costmap.data)
73 data.resize(costmap.metadata.size_y, costmap.metadata.size_x)
74 data = np.where(data <= 253, 0, data)
77 ax = sns.heatmap(data, cmap=
'Greys', cbar=
False)
78 for i
in range(0, len(coords), 2):
79 ax.plot(coords[i], coords[i + 1], linewidth=0.7)
81 ax.set_aspect(
'equal',
'box')
85 def averagePathCost(paths, costmap, num_of_planners):
86 coords = getMapCoordsFromPaths(paths, costmap.metadata.resolution)
87 data = np.asarray(costmap.data)
88 data.resize(costmap.metadata.size_y, costmap.metadata.size_x)
90 average_path_costs = []
91 for i
in range(num_of_planners):
92 average_path_costs.append([])
95 for i
in range(0, len(coords), 2):
97 for j
in range(len(coords[i])):
98 costs.append(data[math.floor(coords[i + 1][j])][math.floor(coords[i][j])])
99 average_path_costs[k % num_of_planners].append(sum(costs) / len(costs))
102 return average_path_costs
105 def maxPathCost(paths, costmap, num_of_planners):
106 coords = getMapCoordsFromPaths(paths, costmap.metadata.resolution)
107 data = np.asarray(costmap.data)
108 data.resize(costmap.metadata.size_y, costmap.metadata.size_x)
111 for i
in range(num_of_planners):
112 max_path_costs.append([])
115 for i
in range(0, len(coords), 2):
117 for j
in range(len(coords[i])):
118 cost = data[math.floor(coords[i + 1][j])][math.floor(coords[i][j])]
121 max_path_costs[k % num_of_planners].append(max_cost)
124 return max_path_costs
130 with open(os.getcwd() +
'/results.pickle',
'rb')
as f:
131 results = pickle.load(f)
133 with open(os.getcwd() +
'/planners.pickle',
'rb')
as f:
134 planners = pickle.load(f)
136 with open(os.getcwd() +
'/costmap.pickle',
'rb')
as f:
137 costmap = pickle.load(f)
139 paths = getPaths(results)
143 path_lengths.append(getPathLength(path))
144 path_lengths = np.asarray(path_lengths)
145 total_paths = len(paths)
147 path_lengths.resize((int(total_paths / len(planners)), len(planners)))
148 path_lengths = path_lengths.transpose()
150 times = getTimes(results)
151 times = np.asarray(times)
152 times.resize((int(total_paths / len(planners)), len(planners)))
153 times = np.transpose(times)
156 average_path_costs = np.asarray(averagePathCost(paths, costmap, len(planners)))
157 max_path_costs = np.asarray(maxPathCost(paths, costmap, len(planners)))
163 'Average path length (m)',
170 for i
in range(0, len(planners)):
171 planner_table.append(
174 np.average(path_lengths[i]),
175 np.average(times[i]),
176 np.average(average_path_costs[i]),
177 np.average(max_path_costs[i]),
182 print(tabulate(planner_table))
183 plotResults(costmap, paths)
186 if __name__ ==
'__main__':