Learn R Programming

simEd (version 1.0.3)

ssq: 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       = defaultInterarrival,
    serviceFcn            = defaultService,
    maxTime               = Inf,
    maxDepartures         = Inf,
    saveAllStats          = FALSE,
    saveInterarrivalTimes = FALSE,
    saveServiceTimes      = FALSE,
    saveWaitTimes         = FALSE,
    saveSojournTimes      = FALSE,
    saveNumInQueue        = FALSE,
    saveNumInSystem       = FALSE,
    saveServerStatus      = FALSE,
    showOutput            = TRUE,
    showProgress          = TRUE
  )

Arguments

maxArrivals

maximum number of 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 that generates next interarrival time (default uses vexp(1, 1.0, stream = 1))

serviceFcn

function that generates next service time (default uses vexp(1, 10/9, stream = 2))

maxTime

maximum time to simulate

maxDepartures

maximum number of customer departures to process

saveAllStats

if TRUE, returns all vectors of statistics (see below) collected by the simulation

saveInterarrivalTimes

if TRUE, returns a vector of all interarrival times generated

saveServiceTimes

if TRUE, returns a vector of all service times generated

saveWaitTimes

if TRUE, returns a vector of all wait times (in the queue) generated

saveSojournTimes

if TRUE, returns a vector of all sojourn (time in the system) times generated

saveNumInQueue

if TRUE, returns a vector of times and a vector of counts for whenever the number in the queue changes

saveNumInSystem

if TRUE, returns a vector of times and a vector of counts for whenever the number in the system changes

saveServerStatus

if TRUE, returns a vector of times and a vector of server status (0:idle, 1:busy) for whenever the status changes

showOutput

if TRUE, displays summary statistics upon completion

showProgress

if TRUE, displays a progress bar on screen during execution

Value

A list.

Details

Implements a next-event implementation of a single-server queue simulation. 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), and

  • server utilization (utilization).

of the single-server 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), and

  • two vectors (time and status) noting changes to server status (serverStatusT, serverStatusN).

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 to set.seed, thereby setting the initial seed using the system clock.

Examples

Run this code
# NOT RUN {
  # process 2000 arrivals, R-provided seed (via NULL seed)
  ssq(2000, NULL)

  ssq(maxArrivals = 2000, seed = 54321)
  ssq(maxDepartures = 2000, seed = 54321)
  ssq(maxTime = 1000, seed = 54321)

  ############################################################################
  # example to show use of seed = NA (default) to rely on current state of generator
  output1 <- ssq(2000, 8675309, showOutput = FALSE, saveAllStats = TRUE)
  output2 <- ssq(3000,          showOutput = FALSE, saveAllStats = TRUE)
  set.seed(8675309)
  output3 <- ssq(2000,          showOutput = FALSE, saveAllStats = TRUE)
  output4 <- ssq(3000,          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 = 1000, 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
  #
  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

  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)
# }

Run the code above in your browser using DataLab