--- title: "Queueing" author: "Math 365 Tanya Leise" date: "January 1, 2018" output: html_document --- The objective of this lab is to further study queueing as a type of birth and death process using a combination of simulations and theoretical analysis. ## Simulation of M/M/1 Queue In general for birth-and-death queueing processes, we let $\lambda_n$ denote the arrival rate when there are currently $n$ customers, and $\mu_n$ denote the departure rate, where both arrivals and departures are treated as exponential processes. That is, if there are currently $n$ customers, the time until the next event is an exponential random variable with rate $\lambda_n+\mu_n$; the probability that the next event is an arrival is $\frac{\lambda_n}{\lambda_n+\mu_n}$ and the probability that the next event is a departure is $\frac{\mu_n}{\lambda_n+\mu_n}$. The M/M/1 queue has constant rates $\lambda_n=\lambda$ for $n\ge0$ and $\mu_n=\mu$ for $n\ge1$ ($\mu_0=0$). We'll start by running some simulations of an M/M/1 queue to see some examples of what such processes look like for the positive recurrent case ($\lambda<\mu$) in which we expect to repeatedly return to an empty queue. Feel free to vary the values of $\lambda$ and $\mu$ in the simulation. If it seems to be running too long, halt it and try a different pair of values (make sure $\lambda<\mu$ so you don't get exploding queues). First initialize some variables: ```{r} # M/M/1 queue simulator lambda <- 1 # arrival rate mu <- 4 # service rate duration <- 10000 # total duration of the simulation t <- 0 # current time in the simulation queue <- 0 # start with empty queue s <- 0 # running sum for computing average queue length ``` We assume the queue is initially empty, so generate a first arrival to the queue with rate $\lambda$: ```{r} # first arrival to start process T1 <- rexp(1,rate=lambda) currentqueue <- 1 eventsTime <- T1 t <- T1 nEvents <- 1 # total number of events that have occurred ``` The next event can be either an arrival or departure. The time $T_1$ until that next event is an exponential random variable with rate $\lambda+\mu$. To determine whether that event is an arrival or a departure, generate a random number $p$ between 0 and 1; if $p<\frac{\lambda}{\lambda+\mu}$, then the event is an arrival, otherwise it's a departure. ```{r} while (t0) { # nonempty queue T1 <- rexp(1,rate=lambda+mu) # time until next event # is event an arrival or departure? p <- runif(1,0,1) queue[nEvents] <- currentqueue # length of queue before this new event currentqueue <- ifelse(p