Extended Example: Discrete-Event Simulation in R
Discrete-event simulation (DES) is a technique for modeling systems where state changes occur at discrete points in time. In R, you can simulate such systems using functions, loops, and random number generation. Let’s delve into a more detailed example of discrete-event simulation in R.
Scenario: Event Simulation for a Queuing System
Consider a simple queuing system where customers arrive at a service center and wait for service. The goal is to simulate this process over a period to understand metrics like average wait time and queue length.
Define the Parameters
First, define the parameters for the simulation:
- Arrival Rate: The average number of customers arriving per unit time.
- Service Rate: The average number of customers served per unit time.
- Simulation Time: The total time to run the simulation.
arrival_rate <- 5 # customers per unit time service_rate <- 4 # customers per unit time simulation_time <- 100 # total time for simulation
Initialize Variables
Set up variables to keep track of the queue, event times, and statistics.
queue <- numeric() # Queue to store waiting times arrival_times <- numeric() # Record of customer arrival times service_times <- numeric() # Record of service start times current_time <- 0 # Simulation time counter next_arrival <- rexp(1, rate = arrival_rate) # Time of next arrival next_departure <- Inf # Time of next departure
Simulate the System
Use a loop to run the simulation. In each iteration, determine which event (arrival or departure) occurs next.
while (current_time < simulation_time) { if (next_arrival < next_departure) { # Process arrival current_time <- next_arrival arrival_times <- c(arrival_times, current_time) queue <- c(queue, current_time) # Add arrival time to the queue # Schedule the next arrival next_arrival <- current_time + rexp(1, rate = arrival_rate) # Start service if queue was not empty if (length(queue) == 1) { next_departure <- current_time + rexp(1, rate = service_rate) } } else { # Process departure current_time <- next_departure service_times <- c(service_times, current_time) # Remove the first customer from the queue queue <- queue[-1] # Schedule the next departure if queue is not empty if (length(queue) > 0) { next_departure <- current_time + rexp(1, rate = service_rate) } else { next_departure <- Inf # No departure if queue is empty } } }
Analyze Results
After the simulation, analyze the results to compute metrics like average wait time and queue length.
# Calculate waiting times waiting_times <- service_times - arrival_times[1:length(service_times)] # Average waiting time avg_waiting_time <- mean(waiting_times) cat("Average Waiting Time:", avg_waiting_time, "\n") # Queue length over time queue_length <- sapply(seq(0, simulation_time, by = 1), function(t) sum(arrival_times <= t & service_times > t)) plot(seq(0, simulation_time, by = 1), queue_length, type = "l", xlab = "Time", ylab = "Queue Length", main = "Queue Length Over Time")
Summary
- Parameters: Set arrival rate, service rate, and simulation time.
- Initialization: Create variables to track arrivals, services, and queue status.
- Simulation Loop: Use a loop to process arrivals and departures based on time.
- Analysis: Calculate metrics like average waiting time and plot queue length.