-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.py
359 lines (281 loc) · 14.1 KB
/
src.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
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
# Classes for the model, vehicle, ports and hub
# Unit of analysis: minute
import matplotlib.pyplot as plt
import numpy as np
import copy, os
import imageio.v2 as imageio
class Vehicle():
def __init__(self, id, location, max_energy, recharge_rate, discharge_rate):
self.id = id
self.location = location
self.max_energy = max_energy # units
self.avail_energy = copy.deepcopy(max_energy) # units
self.recharge_rate = recharge_rate # units/min
self.discharge_rate = discharge_rate # units/min
class Hub():
def __init__(self, location, hover_location, turnaround_time):
self.location = location
self.hover_location = hover_location
self.num_pads = location.shape[0]
self.turnaround_time = turnaround_time
class Port():
def __init__(self, location, hover_location, num_pads, turnaround_time, time_to_hub):
self.location = location
self.hover_location = hover_location
self.num_pads = num_pads
self.turnaround_time = turnaround_time
self.time_to_hub = time_to_hub
class Model():
def __init__(self, hub, ports, vehicles):
"""
Method to initialize the model
Parameters
----------
hub : class
Hub class
ports : list
List of port classes
vehicles : list
List of vehicle classes
"""
# Instance variables
self.hub = hub
self.ports = ports
self.vehicles = vehicles
# Assigning the initial variables for the hub
self.hub.avail_pads = np.linspace(1, self.hub.location.shape[0], self.hub.location.shape[0], dtype=int).tolist()
self.hub.takeoff = 0
self.hub.land = 0
self.hub.queue = []
# Assigning the initial variables for the ports
for port in self.ports:
port.avail_pads = [] # Since vehicles are occupying the pads
port.takeoff = 0
port.land = 0
port.queue = []
# Assigning the initial variables for the vehicles
for vehicle in self.vehicles:
vehicle.destination = None
vehicle.pad = 1
vehicle.wait_time = 0
vehicle.status = 'ground'
vehicle.trip = 0
vehicle.travel_time = self.ports[vehicle.id].time_to_hub
vehicle.dx = (self.hub.hover_location[vehicle.id,0] - vehicle.location[0])/vehicle.travel_time
vehicle.dy = (self.hub.hover_location[vehicle.id,1] - vehicle.location[1])/vehicle.travel_time
def run(self, iterations):
"""
Method to run the simulation
Parameters
----------
iterations : int
Number of iterations
"""
# Creating directory for storing the results
self.directory = f'output_{self.hub.num_pads}_{self.hub.turnaround_time}_{self.vehicles[0].recharge_rate}'
if not os.path.isdir(self.directory):
os.system("mkdir {}".format(self.directory))
else:
os.system("rm -r {}".format(self.directory))
os.system("mkdir {}".format(self.directory))
images = []
# Simlulation loop
for i in range(iterations+1):
# Plotting
self.plot(i)
# Images
images.append(imageio.imread(self.directory+'/iteration'+str(i+1)+'.png'))
if np.min([vehicle.avail_energy for vehicle in self.vehicles]) < 0:
print('Energy level is below 0. Simulation stopped at iteration: {}'.format(i))
break
# Vehicle position updated asynchronusly
for vehicle in self.vehicles:
# Vehicle is on the ground and ready to liftoff
if vehicle.status == 'ground' and vehicle.wait_time == 0:
# Only takeoff if vehicle has enough energy for round trip
if isinstance(vehicle.destination, Hub) and vehicle.avail_energy < 3.0*self.ports[vehicle.id].time_to_hub:
vehicle.avail_energy = min(vehicle.avail_energy + vehicle.recharge_rate, vehicle.max_energy)
else:
if isinstance(vehicle.destination, Hub):
# Updating the hub variables
self.hub.takeoff += 1
self.hub.avail_pads.append(vehicle.pad)
# Takeoff to hover location
vehicle.location[0] = self.hub.hover_location[vehicle.id,0]
vehicle.location[1] = self.hub.hover_location[vehicle.id,1]
# Setting the next destination
vehicle.destination = self.ports[vehicle.id]
vehicle.travel_time = self.ports[vehicle.id].time_to_hub
vehicle.dx = (vehicle.destination.hover_location[0] - vehicle.location[0])/vehicle.travel_time
vehicle.dy = (vehicle.destination.hover_location[1] - vehicle.location[1])/vehicle.travel_time
else:
# Updating the port variables
self.ports[vehicle.id].takeoff += 1
self.ports[vehicle.id].avail_pads.append(vehicle.pad)
# Takeoff to hover location
vehicle.location[0] = self.ports[vehicle.id].hover_location[0]
vehicle.location[1] = self.ports[vehicle.id].hover_location[1]
# Setting the next destination
vehicle.destination = self.hub
vehicle.travel_time = self.ports[vehicle.id].time_to_hub
vehicle.dx = (vehicle.destination.hover_location[vehicle.id,0] - vehicle.location[0])/vehicle.travel_time
vehicle.dy = (vehicle.destination.hover_location[vehicle.id,1] - vehicle.location[1])/vehicle.travel_time
vehicle.avail_energy -= vehicle.discharge_rate/2 # discharge rate is half when taking off
vehicle.status = 'flight'
# Vehicle is on the ground until turnaround time
elif vehicle.status == 'ground' and vehicle.wait_time > 0:
vehicle.wait_time -= 1 # decrementing the wait time
# Recharge the vehicle if it is at the hub
if isinstance(vehicle.destination, Hub) and vehicle.avail_energy < vehicle.max_energy:
vehicle.avail_energy = min(vehicle.avail_energy + vehicle.recharge_rate, vehicle.max_energy)
# Vehicle has reached the destination's hover location
elif vehicle.status == 'flight' and vehicle.travel_time == 0:
vehicle.avail_energy -= vehicle.discharge_rate/2 # discharge rate is half when hovering/landing
# Adding to the queue
if vehicle.id not in vehicle.destination.queue:
vehicle.destination.queue.append(vehicle.id)
# Land if a pad is available and next in queue or else hover
if len(vehicle.destination.avail_pads) > 0 and vehicle.id == vehicle.destination.queue[0]:
vehicle.status = 'ground'
vehicle.trip += 1
vehicle.wait_time = vehicle.destination.turnaround_time
vehicle.destination.land += 1
vehicle.destination.queue.remove(vehicle.id)
vehicle.pad = vehicle.destination.avail_pads.pop(0) # Removing the pad number from the available pads and assign to vehicle
# Moving vehicle to assigned pad
if isinstance(vehicle.destination, Hub):
vehicle.location[0] = vehicle.destination.location[vehicle.pad-1,0]
vehicle.location[1] = vehicle.destination.location[vehicle.pad-1,1]
else:
vehicle.location[0] = vehicle.destination.location[0]
vehicle.location[1] = vehicle.destination.location[1]
# Vehicle is flying towards destination
elif vehicle.status == 'flight' and vehicle.travel_time > 0:
vehicle.location[0] += vehicle.dx
vehicle.location[1] += vehicle.dy
vehicle.travel_time -= 1
vehicle.avail_energy -= vehicle.discharge_rate
imageio.mimsave(self.directory+'/animation.gif', images, format='.gif', fps=2.0, loop=0)
def plot(self, i=None):
"""
Method to plot all the entities within simulation
"""
color = ['r', 'b', 'g', 'm', 'k', 'c']
pad_radius = 0.3
hub_pad_radius = 0.4
fig, ax = plt.subplots(figsize=(8, 8))
# Plotting the hub
for index in range(self.hub.num_pads):
pad = self.hub.location[index,:]
circle = plt.Circle((pad[0], pad[1]), hub_pad_radius, fill=False)
ax.add_patch(circle)
ax.annotate(index+1, (pad[0], pad[1]), va="center", ha="center", fontsize=14)
circle = plt.Circle((0, 0), self.hub.hover_location[0,0]+0.15, fill=False, linestyle='--')
ax.add_patch(circle)
ax.annotate('HUB', (0, 0), va="center", ha="center", fontsize=14, fontweight='bold')
# Plotting the ports
for index, port in enumerate(self.ports):
circle = plt.Circle((port.location[0], port.location[1]), pad_radius, fill=True, color=color[index], alpha=0.2)
ax.add_patch(circle)
ax.annotate(index+1, (port.location[0], port.location[1]), va="center", ha="center", fontsize=14)
ax.plot(self.hub.hover_location[index,0], self.hub.hover_location[index,1], 'kx')
ax.plot(port.hover_location[0], port.hover_location[1], 'kx')
# Plotting the vehicles
for index, vehicle in enumerate(self.vehicles):
ax.quiver(vehicle.location[0], vehicle.location[1], vehicle.dx, vehicle.dy,
angles='xy', zorder=10, color=color[index], headlength=5, headwidth=4,
headaxislength=5, pivot="middle", alpha=0.6, label=f'Vehicle {index+1}: {vehicle.trip} trips')
# Annotate charge level
ax.annotate(vehicle.avail_energy, (vehicle.location[0]+0.1, vehicle.location[1]+0.1), va="bottom",
ha="left", fontsize=10)
# Add no of takeoffs and landings
bbox_props = dict(boxstyle="round, pad=0.3", fc="w", ec="k", lw=0.72)
msg = f'# of T/LO operations\nHub: T={self.hub.takeoff}, LO={self.hub.land}'
for index, port in enumerate(self.ports):
msg += f'\nPort {index+1}: T={port.takeoff}, LO={port.land}'
ax.annotate(msg, (3.5, -1.8), va="center", ha="center", fontsize=12, bbox=bbox_props)
# Adding a note
ax.annotate('Number near vehicle\ndenotes energy level', (-3.5, -2.0), va="center",
ha="center", fontsize=12, bbox=bbox_props)
ax.annotate('\'x\' denotes\nhover location', (-3.5, -1.2), va="center",
ha="center", fontsize=12, bbox=bbox_props)
ax.annotate(f'Turnaround time \nat hub: {self.hub.turnaround_time} min', (3.5, 2.2), va="center",
ha="center", fontsize=12, bbox=bbox_props)
ax.annotate(f'Recharge rate:\n{self.vehicles[0].recharge_rate} units/min', (3.5, 1.5), va="center",
ha="center", fontsize=12, bbox=bbox_props)
# ax.annotate(f'Queue: {self.hub.queue}\nAvail pads: {self.hub.avail_pads}', (0.0, -3.5), va="center", ha="center", fontsize=12, bbox=bbox_props)
# Asthetics
if i is not None:
ax.set_title('UAM Dashboard - Time: {}'.format(itr2time(i)))
ax.axis('equal')
ax.set_xticks([])
ax.set_yticks([])
ax.legend(loc=(0.01, 0.58), fontsize=12)
fig.tight_layout()
plt.savefig(f'{self.directory}/iteration{i+1}.png')
plt.close()
def pol2cart(rho, phi):
"""
Function to convert polar coordinates to cartesian coordinates
Parameters
----------
rho : float
Distance from the origin
phi : float
Angle in radians
Returns
-------
numpy array
"""
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return np.hstack(( x.reshape(-1,1), y.reshape(-1,1) ))
def hubTopology(num_pads, num_ports):
"""
Function to create the hub topology based on
takeoff & landing pads and number of remote ports
Parameters
----------
num_pads : int
Number of pads
num_ports : int
Number of remote ports
Returns
-------
pad_location : numpy array
Location of the pads within hub
hover_location : numpy array
Location of the hover points around hub
"""
if num_pads == 1:
pad_location = np.array([[0.0, 0.0]])
else:
pad_location = pol2cart(0.8, np.linspace(0, 2*np.pi, num_pads, endpoint=False))
hover_location = pol2cart(1.4, np.linspace(0, 2*np.pi, num_ports, endpoint=False))
return pad_location, hover_location
def itr2time(min):
"""
Function to convert iteration counter to time
Parameters
----------
itr : int
iteration counter
Returns
-------
time : str
time in HH:MM format
"""
start_hour = 7
start_min = 0
current_hour = start_hour + min//60
current_min = start_min + min%60
if current_hour < 10:
current_hour = '0{}'.format(current_hour)
else:
current_hour = '{}'.format(current_hour)
if current_min < 10:
current_min = '0{}'.format(current_min)
else:
current_min = '{}'.format(current_min)
current_time = "{}:{}".format(current_hour, current_min)
return current_time