Basic usage

First, load the package.

library(simmer)

Set-up a simple trajectory. Let’s say we want to simulate an ambulatory consultation where a patient is first seen by a nurse for an intake, next by a doctor for the consultation and finally by administrative staff to schedule a follow-up appointment.

t0 <- create_trajectory("my trajectory") %>%
  ## add an intake activity 
  seize("nurse", 1) %>%
  timeout(function() rnorm(1, 15)) %>%
  release("nurse", 1) %>%
  ## add a consultation activity
  seize("doctor", 1) %>%
  timeout(function() rnorm(1, 20)) %>%
  release("doctor", 1) %>%
  ## add a planning activity
  seize("administration", 1) %>%
  timeout(function() rnorm(1, 5)) %>%
  release("administration", 1)

The argument of the timeout function is evaluated dynamically, so it must be callable and must return a numeric value (note: negative values are automatically converted to positive ones). Apart from that, this function may be as complex as you need and may do whatever you want: interact with entities in your simulation model, get resources’ status, make decisions according to the latter…

When the trajectory is known, the simulation environment can be built. In the example below, an environment is instantiated and three types of resources are added. The nurse and administration resources, each one with a capacity of 1, and the doctor resource, with a capacity of 2. The last method adds a generator of arrivals (patients) following the trajectory t0. The time between patients is about 10 minutes (a Gaussian of mean=10 and sd=2). (Note: returning a negative interarrival time at some point would stop the generator).

env <- simmer("SuperDuperSim") %>%
  add_resource("nurse", 1) %>%
  add_resource("doctor", 2) %>%
  add_resource("administration", 1) %>%
  add_generator("patient", t0, function() rnorm(1, 10, 2))

The simulation is now ready for a test run; just let it simmer for a bit. Below, we specify that we want to limit the runtime to 80 time units using the until argument. After that, we verify the current simulation time (now) and when will be the next event (peek).

env %>% run(until=80)
#> Simmer environment: SuperDuperSim | now: 81.1279429037494 | next: 81.1279429037494
#> { Resource: nurse | monitored: 1 | server status: 1(1) | queue status: 2(Inf) }
#> { Resource: doctor | monitored: 1 | server status: 1(2) | queue status: 0(Inf) }
#> { Resource: administration | monitored: 1 | server status: 0(1) | queue status: 0(Inf) }
#> { Generator: patient | monitored: 1 | n_generated: 9 }
env %>% now()
#> [1] 81.12794
env %>% peek()
#> [1] 81.12794

It is possible to run the simulation step by step, and such a method is chainable too.

env %>% onestep()
#> Simmer environment: SuperDuperSim | now: 81.1279429037494 | next: 81.1279429037494
#> { Resource: nurse | monitored: 1 | server status: 1(1) | queue status: 2(Inf) }
#> { Resource: doctor | monitored: 1 | server status: 2(2) | queue status: 0(Inf) }
#> { Resource: administration | monitored: 1 | server status: 0(1) | queue status: 0(Inf) }
#> { Generator: patient | monitored: 1 | n_generated: 9 }
env %>% onestep() %>% onestep() %>% onestep()
#> Simmer environment: SuperDuperSim | now: 86.3680143899388 | next: 86.3680143899388
#> { Resource: nurse | monitored: 1 | server status: 1(1) | queue status: 2(Inf) }
#> { Resource: doctor | monitored: 1 | server status: 1(2) | queue status: 0(Inf) }
#> { Resource: administration | monitored: 1 | server status: 0(1) | queue status: 0(Inf) }
#> { Generator: patient | monitored: 1 | n_generated: 9 }
env %>% now()
#> [1] 86.36801
env %>% peek()
#> [1] 86.36801

Also, it is possible to resume the automatic execution simply by specifying a longer runtime. Below, we continue the execution until 120 time units.

env %>% 
  run(until=120) %>%
  now()
#> [1] 121.3049

Finally, you can reset the simulation, flush all results, resources and generators, and restart from the beginning.

env %>% 
  reset() %>% 
  run(until=80) %>%
  now()
#> [1] 81.19926

Replication

