Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Imports:
rlang,
tidyr,
R6,
tidyselect
tidyselect,
future,
future.apply
Suggests:
testthat (>= 3.0.0),
lintr,
Expand Down
10 changes: 4 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ importFrom(dplyr,lead)
importFrom(dplyr,mutate)
importFrom(dplyr,n_distinct)
importFrom(dplyr,summarise)
importFrom(future,multisession)
importFrom(future,plan)
importFrom(future,sequential)
importFrom(future.apply,future_lapply)
importFrom(magrittr,"%>%")
importFrom(parallel,clusterEvalQ)
importFrom(parallel,clusterExport)
importFrom(parallel,detectCores)
importFrom(parallel,makeCluster)
importFrom(parallel,parLapply)
importFrom(parallel,stopCluster)
importFrom(purrr,reduce)
importFrom(rlang,.data)
importFrom(simmer,add_generator)
Expand Down
2 changes: 1 addition & 1 deletion R/defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Defaults <- R6Class( # nolint
#' the class without needing to run `Defaults[["new"]]()` every time.
#'
#' @return Instance of the Defaults class.
#'
#'
#' @export
#' @rdname defaults

Expand Down
9 changes: 7 additions & 2 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#'
#' @param run_number Integer representing index of current simulation run.
#' @param param_class Instance of Defaults containing model parameters.
#' @param set_seed Whether to set seed within the model function (which we
#' may not wish to do if being set elsewhere - such as done in trial()).
#' Default is TRUE.
#'
#' @importFrom simmer trajectory seize timeout release simmer add_resource
#' @importFrom simmer add_generator run wrap get_mon_arrivals
Expand All @@ -12,7 +15,7 @@
#' @return Named list with two tables: monitored arrivals and resources
#' @export

model <- function(run_number, param_class) {
model <- function(run_number, param_class, set_seed = TRUE) {

# Extract parameter list from the parameter class
# It is important to do this within the model function (rather than
Expand All @@ -25,7 +28,9 @@ model <- function(run_number, param_class) {
valid_inputs(run_number, param)

# Set random seed based on run number
set.seed(run_number)
if (set_seed) {
set.seed(run_number)
}

# Define the patient trajectory
patient <- trajectory("appointment") %>%
Expand Down
46 changes: 19 additions & 27 deletions R/trial.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#' Run simulation for multiple replications, sequentially or in parallel.
#'
#' Note: The parallel processing is implemented using `parLapply` because
#' `mcLapply`` does not work on Windows and `future_lapply`` would often get
#' stuck.
#'
#' @param param_class Instance of Defaults containing model parameters.
#'
#' @importFrom parallel detectCores makeCluster stopCluster clusterEvalQ
#' @importFrom parallel clusterExport parLapply
#' @importFrom future plan multisession sequential
#' @importFrom future.apply future_lapply
#'
#' @return Named list with two tables: monitored arrivals and resources.
#' @export
Expand All @@ -17,35 +13,31 @@ trial <- function(param_class) {
n_cores <- param_class[["get"]]()[["cores"]]
n_runs <- param_class[["get"]]()[["number_of_runs"]]

# Determine the parallel execution plan
if (n_cores == 1L) {

# Sequential execution
results <- lapply(
1L:n_runs,
function(i) simulation::model(run_number = i, param_class = param_class)
)

plan(sequential) # Sequential execution
} else {
# Parallel execution

# Create a cluster with specified number of cores
if (n_cores == -1L) {
cores <- detectCores() - 1L
cores <- future::availableCores() - 1L
} else {
cores <- n_cores
}
cl <- makeCluster(cores)

# Ensure the cluster is stopped upon exit
on.exit(stopCluster(cl))

# Run simulations in parallel
results <- parLapply(
cl, 1L:n_runs,
function(i) simulation::model(run_number = i, param_class = param_class)
)
plan(multisession, workers = cores) # Parallel execution
}

# Run simulations (sequentially or in parallel)
# Mark set_seed as FALSE as we handle this using future.seed(), rather than
# within the function, and we don't want to override future.seed
results <- future_lapply(
1L:n_runs,
function(i) {
simulation::model(run_number = i,
param_class = param_class,
set_seed = FALSE)
},
future.seed = 123456L
)

# Combine the results from multiple replications into just two dataframes
if (n_runs > 1L) {
all_arrivals <- do.call(
Expand Down
6 changes: 5 additions & 1 deletion man/model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions man/trial.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

200 changes: 100 additions & 100 deletions outputs/base_trial.csv
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
"","replication","arrivals","mean_waiting_time_nurse","mean_activity_time_nurse","utilisation_nurse"
"1",1,15,0,8.92266571894842,0.371528668451366
"2",2,16,0,7.7255587739498,0.399865009965906
"3",3,14,0,7.55613765895507,0.34983454158788
"4",4,19,1.53581418282632,8.0588103472346,0.604995664123677
"5",5,21,0.0519866784761905,9.35135333211744,0.573584434011877
"6",6,29,0.384083299862069,8.20993074704441,0.611272646882443
"7",7,16,0,9.54055185249263,0.401321053724437
"8",8,19,0.497575033078947,8.57860709328252,0.448350482951233
"9",9,19,0,10.0769710427457,0.627360482435886
"10",10,15,0,8.01416495697315,0.336149156108189
"11",11,8,0,8.19813956226296,0.266580242951386
"12",12,12,0,12.4423690398617,0.4411930714609
"13",13,20,0.227177603555,9.34805206374257,0.515029185220157
"14",14,18,0,5.23327658447847,0.385068311969305
"15",15,22,0.0589624537590909,6.84200713184188,0.508430352633121
"16",16,22,0.926212363481818,9.48213350689228,0.588485465605031
"17",17,20,0.07705566903,9.27393115935178,0.536720444332965
"18",18,26,0.0945137374423077,10.8162048914333,0.810276629606859
"19",19,22,1.2570415664,7.78838345773335,0.561919275982066
"20",20,14,0,10.5890431482814,0.46375500374331
"21",21,18,0.0716436412222222,5.10846690914392,0.24044400368886
"22",22,21,0.0591580490190476,9.02494565357537,0.503210923332613
"23",23,21,0.175647701361905,8.22091740614917,0.508214600684611
"24",24,22,0,6.39763986519681,0.378833989818279
"25",25,19,0,8.5503512865253,0.447278181574428
"26",26,21,0.783577928152381,7.45344603512225,0.662036006480184
"27",27,20,1.2903625216,11.9739449424173,0.745215734677497
"28",28,16,0.00683261825625,8.37759384824821,0.551655575186502
"29",29,12,0,5.86264785410928,0.266510144455835
"30",30,20,0.04474548582,9.01076862622505,0.571228028042687
"31",31,16,0.06295110160625,10.0528316919784,0.486085855334318
"32",32,18,0,6.14756691173725,0.349095910560405
"33",33,18,0,5.99381148722807,0.3896454176773
"34",34,20,0.0106671803,8.78699768495577,0.584918663395058
"35",35,24,0.0375949580125,6.1250148346272,0.427273068939378
"36",36,16,0,5.08653652591333,0.214553253790685
"37",37,12,0,9.22254811747763,0.295193944486686
"38",38,15,0,8.30040012220122,0.338457396220477
"39",39,15,0,8.58648124293031,0.360080420090988
"40",40,19,0.00535527148947368,10.7022239471977,0.528871131735171
"41",41,18,0,7.57769866661476,0.436048290674667
"42",42,21,0.266500101061905,10.7945944992205,0.589837388963679
"43",43,17,0.397722008847059,8.89793112128514,0.509968362924036
"44",44,25,0,6.40573070245996,0.418591631105296
"45",45,18,0,6.05024950804647,0.283169912660256
"46",46,17,0.179988911964706,8.67006372925668,0.503745498250098
"47",47,17,0,8.04598031261248,0.496478425937344
"48",48,17,0,9.9499986364565,0.563482722117011
"49",49,17,0.264439412652941,8.02227432181301,0.385527495971647
"50",50,14,0,8.76117602769676,0.335233570735889
"51",51,20,0.28362357005,9.26156379184728,0.546198822373836
"52",52,20,0,7.39720045605086,0.4143982432236
"53",53,19,0.0938929813473684,8.56326729947669,0.45861277528473
"54",54,14,0,10.659332903371,0.382246636436652
"55",55,18,0,7.87964575416427,0.374564675074477
"56",56,18,0,7.37112380014318,0.460975698751186
"57",57,19,0,6.93446257276265,0.365720038954959
"58",58,22,0.842384962627273,10.9114072850773,0.643990582947036
"59",59,16,0,7.33445308964826,0.447441729120187
"60",60,16,0.0918236140875,8.03323464120855,0.369527919200035
"61",61,11,0,13.5779057867523,0.483989102395751
"62",62,18,0.395429763688889,9.49766947771225,0.448792455864958
"63",63,15,0,8.0549803191723,0.388190967309224
"64",64,25,0.683926920868,10.5707000369387,0.808254250632857
"65",65,12,0,9.2658170718876,0.441807668730405
"66",66,14,0,7.86698203584097,0.387681107582835
"67",67,23,0.23453886086087,10.5046831987208,0.703233950990321
"68",68,17,1.14385153219412,7.18628445207151,0.551470182223506
"69",69,18,0,9.32508026499198,0.424200095363553
"70",70,15,0,9.88855559309646,0.462207362572994
"71",71,18,0,7.07917744950834,0.378879954108974
"72",72,22,0,5.05825135675878,0.303948460159635
"73",73,16,0,6.54817416100394,0.276552161950288
"74",74,22,0.0762762008636364,7.08570390399686,0.520615628616018
"75",75,15,0,8.35517359316539,0.3462056554242
"76",76,26,0.231123361023077,9.34246451197043,0.667359672400375
"77",77,13,0,5.53465633946736,0.226673832853035
"78",78,18,0,9.61261062598683,0.498084396177169
"79",79,18,0.245395860016667,9.04738422111327,0.553657714791215
"80",80,9,0,5.50085843079494,0.201336272341734
"81",81,16,0,9.0184545999554,0.419145974185224
"82",82,11,0,8.31245460334258,0.280365167135215
"83",83,13,0.0655415281846154,8.43917168711658,0.471247105983744
"84",84,14,0,5.66624868295414,0.25804810923752
"85",85,13,0,8.59084061245018,0.304516812184508
"86",86,17,0,5.91406597581937,0.280350493672794
"87",87,20,0.205811518405,9.2933162848188,0.69631283888828
"88",88,11,0,8.43495978322226,0.27987165805182
"89",89,12,0,5.21308985165848,0.204204859226964
"90",90,24,0.571064473258333,9.9324202708865,0.60481764623509
"91",91,16,0,9.04945415537796,0.414276432818536
"92",92,18,0,8.81995817950373,0.574697475286729
"93",93,12,0.271884909941667,11.6058512500617,0.519983434440122
"94",94,17,0.0601171019647059,7.56813893525244,0.362623085040635
"95",95,11,0,9.75339371460961,0.437785712688619
"96",96,19,0,11.1541294873282,0.558235643922686
"97",97,12,0,7.37929923388839,0.291381665516697
"98",98,14,0,10.0787670561435,0.447482270035736
"99",99,16,0.14587324370625,7.78504644281919,0.562039183458945
"100",100,19,0,7.04299845211555,0.360356855474523
"1",1,21,0.172634907604762,10.7405465431143,0.6857207386012
"2",2,16,0,7.10057623127618,0.339653664237136
"3",3,13,0,6.88747631698383,0.264798148560583
"4",4,16,0.01773150233125,9.28954224746827,0.454079770130564
"5",5,17,0,4.78654797563128,0.266413092811991
"6",6,18,0.393059339322222,8.11718767381037,0.58554969502398
"7",7,25,0.111893570592,8.41810983409961,0.55033912691785
"8",8,16,0,6.66989735996729,0.346967751006821
"9",9,22,1.94776083503636,9.00372743303383,0.644303386268358
"10",10,22,0.0487056471818182,8.99915934342635,0.566310989374246
"11",11,13,0,6.7943906100343,0.259819956897941
"12",12,13,0,6.07836862088896,0.259628997899959
"13",13,14,0.157631317071429,7.94243342769353,0.355801959587693
"14",14,22,0.175854982204545,9.71531458660121,0.598313695770614
"15",15,18,0,9.39191265113015,0.546128420946744
"16",16,13,0,5.90821888504484,0.228154581728323
"17",17,17,0,6.06421073513055,0.321583877537017
"18",18,12,0,6.77546466202225,0.210183884317542
"19",19,24,0,5.44514836439291,0.366612655205927
"20",20,15,0,6.18916668588235,0.265184977125269
"21",21,12,0,4.61619341536056,0.232317570980903
"22",22,19,0.116305906489474,10.2683047405045,0.512079315391267
"23",23,21,0.0749881320190476,10.659018482097,0.698974517960742
"24",24,24,0.4664847124,7.08382960437018,0.539403679605581
"25",25,15,0,7.23255467683587,0.384654754001199
"26",26,18,0,6.85819116541343,0.399796048788589
"27",27,23,0.014511273173913,7.97383702009454,0.499049148145453
"28",28,23,0,7.46106098517586,0.45851030026982
"29",29,20,0.01418191837,11.503573117205,0.674316256541288
"30",30,12,0,12.4820647109409,0.592051818635463
"31",31,17,0,8.9585579247382,0.475338691956434
"32",32,22,1.14093051919091,8.85676296326767,0.596681932320881
"33",33,21,0.445679449809524,11.7769481804205,0.64686352151671
"34",34,22,0.0558661645636364,7.59793643959547,0.4242058989872
"35",35,20,0.279059747785,9.8092481090931,0.532472767720392
"36",36,12,0,8.88627405823332,0.296645804805517
"37",37,14,0,6.16763443942273,0.260984326563795
"38",38,16,0,11.0374009522411,0.584349991863959
"39",39,23,0,5.83910191928464,0.373047338281816
"40",40,13,0,8.85535418008837,0.321048787172079
"41",41,17,0,9.86153219518894,0.471157745370755
"42",42,18,0.265085758005556,9.32962711420884,0.463630613451225
"43",43,30,0.413022382443333,7.28345583792507,0.632923144372885
"44",44,19,0,6.20265200760801,0.355604631521442
"45",45,17,0,9.16558333047932,0.498215814669524
"46",46,14,0,8.50358144323973,0.40451293404052
"47",47,20,0,4.9416280548959,0.280518734931329
"48",48,18,0.338606286466667,10.8625905555314,0.618974673076519
"49",49,16,0.09190991038125,7.45822771304233,0.304140833413389
"50",50,18,0.350054970055556,11.4160970539544,0.571846232578289
"51",51,24,1.22428705825833,11.1141699595254,0.77435356741355
"52",52,22,1.0747237336,10.9845036501615,0.762205674869442
"53",53,24,0.2956247858125,8.29628122523315,0.593217310157795
"54",54,20,0,7.42836215073758,0.437976044150862
"55",55,25,0.701470431216,10.7697143197363,0.792437085562371
"56",56,18,0,3.68742876166258,0.232531394741985
"57",57,18,1.11819213068333,12.8372662960699,0.647481513718578
"58",58,15,0,8.57061685914263,0.35528893436266
"59",59,18,0,13.2698615975004,0.646487895791919
"60",60,20,0.163873754455,5.73347085933515,0.344016064107376
"61",61,19,0.291918888036842,8.89945109341046,0.454234823022512
"62",62,13,0,8.98985445679083,0.372221663736418
"63",63,24,0.0741459701458333,8.770849255326,0.661904843807603
"64",64,16,0.6466449799375,10.8619650858589,0.653223825542272
"65",65,16,0,8.65165295002729,0.428122599229444
"66",66,19,0.331859246468421,9.72538181217374,0.482166296787823
"67",67,15,0,8.90898881288831,0.491279299284598
"68",68,13,0.0347675995307692,9.57747118609483,0.46226336902312
"69",69,12,0,8.41563988505943,0.273131482072979
"70",70,18,0.0100520341111111,7.22101186179044,0.362790629624835
"71",71,13,0.0726815632923077,6.02743235273608,0.249677973975859
"72",72,16,0,8.94662318051259,0.396719862555918
"73",73,15,0,6.00094877202697,0.253757037388511
"74",74,24,0.9257580821375,6.23331615319016,0.525902232465197
"75",75,23,0,4.65243453860885,0.449184422355558
"76",76,14,0,6.47874777951893,0.333694339335394
"77",77,24,0.874672642745833,9.76727133229084,0.741477726930124
"78",78,15,0,10.2072754254209,0.488768954419666
"79",79,16,0.05332475145,7.08323650705356,0.440013578258183
"80",80,19,0.324464807021053,12.0887824219051,0.594428948563637
"81",81,24,1.02711901124583,8.82194799562383,0.579714838504693
"82",82,23,0.848633635556522,7.33478428088739,0.612070570432051
"83",83,19,0,6.72578993443048,0.4010223968927
"84",84,17,0.0086244323,7.49703271709652,0.336173465508689
"85",85,19,0.0206446611684211,9.03665844396845,0.466591100821903
"86",86,25,0.708471232184,8.73863864542212,0.64752461004885
"87",87,20,0.358855828215,8.97433462369758,0.495032753524222
"88",88,13,0,14.1183180194591,0.56577858211405
"89",89,15,0,8.51856460928998,0.368011304895727
"90",90,21,0.019621619152381,9.41218877944077,0.572715294625353
"91",91,24,0,7.43438548758834,0.552238861244287
"92",92,16,2.12286939339375,12.4974164222768,0.655423578887326
"93",93,23,0.56134304333913,10.612379013797,0.783901564319108
"94",94,13,0,4.90019655529476,0.186899046605271
"95",95,14,0,8.44998224756754,0.316635268100141
"96",96,23,0.30675455233913,7.45276488235241,0.506751654132952
"97",97,17,0.144732497258824,9.26537058668207,0.426026115330518
"98",98,20,0.238843066105,9.94408414189002,0.538063695810092
"99",99,17,0.112457353976471,9.82925194721256,0.550634397521811
"100",100,16,0.2714164474625,7.80477320313942,0.422094474520408
Binary file modified outputs/choose_param_conf_int_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified outputs/choose_param_conf_int_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified outputs/choose_param_conf_int_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified outputs/cores1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified outputs/cores2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified outputs/scenario_nurse_util.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions outputs/scenario_nurse_util.tex
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
\hline
& Patient inter-arrival time & 5 nurses & 6 nurses & 7 nurses & 8 nurses \\
\hline
1 & 3 & 0.58 (0.55, 0.61) & 0.49 (0.46, 0.51) & 0.42 (0.40, 0.44) & 0.37 (0.35, 0.39) \\
2 & 4 & 0.45 (0.42, 0.48) & 0.38 (0.35, 0.40) & 0.32 (0.30, 0.34) & 0.28 (0.27, 0.30) \\
3 & 5 & 0.38 (0.35, 0.40) & 0.31 (0.29, 0.33) & 0.27 (0.25, 0.28) & 0.23 (0.22, 0.25) \\
4 & 6 & 0.31 (0.29, 0.34) & 0.26 (0.24, 0.28) & 0.22 (0.21, 0.24) & 0.20 (0.18, 0.21) \\
5 & 7 & 0.28 (0.26, 0.30) & 0.23 (0.22, 0.25) & 0.20 (0.18, 0.21) & 0.17 (0.16, 0.19) \\
1 & 3 & 0.60 (0.57, 0.63) & 0.51 (0.49, 0.54) & 0.44 (0.42, 0.46) & 0.39 (0.36, 0.41) \\
2 & 4 & 0.47 (0.44, 0.50) & 0.39 (0.37, 0.42) & 0.34 (0.32, 0.36) & 0.30 (0.28, 0.31) \\
3 & 5 & 0.38 (0.36, 0.41) & 0.32 (0.30, 0.34) & 0.28 (0.26, 0.30) & 0.24 (0.22, 0.26) \\
4 & 6 & 0.32 (0.29, 0.35) & 0.27 (0.24, 0.29) & 0.23 (0.21, 0.25) & 0.20 (0.18, 0.22) \\
5 & 7 & 0.28 (0.26, 0.31) & 0.24 (0.21, 0.26) & 0.20 (0.18, 0.22) & 0.18 (0.16, 0.19) \\
\hline
\end{tabular}
\end{table}
Binary file modified outputs/scenario_nurse_wait.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified outputs/sensitivity_consult_time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading