Simulate Doctor Visit Resource Planning Using Simmer

Here I’ve tried to come up with a simple layout on how we might simulate doctor’s visits.

Following code tries to resource plan certain doctor visits based on the vignette that was provided with the simmer package.

library(simmer)
## Warning: package 'simmer' was built under R version 3.6.2
set.seed(42)
env <- simmer("SuperDuperSim")
env
## simmer environment: SuperDuperSim | now: 0 | next: 
## { Monitor: in memory }
patient <- trajectory("patients' path") %>%
  ## 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)


env %>%
  add_resource("nurse", 1) %>%
  add_resource("doctor", 2) %>%
  add_resource("administration", 1) %>%
  add_generator("patient", patient, function() rnorm(1, 10, 2))
## simmer environment: SuperDuperSim | now: 0 | next: 0
## { Monitor: in memory }
## { Resource: nurse | monitored: TRUE | server status: 0(1) | queue status: 0(Inf) }
## { Resource: doctor | monitored: TRUE | server status: 0(2) | queue status: 0(Inf) }
## { Resource: administration | monitored: TRUE | server status: 0(1) | queue status: 0(Inf) }
## { Source: patient | monitored: 1 | n_generated: 0 }
env %>% 
  run(80) %>% 
  now()
## [1] 80
env %>% peek(3)
## [1] 80.69540 81.62105 81.62105
env %>%
  stepn() %>% # 1 step
  print() %>%
  stepn(3)    # 3 steps
## simmer environment: SuperDuperSim | now: 80.6953988949657 | next: 80.6953988949657
## { Monitor: in memory }
## { Resource: nurse | monitored: TRUE | server status: 1(1) | queue status: 1(Inf) }
## { Resource: doctor | monitored: TRUE | server status: 1(2) | queue status: 0(Inf) }
## { Resource: administration | monitored: TRUE | server status: 0(1) | queue status: 0(Inf) }
## { Source: patient | monitored: 1 | n_generated: 7 }
## simmer environment: SuperDuperSim | now: 81.6210531397386 | next: 81.6210531397386
## { Monitor: in memory }
## { Resource: nurse | monitored: TRUE | server status: 1(1) | queue status: 2(Inf) }
## { Resource: doctor | monitored: TRUE | server status: 1(2) | queue status: 0(Inf) }
## { Resource: administration | monitored: TRUE | server status: 0(1) | queue status: 0(Inf) }
## { Source: patient | monitored: 1 | n_generated: 7 }
envs <- lapply(1:100, function(i) {
  simmer("SuperDuperSim") %>%
    add_resource("nurse", 1) %>%
    add_resource("doctor", 2) %>%
    add_resource("administration", 1) %>%
    add_generator("patient", patient, function() rnorm(1, 10, 2)) %>%
    run(80)
})


envs %>% 
  get_mon_resources() %>%
  head()
##   resource     time server queue capacity queue_size system limit replication
## 1    nurse 12.64023      1     0        1        Inf      1   Inf           1
## 2    nurse 22.02695      1     1        1        Inf      2   Inf           1
## 3    nurse 25.85892      1     0        1        Inf      1   Inf           1
## 4   doctor 25.85892      1     0        2        Inf      1   Inf           1
## 5    nurse 31.68311      1     1        1        Inf      2   Inf           1
## 6    nurse 40.82218      1     2        1        Inf      3   Inf           1
envs %>% 
  get_mon_arrivals() %>%
  head()
##       name start_time end_time activity_time finished replication
## 1 patient0   12.64023 52.11412      39.47389     TRUE           1
## 2 patient1   22.02695 68.03865      42.17973     TRUE           1
## 3 patient2   31.68311 78.93133      36.85773     TRUE           1
## 4 patient0   10.41200 51.92490      41.51291     TRUE           2
## 5 patient1   19.68988 65.73408      39.56392     TRUE           2
## 6 patient0   10.37046 52.01586      41.64540     TRUE           3

comments powered by Disqus