Python – simulation of M / M / 1 queues

I would like to simulate a M / M / 1 queue where both the arrivals and the portions are Poisson processes. In order to reproduce the results, I fixed the lambda and mu values. In the end I would like to get some statistics of the simulation as

  • average number of events in the queue
  • average number of events in the system
  • average wait time
  • average arrival and service charge
  • average time spent in the system

This is the code that I wrote

tail queue import
Random import expovariable, random


Class event (object):
def __init __ (self, arrival_time, service_start_time, service_time):
self.arrival_time = arrival_time
self.service_start_time = service_start_time
self.service_time = service_time
self.service_end_time = self.service_start_time + self.service_time
self.wait_time = self.service_start_time - self.arrival_time
self.time_spent_in_the_system = self.wait_time + self.service_time

# lamb = random () # arrival rate
# mu = 2 * lamb # Service fee
#print ("Rho: {}". format (lamb / mu))

lamb = 0.9006602339606232 # Arrival rate
mu = 1.8013204679212464 # Service fee

events_queue = Queue ()

last_event = None
currently_processed_event = None

mean_wait_time = 0
mean_time_in_sys = 0
mean_service_rate = 0
mean_arrival_rate = 0
mean_num_customers_in_queue = 0

events_served = 0
i = 1

try:

# We calculate the time of the first arrival.
next_arrival = expovariate (lamb)

# Until we finish the program by Ctrl + C
while True

if events_queue.empty ():
Event = Event (next_arrival, # arrival time
next_arrival, # the service of the first event begins with your arrival
expovariate (mu)) # service time
Currently_processed_event = event
last_event = event
i + = 1

# We put the events in the queue until the processing of the current event ends
while next_arrival <current_processed_event.service_end_time:

# Simulate an event
Event = Event (next_arrival, # arrival time
max (next_arrival, last_event.service_end_time), # moment when the service starts
expovariable (mu)

last_event = event # pointer to the last event
events_queue.put (event) # we put the event in the queue

x = expovariable (lamb)
i + = 1
mean_arrival_rate + = x
next_arrival + = x # next arrival time (expovariate)

# when finished serving

event = events_queue.get () # remove the first event from the queue
mean_wait_time + = event.wait_time
mean_time_in_sys + = event.time_spent_in_the_system
mean_service_rate + = event.service_time
events_servidos + = 1


if it is not events_queue.empty (): # if the queue is not empty after the previous service
currently_processed_event = events_queue.queue[0]   # event currently processed will be the following

mean_num_customers_in_queue + = events_queue.qsize ()

except:

# Statistics
print ("Average number of clients in queue {}". format (mean_num_customers_in_queue / events_served))
print ("Average wait time {}". format (mean_wait_time / events_served))
print ("Mean time in the system {}". format (mean_time_in_sys / events_served))
print ("average service rate {}". format (mean_service_rate / events_served))
print ("average arrival rate {}". format (mean_arrival_rate / i))

For some reason I can not get the correct results from this simulation (the ones I got from the analytical solution). I should get that

  • Average wait time is ~ 0.5
  • the average number of events in the queue is also 0.5
  • The average time spent in the system is ~ 1.10.
  • the average time spent in the queue is ~ 0.5

but what I get is

Average number of customers in the queue 1.295122004194309
Average waiting time 1.1152014470674076
Average time in the system 1.6698933438542574
Average service charge 0.5546918967868321
Average arrival rate 0.777222986586766