-
Notifications
You must be signed in to change notification settings - Fork 23
/
plot.py
134 lines (107 loc) · 4.38 KB
/
plot.py
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
# -*- coding: utf-8 -*-
import json
import matplotlib.pyplot as plt
import sys
from utils.color_list import color_list
# Very simple plot for a VROOM solution file.
TASKS_TYPES = ["job", "pickup", "delivery"]
def plot_routes(solution, plot_base_name):
fig, ax1 = plt.subplots(1, 1)
fig.set_figwidth(15)
plt.subplots_adjust(left=0.03, right=1, top=1, bottom=0.05, wspace=0.03)
if "routes" not in solution:
return
first_start = solution["routes"][0]["steps"][0]["location"]
first_end = solution["routes"][0]["steps"][-1]["location"]
xmin = min(first_start[0], first_end[0])
xmax = xmin
ymin = min(first_start[1], first_end[1])
ymax = ymin
vehicles_have_same_start_end = True
for route in solution["routes"]:
current_start = route["steps"][0]["location"]
current_end = route["steps"][-1]["location"]
if current_start != first_start or current_end != first_end:
vehicles_have_same_start_end = False
break
for route in solution["routes"]:
lons = [
step["location"][0]
for step in route["steps"]
if not vehicles_have_same_start_end or step["type"] in TASKS_TYPES
]
lats = [
step["location"][1]
for step in route["steps"]
if not vehicles_have_same_start_end or step["type"] in TASKS_TYPES
]
ax1.plot(lons, lats, color=color_list[route["vehicle"] % len(color_list)])
xmin = min(xmin, min(lons))
xmax = max(xmax, max(lons))
ymin = min(ymin, min(lats))
ymax = max(ymax, max(lats))
step = route["steps"][-1]
if step["type"] == "end":
ax1.scatter(
[step["location"][0]], [step["location"][1]], color="red", linewidth=8
)
step = route["steps"][0]
if step["type"] == "start":
ax1.scatter(
[step["location"][0]], [step["location"][1]], color="green", linewidth=1
)
for step in route["steps"]:
if step["type"] == "job":
marker_shape = "o"
marker_color = "blue"
elif step["type"] == "pickup":
marker_shape = "^"
marker_color = "red"
elif step["type"] == "delivery":
marker_shape = "v"
marker_color = "green"
else:
continue
ax1.scatter(
[step["location"][0]],
[step["location"][1]],
facecolor="none",
edgecolor=marker_color,
marker=marker_shape,
linewidth=0.7,
)
if "unassigned" in solution and len(solution["unassigned"]) > 0:
unassigned_lons = [u["location"][0] for u in solution["unassigned"]]
unassigned_lats = [u["location"][1] for u in solution["unassigned"]]
ax1.scatter(unassigned_lons, unassigned_lats, marker="x", color="red", s=100)
xmin = min(xmin, min(unassigned_lons))
xmax = max(xmax, max(unassigned_lons))
ymin = min(ymin, min(unassigned_lats))
ymax = max(ymax, max(unassigned_lats))
computing_time = solution["summary"]["computing_times"]["loading"]
computing_time += solution["summary"]["computing_times"]["solving"]
if "routing" in solution["summary"]["computing_times"]:
computing_time += solution["summary"]["computing_times"]["routing"]
# Handle margins.
size_factor = max((xmax - xmin) / 100, (ymax - ymin) / 100)
margin_delta = 3 * size_factor
title = plot_base_name
title += " ; cost: " + str(solution["summary"]["cost"])
title += " ; computing time: " + str(computing_time)
title += "ms"
ax1.set_title(title)
ax1.set_xlim(xmin - margin_delta, xmax + margin_delta)
ax1.set_ylim(ymin - margin_delta, ymax + margin_delta)
ax1.set_aspect("equal")
print("Plotting file " + plot_base_name + ".svg")
plt.savefig(plot_base_name + ".svg", bbox_inches="tight")
plt.close()
# plt.show()
if __name__ == "__main__":
# Argument are the name of the solution files to plot.
for sol_file_name in sys.argv[1:]:
plot_base_name = sol_file_name[0 : sol_file_name.rfind(".json")]
print("Parsing " + sol_file_name)
with open(sol_file_name, "r") as sol_file:
solution = json.load(sol_file)
plot_routes(solution, plot_base_name)