It is very easy to replicate a simulation multiple times using standard R functions.

envs <- lapply(1:100, function(i) {
  simmer("SuperDuperSim") %>%
    add_resource("nurse", 1) %>%
    add_resource("doctor", 2) %>%
    add_resource("administration", 1) %>%
    add_generator("patient", t0, function() rnorm(1, 10, 2)) %>%
    run(80)
})

The advantage of the latter approach is that, if the individual replicas are heavy, it is straightforward to parallelize their execution (for instance, in the next example we use the function mclapply from the package parallel). Nevertheless, the external pointers to the C++ simmer core are no longer valid when the parallelized execution ends. Thus, it is necessary to extract the results for each thread at the end of the execution. This can be done with the helper function wrap as follows.

library(parallel)

envs <- mclapply(1:100, function(i) {
  simmer("SuperDuperSim") %>%
    add_resource("nurse", 1) %>%
    add_resource("doctor", 2) %>%
    add_resource("administration", 1) %>%
    add_generator("patient", t0, function() rnorm(1, 10, 2)) %>%
    run(80) %>%
    wrap()
})

This helper function brings the simulation data back to R and makes it accessible through the same methods that a simmer environment.

envs[[1]] %>% get_n_generated("patient")
#> [1] 9
envs[[1]] %>% get_capacity("doctor")
#> [1] 2
envs[[1]] %>% get_queue_size("doctor")
#> [1] Inf
head(
  envs[[1]] %>% get_mon_resources()
)
#>       time server queue system resource
#> 1 12.62733      0     0      0    nurse
#> 2 23.40633      1     0      1    nurse
#> 3 27.20772      1     1      2    nurse
#> 4 34.66746      1     0      1    nurse
#> 5 41.54218      1     1      2    nurse
#> 6 42.62761      1     2      3    nurse
head(
  envs[[1]] %>% get_mon_arrivals()
)
#>       name start_time end_time activity_time finished
#> 1 patient0   12.62733 52.44000      39.81267     TRUE
#> 2 patient1   23.40633 68.10617      40.89845     TRUE

Unfortunately, as the C++ simulation cores are destroyed, parallelization does not allow to resume the execution of replicas.

Advanced trajectories

The branch method offers the possibility of adding alternative paths in the trajectory. The following example shows how a trajectory can be built with a 50-50 chance for an arrival to pass through each path of a two-path branch.

t1 <- create_trajectory("trajectory with a branch") %>%
  seize("server", 1) %>%
  branch(function() sample(1:2, 1), merge=c(T, F), 
    create_trajectory("branch1") %>%
      timeout(function() 1),
    create_trajectory("branch2") %>%
      timeout(function() rexp(1, 3)) %>%
      release("server", 1)
  ) %>%
  release("server", 1)

When an arrival gets to the branch, the first argument is evaluated to select a specific path to follow, so it must be callable and must return a numeric value between 1 and n, where n is the number of paths defined. The second argument, merge, indicates whether the arrival must continue executing the activities after the selected path or not. In the example above, only the first path continues to the last release.

Resource utilization

After you’ve left it simmering for a bit (pun intended), we can have a look at the overall resource utilization. The top and bottom of the error bars show respectively the 25th and 75th percentile of the utilization across all the replications. The top of the bar shows the median utilization.

plot_resource_utilization(envs, c("nurse", "doctor","administration"))

It is also possible to have a look at a specific resource and its activity during the simulation.

plot_resource_usage(envs, "doctor", items="server", steps=T)

In the above graph, the individual lines are all separate replications. The step lines are instantaneous utilization and the smooth line is a running average. Let’s take a look now at a specific replication. In the example below the 6th replication is shown.

plot_resource_usage(envs[[6]], "doctor", items="server", steps=T)

Flow time

Next we can have a look at the evolution of the arrivals’ flow time during the simulation. In the below plot, each individual line represents a replication. A smooth line is drawn over them. All arrivals that didn’t finish their entire trajectory are excluded from the plot.

plot_evolution_arrival_times(envs, type = "flow_time")

Similarly one can have a look at the evolution of the activity times with type = "activity_time" and waiting times with type = "waiting_time".