forked from heronyang/airport-simulation
-
Notifications
You must be signed in to change notification settings - Fork 11
/
itinerary.py
324 lines (271 loc) · 11.3 KB
/
itinerary.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
"""Class file for `Itinerary`."""
from link import HoldLink
from utils import str2sha1
from copy import deepcopy
from node import Node
from surface import *
class Itinerary:
"""Itinerary is a list of target nodes that an aircraft follows per tick.
"""
def __init__(self, targets=None, unfinished_distance=0):
# unfinished_distance == 0 means it's
self.targets = [HoldLink()] if unfinished_distance == 0 else []
self.targets += targets if targets else [] # links\
self.backup = deepcopy(targets)
self.unfinished_distance = unfinished_distance
# distance: the distance travelled on the link
# distance_left: the distance left for entire itinerary
self.index, self.distance, self.distance_left = None, None, None
self.reset()
self.hash = str2sha1("#".join(str(self.targets)))
self.uncertainty_delayed_index = []
self.scheduler_delayed_index = []
self.links_this_tick = []
def tick(self, tick_distance):
"""Ticks this itinerary for moving to the next state."""
if self.is_completed:
return
cur_index = self.index
if type(self.targets[cur_index]) is not HoldLink:
self.distance_left = max(0, self.distance_left - tick_distance)
# Change current link if passing the last ones
next_index, distance, _ = self.get_next_location(tick_distance)
self.index = next_index
self.distance = distance
# return the link that it has passes
return self.targets[cur_index:next_index]
"""
Get the next location, update the link the aircraft finished as well
@return index, distance, Node
Return (None, None, None) if completed.
"""
def get_next_location(self, tick_distance):
# Return the last node in the itinerary if completed
self.links_this_tick = [] # reset the link list at beginning
completed_itinerary = self.length, 0, self.targets[-1].end
if self.is_completed:
return completed_itinerary
index, distance = self.index, self.distance
# Skip delays
if type(self.targets[index]) is HoldLink:
return self.index + 1, self.distance, self.current_precise_location
# Find the link which the next location is on
while tick_distance >= self.targets[index].length - distance:
tick_distance -= self.targets[index].length - distance
self.links_this_tick.append(self.targets[index]) # append the link the aircraft will go through
index += 1
distance = 0
# Return the last node in the itinerary if completed
while index < self.length and type(self.targets[index]) is HoldLink:
index += 1
if index >= self.length:
return completed_itinerary
# Update the distance on the link
return index, distance + tick_distance, self.targets[index].get_middle_node(distance + tick_distance)
def get_nth_target(self, n):
""" Returns the nth link/target of the route/targets """
if n >= self.length:
return None
return self.targets[n]
# def get_ahead_intersections_and_link(self, ahead_distance):
# """get the intersections and future links with certain distance"""
# ahead_intersections = []
# ahead_links = []
# index, distance = self.index, self.distance
# while ahead_distance >= self.targets[index].length - distance:
# if type(self.targets[index]) is HoldLink:
# index += 1
# continue
# ahead_distance -= self.targets[index].length - distance
# ahead_intersections.append(self.targets[index].end)
# ahead_links.append(self.targets[index])
# index += 1
# distance = 0
# if index >= self.length:
# break
# return ahead_intersections, ahead_links
def get_ahead_intersections_and_link(self, ahead_distance):
"""get the intersections and future links with certain distance"""
ahead_intersections = []
distances_to_intersections = []
index, distance = self.index, self.distance
relative_distance = -distance
while ahead_distance >= self.targets[index].length - distance:
if type(self.targets[index]) is HoldLink:
index += 1
continue
ahead_distance -= self.targets[index].length - distance
ahead_intersections.append(self.targets[index].end)
relative_distance += self.targets[index].length
distances_to_intersections.append(relative_distance)
index += 1
distance = 0
if index >= self.length:
break
return ahead_intersections, distances_to_intersections
def __add_delay(self):
if self.is_completed:
return None
self.targets.insert(0, HoldLink())
return self.targets[0]
def add_uncertainty_delay(self, amount=1):
"""Adds `amount` of uncertainty delays at the head of this itinerary.
"""
for _ in range(amount):
self.__update_delayed_index(self.index)
self.uncertainty_delayed_index.append(self.index)
self.__add_delay()
def add_scheduler_delay(self):
"""Adds a single scheduler delay at the head of this itinerary."""
self.__update_delayed_index(self.index)
self.scheduler_delayed_index.append(self.index)
return self.__add_delay()
def __update_delayed_index(self, new_index):
for i in range(len(self.uncertainty_delayed_index)):
if self.uncertainty_delayed_index[i] >= new_index:
self.uncertainty_delayed_index[i] += 1
for i in range(len(self.scheduler_delayed_index)):
if self.scheduler_delayed_index[i] >= new_index:
self.scheduler_delayed_index[i] += 1
def reset(self):
"""Reset the index of this itinerary."""
self.index = 0
self.distance = self.unfinished_distance
self.distance_left = -self.unfinished_distance # distance till destination
for link in self.targets:
self.distance_left += link.length
@property
def length(self):
"""Returns the length of this itinerary."""
return len(self.targets)
@property
def is_delayed_by_uncertainty(self):
"""Returns true if the next tick is delayed by the uncertainty."""
if self.next_target is None or self.index <= 0:
return False
return self.index - 1 in self.uncertainty_delayed_index
@property
def is_delayed_by_uncertainty_now(self):
"""Returns true if the current tick is delayed by the uncertainty."""
return self.index in self.uncertainty_delayed_index
@property
def is_delayed_by_scheduler(self):
"""Returns true if the next tick is delayed by the scheduler."""
if self.next_target is None or self.index <= 0:
return False
return self.index - 1 in self.scheduler_delayed_index
@property
def is_delayed(self):
"""Returns true if the next tick is delayed."""
return self.is_delayed_by_scheduler or self.is_delayed_by_uncertainty
@property
def current_target(self):
"""Returns the current target."""
index = self.index
while index < self.length and type(self.targets[index]) is HoldLink:
index += 1
if self.is_completed:
return None
return self.targets[index]
@property
def current_target_index(self):
"""Returns the current target."""
index = self.index
while index < self.length and type(self.targets[index]) is HoldLink:
index += 1
return index
@property
def current_distance(self):
"""Returns the current target."""
if self.is_completed:
return None
return self.distance
@property
def current_coarse_location(self):
"""Returns the current location (the end node of current target/link)."""
index = self.index
while index < self.length and type(self.targets[index]) is HoldLink:
index += 1
if self.is_completed:
return self.targets[-1].end
return self.targets[index].end
@property
def current_precise_location(self):
"""Returns the current location (the precise node of current target/link)."""
index = self.index
while index < self.length and type(self.targets[index]) is HoldLink:
index += 1
if self.is_completed:
return self.targets[-1].end
return self.targets[index].get_middle_node(self.distance)
@property
def next_target(self):
"""Returns the next target."""
if self.index >= self.length - 1:
return None
return self.targets[self.index + 1]
@property
def is_completed(self):
"""Returns true if this itinerary had been completed."""
return self.index >= self.length
@property
def n_scheduler_delay(self):
"""Returns the number of delays added by the scheduler."""
return len(self.scheduler_delayed_index)
@property
def n_uncertainty_delay(self):
"""Returns the number of delays added by the uncertainty."""
return len(self.uncertainty_delayed_index)
@property
def n_future_uncertainty_delay(self):
"""Returns the number of delays added by the uncertainty from now on.
"""
return len([i for i in self.uncertainty_delayed_index
if i >= self.index])
@property
def detailed_description(self):
return "index=" + str(self.index) + \
", distance=" + str(self.distance) + \
", distance_left=" + str(self.distance_left) + \
", targets=" + "\n".join([link.detailed_description for link in self.targets])
def __repr__(self):
return "<Itinerary: %d target>" % len(self.targets)
def __hash__(self):
return self.hash
def __eq__(self, other):
return self.hash == other.hash
def __ne__(self, other):
return not self == other
@property
def distance_to_intersection(self):
# get the distance to the next intersection (the end of current link)
current_target = self.targets[self.index]
print (current_target.detailed_description)
if type(current_target) is HoldLink:
return 0
else:
return current_target.length - self.distance
@property
def distance_to_intersection_point(self):
# get the distance to the next intersection point in the map
idx = -1
distance = 0
for i in range(self.index, len(self.targets)):
# if type(self.targets[i]) is PushbackWay:
# continue
target_nodes = self.targets[i].nodes
if len(target_nodes) <= 0:
continue
target_node = target_nodes[0]
if type(target_node) is not Node:
continue
distance += self.targets[i].length
if target_node.name.startswith("I"):
idx = i
break
if idx == -1:
return 0
elif type(self.targets[idx]) is HoldLink:
return 0
else:
return distance