forked from viranca/Research_Internship_ENAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Distribute_demand.py
382 lines (324 loc) · 19.4 KB
/
Distribute_demand.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import pandas as pd
import geopandas
import numpy as np
from statistics import mean
import math
##input variables (independent variables for schedule)
#total demand (very high, high, medium, low, very low)
#proportion of traffic from Dcenters (30, 50, 80)
#number of loitering missions and radius
"""
##Method pseudo code:
for every vertiport in Vertipors_df:
1. Find the four closest Distribution centers and make them very important (increase their priority temporarily).
2. Compute the lambda's (demand rates) for vertiports and for distribution centers:
- Percentage_Dcenter will come from Distribution centers:
* Based on the relative size and distance, divide the lambda across all the distribution centers.
- The remaining percentage will come from other vertiports:
* Based on the relative size and distance, divide the lambda across all the vertiports.
3. Draw from a poisson process with the previously obtained lambda's in a timeframe of all timesteps.
4. Save the labels of the recieving and sending points at every time.
Based on this create the flight schedule in the requested format.
"""
def Distribute_demand(timesteps, Percentage_Dcenters, Percentage_closest_Dcenters,
Number_of_Dcenters_per_vertiport, Percentage_known_flights,
Percentage_emergency_flights, ac_types,
Distribution_centers_df, Vertiports_df):
##load distribution centers and vertiport locations + average hourly demand
#Distribution_centers_df = pd.read_csv('Distribution_centers_locations.csv')
#Vertiports_df = pd.read_csv('Vertiport_locations.csv')
try:
Distribution_centers_df = Distribution_centers_df.drop(['Unnamed: 0', 'Latitude', 'Longitude'], axis = 1)
Vertiports_df = Vertiports_df.drop(['Unnamed: 0'], axis = 1)
except:
print('Not needed.')
"""
Distribution centers Dataframe format:
,Unnamed: 0,x,y,Relative_size,Number_of_ports,geometry,node_x_send_1,node_y_send_1,node_x_send_2,node_y_send_2,node_x_send_3,node_y_send_3
Vertiports Dataframe format:
Label, x_location, y_location, Municipal_demand, relative_vport_size,
"""
def dist(x1, y1, x2 , y2):
dist = ((x1 - x2)**2 + (y1 - y2)**2) ** 0.5
return dist
def Distance_distribution_centers(x1, y1):
dist_list = []
for index, row in Distribution_centers_df.iterrows():
x2 = row['x']
y2 = row['y']
dist_D_center = dist(x1, y1, x2 , y2)
dist_list.append(dist_D_center)
return dist_list
def Create_distribution_center_priority(Distribution_centers_df, dist_list):
relative_size_list = []
for index, row in Distribution_centers_df.iterrows():
relative_size_d = row['Relative_size']
relative_size_list.append(relative_size_d)
#find the Index/label of the closest D centers:
K = Number_of_Dcenters_per_vertiport
four_closest_index = sorted(range(len(dist_list)), key = lambda sub: dist_list[sub])[:K]
priority_sum = 0
for Dcenter in range(len(relative_size_list)):
priority_sum +=relative_size_list[Dcenter]
#print(priority_sum)
#17.12042308855761
#increase the relative size of the K-closest D centers:
non_closest_index = list(range(len(relative_size_list)))
non_closest_index = [x for x in non_closest_index if x not in four_closest_index]
for close_Dcenter in four_closest_index:
#non_closest_index.pop(close_Dcenter)
relative_size_list[close_Dcenter] = relative_size_list[close_Dcenter] * priority_sum * (Percentage_closest_Dcenters/Number_of_Dcenters_per_vertiport)
#decrease the relative size of the non K-closest D centers:
for non_close_Dcenter in non_closest_index:
relative_size_list[non_close_Dcenter] = relative_size_list[close_Dcenter] * priority_sum * ((1-Percentage_closest_Dcenters)/(16 - Number_of_Dcenters_per_vertiport))
#compute priority
priority_list = []
dist_percentage_list = []
for Dcenter_i in range(len(relative_size_list)):
dist_percent = (dist_list[Dcenter_i]/sum(dist_list))
dist_percentage_list.append(dist_percent)
for Dcenter_i in range(len(relative_size_list)):
priority = (relative_size_list[Dcenter_i]/sum(relative_size_list) ) + (dist_percentage_list[Dcenter_i] - mean(dist_percentage_list))
priority_list.append(priority)
#replace negative values with zero, perhaps reduce the other priorities accordingly in the future.
priority_list_Dcenters = [0 if i<0 else i for i in priority_list]
return priority_list_Dcenters
#repeat Create_distribution_center_priority for vertiports.
def Create_vertiport_priority(Vertiports_df):
relative_size_list = []
for index, row in Vertiports_df.iterrows():
relative_size_d = row['Relative_size']
relative_size_list.append(relative_size_d)
priority_list_vertiports = []
for vertiport in range(len(relative_size_list)):
priority_percentage = relative_size_list[vertiport]/sum(relative_size_list)
priority_list_vertiports.append(priority_percentage)
return priority_list_vertiports
def Make_poisson_tableu_schedule(priority_list_vertiports, Vertiports_df, Distribution_centers_df):
label_recieving_vertiport = 0
label_recieving_vertiport2 = 0
flight_schedule_unsorted = []
schedule_from_v_total = []
schedule_from_D_total = []
aircraft_id = 0
emergency = 0
for recieving_vertiport in range(len(priority_list_vertiports)):
#Retrieve municipal demand
Total_demand = Vertiports_df.iloc[recieving_vertiport]['demand']
#compute lambda and make initial split
Total_lambda = (Total_demand/3600)
Total_lambda_vertiports = (1-Percentage_Dcenters) * Total_lambda
Total_lambda_Dcenters = Percentage_Dcenters * Total_lambda
#recompute the Dcenter priority list
x1 = Vertiports_df.iloc[recieving_vertiport]['x']
y1 = Vertiports_df.iloc[recieving_vertiport]['y']
dist_list = Distance_distribution_centers(x1, y1)
priority_list_Dcenters = Create_distribution_center_priority(Distribution_centers_df, dist_list)
#Distribute lambda over all vertiports and Dcenters based on their priority lists
Lambda_list_vertiports = Total_lambda_vertiports * np.array(priority_list_vertiports)
Lambda_list_Dcenters = Total_lambda_Dcenters * np.array(priority_list_Dcenters)
#for each entry in the vertiport lambda lists run a poisson process over the timesteps
label_sending_vertiport = 0
schedule_from_v = []
for vertiport_lambda in Lambda_list_vertiports:
Traffic_i = np.random.poisson(vertiport_lambda ,timesteps)
timestep_index = 0
for timestep_traffic in Traffic_i:
flight =[]
if timestep_traffic == 1:
flight.append(label_recieving_vertiport)
flight.append(label_sending_vertiport)
flight.append(timestep_index)
schedule_from_v.append(flight)
#flight.append(list(Vertiports_df.iloc[label_sending_vertiport].geometry.coords)[0][0])
#flight.append(list(Vertiports_df.iloc[label_sending_vertiport].geometry.coords)[0][1])
#flight.append(list(Vertiports_df.iloc[label_recieving_vertiport].geometry.coords)[0][0])
#flight.append(list(Vertiports_df.iloc[label_recieving_vertiport].geometry.coords)[0][1])
#schedule_from_v_total.append(flight)
timestep_index += 1
label_sending_vertiport += 1
label_recieving_vertiport += 1
#for each entry in the Dcenter lambda lists run a poisson process over the timesteps
label_sending_dcenter = 0
schedule_from_D = []
for dcenter_lambda in Lambda_list_Dcenters:
Traffic_i = np.random.poisson(dcenter_lambda ,timesteps)
timestep_index = 0
for timestep_traffic in Traffic_i:
flight =[]
if timestep_traffic == 1:
flight.append(label_recieving_vertiport2)
flight.append(label_sending_dcenter)
flight.append(timestep_index)
schedule_from_D.append(flight)
#flight.append(list(Distribution_centers_df.iloc[label_sending_dcenter].geometry.coords)[0][0])
#flight.append(list(Distribution_centers_df.iloc[label_sending_dcenter].geometry.coords)[0][1])
#flight.append(list(Vertiports_df.iloc[label_recieving_vertiport2].geometry.coords)[0][0])
#flight.append(list(Vertiports_df.iloc[label_recieving_vertiport2].geometry.coords)[0][1])
#schedule_from_D_total.append(flight)
timestep_index += 1
label_sending_dcenter += 1
label_recieving_vertiport2 += 1
flight_row_v = []
for flight in schedule_from_v:
flight_row = []
known_at_start = np.random.choice([True, False], 1, p = [Percentage_known_flights, (1-Percentage_known_flights)])
if known_at_start:
#append time of recieving flight plan: 00:00:00
flight_row.append("00:00:00")
else:
#append reveal time (600 seconds for normal flights and 60 seconds for emergency flights):
emergency = np.random.choice([True, False], 1, p = [Percentage_emergency_flights, (1-Percentage_emergency_flights)])
if emergency:
revealtime = 60
else:
revealtime = 300
time_in_seconds = max(0, (flight[2] - revealtime))
whole_minutes = math.floor(time_in_seconds/60) #make this round down
whole_minutes = str(whole_minutes)
if len(str(whole_minutes)) == 1:
whole_minutes = "0" + str(whole_minutes)
seconds_left = time_in_seconds - int(whole_minutes)*60
if len(str(seconds_left)) == 1:
seconds_left = "0" + str(seconds_left)
flight_row.append("00:" + str(whole_minutes) + ":" + str(seconds_left))
#append aircraft ID:
flight_row.append(aircraft_id)
aircraft_id += 1
#append aircraft type:
drone_type = np.random.choice(ac_types, 1, p = [1/len(ac_types)]*len(ac_types))
flight_row.append(drone_type[0])
#append flight departure time
time_in_seconds = flight[2]
whole_minutes = math.floor(time_in_seconds/60) #make this round down
whole_minutes = str(whole_minutes)
if len(str(whole_minutes)) == 1:
whole_minutes = "0" + str(whole_minutes)
seconds_left = time_in_seconds - int(whole_minutes)*60
if len(str(seconds_left)) == 1:
seconds_left = "0" + str(seconds_left)
flight_row.append("00:" + str(whole_minutes) + ":" + str(seconds_left))
if len(flight) > 0:
#port location:
x_loc_sending = Vertiports_df.iloc[flight[1]]['node_x_send']
y_loc_sending = Vertiports_df.iloc[flight[1]]['node_y_send']
#find closest node to port location
#tree = spatial.KDTree(nodes)
#index_closest = tree.query(x_loc_sending_port, y_loc_sending_port)[1]
#append origin node location
#x_loc_sending = nodes[index_closest][0]
#y_loc_sending = nodes[index_closest][1]
flight_row.append('(' + str(x_loc_sending) + ', ' + str(y_loc_sending) + ')')
#find closest node to port location
#append recieving/destination airport locationn
x_loc_recieving = Vertiports_df.iloc[flight[0]]['node_x_recieve']
y_loc_recieving = Vertiports_df.iloc[flight[0]]['node_y_recieve']
#x_loc_recieving = Vertiports_df.iloc[flight[0]]['x']
#y_loc_recieving = Vertiports_df.iloc[flight[0]]['y']
flight_row.append('(' + str(x_loc_recieving) + ', ' + str(y_loc_recieving) + ')')
#add priority level (1 for now)
if emergency:
priority = 4
else:
priority = np.random.randint(1,4)
flight_row.append(priority)
flight_row.append(time_in_seconds)
flight_row.append('')
flight_row.append('')
flight_row.append('')
flight_row.append('')
flight_row.append('')
flight_row.append('')
flight_row_v.append(flight_row)
flight_row_D = []
port_selector = 0
for flight in schedule_from_D:
flight_row = []
known_at_start = np.random.choice([True, False], 1, p = [Percentage_known_flights, (1-Percentage_known_flights)])
if known_at_start:
#append time of recieving flight plan: 00:00:00
flight_row.append("00:00:00")
else:
#append reveal time (600 seconds for normal flights and 60 seconds for emergency flights):
emergency = np.random.choice([True, False], 1, p = [Percentage_emergency_flights, (1-Percentage_emergency_flights)])
if emergency:
revealtime = 60
else:
revealtime = 600
time_in_seconds = max(0, (flight[2] - revealtime))
whole_minutes = math.floor(time_in_seconds/60) #make this round down
whole_minutes = str(whole_minutes)
if len(str(whole_minutes)) == 1:
whole_minutes = "0" + str(whole_minutes)
seconds_left = time_in_seconds - int(whole_minutes)*60
if len(str(seconds_left)) == 1:
seconds_left = "0" + str(seconds_left)
flight_row.append("00:" + str(whole_minutes) + ":" + str(seconds_left))
#append aircraft ID:
flight_row.append(aircraft_id)
aircraft_id += 1
#append aircraft type:
drone_type = np.random.choice(ac_types, 1, p = [1/len(ac_types)]*len(ac_types))
flight_row.append(drone_type[0])
#append flight departure time
time_in_seconds = flight[2]
whole_minutes = math.floor(time_in_seconds/60) #make this round down
whole_minutes = str(whole_minutes)
if len(str(whole_minutes)) == 1:
whole_minutes = "0" + str(whole_minutes)
seconds_left = time_in_seconds - int(whole_minutes)*60
if len(str(seconds_left)) == 1:
seconds_left = "0" + str(seconds_left)
flight_row.append("00:" + str(whole_minutes) + ":" + str(seconds_left))
if len(flight) > 0:
#append origin airport location
if port_selector == 0:
x_loc_sending = Distribution_centers_df.iloc[flight[1]]['node_x_send_1']
y_loc_sending = Distribution_centers_df.iloc[flight[1]]['node_y_send_1']
port_selector += 1
elif port_selector == 1:
x_loc_sending = Distribution_centers_df.iloc[flight[1]]['node_x_send_2']
y_loc_sending = Distribution_centers_df.iloc[flight[1]]['node_y_send_2']
port_selector += 1
else:
x_loc_sending = Distribution_centers_df.iloc[flight[1]]['node_x_send_3']
y_loc_sending = Distribution_centers_df.iloc[flight[1]]['node_y_send_3']
port_selector = 0
flight_row.append('(' + str(x_loc_sending) + ', ' + str(y_loc_sending) + ')')
#append recieving/destination airport locationn
x_loc_recieving = Vertiports_df.iloc[flight[0]]['node_x_recieve']
y_loc_recieving = Vertiports_df.iloc[flight[0]]['node_y_recieve']
#x_loc_recieving = Vertiports_df.iloc[flight[0]]['x']
#y_loc_recieving = Vertiports_df.iloc[flight[0]]['y']
flight_row.append('(' + str(x_loc_recieving) + ', ' + str(y_loc_recieving) + ')')
#add priority level (1 for now)
if emergency:
priority = 4
else:
priority = np.random.randint(1,4)
flight_row.append(priority)
flight_row.append(time_in_seconds)
flight_row.append('')
flight_row.append('')
flight_row.append('')
flight_row.append('')
flight_row.append('')
flight_row.append('')
flight_row_D.append(flight_row)
for i in flight_row_v:
flight_schedule_unsorted.append(i)
for i in flight_row_D:
flight_schedule_unsorted.append(i)
#sort the flights and generate the flight schedule:
flight_schedule_df = pd.DataFrame.from_records(flight_schedule_unsorted)
flight_schedule_df = flight_schedule_df.sort_values(by=[flight_schedule_df.columns[7]])
flight_schedule_df = flight_schedule_df.drop(columns = [ flight_schedule_df.columns[7]])
#flight_schedule_df.to_csv('Initial_flight_intention.csv', header = False, index = False)
#df = pd.DataFrame(schedule_from_D_total)
#df.to_csv('schedule_from_D_total.csv', header = False)
#df = pd.DataFrame(schedule_from_v_total)
#df.to_csv('schedule_from_v_total.csv', header = False)
return flight_schedule_df
priority_list_vertiports = Create_vertiport_priority(Vertiports_df)
Schedule = Make_poisson_tableu_schedule(priority_list_vertiports, Vertiports_df, Distribution_centers_df)
return Schedule