#
# file: Qsim.R
#
# Author: B. M. Bolstad
# Date: Oct 14, 2004
#
# Aim: Partial queuing simulation code.
#
#
#
#
#rateperhour
rate <- 35
# simulate the service times
servicetimes <- rnorm(1000,1.8,0.25) # ie S
arrivaltimes <- rexp(1000,rate/60)
# figure out when customers actually arrive
cum.arrival <- cumsum(arrivaltimes)
# each customer could possibly be served as soon as the server
# becomes free. First customer is always served immediately.
# Otherwise the first possible time that they could start being served is
# the maximum of their arrival time and the time at which the the
# previous customer finished being served.
start.servicetimes <- matrix(0,1000,1)
start.servicetimes[1] <- cum.arrival[1]
for (i in 2:1000){
start.servicetimes[i] <- max(cum.arrival[i], start.servicetimes[i-1]+ servicetimes[i-1])
}
# The time that the customer leaves the store is when they
# complete being served. which is the time that the start being served plus
# their service time
leave.times <- start.servicetimes + servicetimes
#
# Queue length changes whenever anyone arrives or leaves
# Count the person being served as being a member of the
# queue. Queue starts out empty.
#
how.many.changes <- 2*1000 + 1 #every customer joins and leaves plus the starting time
queue.length <- matrix(0,how.many.changes,2) # store both times and lengths
queue.length[1,] <- c(0,0) # empty queue at beginning
change.times <- sort(c(cum.arrival,leave.times))
queue.length[2:how.many.changes,1] <- change.times
for (i in 2:how.many.changes){
if (is.element(change.times[i-1],cum.arrival)){
queue.length[i,2] <- queue.length[i-1,2] +1
} else {
queue.length[i,2] <- queue.length[i-1,2] -1
}
}
#
# Helpful hints:
#
# 1. W is the difference between the starting service time and arrival time
# 2. T is the difference between the leaving time and arrival time
# 3. The period of time between when the previous customer left
# and service for the next customer begins gives the idle time I.