simulating a queueing system in python An excellent way to interpret the movement of service models such as computer networks, manufacturing procedures, or customer service lines is the process of simulating a queuing model in Python. Generally, the entities like packets, missions, consumers occurring at a service point, remaining when the service point is engaged, and then being assisted are encompassed in a queuing model.
We provide a procedural instruction to simulate a simple queuing model in Python. Along with service times adhering to exponential distribution which is referred to as an M/M/1 queue and arrivals adhering to a Poisson process, we plan to simulate a single-server queue in an effective manner:
Step 1: Install Necessary Libraries
We intend to employ numpy for simple numerical processes and random number generation in this simulation. When we don’t have already, it is significant to install it through the utilization of pip:
pip install numpy
Step 2: Write the Queueing System Simulation Code
The following is a basic Python script to simulate an M/M/1 queue:
import numpy as np
import matplotlib.pyplot as plt
# Parameters for the simulation
arrival_rate = 5 # Arrival rate (lambda) for the Poisson process (customers per time unit)
service_rate = 6 # Service rate (mu) for the exponential distribution (customers served per time unit)
simulation_time = 100 # Total time to run the simulation
# Initialize variables
time = 0
num_in_queue = 0 # Number of customers in the queue (including the one in service)
arrival_times = []
departure_times = []
queue_lengths = []
# Generate inter-arrival and service times
np.random.seed(42) # For reproducibility
arrival_times.append(np.random.exponential(1/arrival_rate))
service_times = [np.random.exponential(1/service_rate)]
# Simulation loop
while time < simulation_time:
# Determine the next event (arrival or departure)
if arrival_times[-1] <= time:
num_in_queue += 1
queue_lengths.append(num_in_queue)
arrival_times.append(arrival_times[-1] + np.random.exponential(1/arrival_rate))
if num_in_queue == 1:
departure_times.append(time + service_times[-1])
else:
if num_in_queue > 0:
time = departure_times[-1]
num_in_queue -= 1
queue_lengths.append(num_in_queue)
if num_in_queue > 0:
service_times.append(np.random.exponential(1/service_rate))
departure_times.append(time + service_times[-1])
else:
service_times = [np.random.exponential(1/service_rate)]
else:
time = arrival_times[-1]
# Plot the results
plt.figure(figsize=(10, 6))
plt.step(np.arange(len(queue_lengths)), queue_lengths, where=’post’)
plt.title(‘Queue Length Over Time’)
plt.xlabel(‘Event Index’)
plt.ylabel(‘Number of Customers in Queue’)
plt.grid(True)
plt.show()
Step 3: Run the Simulation
- Generally, the code should be saved as queueing_system_simulation.py.
- By means of employing Python, we plan to execute the script:
python queueing_system_simulation.py
Step 4: Customize the Simulation
In several techniques, we are able to prolong and adapt this simple simulation:
- Multiple Servers (M/M/c Queue): To manage numerous servers in which consumers could be assisted by any accessible server, we intend to prolong the simulation.
- Finite Queue Capacity: Generally, a finite queue must be executed at which consumers are turned back when the queue is occupied.
- Priority Queue: A priority queue has to be initiated in which few consumers have precedence over others.
- Batch Arrivals: To enable for batch arrivals in which numerous consumers arrive at the same time, we focus on altering the arrival procedure.
- Variable Service Rate: A variable service rate ought to be applied which relies on the number of consumers in the queue or varies periodically.
- Customer Patience (Balking and Reneging): Typically, consumers ought to be simulated who determine not to join the queue when it is lengthy (balking) or quit the queue when the wait is lengthy(reneging).
- Visualize Time in Queue: To monitor and visualize the duration spent by every consumer in the queue, we intend to prolong the simulation.
Description of the Code
- Arrival Rate (lambda): A Poisson process is adhered to by the arrival rate of consumers or missions. In order to produce the inter-arrival times, our team plans to employ np.random.exponential(1/arrival_rate).
- Service Rate (mu): We focus on disseminating the service times in an exponential manner. For producing the service times, it is beneficial to utilize np.random.exponential(1/service_rate).
- Simulation Loop: For managing incidents either an arrival or a departure, the simulation loops. The consumer either moves into service or waits in the queue, when they reach. The consumer exits a model and the following consumer (if available) begins service, when a consumer leaves.
- Queue Length Tracking: At every incident (arrival or departure), we often monitor the number of consumers in the queue.
- Plotting: For offering a visual demonstration based on how the queue progresses at the time of simulation, the queue length is plotted in a periodic manner.
Instance: Multiple Servers (M/M/c Queue)
The following instance is based on how we could alter the simulation to manage numerous servers effectively:
num_servers = 3 # Number of servers
# Modify the simulation loop for multiple servers
while time < simulation_time:
if arrival_times[-1] <= time:
num_in_queue += 1
queue_lengths.append(num_in_queue)
arrival_times.append(arrival_times[-1] + np.random.exponential(1/arrival_rate))
if num_in_queue <= num_servers:
departure_times.append(time + service_times[-1])
else:
if num_in_queue > 0:
time = departure_times[-1]
num_in_queue -= 1
queue_lengths.append(num_in_queue)
if num_in_queue >= num_servers:
service_times.append(np.random.exponential(1/service_rate))
departure_times.append(time + service_times[-1])
else:
service_times = [np.random.exponential(1/service_rate)]
else:
time = arrival_times[-1]
Additional Extensions
- Simulate Different Queue Disciplines: Generally, various queue disciplines such as Priority Queueing, First-Come-First-Serve (FCFS), or Last-Come-First-Serve (LCFS), have to be applied.
- Simulate a Network of Queues: A network of queues ought to be designed in which consumers travel among various service stations.
- Real-Time Visualization: Through the utilizations of libraries such as pygame or matplotlib, we focus on including actual time visualizations of the queue and service procedure.
- Statistical Analysis: As a means to interpret the effectiveness of the model, our team aims to carry out statistical analysis on the queue length, service time, and waiting time.
simulating a queueing system in python projects
In the contemporary years, several projects based on queuing system simulation are emerging continuously. For enabling you to investigate various aspects of queuing theory and its actual world applications, we suggest some projects that extend from simple simulations to highly innovative applications and modifications:
Basic Queueing System Simulations
- Basic M/M/1 Queue Simulation: With exponential service times and Poisson arrivals, a single-server queue must be simulated.
- Multiple Server M/M/c Queue Simulation: In order to manage numerous servers (c servers), we plan to prolong the simple M/M/1 simulation.
- Finite Capacity M/M/1/K Queue: A single-server queue with finite capacity should be simulated. When the queue is occupied, the consumers are deviated.
- M/D/1 Queue Simulation: Generally, with Poisson arrivals and deterministic (constant) service times, our team aims to simulate a single-server queue.
- M/M/1 Queue with Batch Arrivals: As a means to enable for batch arrivals in which several consumers reach at the same time, it is advisable to alter the M/M/1 queue.
- M/M/1 Queue with Variable Service Rate: A single-server queue must be executed at which the service rate varies in periodic manner.
- Priority Queue Simulation: A priority queue should be simulated. As consumers contain various priority levels they are assisted in an appropriate manner.
- M/M/1 Queue with Reneging: A queue has to be simulated at which consumers exit (renege) when they wait for a long period.
- M/M/1 Queue with Balking: It is appreciable to execute a queue by us. When the queue is lengthy, the consumers determine not to join (balk) a queue.
- M/M/1 Queue with Preemption: Generally, a preemptive priority queue must be simulated. In this technique, service is disrupted for lower-priority consumers by higher-priority consumers.
Intermediate Queueing System Simulations
- M/M/c Queue with Shared Resources: A multi-server queue ought to be simulated in which resources are distributed by servers and based on the number of active servers, it exhibits the service rate in a crucial manner.
- M/M/c Queue with Heterogeneous Servers: We focus on executing a multi-server queue. Typically, various service rates are included in servers.
- M/M/c Queue with Dynamic Server Allocation: Mainly, a model has to be simulated. In terms of the queue length, the number of active servers varies in a dynamic manner.
- M/G/1 Queue Simulation: Including general (arbitrary) service time distribution and Poisson arrivals, we focus on simulating a single-server queue.
- G/G/1 Queue Simulation: A single-server queue must be simulated with general (arbitrary) arrival and service time distributions.
- Erlang Queue Simulation (M/Ek/1): A queue ought to be simulated. For designing multi-phase service procedures, service times adhere to an Erlang distribution.
- M/M/1 Queue with Time-Dependent Arrival Rate: A queue has to be applied at which the arrival rate varies in a periodic manner (For instance., evening lull, morning peak).
- M/M/1 Queue with Server Downtime: A queue is required to be simulated by us specifically for the server that is intended to be fixed due to the periodic breakdowns.
- M/M/1 Queue with Scheduled Arrivals: Mainly, a queue should be simulated in which arrivals exist with some uncertainty at planned times.
- Queue Length Distribution Analysis: For various kinds of queues, we intend to examine and plot the distribution of queue lengths periodically.
Advanced Queueing System Simulations
- Network of Queues Simulation: A system of interrelated queues has to be designed in which consumers travel among various service stations.
- M/M/1 Queue with Feedback Loop: It is significant to execute a queue. For supplementary service, there is a possibility for assisted consumers to come back to the queue.
- Closed Queueing Network Simulation: A closed network ought to be simulated at which a determined number of consumers moves through service stations.
- Jackson Network Simulation: A Jackson network has to be designed. It is a kind of queuing network in which every queue works in an individual manner.
- M/M/1 Queue with Service Interruptions: It is approachable to simulate a queue for service that could be disrupted in the case of exterior conditions or outside realities which results in postponement of process.
- Pollaczek-Khinchine Formula Verification: Typically, an M/G/1 queue must be simulated. For average waiting time, we aim to validate the Pollaczek-Khinchine formula.
- M/M/1 Queue with Service Level Agreement (SLA): A queue with an SLA has to be simulated. In a specific time limit, the service should be accomplished in this approach.
- Transient Behavior of M/M/1 Queue: The temporary (short-term) activity of a queue before it attains balanced state ought to be investigated.
- Comparing M/M/1 and M/M/c Queue Performance: Generally, M/M/1 as well as M/M/c queues ought to be simulated. It is advisable to contrast their performance metrics such as average waiting time.
- Queue Simulation with Real-Time Visualization: By means of employing pygame or Matplotlib, our team aims to execute actual time visualization of a queuing model.
Applications of Queueing Systems
- Call Center Simulation (M/M/c): A call center including numerous agents assisting incoming calls should be simulated. Generally, call center simulation is designed as an M/M/c queue.
- Web Server Request Queue Simulation: To manage incoming HTTP queries, we focus on designing the request queue of a web server.
- Hospital Emergency Room Queue Simulation: In a hospital emergency room, our team plans to simulate the patient movement. On the basis of the preference, patients are classified and handled.
- Restaurant Waitlist Simulation: Involving consumers who exit when the wait is lengthy, it is significant to design the waitlist model of an engaged restaurant.
- Airport Security Queue Simulation: For managing numerous lanes and various kinds of travellers, we aim to simulate the security checkpoint at an airport.
- Traffic Light Queue Simulation: The collection of cars waiting at a traffic light must be designed. It is appreciable to involve the impacts of traffic light timing on queue length.
- Amusement Park Ride Queue Simulation: Encompassing priority passes and batch arrivals, we intend to simulate the queue for a prevalent amusement park ride.
- Bank Teller Queue Simulation: Including various service times for various kinds of transactions, our team designs a suitable bank with several tellers serving consumers.
- Supermarket Checkout Queue Simulation: In a supermarket, the checkout procedure must be simulated at which consumers select among numerous queues.
- Tech Support Queue Simulation: Encompassing priority tickets for critical problems, we plan to design the queue for technical assistance in an IT domain.
Complex and Multi-Stage Queueing Systems
- Assembly Line Queue Simulation: An assembly line with several phases has to be simulated. Generally, an individual queue is included in every phase.
- M/M/c Queue with Load Balancing: It is appreciable to apply a load-balancing technique. To the least engaged server, the consumers are allocated in a dynamic manner.
- Queueing System with Machine Learning Optimization: In order to reinforce the metrics of a queuing model like number of servers or service rates, it is beneficial to utilize machine learning.
- Dynamic Pricing Based on Queue Length: As a means to handle necessity, our team simulates a model in which price for a service enhances as the length of the queue extends.
- Multi-Class Queueing System: A queue with various groups of consumers has to be simulated in which service necessities and arrival rates are diverse for every group.
- Queueing System for Manufacturing Plant: Encompassing machine failures and restorations, we intend to design the production line of a manufacturing plant.
- Simulation of a Batch Processing Queue: Typically, a queue should be simulated in which consumers are processed in groups instead of one by one.
- Queueing System for Disaster Relief Operations: The arrangement of a disaster relief process has to be designed in such a manner which disseminates goods on the basis of the queuing theory.
- Dynamic Queue Reassignment: According to the wait times and current load, consumers could be reallocated to various queues through simulating a suitable model.
- Queueing System with Real-Time Data Integration: In order to adapt service rates in a dynamic manner, our team focuses on executing a queuing model which is capable of incorporating actual time data like network load or current traffic.
We have offered a gradual direction to simulate a simple queuing model in Python. Also, for facilitating you to examine several factors of queuing theory and its actual world uses, 50 queueing system simulation projects that extend from fundamental simulations to highly progressive uses and alterations are recommended by us in this article.
We provide a comprehensive guide to modeling a fundamental queueing system using Python. This simulation will focus on a single-server queue, where arrivals adhere to a Poisson process and service durations are characterized by an exponential distribution. For optimal project assistance, rely on our expertise.