This notebook simulates a taxi stand problem posed OR Stack Exchange. There is a single queue with Markovian arrivals and multiple taxis available. Taxis all have the same capacity and are only dispatched when full. Round trip travel times for taxis are constant. The measure of interest is average time between dispatches.

# Parameters

These are the model parameters we will use.

nTaxis <- 5             # number of taxis
travelTime <- 20        # round trip travel time
arrivalRate <- 0.7      # mean arrival rate of passengers

These are the simulation parameters.

seed <- 789              # random number seed
passengerLimit <- 10000  # number of passenger arrivals to simulate

# Events

The simulation has three types of events, coded by the numbers 1, 2 and 3 in the simulation.

1. Passenger arrival. This results in the queue being increased by one. If the queue length is loadSize and the taxi line contains at least one taxi, a dispatch event occurs immediately. All passenger arrivals are scheduled at the outset.

2. Taxi dispatch. This results in loadSize passengers being removed from the queue and one taxi being removed from the line. A return event is scheduled for travelTime units in the future.

3. Taxi return. This results in the taxi line count being increased by 1. If at least loadSize passengers are already in the queue, a dispatch event occurs immediately.

# Event calendar

The event calendar is a data frame containing a time index, the type of event (1, 2, 3) scheduled at that time, the passenger queue size, and the size of the taxi line. We maintain a pointer to the row containing the next event to process.

We will need a function to insert an event into the calendar as well as a function to update the calendar when an event occurs.

# Inputs: t = time of the event, e = event type (1 - 3)
# Output; none (the calendar is expanded by one row)
schedule <- function(t, e) {
# Find the indices of rows before/after the event.
calendar <<- rbind(calendar[calendar$Time <= t, ], c(t, e, 0, 0), calendar[calendar$Time > t, ])
}

# Inputs: r = row index of an event in the calendar
# Output: none (the designated calendar row is updated, and a subsequent event may be scheduled)
process <- function(r) {
if (calendar[r, "Event"] == 1) {
# Passenger arrival. Copy the line length and bump the queue size.
if (r == 1) {
# Start of the simulation.
calendar[r, "Queue"] <<- 1
} else {
calendar[r, "Line"] <<- calendar[r - 1, "Line"]
calendar[r, "Queue"] <<- calendar[r - 1, "Queue"] + 1
# If a taxi is available and can make load, schedule a dispatch.
if ((calendar[r, "Queue"] == loadSize) & (calendar[r, "Line"] > 0)) {
schedule(calendar[r, "Time"], 2)
}
}
} else if (calendar[r, "Event"] == 2) {
# Cab dispatch. Reduce the passenger queue and line contents and schedule a return.
calendar[r, "Queue"] <<- calendar[r - 1, "Queue"] - loadSize
calendar[r, "Line"] <<- calendar[r - 1, "Line"] - 1
schedule(calendar[r, "Time"] + travelTime, 3)
} else if (calendar[r, "Event"] == 3) {
# Cab return. Bump the line by 1 and copy the queue size. If the queue is at least the load size, schedule an immediate dispatch.
calendar[r, "Queue"] <<- calendar[r - 1, "Queue"]
calendar[r, "Line"] <<- calendar[r - 1, "Line"] + 1
if (calendar[r, "Queue"] >= loadSize) {
schedule(calendar[r, "Time"], 2)
}
} else {
# This should never happen.
stop("Invalid event code: ", calendar[r, "Event"])
}
}

# Initialization

We initialize the event calendar by scheduling all passenger arrivals.

# Seed the random number generator.
set.seed(seed)
# Generate the interarrival times.
iat <- rexp(passengerLimit, arrivalRate)
# Set up the event calendar.
calendar <- data.frame(Time = cumsum(iat), Event = 1, Queue = 0, Line = nTaxis)
nextEvent <- 1  # row with next event to process
rm(iat)

# Running the simulation.

We process the event pointed to by nextEvent (and increment the pointer) until it exceeds the number of rows in the data frame.

while (nextEvent <= nrow(calendar)) {
process(nextEvent)
nextEvent <- nextEvent + 1
}

# Analyzing interdispatch times

We want to focus on the time intervals between dispatches.

# Calculate time intervals between consecutive dispatches.
tbd <- calendar[calendar\$Event == 2, "Time"] |> diff()
# Print the sample mean time between dispatches.
cat("Mean TBD = ", mean(tbd), ".\n")
Mean TBD =  5.702537 .
# Print the ratio of load size to arrival rate.
cat("Load size / arrival rate = ", loadSize / arrivalRate, ".\n")
Load size / arrival rate =  5.714286 .