# Single server queueing simulation # 0. Save this file into a directory as queue1.r # 1. Download R from http://www.r-project.org/ # 2. Run R; File -> Change directory to the directory including this file # 3. At the R prompt, issue source("queue1.r") to make R read queue1.r # 4. Try simulating a queue with exponential intearrival and service times # where "a"=average interarrivval time; "p"=average service time # for "numarrivals" many arrivals # For example, queue1(2,1,100) simulates 100 customers with # average interarrival time of 2 and service time of 1 # More examples # > queue1(5,1,1000000) # Utilization TimeInQueue TimeInSystem NumberInQueue NumberInSystem # 1 0.2001452 0.2501673 1.250167 0.04998645 0.2499865 # > queue1(2,1,1000000) # Utilization TimeInQueue TimeInSystem NumberInQueue NumberInSystem # 1 0.4997648 1.006667 2.006667 0.5033223 1.003322 queue1 <- function(a, p, numarrivals) { wait_time <- 0; # waiting time excluding service time total_wait_time <- 0; # total waiting time total_idle_time <- 0; # total idle time total_arrival_time <- 0; # total arrival time # Suppose that the queue already has a customer who arrived at time total_arrival_time=0 # Name this customer as customer 0 for (i in 1:numarrivals) { service_time <- rexp(1, 1/p); # service time of customer i-1 interarival_time <- rexp(1, 1/a); # interarrival time between customer i-1 and i total_arrival_time <- total_arrival_time + interarival_time; # advancing the clock from total_arrival_time to total_arrival_time+interarival_time wait_time <- wait_time - interarival_time + service_time; # wait_time-interarival_time+service_time is custoimer i's waiting time given that customer (i-1) waited for wait_time time # if wait_time>=0, it is the work found by customer i # if wait_time<0, it is the server idle time between finishing customer i-1 and starting customer i if (wait_time >= 0) total_wait_time <- total_wait_time + wait_time # total_wait_time includes the waiting time of the (i)th customer else { total_idle_time <- total_idle_time - wait_time; # the (i)th customer does not experince wait but finds service idle wait_time <- 0; # since the queue is idle, customer i waiting time is zero } } result <- data.frame(Utilization=1-total_idle_time/total_arrival_time, TimeInQueue=total_wait_time/numarrivals, TimeInSystem=total_wait_time/numarrivals+p, NumberInQueue=total_wait_time/total_arrival_time, NumberInSystem=total_wait_time/total_arrival_time+p/a); return(result) }