forked from heronyang/airport-simulation
-
Notifications
You must be signed in to change notification settings - Fork 11
/
aircraft.py
503 lines (428 loc) · 17.1 KB
/
aircraft.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
"""`Aircraft` and `State` represents an aircraft in the simulation and its
state.
"""
import enum
import logging
import random
from surface import *
from link import HoldLink
from config import Config
from utils import get_seconds_after
import re
AIRLINES_TO_CODE = {
'Aer Lingus': "EI",
'AeroMexico': "AM",
'Air Canada': "AC",
'Air China': "CA",
'Air France': "AF",
'Air India': "AI",
'Air New Zealand': "NZ",
'Alaska Airlines': "AS",
'Alaska': "AS",
'All Nippon Airways': "NH",
'American Airlines': "AA",
'American': "AA",
'Asiana': "OZ",
'Avianca': "AV",
'British Airways': "BA",
'Cathay Pacific': "CX",
'China Airlines': "CI",
'China': "CI",
'China Eastern': "MU",
'China Southern': "CZ",
'Copa Airlines': "CM",
'Copa': "CM",
'Delta Air Lines': "DL",
'EVA AIR': "BR",
'Emirates': "EK",
'Finnair': "AY",
'French Bee': "BE",
'Frontier Airlines': "F9",
'Frontier': "F9",
'Hawaiian Airlines': "HA",
'Hawaiian': "HA",
'Hong Kong Airlines': "HX",
'Hong Kong': "HX",
'Iberia Airlines': "IB",
'Iberia': "IB",
'Interjet': "4O",
'Japan Airlines': "JL",
'Japan': "JL",
'JetBlue': "B6",
'KLM Royal Dutch Airlines': "KL",
'KLM Royal Dutch': "KL",
'Korean Air': "KE",
'Lufthansa': "LH",
'Norwegian': "DY",
'Philippine Airlines': "PR",
'Philippine': "PR",
'Qantas': "QF",
'SWISS International Airlines': "LX",
'SWISS International': "LX",
'Scandinavian Airlines': "SK",
'Scandinavian': "SK",
'Singapore Airlines': "SQ",
'Singapore': "SQ",
'Southwest': "WN",
'SunCountry': "SY",
'Turkish Airlines': "TK",
'Turkish': "TK",
'United': "UA",
'Virgin Atlantic': "VS",
'WestJet': "WS"
}
class State(enum.Enum):
"""`State` is a enum object that represents a possible state of an aircraft.
"""
unknown = 0
stop = 1 # default for departure flights
moving = 2 # on taxiway
hold = 3
flying = 4 # default for arrival flights
pushback = 5 # new added, on pushback way
ramp = 6
taxi = 7
queue = 8
atGate = 9
conflict = 10
class Aircraft:
"""`Aircraft` represents an aircraft in the airport.
"""
LOCATION_LEVEL_COARSE = 0
LOCATION_LEVEL_PRECISE = 1
def __init__(self, fullname, model, location, is_departure):
self.logger = logging.getLogger(__name__)
self.callsign = self.fullname2callsign(fullname)
self.model = model
# Aircraft's location as a vertex in on the node-link graph
# If it's on the middle of a link, the coarse location will be the next node it will traverse.
self.__coarse_location = location
# aircraft's location as some point on a link
self.__precise_location = None
self.itinerary = None
self.is_departure = is_departure
self.speed = self.speed_knots_to_ft_per_sec(Config.params["aircraft_model"]["init_speed"])
self.pushback_speed = self.speed_knots_to_ft_per_sec(Config.params["aircraft_model"]["pushback_speed"])
self.ramp_speed = self.speed_knots_to_ft_per_sec(Config.params["aircraft_model"]["ramp_speed"])
# self.queue_speed = self.speed_knots_to_ft_per_sec(Config.params["aircraft_model"]["queue_speed"])
self.IDEAL_DISTANCE = Config.params["aircraft_model"]["ideal_distance"]
self.MIN_DISTANCE = Config.params["aircraft_model"]["min_distance"]
self.MAX_SPEED = self.speed_knots_to_ft_per_sec(Config.params["aircraft_model"]["max_speed"])
self.IDEAL_SPEED = self.speed_knots_to_ft_per_sec(Config.params["aircraft_model"]["ideal_speed"])
self.IDEAL_ACC = Config.params["aircraft_model"]["ideal_acc"]
self.fronter_info = None
self.fronter_aircraft = None
self.speed_uncertainty = 0
self.is_reroute_necessary = True
self.take_off = False
self.tick_count = 0
self.ramp_distance = -1
self.ramp_flag = 1
self.calculate_ramp_distance = 0
self.prev_tick_count = 0
self.status = None
self.delayed = False
self.estimated_time = ""
self.real_time = ""
self.appear_time = ""
self.sim_time = 0
self.has_conflict = False
@staticmethod
def fullname2callsign(fullname):
flight_number = re.search('[0-9]+', fullname).group()
airline = re.search("[a-zA-Z ]+", fullname).group()
flight_number += fullname[-2:]
if airline in AIRLINES_TO_CODE:
return AIRLINES_TO_CODE[airline] + flight_number
else:
return 'N ' + flight_number
def set_location(self, location, level=LOCATION_LEVEL_COARSE):
"""Sets the location of this aircraft to a given location."""
if level == Aircraft.LOCATION_LEVEL_COARSE:
self.__coarse_location = location
self.__precise_location = location # must reset precise location because the aircraft moved
self.logger.info("%s coarse location changed to %s", self, location)
elif level == Aircraft.LOCATION_LEVEL_PRECISE:
self.__precise_location = location
self.logger.info("%s precise location changed to %s", self, location)
else:
raise Exception("Unrecognized location level.")
def set_estimated(self, estimated_time):
self.estimated_time = estimated_time
def set_appear_time(self, appear_time):
self.appear_time = appear_time
def set_sim_time(self, sim_time):
self.sim_time = sim_time
@property
def location(self):
"""Same as coarse location. Keep the naming for compatibility. """
return self.__coarse_location
@property
def precise_location(self):
return self.__precise_location if self.__precise_location else self.__coarse_location
def get_next_location(self, level=LOCATION_LEVEL_COARSE):
"""Gets the precise location of this aircraft in the next tick."""
if not self.itinerary:
return self.__coarse_location
next_index, _, next_location = self.itinerary.get_next_location(self.next_tick_distance)
if level == Aircraft.LOCATION_LEVEL_COARSE:
if next_index < self.itinerary.length:
next_target = self.itinerary.get_nth_target(next_index)
if type(next_target) is HoldLink:
return None
return next_target.end
elif level == Aircraft.LOCATION_LEVEL_PRECISE:
return next_location
else:
raise Exception("Unrecognized location level.")
return next_location
"""
@:param fronter_info (target_speed, relative_distance)
"""
def set_fronter_info(self, fronter_info):
""" Set the information of the preceding aircraft. """
self.fronter_info = fronter_info
def set_fronter_aircraft(self, aircraft):
self.fronter_aircraft = aircraft
def speed_knots_to_ft_per_sec(self, speed):
return float(speed * 1.0)
"""
@:param fronter_info (target_speed, relative_distance)
"""
def get_next_speed(self, fronter_info, state):
# print ("{0}: {1}".format(self.callsign, self.state))
# if self.is_delayed:
# return 0
if fronter_info is None:
acceleration = 0.0
if state is State.pushback:
new_speed = self.pushback_speed
elif state is State.ramp:
new_speed = self.ramp_speed
else:
new_speed = self.IDEAL_SPEED
if self.speed < new_speed:
# acceleration phase
acceleration = self.IDEAL_ACC
elif self.speed > new_speed:
# deceleration phase
acceleration = -self.IDEAL_ACC
if acceleration > 0:
new_speed = min(self.speed + acceleration, new_speed)
else:
new_speed = max(self.speed + acceleration, new_speed)
if new_speed < 0:
new_speed = 0
if new_speed > self.MAX_SPEED:
new_speed = self.MAX_SPEED
return new_speed
# calculate the new speed when it is following another aircraft
fronter_speed = fronter_info[0]
relative_distance = fronter_info[1]
if fronter_speed <= 0:
return 0
return self.brake_hard()
# Brake hard if less than MIN_DISTANCE
if relative_distance <= self.MIN_DISTANCE:
return 0
return self.brake_hard()
""" Calculate the speed based on following model."""
# Adjust the speed
if relative_distance > self.IDEAL_DISTANCE:
# acceleration phase
c, l, m = 1.1, 0.1, 0.2
acc_flag = True
elif relative_distance < self.IDEAL_DISTANCE or fronter_speed < self.speed:
# deceleration phase
c, l, m = -2, 1.2, 0.7
else:
c, l, m = 0, 0, 0
acceleration = c * (self.speed ** m) \
* (abs(self.speed - fronter_speed) / (relative_distance ** l))
""" Make sure the speed is always valid """
new_speed = self.speed + acceleration
if new_speed < 0:
new_speed = 0
if new_speed > self.MAX_SPEED:
new_speed = self.MAX_SPEED
return new_speed
def set_speed(self, speed):
""" Set the speed of the aircraft"""
# Revise the value if the input speed is valid
if speed < 0:
self.speed = 0
return
if speed > self.MAX_SPEED:
self.speed = self.MAX_SPEED
return
self.speed = speed
def brake_hard(self):
""" Brake hard to avoid potential crash"""
# TODO: revise the model
# new_speed = self.speed / 1.5
# new_speed = self.speed / 3
new_speed = 5
# self.set_speed(new_speed)
self.logger.info("%s with speed %f brakes hard", self, self.speed)
return new_speed
@property
def tick_distance(self):
""" Get the distance the aircraft passed in this tick"""
return self.speed * 1 # 1 is the time of a tick
@property
def next_tick_distance(self):
return self.get_next_speed(self.fronter_info, self.state) * 1
def set_itinerary(self, itinerary):
"""Sets the itinerary of this aircraft."""
self.itinerary = itinerary
# self.logger.debug("%s: Roger, %s received.", self, itinerary)
# for target in itinerary.targets:
# self.logger.debug(target)
def add_speed_uncertainty(self, speed_bias):
self.speed_uncertainty = speed_bias
""" original """
def add_scheduler_delay(self):
"""Adds a scheduler delay on this aircraft."""
if not self.itinerary:
# self.logger.debug("%s: No itinerary to add delay", self)
return
delay_added_at = self.itinerary.add_scheduler_delay()
# self.logger.debug("%s: Delay added at %s by scheduler",
# self, delay_added_at)
def tick(self):
if self.real_time != "":
# print(self.estimated_time < self.real_time)
if (self.estimated_time < self.real_time):
self.delayed = True
"""Ticks on this aircraft and its subobjects to move to the next state.
"""
passed_links = None
if self.itinerary:
self.tick_count += 1
# print("AIR %s: aircraft tick.", self)
passed_links = self.itinerary.tick(self.tick_distance)
new_speed = self.get_next_speed(self.fronter_info, self.state) + self.speed_uncertainty
self.set_speed(new_speed)
if self.itinerary.is_completed:
self.logger.debug("%s: %s completed.", self, self.itinerary)
last_target = self.itinerary.backup[-1]
is_arrival_aircraft = type(last_target.end) is Gate
if is_arrival_aircraft and self.itinerary.current_target is not None \
and type(self.itinerary.current_target.end) is Spot:
self.is_reroute_necessary = False
self.set_location(self.itinerary.current_coarse_location, Aircraft.LOCATION_LEVEL_COARSE)
self.set_location(self.itinerary.current_precise_location, Aircraft.LOCATION_LEVEL_PRECISE)
else:
# self.logger.debug("%s: No itinerary request.", self)
# print("AIR %s: No itinerary request.", self)
pass
self.logger.info("%s at %s", self, self.__coarse_location)
return passed_links
def count_intersection(self):
count = 0
targetIdx = self.itinerary.current_target_index
targetLen = len(self.itinerary.targets)
# print(targetIdx, targetLen)
for link in self.itinerary.targets[targetIdx: targetLen]:
# print(link)
node = link.end
# for node in self.itinerary.current_target.nodes:
if node.name.startswith('I'):
# print(node)
count += 1
# print("count:", count)
return count
@property
def state(self):
"""Identify whether the aircraft is on pushbackway or taxiway"""
# add conflict state
if self.has_conflict is True:
return State.conflict
if self.is_delayed is True:
self.delayed = True
if self.itinerary is None or self.itinerary.is_completed:
# self.status = State.stop
return State.stop
if self.itinerary.next_target is None or \
self.itinerary.current_target is None:
# self.status = State.stop
return State.stop
if self.is_departure is True:
if type(self.itinerary.current_target.start) is Gate:
# do not update state if holdlink is added at gate
if self.real_time == "":
self.real_time = get_seconds_after(self.appear_time, self.sim_time * self.tick_count)
return State.atGate
elif type(self.itinerary.current_target) is PushbackWay:
if self.real_time == "":
self.real_time = get_seconds_after(self.appear_time, self.sim_time * self.tick_count)
return State.pushback
elif type(self.itinerary.current_target) is Taxiway:
if len(self.itinerary.current_target.nodes) > 0 and self.itinerary.current_target.nodes[0].name.startswith('I'):
self.ramp_flag = 0
# self.status = State.moving
return State.taxi
if self.ramp_flag:
# self.status = State.ramp
return State.ramp
return State.taxi
elif self.is_departure is False:
if len(self.itinerary.current_target.nodes) > 0 and self.count_intersection() == 0: # and self.itinerary.current_target.nodes[0].name.startswith('I'):
# self.status = State.ramp
self.ramp_flag = 0
# self.status = State.ramp
return State.ramp
if not self.ramp_flag:
# self.status = State.ramp
return State.ramp
return State.taxi
# self.status = State.moving
return State.moving
@property
def is_delayed(self):
"""Returns True if the aircraft is currently be delayed."""
return self.itinerary.is_delayed if self.itinerary else False
@property
def is_predict_delayed(self):
"""Returns True if the aircraft is predicted to be delayed."""
if self.itinerary:
return True if type(self.itinerary.targets[0]) is HoldLink else False
return False
@property
def link_this_tick(self):
"""Returns the link finished in this tick."""
return self.itinerary.links_this_tick
@property
def current_target(self):
"""return the current exact link"""
return self.itinerary.current_target
def get_ahead_intersections_and_link(self):
"""get the intersections and future links with certain distance"""
ahead_distance = 800.0
return self.itinerary.get_ahead_intersections_and_link(ahead_distance)
def set_quiet(self, logger):
"""Sets the aircraft into quiet mode where less logs are printed."""
self.logger = logger
def __hash__(self):
return hash(self.callsign)
def __eq__(self, other):
return self.callsign == other.callsign
def __ne__(self, other):
return not self == other
def __repr__(self):
if self.fronter_info == None:
fronter_speed = -999
relative_dist = -999
else:
fronter_speed, relative_dist = self.fronter_info
if self.fronter_aircraft is None:
fronter_callsign = None
else:
fronter_callsign = self.fronter_aircraft.callsign
return "<Aircraft: %s %s %.2f fronter: %s fronter_speed: %d relative_dist: %d>" % (self.callsign, self.state, self.speed, fronter_callsign, fronter_speed, relative_dist)
def __getstate__(self):
attrs = dict(self.__dict__)
del attrs["logger"]
return attrs
def __setstate__(self, attrs):
self.__dict__.update(attrs)