Search code examples
rqueuequeueing

R Simmer - "Number of keys and values don't match"


I am trying to simulate a JSQ(d) (join-shortest-queue of d sampled queues) system but am having trouble executing my code. In this case, let us assume d=2. I wish to have job size determined upon arrival (the value of "X") as well as the selection of which 2 queues will be sampled and "JSQ-ed" (this could be done perhaps differently, though for my purposes beyond this minimal working example, these must be determined upon arrival).

library(simmer)
set.seed(1337)

sim <- simmer("sim")

queues <- vector(length=1000)
for (i in 1:1000) {
  queues[i] <- paste0("q_",i)
}

queueing_system <- trajectory() %>%
  set_attribute("X", function() rexp(1)) %>%
  set_attribute("d", function() sample(1000,2)) %>%
  select(function() queues[get_attribute(sim, "d")], policy="shortest-queue") %>%
  seize_selected()%>%
  timeout(function() get_attribute(sim, "X")*(rpois(1, 1)+1)) %>%
  release_selected()


for (i in 1:1000) {
  sim %>%
    add_resource(queues[i], 1)
}
sim %>%
  add_generator("path", queueing_system, function() rexp(1,1)) %>%
  run(400) %>%
  now()

I receive

Error: 'path0' at 0.15 in [SetAttribute]->SetAttribute->[Select]:
 number of keys and values don't match

What seems to be the issue and how might I go about fixing this?


Solution

  • Attributes store a single value. Why don't you sample directly in the select activity? I.e.,

    queueing_system <- trajectory() %>%
      set_attribute("X", function() rexp(1)) %>%
      select(function() queues[sample(1000, 2)], policy="shortest-queue") %>%
      seize_selected()%>%
      timeout(function() get_attribute(sim, "X")*(rpois(1, 1)+1)) %>%
      release_selected()
    

    I would call rexp(1) in place too, instead of defining X.