ENGR 102-214 (Socolofsky) Week 14 Python scripts · ENGR 102-214 (Socolofsky) Week 14 Python...
Transcript of ENGR 102-214 (Socolofsky) Week 14 Python scripts · ENGR 102-214 (Socolofsky) Week 14 Python...
ENGR 102-214 (Socolofsky) Week 14
Python scripts
During the lecture this week, we are learning to create user-defined classes that allow us to create
our own objects in our codes. As an example, we convert the program and functions to simulate
an airport runway developed during Week 12 from a modular, functional program design to one
using object-oriented design. In the lecture notes, we outline two class definitions that we will
create, one called RunwayQueue, which we will use to create the take-off and landing queue objects,
and one called Runway, which will manages the two queues at a single runway. Both classes have
methods to report the object status and compute the object statistics. Our program also includes
additional functions to request the simulation parameters, print a status report, and print the
simulation statistics.
The script file runway objects.py contains the two class definitions and definitions of the
helper functions. A separate script test runway.py provides a set of tests using objects created
from these two classes. The script file runway sim.py creates the simulation of an airport runway
using the helper functions and using objects initialized from these classes. The file listings and
example code execution results follow below. See the script files for Week 12 for a more detailed
explanation of the problem statement and solution strategy.
Listing for runway objects.py
1 # runway_objects.py
2 #
3 # Python script to simulate an airport runway using classes and objects
4 #
5 # Convert the airport runway simulation from the modular programming
6 # example using functions during Week 12 to an example using object -oriented
7 # programming.
8 #
9 # S. Socolofsky
10 # ENGR 102
11 # November 2018
12
13 from scipy.stats import poisson
14
15 class RunwayQueue(object ):
16 """
17 Class to create queues that use an airport runway
18
19 Attributes
20 ----------
21 queue : list
22 Python list of planes waiting in the queue. The value stored in the
23 list is the number of minutes the plane has been in the queue.
24 arrival_rate : float
25 Average number of planes arriving in the queue per hour (#/hr)
26 activity_time : float
27 Time required (min) when a plane from this queue uses the runway
28 tally_list : list
29 List of all planes that were in the queue. The value stored in the
30 list is the total number of minutes the plane was in the queue ,
31 including the time spent using the runway.
32
33 Methods
34 -------
35 arrival(delta_t=1)
36 Method to add planes to a runway queue. Adds planes to the queue
37 following a Poisson process. Has no return value.
38 waiting(delta_t=1)
39 Update the wait times for all planes in a queue. Adds delta_t to the
40 wait times of all planes currently in the queue. Has no return value.
41 use_runway ()
42 Remove a plane from a queue and occupy the runway. Removes the first
43 plane in the queue and puts that plane's wait time in the tally_list.
44 Returns the time the runway will be occupied.
45 status ()
46 Report the status of the present runway queue. Returns a list
47 containing the number of planes in the queue and the wait time of the
48 plane at the head of the queue.
49 statistics ()
2
50 Compute and return the statistics for this runway queue. Returns a
51 list containing the total number of planes that were in the queue ,
52 their average wait times , and the maximum wait time. Uses the
53 tally_list to compute statistics.
54
55 """
56 def __init__(self , arrival_rate , activity_time , queue =[]):
57 """
58 Method to instantiate RunwayQueue objects
59
60 Creates an instance of a RunwayQueue class object
61
62 Parameters
63 ----------
64 arrival_rate : float
65 Average number of planes arriving in the queue per hour (#/hr)
66 activity_time : float
67 Time required (min) when a plane from this queue uses the runway
68 queue : list , default =[]
69 Python list of planes waiting in the queue. The value stored in
70 the list is the number of minutes the plane has been in the queue.
71
72 """
73 super(RunwayQueue , self). __init__ ()
74
75 # Store the relevant object attributes passed as parameters
76 self.arrival_rate = arrival_rate
77 self.activity_time = activity_time
78 self.queue = queue
79
80 # Instantiate other object attributes
81 self.tally_list = []
82
83 def arrival(self , delta_t=1):
84 """
85 Method to add planes to a runway queue
86
87 Uses a Poisson distribution random number generator to simulate the
88 arrival of planes in a runway queue.
89
90 Parameters
91 ----------
92 delta_t : int , default=1
93 Interval to simulate (min)
94
95 """
96 # Get the random numbers from the Poisson distribution
97 r = poisson.rvs(float(self.arrival_rate) / 60., size=delta_t)
98
99 # Add all new planes to the queue with their wait times
100 for i in range(len(r)):
101 if r[i] > 0:
102 self.queue.append(delta_t - i - 1)
3
103
104 def waiting(self , delta_t=1):
105 """
106 Update the wait times for the planes in the queue
107
108 Add the simulation time step to the queue wait times
109
110 Parameters
111 ----------
112 delta_t : int , default=1
113 Amount of time to add to each plane in the queue (min)
114
115 """
116 # Add delta_t to each element in the queue
117 for i in range(len(self.queue )):
118 self.queue[i] += delta_t
119
120 def use_runway(self):
121 """
122 Remove a plane from the queue and occupy the runway
123
124 Simulates a plane leaving the queue and occupying the runway (e.g.,
125 for take off or landing ). Returns the time span that the runway will
126 be occupied for the present event. This method also stores the total
127 time this plane waited in the queue in the object tally_list.
128
129 Returns
130 -------
131 event_duration : int
132 Time it takes (min) for planes to take off or land
133
134 """
135 # Remove the first plane in the queue and get its wait time
136 queue_time = self.queue.pop(0)
137
138 # Add the time for using the runway
139 queue_time += self.activity_time
140
141 # Put this plane into the tally list of planes that have exited
142 # the queue
143 self.tally_list.append(queue_time)
144
145 # Return the time the runway will be occupied by this plane
146 return self.activity_time
147
148 def status(self):
149 """
150 Report the status of the present runway queue
151
152 Reports the number of planes in the queue and the total time (min)
153 that the plane in the front of the line has been waiting in the queue.
154
155 Returns
4
156 -------
157 queue_len : int
158 The number of planes waiting in the present queue
159 wait_time : int
160 The number of minutes the plane at the front of the line has been
161 waiting in this queue.
162
163 """
164 # Get the correct wait time
165 if len(self.queue) == 0:
166 wait_time = 0
167 else:
168 wait_time = self.queue[0]
169
170 return (len(self.queue), wait_time)
171
172 def statistics(self):
173 """
174 Compute and return the statistics for this runway queue
175
176 Computes the total number of planes than occupied the queue with their
177 average wait times and the maximum wait time. The average wait time
178 is always the average time for planes that have exited the queue. For
179 the maximum wait time , this method returns the longest wait time for
180 any plane that has left the queue or is still waiting in the queue.
181
182 Returns
183 -------
184 num : int
185 Number of planes that have been in the queue. Planes currently
186 waiting in the queue are *not* counted.
187 ave_wait : int
188 Average wait time of planes that have exited the queue (min).
189 max_wait : int
190 Maximum wait time of all planes that have passed through the
191 queue , including planes presently in the queue (min).
192
193 """
194 # Count the planes that have left the queue
195 num = len(self.tally_list)
196
197 # Get the wait time of the plane at the head of the queue
198 if len(self.queue) > 0:
199 wait_now = self.queue[0]
200 else:
201 # No planes are presently in the queue
202 wait_now = 0
203
204 # Compute average and maximum wait times
205 if num == 0:
206 # No planes have left the queue
207 ave_wait = 0.
208 max_wait = wait_now
5
209 else:
210 # Return statistics of the planes that were in the queue
211 ave_wait = sum(self.tally_list) / num
212 queue_wait = max(self.tally_list)
213 if queue_wait > wait_now:
214 # The longest wait was for a plane that left the queue
215 max_wait = queue_wait
216 else:
217 # The plane at the head of the present queue is waiting the
218 # longest
219 max_wait = wait_now
220
221 return (num , ave_wait , max_wait)
222
223 class Runway(object ):
224 """
225 Class to manage queues that use an airport runway
226
227 Class definitions for an object that manages the take -off and landing
228 queues at an airport runway.
229
230 Attributes
231 ----------
232 landing : RunwayQueue object
233 RunwayQueue object that manages the landing queue at this runway.
234 takeoff : RunwayQueue object
235 RunwayQueue object that manages the take -off queue at this runway.
236 timer : int
237 Counter that keeps track of the amount of time the runway is
238 occupied for a take -off or landing event (min)
239
240 Methods
241 -------
242 tower(delta_t=1)
243 Simulate the action of the airport tower controlling the runway. If
244 timer is zero , the tower will check the landing and takeoff queues ,
245 giving the landing queue priority for landing. If a plane occupies
246 the runway , the timer will be set to the time required for that plane
247 to use the runway. If the timer is not zero , its time will be reduced
248 by delta_t. The wait times of all planes in queues will be updated by
249 delta_t. Has to return value.
250 status(t)
251 Reports the status of the runway and its queues. Returns a list
252 containing the simulation time t (min), number of planes in the
253 take -off queue (#), the wait time for the plane in the front of the
254 take -off queue (min), the number of planes in the landing queue (#),
255 the wait time for the plane in the front of the landing queue (min),
256 and the value of the runway timer (min).
257 statistics(t)
258 Computes and returns the present runway statistics. Returns a list
259 containing the simulation duration t (min), total number of take offs
260 (#), average wait in the take -off queue (min), maximum wait in the
261 take -off queue (min), total number of landings (#), average wait in
6
262 the landing queue (min), and maximum wait in the landing queue (min).
263
264 """
265 def __init__(self , l_rate , t_rate , l_dur , t_dur , l_list =[], t_list =[]):
266 """
267 Method to instantiate Runway objects
268
269 Creates an instance of a Runway class object. This object has two
270 RunwayQueue objects , one for the take -off queue and another for the
271 landing queue.
272
273 Parameters
274 ----------
275 l_rate : float
276 Average number of planes landing per hour (#/hr)
277 t_rate : float
278 Average number of planes taking off per hour (#/hr)
279 l_dur : float
280 Time required (min) for plane to land
281 t_dur : float
282 Time required (min) for plane to take off
283 l_list : list , default =[]
284 Python list of planes waiting in the landing queue. The value
285 stored in the list is the number of minutes the plane has been in
286 the queue.
287 t_list : list , default =[]
288 Python list of planes waiting in the takeoff queue. The value
289 stored in the list is the number of minutes the plane has been in
290 the queue.
291
292 """
293 super(Runway , self). __init__ ()
294
295 # Use the input parameters to create RunwayQueue objects for landing
296 # and takeoff
297 self.landing = RunwayQueue(l_rate , l_dur , l_list)
298 self.takeoff = RunwayQueue(t_rate , t_dur , t_list)
299
300 # Instantiate other object attributes
301 self.timer = 0.
302
303 def tower(self , delta_t=1):
304 """
305 Simulate the action of the airport tower controlling the runway
306
307 Simulate the action of the airport tower controlling the runway. If
308 timer is zero , the tower will check the landing and takeoff queues ,
309 giving the landing queue priority for landing. If a plane occupies
310 the runway , the timer will be set to the time required for that plane
311 to use the runway. If the timer is not zero , its time will be reduced
312 by delta_t. The wait times of all planes in queues will be updated by
313 delta_t. Has to return value.
314
7
315 Parameters
316 ----------
317 delta_t : int , default=1
318 Interval to simulate (min)
319
320 """
321 # Check for new planes arriving in queues
322 self.landing.arrival(delta_t)
323 self.takeoff.arrival(delta_t)
324
325 # Check the runway for activity and use if open and needed
326 if self.timer == 0:
327 # Runway is open for take off or landing
328
329 if len(self.landing.queue) > 0:
330 # Let planes land first
331 self.timer = self.landing.use_runway ()
332 print('Plane is landing ...')333
334 elif len(self.takeoff.queue) > 0:
335 # Or let planes take off
336 self.time = self.takeoff.use_runway ()
337 print('Plane is taking off...')338
339 else:
340 # The runway is occupied; decrease the timer by one minute
341 self.timer -= 1
342
343 # Update the runway queue waits by the present time step
344 self.landing.waiting(delta_t)
345 self.takeoff.waiting(delta_t)
346
347 def status(self , t):
348 """
349 Report the status of the runway and its queues
350
351 Returns a list of values summarizing the present status of the runway
352 and its queues.
353
354 Parameters
355 ----------
356 t : int
357 The present simulation time (min)
358
359 Returns
360 -------
361 data : list
362 A list of the values of the present runway parameters. This list
363 contains the simulation time (min), number of planes in the
364 take -off queue (#), the wait time for the plane in the front of
365 the take -off queue (min), the number of planes in the landing
366 queue (#), the wait time for the plane in the front of the landing
367 queue (min), and the value of the runway timer (min).
8
368
369 """
370 # Pull the data from its relevant sources
371 data = [t]
372 data = data + list(self.takeoff.status ())
373 data = data + list(self.landing.status ())
374 data.append(self.timer)
375
376 return data
377
378 def statistics(self , t):
379 """
380 Compute and return the present runway statistics
381
382 Returns a list of values summarizing the present statistics of the
383 runway and its queues.
384
385 Parameters
386 ----------
387 t : int
388 The present simulation time (min)
389
390 Returns
391 -------
392 data : list
393 A list of the values of the present runway statistics. This list
394 contains the simulation duration (min), total number of take offs
395 (#), average wait in the take -off queue (min), maximum wait in
396 the take -off queue (min), total number of landings (#), average
397 wait in the landing queue (min), and maximum wait in the landing
398 queue (min).
399 """
400 # Pull the data from its relevant sources
401 data = [t]
402 data = data + list(self.takeoff.statistics ())
403 data = data + list(self.landing.statistics ())
404
405 return data
406
407 # There are a few functions that still ought to be defined outside of these
408 # RunwayQueue and Runway objects. These are listed here.
409
410 def get_sim_parameters ():
411 """
412 Get the simulation parameters from the user
413
414 Ask the user to input the values for the average arrival rates of planes
415 and durations for take -off and landing. Uses console input to obtain
416 parameter values.
417
418 Returns
419 -------
420 t_dur : float
9
421 Time required (min) for plane to take off
422 l_dur : float
423 Time required (min) for plane to land
424 t_rate : float
425 Average number of planes taking off (#/hr)
426 l_rate : float
427 Average number of plane landing (#/hr)
428 sim_dur : float
429 The desired simulation duration (hr)
430
431 """
432 print('\nEnter the parameter values of the runway simulation:')433 t_dur = float(input(
434 ' Time required to take off (min): '))435 l_dur = float(input(
436 ' Time required to land (min): '))437 t_rate = float(input(
438 ' Average number of planes taking off per hour: '))439 l_rate = float(input(
440 ' Average number of planes landing per hour: '))441 sim_dur = float(input(
442 '\nEnter the number of hours to simulate: '))443
444 return (t_dur , l_dur , t_rate , l_rate , sim_dur)
445
446 def print_report(t, runway ):
447 """
448 Report the status of each queue and the runway
449
450 Reports the number of planes and their queue wait times for planes in the
451 take -off and landing queues together with the simulation time and the
452 timer value for the runway activity timer.
453
454 Parameters
455 ----------
456 t : int
457 The present simulation time (min)
458 runway : Runway object
459 Runway object that contains the present takeoff and landing queues and
460 the runway timer.
461
462 Returns
463 -------
464 text : str
465 String containing the information printed to the screen. This string
466 could be used , for instance , to write a file to store the simulation
467 results.
468
469 """
470 col_widths = [6, 14, 13, 14, 13, 15]
471 if t == 0:
472 # Print the header information
473 cols = ['Time', 'Takeoff Queue', 'Takeoff Wait', 'Landing Queue',
10
474 'Landing Wait', 'Runway Activity ']475 units = ['(min)', '(#)', '(min)', '(#)', '(min)', '(min)']476 for i in range(len(cols )):
477 cols[i] = cols[i].ljust(col_widths[i])
478 units[i] = units[i]. center(col_widths[i])
479 header = ['| '.join(cols) + '\n']480 header.append('| '.join(units) + '\n')481 header = ''.join(header)482 text = header
483
484 else:
485 # Create a list of the current runway state
486 data = runway.status(t)
487
488 # Create a string of data to print
489 for i in range(len(data )):
490 data[i] = str(data[i]). center(col_widths[i])
491 data = '| '.join(data) + '\n'492 text = data
493
494 return text
495
496 def print_stats(t, runway ):
497 """
498 Parameters
499 ----------
500 t : int
501 The present simulation time (min)
502 runway : Runway object
503 Runway object that contains the present takeoff and landing queues and
504 the runway timer.
505
506 """
507 # Get the statistics from the runway object
508 data = runway.statistics(t)
509
510 # Print the statistics
511 print('\nSimulation statistics:')512 print(' Simulation duration (hr) = ', data[0]/60.)
513 print('\n Number of take -offs = ', data[1])
514 print(' Average wait in take -off queue %4.4g (min)' % data[2])
515 print(' Maximum wait in take -off queue %4.4g (min)' % data[3])
516 print('\n Number of landings = ', data[4])
517 print(' Average wait in landing queue %4.4g (min)' % data[5])
518 print(' Maximum wait in landing queue %4.4g (min)' % data[6])
519
520 return data
11
Listing for test runway.py
1 #!/usr/bin/env python3
2 # -*- coding: utf -8 -*-
3 """
4 Script: runway_tests.py
5
6 Provides unit testing of class objects defined in the `runway_objects ` module
7 in a format that can be used by the `pytest ` module of Python.
8
9 Created on Tue Nov 12 11:09:50 2019
10 @author: Scott A. Socolofsky
11 <[email protected] >
12 """
13
14 import runway_objects as ro
15
16 # ----------------------------------------------------------------------------
17 # Helper functions
18 # ----------------------------------------------------------------------------
19
20
21 def define_simulation_parameters ():
22 """
23 Return a sample set of simulation parameters
24
25 Return sample values for the takeoff and landing rates and durations
26
27 Returns
28 -------
29 landing_rate : int
30 Rate of planes entering the landing queue (#/hr)
31 landing_dur : int
32 Number of minutes required for a plane to land
33 takeoff_rate : int
34 Rate of planes entering the take -off queue (#/hr)
35 takeoff_dur : int
36 Number of minutes required for a plane to take off
37
38 """
39 # Set parameter values
40 landing_rate = 8
41 landing_dur = 3
42 takeoff_rate = 8
43 takeoff_dur = 2
44
45 return (landing_rate , landing_dur , takeoff_rate , takeoff_dur)
46
47
48 # ----------------------------------------------------------------------------
49 # Unit tests
12
50 # ----------------------------------------------------------------------------
51
52 def test_RunwayQueue_obj ():
53 """
54 Test creating and using a RunwayQueue object
55
56 """
57 # Get the simulation parameters
58 landing_rate , landing_dur , takeoff_rate , takeoff_dur = \
59 define_simulation_parameters ()
60
61 # Create an empty queue list
62 l_list = []
63
64 # Create the RunwayQueue object
65 print('\nCreate a RunwayQueue object for landing ...')66 landing = ro.RunwayQueue(landing_rate , landing_dur , l_list)
67
68 # Print the object attributes
69 print('Object attributes are:')70 print(' Landing Rate: ', landing.arrival_rate)
71 print(' Landing Duration: ', landing.activity_time)
72 print(' Landing Queue: ', landing.queue)
73 print(' Tally List: ', landing.tally_list)
74
75 # Check that object attributes have correct values
76 assert landing.arrival_rate == landing_rate
77 assert landing.activity_time == landing_dur
78 assert len(landing.queue) == len(l_list)
79 assert len(landing.tally_list) == 0
80
81 # Test adding planes to the landing queue
82 print('\nSimulate landing queue until we have at least five planes ...')83 while len(landing.queue) < 5:
84 landing.arrival ()
85 landing.waiting ()
86 print('At end of interval:')87 print(' Landing Queue: ', landing.queue)
88
89 # Test adding time to the landing queue wait times
90 print('\nAdd 5 minutes to the landing queue wait times ...')91 old_queue = landing.queue.copy()
92 landing.waiting(5)
93 print('The new wait list is:')94 print(' Landing Queue: ', landing.queue)
95
96 # Check that the operation was correct
97 for i in range(len(old_queue )):
98 assert old_queue[i] + 5 == landing.queue[i]
99
100 # Simulating letting a plane land on the runway
101 print('\nLand the plane at the head of the landing list ...')102 old_queue = landing.queue.copy()
13
103 old_tally = landing.tally_list.copy()
104 timer = landing.use_runway ()
105 print('After sending plane to runway:')106 print(' Landing Queue: ', landing.queue)
107 print(' Tally List: ', landing.tally_list)
108 print(' Runway Timer: ', timer)
109
110 # Check that the operation was correct
111 for i in range(len(landing.queue )):
112 assert landing.queue[i] == old_queue[i+1]
113 for i in range(len(old_tally )):
114 assert landing.tally_list[i] == old_tally[i]
115 assert landing.tally_list[-1] == old_queue[0] + landing.activity_time
116 assert timer == landing.activity_time
117
118 # Request the present landing queue status
119 print('\nAsk the present landing queue for status ...')120 data = landing.status ()
121 print('The landing queue status is: ')122 print(' Landing Queue: ', landing.queue)
123 print(' Queue length: ', data[0])
124 print(' Current Wait: ', data[1])
125
126 # Check the results are correct
127 assert data[0] == len(landing.queue)
128 assert data[1] == landing.queue[0]
129
130 # Request the landing queue statistics
131 print('\nGet the landing queue statistics ...')132 data = landing.statistics ()
133 print('The landing queue statistics are: ')134 print(' Landing Queue: ', landing.queue)
135 print(' Tally List: ', landing.tally_list)
136 print(' Number landed: ', data[0])
137 print(' Average wait: ', data[1])
138 print(' Maximum wait: ', data[2])
139
140 # Check the results
141 if landing.queue[0] > max(landing.tally_list ):
142 max_w = landing.queue[0]
143 else:
144 max_w = max(landing.tally_list)
145 assert data[0] == len(landing.tally_list)
146 assert data[1] == sum(landing.tally_list) / len(landing.tally_list)
147 assert data[2] == max_w
148
149
150 def test_Runway_obj ():
151 """
152 Test creating and using a Runway object
153
154 """
155 # Get the simulation parameters
14
156 landing_rate , landing_dur , takeoff_rate , takeoff_dur = \
157 define_simulation_parameters ()
158
159 # Create an empty queue list
160 l_list = []
161 t_list = [4, 3, 2]
162
163 # Test creating a Runway object
164 print('\nCreate a Runway object ...')165 rw_01 = ro.Runway(landing_rate , takeoff_rate , landing_dur , takeoff_dur ,
166 l_list , t_list)
167 print('Object attributes are:')168 print(' Takeoff Rate: ', rw_01.takeoff.arrival_rate)
169 print(' Takeoff Duration: ', rw_01.takeoff.activity_time)
170 print(' Takeoff Queue: ', rw_01.takeoff.queue)
171 print(' Landing Rate: ', rw_01.landing.arrival_rate)
172 print(' Landing Duration: ', rw_01.landing.activity_time)
173 print(' Landing Queue: ', rw_01.landing.queue)
174 print(' Runway Timer: ', rw_01.timer)
175
176 # Check the results are correct
177 assert rw_01.takeoff.arrival_rate == takeoff_rate
178 assert rw_01.takeoff.activity_time == takeoff_dur
179 assert len(rw_01.takeoff.queue) == len(t_list)
180 for i in range(len(t_list )):
181 assert rw_01.takeoff.queue[i] == t_list[i]
182 assert rw_01.landing.arrival_rate == landing_rate
183 assert rw_01.landing.activity_time == landing_dur
184 assert len(rw_01.landing.queue) == len(l_list)
185 for i in range(len(l_list )):
186 assert rw_01.landing.queue[i] == l_list[i]
187
188 # Test using Runway
189 print('\nCall the tower and use runway for 15 minutes ...')190 for i in range(15):
191 rw_01.tower()
192 print('At end of interval:')193 print(' Takeoff Queue: ', rw_01.takeoff.queue)
194 print(' Takeoff Tally: ', rw_01.takeoff.tally_list)
195 print(' Landing Queue: ', rw_01.landing.queue)
196 print(' Landing Tally: ', rw_01.landing.tally_list)
197 print(' Runway Timer: ', rw_01.timer)
198
199 # Request the present runway status
200 print('\nAsk the runway for its present status ...')201 data = rw_01.status(15)
202 print('The status report is:')203 print(' Simulation time: ', data[0])
204 print(' Takeoff Queue: ', rw_01.takeoff.queue)
205 print(' Length: ', data[1])
206 print(' Wait: ', data[2])
207 print(' Landing Queue: ', rw_01.landing.queue)
208 print(' Length: ', data[3])
15
209 print(' Wait: ', data[4])
210 print(' Runway Timer: ', data[5])
211
212 # Check the results are right
213 if len(rw_01.takeoff.queue) == 0:
214 twait = 0
215 else:
216 twait = rw_01.takeoff.queue[0]
217 if len(rw_01.landing.queue) == 0:
218 lwait = 0
219 else:
220 lwait = rw_01.landing.queue[0]
221 assert data[0] == 15
222 assert data[1] == len(rw_01.takeoff.queue)
223 assert data[2] == twait
224 assert data[3] == len(rw_01.landing.queue)
225 assert data[4] == lwait
226 assert data[5] == rw_01.timer
227
228 # Check the runway statistics
229 print('\Get the runway statistics ...')230 data = rw_01.statistics(15)
231 print('\nThe runway statistics are:')232 print(' Simulation time: ', data[0])
233 print(' Takeoff Queue: ', rw_01.takeoff.queue)
234 print(' Tally List: ', rw_01.takeoff.tally_list)
235 print(' Took Off: ', data[1])
236 print(' Average Wait: ', data[2])
237 print(' Maximum wait: ', data[3])
238 print(' Landing Queue: ', rw_01.landing.queue)
239 print(' Tally List: ', rw_01.landing.tally_list)
240 print(' Landed: ', data[4])
241 print(' Average Wait: ', data[5])
242 print(' Maximum wait: ', data[6])
243
244 # Check the results are right
245 assert data[0] == 15
246 assert data[1] == len(rw_01.takeoff.tally_list)
247 if len(rw_01.takeoff.queue) > 0:
248 if rw_01.takeoff.queue[0] > max(rw_01.takeoff.tally_list ):
249 max_w = rw_01.takeoff.queue[0]
250 else:
251 max_w = max(rw_01.takeoff.tally_list)
252 else:
253 max_w = max(rw_01.takeoff.tally_list)
254 assert data[2] == sum(rw_01.takeoff.tally_list) \
255 / len(rw_01.takeoff.tally_list)
256 assert data[3] == max_w
257 assert data[4] == len(rw_01.landing.tally_list)
258 if len(rw_01.landing.queue) > 0:
259 if rw_01.landing.queue[0] > max(rw_01.landing.tally_list ):
260 max_w = rw_01.landing.queue[0]
261 else:
16
262 max_w = max(rw_01.landing.tally_list)
263 else:
264 max_w = max(rw_01.landing.tally_list)
265 assert data[5] == sum(rw_01.landing.tally_list) \
266 / len(rw_01.landing.tally_list)
267 assert data[6] == max_w
268
269
270 if __name__ == '__main__ ':271
272 # Print a header message
273 print('\nTesting script for RunwayQueue and Runway objects ')274 print('================================================= ')275
276 # Run each unit test
277 test_RunwayQueue_obj ()
278 test_Runway_obj ()
17
Code execution for test runway.py executes the test cases. Sample output is given in the follow-ing:
Testing script for RunwayQueue and Runway objects
=================================================
Create a RunwayQueue object for landing...
Object attributes are:
Landing Rate: 8
Landing Duration: 3
Landing Queue: []
Tally List: []
Simulate landing queue until we have at least five planes...
At end of interval:
Landing Queue: [43, 32, 28, 19, 1]
Add 5 minutes to the landing queue wait times...
The new wait list is:
Landing Queue: [48, 37, 33, 24, 6]
Land the plane at the head of the landing list...
After sending plane to runway:
Landing Queue: [37, 33, 24, 6]
Tally List: [51]
Runway Timer: 3
Ask the present landing queue for status...
The landing queue status is:
Landing Queue: [37, 33, 24, 6]
Queue length: 4
Current Wait: 37
Get the landing queue statistics...
The landing queue statistics are:
Landing Queue: [37, 33, 24, 6]
Tally List: [51]
Number landed: 1
Average wait: 51.0
Maximum wait: 51
Create a Runway object...
Object attributes are:
Takeoff Rate: 8
Takeoff Duration: 2
Takeoff Queue: [4, 3, 2]
Landing Rate: 8
18
Landing Duration: 3
Landing Queue: []
Runway Timer: 0.0
Call the tower and use runway for 15 minutes...
Plane is taking off...
Plane is taking off...
Plane is taking off...
Plane is taking off...
Plane is taking off...
Plane is landing...
Plane is landing...
Plane is landing...
At end of interval:
Takeoff Queue: [3]
Takeoff Tally: [6, 6, 6, 4, 4]
Landing Queue: [5]
Landing Tally: [3, 5, 8]
Runway Timer: 3
Ask the runway for its present status...
The status report is:
Simulation time: 15
Takeoff Queue: [3]
Length: 1
Wait: 3
Landing Queue: [5]
Length: 1
Wait: 5
Runway Timer: 3
\Get the runway statistics...
The runway statistics are:
Simulation time: 15
Takeoff Queue: [3]
Tally List: [6, 6, 6, 4, 4]
Took Off: 5
Average Wait: 5.2
Maximum wait: 6
Landing Queue: [5]
Tally List: [3, 5, 8]
Landed: 3
Average Wait: 5.333333333333333
Maximum wait: 8
19
Listing for runway sim.py
1 # runway_sim.py
2 #
3 # Simulate a single runway using the RunwayQueue and Runway objects.
4 #
5 # Simulate queues of airplanes waiting to take -off and land at a single
6 # runway. Uses the RunwayQueue and Runway objects defined in
7 # runway_objects.py and their methods.
8 #
9 # S. Socolofsky
10 # ENGR 102
11 # November 2018
12
13 import runway_objects
14
15 # Introduce program
16 print('\nRunway Simulator ')17 print('================ ')18 print('\nSimulates take -off and landing queues for a single runway ')19 print(' The simulation uses a time -step of one minute ')20 print(' and updates the take -off and landing queues each')21 print(' minute while also simulating runway activity.')22
23 # Get the parameters of the airport and runway
24 takeoff_dur , landing_dur , takeoff_rate , landing_rate , sim_dur = \
25 runway_objects.get_sim_parameters ()
26
27 # Create the runway object with these queue parameters
28 runway = runway_objects.Runway(landing_rate , takeoff_rate , landing_dur ,
29 takeoff_dur)
30
31 # Simulate the runway in one -minute intervals
32 t = 0
33
34 # Print a header for the output information
35 print(' ')36 print(runway_objects.print_report(t, runway), end='')37
38 # Run the simulation
39 for i in range(int(sim_dur * 60)):
40
41 # Ask the tower to manage the queues and runway
42 runway.tower ()
43
44 # Update the simulation time
45 t += 1
46
47 # Print runway status
48 print(runway_objects.print_report(t, runway), end='')49
20
50 # Report the statistics of the runway simulation.
51 stats = runway_objects.print_stats(t, runway)
21
Code execution for runway sim.py executes the runway simulation. Sample output for a one-hoursimulation is given in the following:
Runway Simulator
================
Simulates take-off and landing queues for a single runway
The simulation uses a time-step of one minute
and updates the take-off and landing queues each
minute while also simulating runway activity.
Enter the parameter values of the runway simulation:
Time required to take off (min): 2
Time required to land (min): 3
Average number of planes taking off per hour: 8
Average number of planes landing per hour: 8
Enter the number of hours to simulate: 1
Time | Takeoff Queue | Takeoff Wait | Landing Queue | Landing Wait | Runway Activity
(min) | (#) | (min) | (#) | (min) | (min)
1 | 0 | 0 | 0 | 0 | 0.0
2 | 0 | 0 | 0 | 0 | 0.0
3 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
4 | 0 | 0 | 0 | 0 | 0.0
5 | 0 | 0 | 0 | 0 | 0.0
Plane is landing...
6 | 0 | 0 | 0 | 0 | 3.0
7 | 0 | 0 | 0 | 0 | 2.0
8 | 0 | 0 | 0 | 0 | 1.0
9 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
10 | 0 | 0 | 0 | 0 | 0.0
11 | 0 | 0 | 0 | 0 | 0.0
12 | 0 | 0 | 0 | 0 | 0.0
13 | 0 | 0 | 0 | 0 | 0.0
14 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
15 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
16 | 0 | 0 | 0 | 0 | 0.0
17 | 0 | 0 | 0 | 0 | 0.0
18 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
19 | 0 | 0 | 0 | 0 | 0.0
Plane is landing...
22
20 | 1 | 1 | 0 | 0 | 3.0
21 | 1 | 2 | 0 | 0 | 2.0
22 | 1 | 3 | 0 | 0 | 1.0
23 | 1 | 4 | 0 | 0 | 0.0
Plane is taking off...
24 | 0 | 0 | 0 | 0 | 0.0
25 | 0 | 0 | 0 | 0 | 0.0
26 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
27 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
28 | 0 | 0 | 0 | 0 | 0.0
29 | 0 | 0 | 0 | 0 | 0.0
30 | 0 | 0 | 0 | 0 | 0.0
31 | 0 | 0 | 0 | 0 | 0.0
32 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
33 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
34 | 0 | 0 | 0 | 0 | 0.0
35 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
36 | 0 | 0 | 0 | 0 | 0.0
37 | 0 | 0 | 0 | 0 | 0.0
38 | 0 | 0 | 0 | 0 | 0.0
39 | 0 | 0 | 0 | 0 | 0.0
Plane is taking off...
40 | 0 | 0 | 0 | 0 | 0.0
41 | 0 | 0 | 0 | 0 | 0.0
42 | 0 | 0 | 0 | 0 | 0.0
Plane is landing...
43 | 0 | 0 | 0 | 0 | 3.0
44 | 0 | 0 | 1 | 1 | 2.0
45 | 0 | 0 | 1 | 2 | 1.0
46 | 0 | 0 | 1 | 3 | 0.0
Plane is landing...
47 | 0 | 0 | 1 | 1 | 3.0
48 | 0 | 0 | 1 | 2 | 2.0
49 | 0 | 0 | 1 | 3 | 1.0
50 | 0 | 0 | 1 | 4 | 0.0
Plane is landing...
51 | 0 | 0 | 0 | 0 | 3.0
52 | 0 | 0 | 0 | 0 | 2.0
53 | 0 | 0 | 0 | 0 | 1.0
54 | 0 | 0 | 0 | 0 | 0.0
55 | 0 | 0 | 0 | 0 | 0.0
56 | 0 | 0 | 0 | 0 | 0.0
57 | 0 | 0 | 0 | 0 | 0.0
23
58 | 0 | 0 | 0 | 0 | 0.0
59 | 0 | 0 | 0 | 0 | 0.0
60 | 0 | 0 | 0 | 0 | 0.0
Simulation statistics:
Simulation duration (hr) = 1.0
Number of take-offs = 12
Average wait in take-off queue 2.333 (min)
Maximum wait in take-off queue 6 (min)
Number of landings = 5
Average wait in landing queue 4.4 (min)
Maximum wait in landing queue 7 (min)
24