问题
I'm writing a program, that takes 200 planes distributed with the poisson distribution over 12 hours and these planes need to land at an airfield that only has 1 runway. I use the reverse CDF method from the exponential distribution to determine the inter-arrival times. However, I can't seem to calculate the waiting time in the air.
E.g. a plane arrives at 100 seconds, takes 75 seconds to land and is done at 175 seconds. Plane 2 arrives at 150 seconds and must wait 175-150 = 25 seconds. How do I program a variable that my function can output, that takes into consideration that the waiting time also can be 0 seconds?
import math
import random
import numpy as np
def variable(amount_of_planes):
plane_number = []
time_between_planes = []
plane_arrival_time = []
plane_arrival = 0
time_for_landing = np.array(random.choices([15, 45, 75, 105, 135, 165, 195, 225, 255, 285], weights=[0, 8, 16.5, 30.5, 20.5, 12.5, 5, 4, 3, 0], k=amount_of_planes))
waiting_time = []
for i in range(amount_of_planes):
plane_number.append(i)
waiting_time.append(i)
#Take a random value from a uniform spread probability from 0 to 1
n = random.random()
#Generate time between plane arrivals by using the reverse cdf method
time_between_planes = -math.log(1.0 - n) / 0.00462962962
plane_arrival_time.append(time_between_planes)
#Add the inter-event time to the running sum to get the next absolute event time
plane_arrival = plane_arrival + time_between_planes
plane_arrival_time.append(plane_arrival)
#My attemt at determining waiting time
done_with_landing = 0
if done_with_landing > plane_arrival:
plane_waiting_time = done_with_landing - plane_arrival
else:
plane_waiting_time = 0
done_with_landing = plane_arrival + plane_waiting_time + time_for_landing[i]
print(plane_arrival, done_with_landing, abs(plane_waiting_time), time_for_landing[i])
The reason I need the waiting times is to argue wheter it makes sense for this simulated airport to build another runway. In this project I don't care about other planes taking off from the runway.
回答1:
This is a single-server queueing system, where the server is the runway. General discrete event systems such as this can be programmed using event scheduling, based on a priority queue to determine what's the next thing to happen. You can read the PDF file at this github repository for a fairly complete discussion.
However, the single server queue has purely sequential logic and can be implemented as a loop using the following recurrence relations:
- arrival_timei ← arrival_timei-1 + interarrival_timei
- start_landing_timei ← max(arrival_timei, finish_landing_timei-1)
- finish_landing_timei ← start_landing_timei + landing_timei
In words, from a plane's perspective
- you have an arrival event
interarrival_time
time units after the last plane arrived (this is your Poisson process); - you can start landing procedures either when you arrive, or when the previous plane finishes landing, whichever comes later; and
- the landing is actually completed
landing_time
time units after you started landing.
You can initialize the imaginary zero'th airplane's arrival_time
and finish_landing
to be 0.0, put the logic outlined above in a loop, and iterate through either the specified number of aircraft or until the stopping time is reached. Since the logic is purely sequential, you can lose the indices and just recycle the variables. In pseudocode, and assuming that interarrival_time()
and landing_time()
are iterators which cough up the corresponding next value on demand:
N = 200 # number of airplanes
arrival_time = finish_landing = 0.0
N times:
arrival_time += interarrival_time()
start_landing = max(arrival_time, finish_landing)
finish_landing = start_landing + landing_time()
The aircraft's delay is either start_landing - arrival_time
if you're interested in how long the plane spent "in line", or finish_landing - arrival_time
if you want to know how long the plane was "in the system". Place the corresponding statement in the appropriate location in the loop, and use the resulting data however you see fit.
来源:https://stackoverflow.com/questions/65004262/determine-waiting-times-between-plane-arrivals-python