Simulating a Single Server Queue in Python
Source - Georgia Tech Simulation Course Notes

Simulating a Single Server Queue in Python

Simulating a single server queue involves modeling a system where customers (or jobs) arrive, wait in line if the server is busy, and are served in the order they arrive. Here's how to simulate such a queue with the necessary assumptions:

Assumptions

  1. Arrival Process: The arrival of customers follows a specific statistical distribution, often modeled as a Poisson process, which means the inter-arrival times are exponentially distributed.
  2. Service Process: The service times are also usually modeled as exponentially distributed (this is known as an M/M/1 queue). Other distributions can be used depending on the system being modeled.
  3. Queue Discipline: The most common discipline is First-Come, First-Served (FCFS), meaning customers are served in the order they arrive.
  4. Capacity: The queue can have infinite or finite capacity. For simplicity, we often assume infinite capacity.
  5. Population: The source population can be finite or infinite. Typically, we assume an infinite population.

Steps to Simulate a Single Server Queue

Initialize Parameters and Variables:

  1. Define the arrival rate (λ) and service rate (μ).
  2. Initialize the simulation clock.
  3. Initialize the system state (e.g., number of customers in the system).
  4. Initialize statistical counters (e.g., total wait time, number of customers served).

Event List:

  1. Maintain a list of events, primarily arrivals and departures.
  2. The first event is typically an arrival at time 0.

Simulation Loop:

Clock Advancement: Move the simulation clock to the time of the next event.

Event Handling:

  1. Arrival Event: If the server is free, start service immediately. If the server is busy, add the customer to the queue. Schedule the next arrival.
  2. Departure Event: Remove the customer from the server. If there are customers in the queue, start service for the next customer. Schedule the next departure if there are customers in the system.

Statistics Update: Update counters and statistics for performance metrics.

Termination:

  1. Define a stopping criterion (e.g., a fixed number of customers served or a maximum simulation time).
  2. Gather and summarize statistics (e.g., average wait time, average queue length)


Example Code (Python)

Here is a simple example using Python to simulate a single server queue:

import random
import heapq

# Parameters
arrival_rate = 2.0  # lambda
service_rate = 1.0  # mu
simulation_time = 10.0  # Total simulation time

# Events
ARRIVAL = 1
DEPARTURE = 2

# State variables
current_time = 0.0
queue = []
server_busy = False
event_list = []

# Statistics
num_in_queue = 0
total_wait_time = 0.0
num_customers_served = 0

# Schedule the first arrival
heapq.heappush(event_list, (random.expovariate(arrival_rate), ARRIVAL))

while current_time < simulation_time:
    event_time, event_type = heapq.heappop(event_list)
    current_time = event_time
    
    if event_type == ARRIVAL:
        if not server_busy:
            server_busy = True
            service_time = random.expovariate(service_rate)
            heapq.heappush(event_list, (current_time + service_time, DEPARTURE))
        else:
            queue.append(current_time)
        next_arrival = current_time + random.expovariate(arrival_rate)
        heapq.heappush(event_list, (next_arrival, ARRIVAL))
    elif event_type == DEPARTURE:
        num_customers_served += 1
        if queue:
            arrival_time = queue.pop(0)
            wait_time = current_time - arrival_time
            total_wait_time += wait_time
            service_time = random.expovariate(service_rate)
            heapq.heappush(event_list, (current_time + service_time, DEPARTURE))
        else:
            server_busy = False

# Results
print(f"Number of customers served: {num_customers_served}")
print(f"Average wait time: {total_wait_time / num_customers_served if num_customers_served > 0 else 0}")        

This code simulates a single server queue with exponentially distributed inter-arrival and service times. The results provide the number of customers served and the average wait time in the system.

要查看或添加评论,请登录

Chirag S.的更多文章

社区洞察

其他会员也浏览了