Writing a Recursive Loop in R

Consider the following variables in a queueing problem:

  • p_1, p2,...p_n people
  • 1 server that works at a rate of k*mu (this approximates the effect of k servers)
  • a_i = arrival time of person i
  • s_i = service time for person i
  • w_i = waiting time for person i
  • f_i = finish time for person i (when the person leaves after being served)

At each value of t between (t=0,t=100), I am trying to calculate a variable w_i for each person currently in the queue based on the following conditions:

  1. For the p1:

    w_1 = 0

    f_1 = a_1 + s_1

  2. For all other people (i > 1):

    If they arrive after the previous person has finished:

    If : a_i >= f_{i-1}:

    w_i = 0

    f_i = a_i + s_i

    If they arrive before the previous person has finished:

    If: a_i < f_{i-1}:

    w_i = f_{i-1} - a_i

    f_i = f_{i-1} + s_i

Here is what I tried:

I first generated some data for this problem

set.seed(123)
k = 1
n_customers <- 50
max_time <- 100
# parameter for exponential distribution
mu <- k*0.2  

# generate arrival times (uniformly distributed between 0 and 100)
arrivals <- sort(runif(n_customers, 0, max_time))

# generate service times (exponentially distributed)
service_times <- rexp(n_customers, mu)

# vectors to store results
queue_lengths <- numeric(max_time)
cumulative_processed <- numeric(max_time)
total_waiting_times <- numeric(max_time)
finish_times <- numeric(n_customers)
waiting_times <- numeric(n_customers)

I then tried to write a for loop to reflect these logical formulas :

for (t in 1:max_time) {
  processed_this_step <- 0
  for (i in 1:n_customers) {
    if (arrivals[i] <= t) {
      if (i == 1 || (i > 1 && t >= finish_times[i-1])) {
        if (finish_times[i] == 0) {
          start_time <- max(t, arrivals[i])
          finish_times[i] <- start_time + service_times[i]
          waiting_times[i] <- start_time - arrivals[i]
        }
        if (finish_times[i] == t) {
          processed_this_step <- processed_this_step + 1
        }
      } else {
        waiting_times[i] <- waiting_times[i] + 1
      }
    }
  }
  
  queue_lengths[t] <- sum(ifelse(arrivals <= t & finish_times > t, 1, 0))
  
  total_waiting_times[t] <- sum(ifelse(arrivals <= t, 
                                       pmin(t - arrivals, pmax(0, t - finish_times + service_times)), 
                                       0))
  
  cumulative_processed[t] <- ifelse(t > 1, cumulative_processed[t-1], 0) + processed_this_step
}

Then, I vertically rearranged everything:

results_long <- pivot_longer(results, cols = c(queue_length, total_waiting_time, cumulative_processed),
                             names_to = "metric", values_to = "value")

I think there is a problem in the way that the cumulative orders are being calculated since they all come out as 0. Can someone please show me where I am wrong with this logic?

Hello,

From the description of this problem it seems this is some sort of assignment for a course. According to our policy FAQ: Homework Policy - General - Posit Community , we can help you with some pointers but can't provide you with the answer to your issue. You should also add the homework tag to the post to be transparant.

I can help out once I know if this is the case :slight_smile:

Kind regards,
PJ