Queuing Theory: Optimizing Wait Times at Fast Food Joints and Banks

Queuing Theory

The frustration of waiting in long queues at fast food joints or banks is all too familiar. How can we tackle this issue effectively?

We have all been to our favorite fast food joints like KFC, Domino’s, and Pizza Hut. We have also hated the long queues while ordering and collecting our food. This issue is not just common to fast food joints, even people waiting to fill their cars with petrol or diesel face a similar issue. So how do we solve this particular issue?

This problem is resolved using the Queuing theory. Queuing theory is used to analyze and potentially improve the system of queues. In this article, we will understand the basics of this particular theory and also touch upon a case study.

Recommended: Fitting a Logistic Regression Model in Python

Recommended: Regression Splines in Python – A Beginners Introduction

Implementing an MM1 Queue in Python

As mentioned earlier, queuing theory deals with people or objects waiting in lines and tries to minimize the waiting time. Queuing theory also invokes Poisson distribution heavily. Poisson distribution gives us the probability of a particular number of events occurring in a fixed interval of time or space. Let us look at its formula.

Poisson Distribution
Poisson Distribution

Now let us look at a simple queuing process and then analyze it. In the code given below, we have defined the arrival rate, the service rate, and the number of customers. We’ve calculated the average waiting time for each customer using exponential distribution and other mathematical functions. Let us look at the code below.

import numpy as np

def mm1_queue(arrival_rate, service_rate, num_customers):
    inter_arrival_times = np.random.exponential(1/arrival_rate, num_customers)
    service_times = np.random.exponential(1/service_rate, num_customers)
    arrival_times = np.cumsum(inter_arrival_times)
    wait_times = np.zeros(num_customers)
    departure_times = np.zeros(num_customers)

    for i in range(1, num_customers):
        departure_times[i] = max(arrival_times[i], departure_times[i-1]) + service_times[i-1]
        wait_times[i] = max(0, departure_times[i-1] - arrival_times[i])

    average_wait_time = np.mean(wait_times)
    return average_wait_time

# Parameters
ARRIVAL_RATE = 0.2  # Mean arrival rate (per time unit)
SERVICE_RATE = 0.3  # Mean service rate (per time unit)

# Run simulation
average_wait_time = mm1_queue(ARRIVAL_RATE, SERVICE_RATE, NUM_CUSTOMERS)
print(f"Average wait time: {average_wait_time:.2f}")

Let’s examine the output.

Queuing Theory Output
Queuing Theory Output

Thus the average wait time is 6.97 after keeping the number of customers constant and 1000.

Simulating Bank Queues

Let us consider a simple case study which I am sure a lot of you have faced. The case study is about long queues in a bank. In addition to that, several receptionists are catering to the wishes of customers. We aim to analyze the queuing time of a customer and analyze the data over time. We also try to plot this concerning time. Let us look at the code below.

import numpy as np
import matplotlib.pyplot as plt

# Function to simulate the bank queue
def simulate_bank_queue(num_tellers, arrival_rate, service_rate, sim_time):
    # Initialize variables
    time = 0
    queue_length = []
    wait_times = []

    # Initialize empty lists for arrival and service times
    arrival_times = []
    service_times = []

    # Simulate until sim_time
    while time < sim_time:
        # Generate inter-arrival time and update time
        inter_arrival_time = np.random.exponential(1 / arrival_rate)
        time += inter_arrival_time

        # Generate service time
        service_time = np.random.exponential(1 / service_rate)

        # Update arrival and service times lists

        # Calculate wait time
        if len(arrival_times) <= num_tellers:
            wait_time = max(0, time - service_times[len(arrival_times) - 1])
            wait_time = max(0, time - arrival_times[len(arrival_times) - num_tellers - 1])

        # Update wait times list

        # Update queue length
        queue_length.append(len(arrival_times) - num_tellers)

    return queue_length, wait_times

# Parameters
ARRIVAL_RATE = 20  # customers per hour
SERVICE_RATE = 25  # customers per hour
SIM_TIME = 8 * 60  # 8 hours

# Run simulation
queue_length, wait_times = simulate_bank_queue(NUM_TELLERS, ARRIVAL_RATE, SERVICE_RATE, SIM_TIME)

# Calculate average wait time
avg_wait_time = np.mean(wait_times)
print(f"Average wait time: {avg_wait_time:.2f} minutes")

# Plot queue length over time
plt.figure(figsize=(10, 6))
plt.plot(np.arange(len(queue_length)), queue_length)
plt.title("Queue Length Over Time")
plt.xlabel("Time (minutes)")
plt.ylabel("Queue Length")

We have defined variables like number of tellers, arrival rate, service rate, etc. Let us look at the output of the same.

Queuing Theory Plot
Queuing Theory Plot
Queuing Case Study Output
Queuing Case Study Output

The average waiting time is around 9 seconds, and the plot shows a linear increase in queue length.


Here you go!! Now you can estimate your waiting time whenever you are stuck in long queues. In this article, we have touched on the basics of queuing theory and also saw a simple case study about queuing in a bank. Queuing theory equips you to navigate through the complexities of wait times, empowering you to optimize your experience in various scenarios.

Hope you enjoyed reading it!

Recommended: Probability Distributions with Python (Implemented Examples)

Recommended: Empirical Distribution in Python: Histograms, CDFs, and PMFs