1

I realize there are not a lot of questions here about using the Simmer package for discrete events simulation in R, but I have gone through all the vignettes and cannot find an answer to perform this seemingly simple task.

I would like to model 2 customers arriving at a random, triangularly distributed check in, with a constant inter-arrival time of 10 minutes. Here is the trajectory I have created using simmer:

 library(simmer)
 traj <- trajectory("admin") %>%
    seize("check_in") %>% 
    timeout(function() rtriangle(a=1, b=3, c=2)) %>% 
    release("check_in")

outpat_clinic <- simmer() %>%
 add_generator("customer", traj, function(){c(0,rep(20,5),-1)}) %>%
 add_resource("check_in", 1)

The 'Many customers' subheading here: https://cran.r-project.org/web/packages/simmer/vignettes/D-bank-1.html#more-customers Is what I have used to get the code above.

Running the above code and checking arrival times shows that the above code simulates a constant inter-arrival time of 20 minutes, but does not have the feature of 2 patients arriving at those times. I am unsure how to create a function to reflect this.

Running the model gives me the following output:

run(clinic, 100)
get_mon_arrivals(clinic)

       name start_time  end_time activity_time finished replication
1 customer0          0  1.623746      1.623746     TRUE           1
2 customer1         20 22.336749      2.336749     TRUE           1
3 customer2         40 42.216531      2.216531     TRUE           1
4 customer3         60 62.019354      2.019354     TRUE           1
5 customer4         80 81.995766      1.995766     TRUE           1

Any insights on this would be much appreciated.

mbc
  • 13
  • 3
  • have you solved this problem yet? The online board for simmer is excellent - run through google, you may be able to get an answer there faster. out of curiosity, do you always need to have two patients arriving at the same time? – MJH Apr 06 '17 at 12:28
  • This is for a class practice exercise, so it was stipulated that there be two patients arriving at the same time with a constant inter-arrival time; I would not think this is very common in a real-world model. – mbc Apr 11 '17 at 17:33

2 Answers2

3

In this case, given that interarrival times are deterministic, you can set another generator as reukil suggests. However, the most general way of doing this is by inserting up to n-1 zeroes if you want to generate arrivals in batches of n. For instance, 3 customers arriving at random:

library(simmer)

traj <- trajectory() %>%
  timeout(1)

simmer() %>%
  add_generator("dummy", traj, function() c(rexp(1, 1), 0, 0)) %>%
  run(4) %>%
  get_mon_arrivals()

#>     name start_time end_time activity_time finished replication
#> 1 dummy0  0.4226528 1.422653             1     TRUE           1
#> 2 dummy1  0.4226528 1.422653             1     TRUE           1
#> 3 dummy2  0.4226528 1.422653             1     TRUE           1
#> 4 dummy3  2.4100059 3.410006             1     TRUE           1
#> 5 dummy4  2.4100059 3.410006             1     TRUE           1
#> 6 dummy5  2.4100059 3.410006             1     TRUE           1
#> 7 dummy6  2.6899432 3.689943             1     TRUE           1
#> 8 dummy7  2.6899432 3.689943             1     TRUE           1
#> 9 dummy8  2.6899432 3.689943             1     TRUE           1

Of course, the batch size may be randomised too:

simmer() %>%
  add_generator("dummy", traj, function() c(rexp(1, 1), rep(0, rpois(1, 2)))) %>%
  run(4) %>%
  get_mon_arrivals()

#>     name start_time end_time activity_time finished replication
#> 1 dummy0  0.6791102  1.67911             1     TRUE           1
#> 2 dummy1  2.7856000  3.78560             1     TRUE           1
#> 3 dummy2  2.7856000  3.78560             1     TRUE           1
#> 4 dummy3  2.7856000  3.78560             1     TRUE           1
Iñaki Úcar
  • 935
  • 1
  • 5
  • 11
  • Thank you for all your work on making an excellent R package for DES and your response. Just to check my understanding, what you are proposing for 2 customers arriving at a constant inter-arrival time 20 would be as follows: ` library(simmer) traj <- trajectory("admin") %>% seize("check_in") %>% timeout(function() rtriangle(a=1, b=3, c=2)) %>% release("check_in") outpat_clinic <- simmer() %>% add_generator("customer", traj, function(){**c(rep(20,5),0,-1)**}) %>% add_resource("check_in", 1) ` – mbc Apr 18 '17 at 17:12
  • Not exactly. It would be `c(0, 0, rep(c(20, 0), 5), -1)`. – Iñaki Úcar Apr 19 '17 at 10:40
2

Just started playing with this nifty package myself. Could you not just include another add_generator for the same trajectory?

# with inter-arrival time = 10 mins
clinic <- simmer() %>%
 add_generator("customer_1", traj, function(){c(0,rep(10,5),-1)}) %>%
 add_generator("customer_2", traj, function(){c(0,rep(10,5),-1)}) %>%
 add_resource("check_in", 1)

Output:

          name start_time  end_time activity_time finished replication
1  customer_10          0  2.391233      2.391233     TRUE           1
2  customer_20          0  4.699580      2.308347     TRUE           1
3  customer_11         10 11.700081      1.700081     TRUE           1
4  customer_21         10 13.459180      1.759099     TRUE           1
5  customer_12         20 21.723494      1.723494     TRUE           1
6  customer_22         20 23.515589      1.792095     TRUE           1
7  customer_13         30 31.279699      1.279699     TRUE           1
8  customer_23         30 32.797642      1.517943     TRUE           1
9  customer_14         40 41.730055      1.730055     TRUE           1
10 customer_24         40 43.690247      1.960192     TRUE           1
11 customer_15         50 52.748773      2.748773     TRUE           1
12 customer_25         50 53.986411      1.237638     TRUE           1

Also i think your trajectory has the timeout following a random triangualar dist, not the arrival. There's also some info here on combining separate trajectories.

reukil
  • 36
  • 2
  • Thank you for the answer. I will compare the result I get with Arena Simulation and see if that produces a similar answer with a lot of repetitions (I'm not sure how to align the seed values so will be slightly different). I apologize with my initial description being cumbersome. There is a constant inter-arrival time, but then the check-in delay is triangularly distributed, so your code should be what I am after as written. Thanks again. – mbc Apr 11 '17 at 17:29
  • I haven't used Arena, so not sure how to align the seeds there. But if you play in Python (eg. [SimPy](https://simpy.readthedocs.io/en/latest/contents.html)), this [post](http://stackoverflow.com/questions/22213298/creating-same-random-number-sequence-in-python-numpy-and-r#) has some info on aligning the seed values. – reukil Apr 12 '17 at 00:20