-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_spare_code.py
More file actions
260 lines (225 loc) · 9.02 KB
/
Copy pathdata_spare_code.py
File metadata and controls
260 lines (225 loc) · 9.02 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline
import itertools
class data:
def __init__(self, airline = 'KLM', t_interval = 5, tot_m = 24*60, mean_early_t = 120, arrival_std = 2, last_checkin = 45, earliest_checkin = 4*60, data_loc = 'data 03_06_2024.xlsx'): #data_loc = 'data 30_04_2024.xlsx'
self.airline = airline
self.t_interval = t_interval
self.tot_m = tot_m
self.mean_early_t = mean_early_t
self.last_checkin = last_checkin
self.earliest_checkin = earliest_checkin
self.arrival_std_dev = last_checkin / arrival_std
self.data_loc = data_loc
self.df = None
self.flights = None
self.d = None
self.T = None
self.too_early = None
self.prep_data()
self.set_d()
self.set_T()
def prep_data(self):
self.organize_rows()
self.add_capacity()
self.set_time_to_minutes()
self.select_airline(self.airline)
# self.get_pax_dist()
def organize_rows(self):
df = pd.read_excel(self.data_loc)
df = df[['AIRCRAFT', 'AIRLINE', 'ETD', 'CARGO']]
df = df.dropna(subset=['ETD'])
df = df[df['CARGO'].isna()]
df['AIRCRAFT'] = df['AIRCRAFT'].str.replace(' WINGLET', '', regex=False)
df = df[~df['AIRCRAFT'].str.contains('FREIGHTER')]
df.drop(columns=['CARGO'], inplace=True)
self.df = df
def add_capacity(self):
max_pax_dict = {
'BOEING 737-800S': 176,
'AIRBUS A321 NEO': 220,
'Embraer E190-E2 (ERJ190-300)': 114,
'BOEING 737-700 Winglets': 149,
'B737-900/Winglets': 189,
'Embraer 190 (IGW)': 100,
'Boeing 737MAX-8': 200,
'AIRBUS A320 NEO': 194,
'AIRBUS A319-111': 160,
'Boeing 737MAX-9': 220,
'EMBRAER ERJ-195-E2 (190-400STD)': 146,
'EMBRAER175(170-200 STD)': 88,
'Airbus A220-300': 160,
'AIRBUS A321-100/200': 220,
'BOMBARDIER CRJ900(CL-600-2D24)': 90,
'EMBRAER170': 76,
'AIRBUS A330-300': 440,
'Embraer 195 ERJ 190-200': 124,
'BOEING B-767-400': 304,
'EMBRAER145': 50,
'BOEING 777-200': 396,
'Boeing 787-10 Dreamliner': 330,
'BOEING 777-300ER': 396,
'AIRBUS A350-900': 325,
'Airbus A330-900neo': 287,
'Boeing 787-8 Dreamliner': 242,
'Boeing 767-300 winglet': 261,
'AIRBUS A350-1000': 400,
'Boeing 787-9 Dreamliner': 290,
'AIRBUS A330-200': 406,
'Airbus A380-800': 520
}
self.df['MAX_PAX'] = self.df['AIRCRAFT'].map(max_pax_dict)
unique_aircraft_list = self.df['AIRCRAFT'].unique()
# print("Unique Aircraft Types:", unique_aircraft_list)
if len(unique_aircraft_list) != len(max_pax_dict):
print(f'{len(unique_aircraft_list)} != {len(max_pax_dict)}')
def set_time_to_minutes(self):
self.df['ETD_minutes'] = self.df['ETD'].apply(lambda x: x.hour * 60 + x.minute)
def select_airline(self, airline='KLM'):
flights = self.df[self.df['AIRLINE'] == airline]
flights = flights.reset_index(drop=True)
self.flights = flights
def set_d(self):
d = {}
too_early = []
count = 0
for index, flight in self.flights.iterrows():
valid_pax_dist = []
etd_minutes = flight['ETD_minutes']
total_passengers = flight['MAX_PAX']
mean_checkin_time = etd_minutes - self.mean_early_t
norm_dist = np.random.normal(loc=mean_checkin_time, scale=self.arrival_std_dev, size=total_passengers)
valid_norm_dist = norm_dist[(norm_dist >= 0) & (norm_dist <= self.tot_m)]
norm_binned = np.floor(valid_norm_dist / self.t_interval).astype(int)
pax_dist, _ = np.histogram(norm_binned, bins=np.arange(0, self.tot_m // self.t_interval + 1))
# earliest_checkin_index = (etd_minutes - self.earliest_checkin)//self.t_interval
# latest_checkin_index = (etd_minutes - self.last_checkin)//self.t_interval
earliest_checkin_index = max(0, (etd_minutes - self.earliest_checkin) // self.t_interval)
latest_checkin_index = min((etd_minutes - self.last_checkin) // self.t_interval, len(pax_dist) - 1)
#print('checkin avialble from index:', earliest_checkin_index, 'to index:', latest_checkin_index)
if earliest_checkin_index >= 0 and latest_checkin_index >= 0:
too_early.append(sum(pax_dist[:earliest_checkin_index]))
pax_dist[:earliest_checkin_index] = 0
pax_dist[latest_checkin_index:] = 0
valid_pax_dist = pax_dist
# valid_pax_dist.append(pax_dist[earliest_checkin_index:latest_checkin_index])
# too_early.append(sum(pax_dist[:earliest_checkin_index]))
d[count] = valid_pax_dist
# print(too_early_pax_dist)
# too_early[count] = sum(too_early_pax_dist)
count += 1
# Restructure d so that it works with d[i,j] instead of d[i][j]
new_d = {}
for i, sublist in d.items():
for j in range(len(sublist)):
new_d[(i, j)] = sublist[j]
self.d = new_d
self.too_early = too_early
def set_T(self):
T = {}
for index, flight in self.flights.iterrows():
etd_minutes = flight['ETD_minutes']
no_checkin_t = set()
for i in range(self.tot_m // self.t_interval):
t = i * self.t_interval
if t < etd_minutes - self.earliest_checkin:
no_checkin_t.add(i)
elif t > etd_minutes - self.last_checkin:
no_checkin_t.add(i)
T[index] = no_checkin_t
self.T = T
@staticmethod
def flights_to_d(flight_schedule, t_interval = 5, tot_m = 24*60, mean_early_t = 2*60, arrival_std = 0.5, last_checkin = 45, earliest_checkin = 4*60):
# flight_schedule = {
# 0: (240, 100), # Flight 0 departs at interval 16 (4 hours into the day)
# 1: (48, 100), # Flight 1 departs at interval 48 (12 hours into the day)
# 2: (80, 50) # Flight 2 departs at interval 80 (20 hours into the day)
# }
arrival_std_dev = last_checkin / arrival_std
#valid_pax_dist = []
d = {}
too_early = []
count = 0
for index, (etd_minutes, total_passengers) in flight_schedule.items():
valid_pax_dist = []
# etd_minutes = flight['ETD_minutes']
# total_passengers = flight['MAX_PAX']
mean_checkin_time = etd_minutes - mean_early_t
norm_dist = np.random.normal(loc=mean_checkin_time, scale=arrival_std_dev, size=total_passengers)
valid_norm_dist = norm_dist[(norm_dist >= 0) & (norm_dist <= tot_m)]
norm_binned = np.floor(valid_norm_dist / t_interval).astype(int)
pax_dist, _ = np.histogram(norm_binned, bins=np.arange(0, tot_m // t_interval + 1))
earliest_checkin_index = max(0, (etd_minutes - earliest_checkin) // t_interval)
latest_checkin_index = min((etd_minutes - last_checkin) // t_interval, len(pax_dist) - 1)
#print('checkin avialble from index:', earliest_checkin_index, 'to index:', latest_checkin_index)
if earliest_checkin_index >= 0 and latest_checkin_index >= 0:
too_early.append(sum(pax_dist[:earliest_checkin_index]))
pax_dist[:earliest_checkin_index] = 0
pax_dist[latest_checkin_index+1:] = 0
valid_pax_dist = pax_dist
# valid_pax_dist = pax_dist[earliest_checkin_index:latest_checkin_index]
# too_early.append(sum(pax_dist[:earliest_checkin_index]))
# print('---->', valid_pax_dist)
# print(len(valid_pax_dist), latest_checkin_index - earliest_checkin_index)
d[count] = valid_pax_dist
# print(too_early_pax_dist)
# too_early[count] = sum(too_early_pax_dist)
count += 1
# Restructure d so that it works with d[i,j] instead of d[i][j]
new_d = {}
for i, sublist in d.items():
for j in range(len(sublist)):
new_d[(i, j)] = sublist[j]
return new_d, too_early
#data = data()
#print(sum(data.too_early))
# test_flights = {
# 0: (90, 100), # Flight 0 departs at interval 16 (4 hours into the day)
# 1: (400, 100), # Flight 1 departs at interval 48 (12 hours into the day)
# 2: (500, 50) # Flight 2 departs at interval 80 (20 hours into the day)
# }
#
#
# def plot_data(d, too_early):
# # test_flights = {
# # 0: (90, 100), # Flight 0 departs at interval 16 (4 hours into the day)
# # 1: (400, 100), # Flight 1 departs at interval 48 (12 hours into the day)
# # 2: (500, 50) # Flight 2 departs at interval 80 (20 hours into the day)
# # }
# # d, too_early = data.flights_to_d(test_flights)
# colors = itertools.cycle(['red', 'green', 'blue'])
#
# plt.figure(figsize=(10, 6))
#
# for flight_index in range(max(x for (x, _), _ in d.items()) + 1):
# # Check if there are any data points for the current flight_index
# data_points = [(time_bin, count) for (idx, time_bin), count in d.items() if idx == flight_index]
# if not data_points:
# continue # Skip this iteration if no data points
#
# # Extract and sort time bins and counts
# times, counts = zip(*sorted(data_points))
#
# # Scatter plot for data points
# color = next(colors)
# plt.scatter(times, counts, color=color, label=f'Flight {flight_index}', alpha=0.6, edgecolors='w')
#
# # Interpolate and plot smooth curve if there are enough points
# #if len(times) > 1:
# # spline = make_interp_spline(times, counts, k=2)
# # smooth_times = np.linspace(min(times), max(times), 300)
# # plt.plot(smooth_times, spline(smooth_times), color=colors[flight_index])
#
# plt.legend()
# plt.title('Passenger Arrivals by Flight and Time Interval')
# plt.xlabel('Time Interval')
# plt.ylabel('Number of Passengers')
# plt.grid(True)
# plt.show()
#
# print('too early', too_early)
#
#
# # need to fix indices, add time or something, because now we rearanged. Could keep same indices but set all too early to 0.