ssq {simEd} | R Documentation |
Single-Server Queue Simulation
Description
A next-event simulation of a single-server queue, with extensible arrival and service processes.
Usage
ssq(
maxArrivals = Inf,
seed = NA,
interarrivalFcn = NULL,
serviceFcn = NULL,
interarrivalType = "M",
serviceType = "M",
maxTime = Inf,
maxDepartures = Inf,
maxInSystem = Inf,
maxEventsPerSkyline = 15,
saveAllStats = FALSE,
saveInterarrivalTimes = FALSE,
saveServiceTimes = FALSE,
saveWaitTimes = FALSE,
saveSojournTimes = FALSE,
saveNumInQueue = FALSE,
saveNumInSystem = FALSE,
saveServerStatus = FALSE,
showOutput = TRUE,
animate = FALSE,
showQueue = NULL,
showSkyline = NULL,
showSkylineSystem = FALSE,
showSkylineQueue = FALSE,
showSkylineServer = FALSE,
showTitle = TRUE,
showProgress = TRUE,
plotQueueFcn = defaultPlotSSQ,
plotSkylineFcn = defaultPlotSkyline,
jobImage = NA,
plotDelay = NA,
respectLayout = FALSE
)
Arguments
maxArrivals |
maximum number of customer arrivals allowed to enter the system |
seed |
initial seed to the random number generator (NA uses current state of random number generator; NULL seeds using system clock) |
interarrivalFcn |
function for generating interarrival times for queue simulation.
Default value ( |
serviceFcn |
function for generating service times for queue simulation.
Default value ( |
interarrivalType |
string representation of desired interarrival process. Options are "M" – exponential with rate 1; "G" – uniform(0,2), having mean 1; and "D" – deterministic with constant value 1. Default is "M". |
serviceType |
string representation of desired service process . Options are "M" – exponential with rate 10/9; "G" – uniform(0, 1.8), having mean 9/10; and "D" – deterministic with constant value 9/10. Default is "M". |
maxTime |
maximum time to simulate |
maxDepartures |
maximum number of customer departures to process |
maxInSystem |
maximum number of customers that the system can hold (server(s) plus queue). Infinite by default. |
maxEventsPerSkyline |
maximum number of events viewable at a time in the skyline plot. A large value for this parameter may result in plotting delays. This parameter does not impact the final plotting, which will show all end-of-simulation results. |
saveAllStats |
if |
saveInterarrivalTimes |
if |
saveServiceTimes |
if |
saveWaitTimes |
if |
saveSojournTimes |
if |
saveNumInQueue |
if |
saveNumInSystem |
if |
saveServerStatus |
if |
showOutput |
if |
animate |
logical; if |
showQueue |
logical; if |
showSkyline |
If |
showSkylineSystem |
logical; if |
showSkylineQueue |
logical; if |
showSkylineServer |
logical; if |
showTitle |
if |
showProgress |
if TRUE, displays a progress bar on screen during no-animation execution |
plotQueueFcn |
plotting function to display queue visualization.
By default, this is provided by |
plotSkylineFcn |
plotting function to display Skyline visualization.
By default, this is provided by |
jobImage |
a vector of URLs/local addresses of images to use as jobs. Requires
package |
plotDelay |
a positive numeric value indicating seconds between plots. A value of -1 enters 'interactive' mode, where the state will pause for user input at each step. A value of 0 will display only the final end-of-simulation plot. |
respectLayout |
logical; if |
Details
Implements a next-event implementation of a single-server queue simulation.
The seed
parameter can take one of three valid
argument types:
-
NA
(default), which will use the current state of the random number generator without explicitly setting a new seed (see examples); a positive integer, which will be used as the initial seed passed in an explicit call to
set.seed
; or-
NULL
, which will be passed in an explicit call to toset.seed
, thereby setting the initial seed using the system clock.
Value
The function returns a list containing:
the number of arrivals to the system (
customerArrivals
),the number of customers processed (
customerDepartures
),the ending time of the simulation (
simulationEndTime
),average wait time in the queue (
avgWait
),average time in the system (
avgSojourn
),average number in the system (
avgNumInSystem
),average number in the queue (
avgNumInQueue
), andserver utilization (
utilization
).
of the queue as computed by the simulation. When requested via the “save...” parameters, the list may also contain:
a vector of interarrival times (
interarrivalTimes
),a vector of wait times (
waitTimes
),a vector of service times (
serviceTimes
),a vector of sojourn times (
sojournTimes
),two vectors (time and count) noting changes to number in the system (
numInSystemT
,numInSystemN
),two vectors (time and count) noting changes to number in the queue (
numInQueueT
,numInQueueN
), andtwo vectors (time and status) noting changes to server status (
serverStatusT
,serverStatusN
).
Author(s)
Barry Lawson (blawson@bates.edu),
Larry Leemis (leemis@math.wm.edu),
Vadim Kudlay (vkudlay@nvidia.com)
See Also
rstream
, set.seed
,
stats::runif
Examples
# process 100 arrivals, R-provided seed (via NULL seed)
ssq(100, NULL)
ssq(maxArrivals = 100, seed = 54321)
ssq(maxDepartures = 100, seed = 54321)
ssq(maxTime = 100, seed = 54321)
############################################################################
# example to show use of seed = NA (default) to rely on current state of generator
output1 <- ssq(200, 8675309, showOutput = FALSE, saveAllStats = TRUE)
output2 <- ssq(300, showOutput = FALSE, saveAllStats = TRUE)
set.seed(8675309)
output3 <- ssq(200, showOutput = FALSE, saveAllStats = TRUE)
output4 <- ssq(300, showOutput = FALSE, saveAllStats = TRUE)
sum(output1$sojournTimes != output3$sojournTimes) # should be zero
sum(output2$sojournTimes != output4$sojournTimes) # should be zero
myArrFcn <- function() { vexp(1, rate = 1/4, stream = 1) } # mean is 4
mySvcFcn <- function() { vgamma(1, shape = 1, rate = 0.3) } # mean is 3.3
output <- ssq(maxArrivals = 100, interarrivalFcn = myArrFcn, serviceFcn = mySvcFcn,
saveAllStats = TRUE)
mean(output$interarrivalTimes)
mean(output$serviceTimes)
meanTPS(output$numInQueueT, output$numInQueueN) # compute time-averaged num in queue
meanTPS(output$serverStatusT, output$serverStatusN) # compute server utilization
############################################################################
# example to show use of (simple) trace data for arrivals and service times;
# ssq() will need one more interarrival (arrival) time than jobs processed
#
arrivalTimes <- NULL
interarrivalTimes <- NULL
serviceTimes <- NULL
initTimes <- function() {
arrivalTimes <<- c(15, 47, 71, 111, 123, 152, 232, 245, 99999)
interarrivalTimes <<- c(arrivalTimes[1], diff(arrivalTimes))
serviceTimes <<- c(43, 36, 34, 30, 38, 30, 31, 29)
}
getInterarr <- function() {
nextInterarr <- interarrivalTimes[1]
interarrivalTimes <<- interarrivalTimes[-1] # remove 1st element globally
return(nextInterarr)
}
getService <- function() {
nextService <- serviceTimes[1]
serviceTimes <<- serviceTimes[-1] # remove 1st element globally
return(nextService)
}
initTimes()
numJobs <- length(serviceTimes)
output <- ssq(maxArrivals = numJobs, interarrivalFcn = getInterarr,
serviceFcn = getService, saveAllStats = TRUE)
mean(output$interarrivalTimes)
mean(output$serviceTimes)
############################################################################
# example to show use of (simple) trace data for arrivals and service times,
# allowing for reuse (recycling) of trace data times
arrivalTimes <- NULL
interarrivalTimes <- NULL
serviceTimes <- NULL
initArrivalTimes <- function() {
arrivalTimes <<- c(15, 47, 71, 111, 123, 152, 232, 245)
interarrivalTimes <<- c(arrivalTimes[1], diff(arrivalTimes))
}
initServiceTimes <- function() {
serviceTimes <<- c(43, 36, 34, 30, 38, 30, 31, 29)
}
getInterarr <- function() {
if (length(interarrivalTimes) == 0) initArrivalTimes()
nextInterarr <- interarrivalTimes[1]
interarrivalTimes <<- interarrivalTimes[-1] # remove 1st element globally
return(nextInterarr)
}
getService <- function() {
if (length(serviceTimes) == 0) initServiceTimes()
nextService <- serviceTimes[1]
serviceTimes <<- serviceTimes[-1] # remove 1st element globally
return(nextService)
}
initArrivalTimes()
initServiceTimes()
output <- ssq(maxArrivals = 100, interarrivalFcn = getInterarr,
serviceFcn = getService, saveAllStats = TRUE)
mean(output$interarrivalTimes)
mean(output$serviceTimes)
############################################################################
# Testing with visualization
# Visualizing ssq with a set seed, infinite queue capacity, 20 arrivals,
# interactive mode (default), showing skyline for all 3 attributes (default)
if (interactive()) {
ssq(seed = 1234, maxArrivals = 20, animate = TRUE)
}
# Same as above, but jump to final queue visualization using plotDelay 0
ssq(seed = 1234, maxArrivals = 20, animate = TRUE, plotDelay = 0)
# Perform simulation again with finite queue of low capacity. Note same
# variate generation but different outcomes due to rejection pathway
ssq(seed = 1234, maxArrivals = 25, animate = TRUE, maxInSystem = 5, plotDelay = 0)
# Using default distributions to simulate a default M/G/1 Queue
ssq(seed = 1234, maxDepartures = 10, interarrivalType = "M", serviceType = "G",
animate = TRUE, plotDelay = 0)