Skip to content

Commit c0b948d

Browse files
committed
feat/refactor/fix(distributionregistry): amend model + add_generator + add_trajectory (#11)
1 parent 2588e9d commit c0b948d

File tree

4 files changed

+36
-58
lines changed

4 files changed

+36
-58
lines changed

R/add_patient_generator.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ add_patient_generator <- function(env, trajectory, unit, patient_type, param) {
2929
name_prefix = paste0(unit, "_", patient_type),
3030
trajectory = trajectory,
3131
distribution = function() {
32-
rexp(1L, 1L / param[[paste0(unit, "_arrivals")]][[patient_type]])
32+
param[["dist"]][["arrivals"]][[unit]][[patient_type]]()
3333
}
3434
)
3535
}

R/create_asu_trajectory.R

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,30 @@ create_asu_trajectory <- function(env, patient_type, param) {
3232

3333
# Sample destination after ASU (as destination influences length of stay)
3434
set_attribute("post_asu_destination", function() {
35-
sample_routing(prob_list = param[["asu_routing"]][[patient_type]])
35+
param[["dist"]][["routing"]][["asu"]][[patient_type]]()
3636
}) |>
3737

3838
log_(function() {
39-
# Retrieve attribute, and use to get post-ASU destination as a string
40-
dest_index <- get_attribute(env, "post_asu_destination")
41-
dest_names <- names(param[["asu_routing"]][[patient_type]])
42-
dest <- dest_names[dest_index]
43-
# Create log message
44-
paste0("\U0001F3AF Planned ASU -> ", dest_index, " (", dest, ")")
39+
dest <- get_attribute(env, "post_asu_destination")
40+
paste0("\U0001F3AF Planned ASU -> ", dest)
4541
}, level = 1L) |>
4642

43+
# Sample ASU LOS. For stroke patients, LOS distribution is based on
44+
# the planned destination after the ASU.
4745
set_attribute("asu_los", function() {
48-
# Retrieve attribute, and use to get post-ASU destination as a string
49-
dest_index <- get_attribute(env, "post_asu_destination")
50-
dest_names <- names(param[["asu_routing"]][[patient_type]])
51-
dest <- dest_names[dest_index]
52-
53-
# Determine which LOS distribution to use
46+
dest <- get_attribute(env, "post_asu_destination")
5447
if (patient_type == "stroke") {
55-
los_params <- switch(
48+
switch(
5649
dest,
57-
esd = param[["asu_los_lnorm"]][["stroke_esd"]],
58-
rehab = param[["asu_los_lnorm"]][["stroke_no_esd"]],
59-
other = param[["asu_los_lnorm"]][["stroke_mortality"]],
50+
esd = param[["dest"]][["los"]][["asu"]][["stroke_esd"]],
51+
rehab = param[["dest"]][["los"]][["asu"]][["stroke_no_esd"]],
52+
other = param[["dest"]][["los"]][["asu"]][["stroke_mortality"]],
6053
stop("Stroke post-asu destination '", dest, "' invalid",
6154
call. = FALSE)
6255
)
6356
} else {
64-
los_params <- param[["asu_los_lnorm"]][[patient_type]]
57+
param[["dest"]][["los"]][["asu"]][[patient_type]]()
6558
}
66-
67-
# Sample LOS from lognormal
68-
rlnorm(
69-
n = 1L,
70-
meanlog = los_params[["meanlog"]],
71-
sdlog = los_params[["sdlog"]]
72-
)
7359
}) |>
7460

7561
log_(function() {
@@ -86,12 +72,7 @@ create_asu_trajectory <- function(env, patient_type, param) {
8672
# If that patient's destination is rehab, then start on that trajectory
8773
branch(
8874
option = function() {
89-
# Retrieve attribute, and use to get post-ASU destination as a string
90-
dest_index <- get_attribute(env, "post_asu_destination")
91-
dest_names <- names(param[["asu_routing"]][[patient_type]])
92-
dest <- dest_names[dest_index]
93-
# Return 1 for rehab and 0 otherwise
94-
if (dest == "rehab") 1L else 0L
75+
if (get_attribute(env, "post_asu_destination") == "rehab") 1L else 0L
9576
},
9677
continue = FALSE, # Do not continue main trajectory after branch
9778
create_rehab_trajectory(env, patient_type, param)

R/create_rehab_trajectory.R

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,29 @@ create_rehab_trajectory <- function(env, patient_type, param) {
3232

3333
# Sample destination after rehab (as destination influences length of stay)
3434
set_attribute("post_rehab_destination", function() {
35-
sample_routing(prob_list = param[["rehab_routing"]][[patient_type]])
35+
param[["dist"]][["routing"]][["rehab"]][[patient_type]]()
3636
}) |>
3737

3838
log_(function() {
39-
# Retrieve attribute, and use to get post-rehab destination as a string
40-
dest_index <- get_attribute(env, "post_rehab_destination")
41-
dest_names <- names(param[["rehab_routing"]][[patient_type]])
42-
dest <- dest_names[dest_index]
43-
# Create log message
39+
dest <- get_attribute(env, "post_rehab_destination")
4440
paste0("\U0001F3AF Planned rehab -> ", dest_index, " (", dest, ")")
4541
}, level = 1L) |>
4642

43+
# Sample rehab LOS. For stroke patients, LOS distribution is based on
44+
# the planned destination after the rehab
4745
set_attribute("rehab_los", function() {
48-
# Retrieve attribute, and use to get post-rehab destination as a string
49-
dest_index <- get_attribute(env, "post_rehab_destination")
50-
dest_names <- names(param[["rehab_routing"]][[patient_type]])
51-
dest <- dest_names[dest_index]
52-
53-
# Determine which LOS distribution to use
46+
dest <- get_attribute(env, "post_rehab_destination")
5447
if (patient_type == "stroke") {
55-
los_params <- switch(
48+
switch(
5649
dest,
57-
esd = param[["rehab_los_lnorm"]][["stroke_esd"]],
58-
other = param[["rehab_los_lnorm"]][["stroke_no_esd"]],
50+
esd = param[["los"]][["rehab"]][["stroke_esd"]](),
51+
other = param[["los"]][["rehab"]][["stroke_no_esd"]](),
5952
stop("Stroke post-rehab destination '", dest, "' invalid",
6053
call. = FALSE)
6154
)
6255
} else {
63-
los_params <- param[["rehab_los_lnorm"]][[patient_type]]
56+
param[["los"]][["rehab"]][[patient_type]]()
6457
}
65-
66-
# Sample LOS from lognormal
67-
rlnorm(
68-
n = 1L,
69-
meanlog = los_params[["meanlog"]],
70-
sdlog = los_params[["sdlog"]]
71-
)
7258
}) |>
7359

7460
log_(function() {

R/model.R

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,20 @@ model <- function(run_number, param, set_seed = TRUE) {
3232
param[["verbose"]] <- any(c(param[["log_to_console"]],
3333
param[["log_to_file"]]))
3434

35-
# Transform LOS parameters to lognormal scale
36-
param[["asu_los_lnorm"]] <- transform_to_lnorm(param[["asu_los"]])
37-
param[["rehab_los_lnorm"]] <- transform_to_lnorm(param[["rehab_los"]])
35+
# Set up sampling distributions
36+
registry <- simulation::DistributionRegistry$new()
37+
param[["dist"]] <- registry$create_batch(as.list(param[["dist_config"]]))
38+
39+
# Restructure as dist[type][unit][patient]
40+
dist <- list()
41+
for (key in names(param[["dist"]])) {
42+
parts <- strsplit(key, "_")[[1]]
43+
dist_type <- parts[2]
44+
unit <- parts[1]
45+
patient <- paste(parts[-(1:2)], collapse = "_")
46+
dist[[dist_type]][[unit]][[patient]] <- param[["dist"]][[key]]
47+
}
48+
param[["dist"]] <- dist
3849

3950
# Create simmer environment - set verbose to FALSE as using custom logs
4051
# (but can change to TRUE if want to see default simmer logs as well)

0 commit comments

Comments
 (0)