diff --git a/DESCRIPTION b/DESCRIPTION index ca6dfe4..1f35eda 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -24,7 +24,9 @@ Imports: rlang, tidyr, R6, - tidyselect + tidyselect, + future, + future.apply Suggests: testthat (>= 3.0.0), lintr, diff --git a/NAMESPACE b/NAMESPACE index 3e60f3e..9065ed9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/defaults.R b/R/defaults.R index e0cf1f9..1a217e9 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -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 diff --git a/R/model.R b/R/model.R index 40854c5..79a17ba 100644 --- a/R/model.R +++ b/R/model.R @@ -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 @@ -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 @@ -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") %>% diff --git a/R/trial.R b/R/trial.R index 9bd86c7..9775a45 100644 --- a/R/trial.R +++ b/R/trial.R @@ -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 @@ -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( diff --git a/man/model.Rd b/man/model.Rd index 2329901..387ef06 100644 --- a/man/model.Rd +++ b/man/model.Rd @@ -4,12 +4,16 @@ \alias{model} \title{Run simulation} \usage{ -model(run_number, param_class) +model(run_number, param_class, set_seed = TRUE) } \arguments{ \item{run_number}{Integer representing index of current simulation run.} \item{param_class}{Instance of Defaults containing model parameters.} + +\item{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.} } \value{ Named list with two tables: monitored arrivals and resources diff --git a/man/trial.Rd b/man/trial.Rd index a6681de..a7538a2 100644 --- a/man/trial.Rd +++ b/man/trial.Rd @@ -13,7 +13,5 @@ trial(param_class) Named list with two tables: monitored arrivals and resources. } \description{ -Note: The parallel processing is implemented using `parLapply` because -`mcLapply`` does not work on Windows and `future_lapply`` would often get -stuck. +Run simulation for multiple replications, sequentially or in parallel. } diff --git a/outputs/base_trial.csv b/outputs/base_trial.csv index 49145a7..9609f35 100644 --- a/outputs/base_trial.csv +++ b/outputs/base_trial.csv @@ -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 diff --git a/outputs/choose_param_conf_int_1.png b/outputs/choose_param_conf_int_1.png index 0cd9a97..d38ac25 100644 Binary files a/outputs/choose_param_conf_int_1.png and b/outputs/choose_param_conf_int_1.png differ diff --git a/outputs/choose_param_conf_int_2.png b/outputs/choose_param_conf_int_2.png index 596d5d7..393487e 100644 Binary files a/outputs/choose_param_conf_int_2.png and b/outputs/choose_param_conf_int_2.png differ diff --git a/outputs/choose_param_conf_int_3.png b/outputs/choose_param_conf_int_3.png index 1dd54c2..29b72b6 100644 Binary files a/outputs/choose_param_conf_int_3.png and b/outputs/choose_param_conf_int_3.png differ diff --git a/outputs/cores1.png b/outputs/cores1.png index 9d9918c..ff130e7 100644 Binary files a/outputs/cores1.png and b/outputs/cores1.png differ diff --git a/outputs/cores2.png b/outputs/cores2.png index 6291fdc..e60ffa5 100644 Binary files a/outputs/cores2.png and b/outputs/cores2.png differ diff --git a/outputs/scenario_nurse_util.png b/outputs/scenario_nurse_util.png index 8b380d7..275bfb3 100644 Binary files a/outputs/scenario_nurse_util.png and b/outputs/scenario_nurse_util.png differ diff --git a/outputs/scenario_nurse_util.tex b/outputs/scenario_nurse_util.tex index c241640..b04d2d0 100644 --- a/outputs/scenario_nurse_util.tex +++ b/outputs/scenario_nurse_util.tex @@ -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} diff --git a/outputs/scenario_nurse_wait.png b/outputs/scenario_nurse_wait.png index 0baa23b..716a85c 100644 Binary files a/outputs/scenario_nurse_wait.png and b/outputs/scenario_nurse_wait.png differ diff --git a/outputs/sensitivity_consult_time.png b/outputs/sensitivity_consult_time.png index 0256329..59afbce 100644 Binary files a/outputs/sensitivity_consult_time.png and b/outputs/sensitivity_consult_time.png differ diff --git a/outputs/sensitivity_consult_time.tex b/outputs/sensitivity_consult_time.tex index 17f072f..0718fc7 100644 --- a/outputs/sensitivity_consult_time.tex +++ b/outputs/sensitivity_consult_time.tex @@ -4,14 +4,14 @@ \hline & Mean nurse consultation time & Mean wait time for nurse (95 percent confidence interval) \\ \hline -1 & 8 & 0.09 (0.01, 0.17) \\ - 2 & 9 & 0.20 (0.04, 0.37) \\ - 3 & 10 & 0.14 (0.08, 0.20) \\ - 4 & 11 & 0.35 (0.17, 0.52) \\ - 5 & 12 & 0.46 (0.26, 0.66) \\ - 6 & 13 & 0.64 (0.37, 0.90) \\ - 7 & 14 & 0.72 (0.41, 1.02) \\ - 8 & 15 & 1.06 (0.67, 1.45) \\ +1 & 8 & 0.09 (0.05, 0.13) \\ + 2 & 9 & 0.16 (0.09, 0.22) \\ + 3 & 10 & 0.22 (0.14, 0.30) \\ + 4 & 11 & 0.32 (0.19, 0.45) \\ + 5 & 12 & 0.55 (0.36, 0.75) \\ + 6 & 13 & 0.64 (0.45, 0.84) \\ + 7 & 14 & 0.84 (0.57, 1.11) \\ + 8 & 15 & 1.02 (0.73, 1.31) \\ \hline \end{tabular} \end{table} diff --git a/outputs/spread_arrivals.png b/outputs/spread_arrivals.png index 7f6b18a..f2ea1bb 100644 Binary files a/outputs/spread_arrivals.png and b/outputs/spread_arrivals.png differ diff --git a/outputs/spread_nurse_time.png b/outputs/spread_nurse_time.png index 854fc5a..0df819b 100644 Binary files a/outputs/spread_nurse_time.png and b/outputs/spread_nurse_time.png differ diff --git a/outputs/spread_nurse_util.png b/outputs/spread_nurse_util.png index a5d8f88..1a2a6ce 100644 Binary files a/outputs/spread_nurse_util.png and b/outputs/spread_nurse_util.png differ diff --git a/outputs/spread_nurse_wait.png b/outputs/spread_nurse_wait.png index 60a106c..3953ee2 100644 Binary files a/outputs/spread_nurse_wait.png and b/outputs/spread_nurse_wait.png differ diff --git a/renv.lock b/renv.lock index 98fcf91..826f1b3 100644 --- a/renv.lock +++ b/renv.lock @@ -589,6 +589,35 @@ ], "Hash": "7f48af39fa27711ea5fbd183b399920d" }, + "future": { + "Package": "future", + "Version": "1.34.0", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "digest", + "globals", + "listenv", + "parallel", + "parallelly", + "utils" + ], + "Hash": "475771e3edb711591476be387c9a8c2e" + }, + "future.apply": { + "Package": "future.apply", + "Version": "1.11.3", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R", + "future", + "globals", + "parallel", + "utils" + ], + "Hash": "0efead26b03bb60716ed29971b1953dc" + }, "generics": { "Package": "generics", "Version": "0.1.3", @@ -668,6 +697,17 @@ ], "Hash": "ab08ac61f3e1be454ae21911eb8bc2fe" }, + "globals": { + "Package": "globals", + "Version": "0.16.3", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R", + "codetools" + ], + "Hash": "2580567908cafd4f187c1e5a91e98b7f" + }, "glue": { "Package": "glue", "Version": "1.8.0", @@ -945,6 +985,16 @@ ], "Hash": "08cff46381a242d44c0d8dd0aabd9f71" }, + "listenv": { + "Package": "listenv", + "Version": "0.9.1", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R" + ], + "Hash": "e2fca3e12e4db979dccc6e519b10a7ee" + }, "magrittr": { "Package": "magrittr", "Version": "2.0.3", @@ -1052,6 +1102,18 @@ ], "Hash": "37a7f0abce0349f5950ce49f38c7626b" }, + "parallelly": { + "Package": "parallelly", + "Version": "1.41.0", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "parallel", + "tools", + "utils" + ], + "Hash": "f5ae11e8e15ee84950752e7b01c55295" + }, "pillar": { "Package": "pillar", "Version": "1.10.1", diff --git a/rmarkdown/analysis.md b/rmarkdown/analysis.md index 9fe09e6..bd6418c 100644 --- a/rmarkdown/analysis.md +++ b/rmarkdown/analysis.md @@ -44,12 +44,12 @@ devtools::install() ## ─ preparing ‘simulation’: ## checking DESCRIPTION meta-information ... ✔ checking DESCRIPTION meta-information ## ─ checking for LF line-endings in source and make files and shell scripts - ## ─ checking for empty or unneeded directories - ## Omitted ‘LazyData’ from DESCRIPTION + ## ─ checking for empty or unneeded directories + ## Omitted ‘LazyData’ from DESCRIPTION ## ─ building ‘simulation_0.1.0.tar.gz’ ## ## Running /opt/R/4.4.1/lib/R/bin/R CMD INSTALL \ - ## /tmp/RtmpJJ60JH/simulation_0.1.0.tar.gz --install-tests + ## /tmp/Rtmpbqk15J/simulation_0.1.0.tar.gz --install-tests ## * installing to library ‘/home/amy/.cache/R/renv/library/rap_template_r_des-cd7d6844/linux-ubuntu-noble/R-4.4/x86_64-pc-linux-gnu’ ## * installing *source* package ‘simulation’ ... ## ** using staged installation @@ -110,12 +110,12 @@ head(trial_results) ## # A tibble: 6 × 5 ## replication arrivals mean_waiting_time_nurse mean_activity_time_nurse ## - ## 1 1 15 0 8.92 - ## 2 2 16 0 7.73 - ## 3 3 14 0 7.56 - ## 4 4 19 1.54 8.06 - ## 5 5 21 0.0520 9.35 - ## 6 6 29 0.384 8.21 + ## 1 1 21 0.173 10.7 + ## 2 2 16 0 7.10 + ## 3 3 13 0 6.89 + ## 4 4 16 0.0177 9.29 + ## 5 5 17 0 4.79 + ## 6 6 18 0.393 8.12 ## # ℹ 1 more variable: utilisation_nurse ``` r @@ -284,12 +284,12 @@ head(scenario_results) ## # A tibble: 6 × 8 ## replication arrivals mean_waiting_time_nurse mean_activity_time_nurse ## - ## 1 1 21 0 8.49 - ## 2 2 22 0.0306 7.74 - ## 3 3 19 0 8.11 - ## 4 4 27 0.831 7.26 - ## 5 5 22 0.248 8.50 - ## 6 6 33 4.26 9.72 + ## 1 1 26 0.287 9.10 + ## 2 2 19 0 7.83 + ## 3 3 22 0.0192 6.74 + ## 4 4 21 0.249 7.97 + ## 5 5 25 0.246 5.75 + ## 6 6 27 3.22 10.0 ## # ℹ 4 more variables: utilisation_nurse , scenario , ## # patient_inter , number_of_nurses @@ -422,18 +422,18 @@ print(table_latex) ``` ## % latex table generated in R 4.4.1 by xtable 1.8-4 package - ## % Mon Jan 27 10:18:09 2025 + ## % Mon Jan 27 16:03:02 2025 ## \begin{table}[ht] ## \centering ## \begin{tabular}{rrllll} ## \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} @@ -484,12 +484,12 @@ head(sensitivity_consult) ## # A tibble: 6 × 7 ## replication arrivals mean_waiting_time_nurse mean_activity_time_nurse ## - ## 1 1 16 0 6.85 - ## 2 2 17 0 6.16 - ## 3 3 14 0 6.04 - ## 4 4 20 0.0276 6.91 - ## 5 5 21 0 7.48 - ## 6 6 31 2.09 7.16 + ## 1 1 22 0 8.42 + ## 2 2 16 0 5.68 + ## 3 3 14 0 5.49 + ## 4 4 16 0 7.43 + ## 5 5 18 0 3.86 + ## 6 6 21 0.172 8.00 ## # ℹ 3 more variables: utilisation_nurse , scenario , ## # mean_n_consult_time @@ -531,21 +531,21 @@ print(sensitivity_table_latex) ``` ## % latex table generated in R 4.4.1 by xtable 1.8-4 package - ## % Mon Jan 27 10:18:14 2025 + ## % Mon Jan 27 16:03:07 2025 ## \begin{table}[ht] ## \centering ## \begin{tabular}{rrl} ## \hline ## & Mean nurse consultation time & Mean wait time for nurse (95 percent confidence interval) \\ ## \hline - ## 1 & 8 & 0.09 (0.01, 0.17) \\ - ## 2 & 9 & 0.20 (0.04, 0.37) \\ - ## 3 & 10 & 0.14 (0.08, 0.20) \\ - ## 4 & 11 & 0.35 (0.17, 0.52) \\ - ## 5 & 12 & 0.46 (0.26, 0.66) \\ - ## 6 & 13 & 0.64 (0.37, 0.90) \\ - ## 7 & 14 & 0.72 (0.41, 1.02) \\ - ## 8 & 15 & 1.06 (0.67, 1.45) \\ + ## 1 & 8 & 0.09 (0.05, 0.13) \\ + ## 2 & 9 & 0.16 (0.09, 0.22) \\ + ## 3 & 10 & 0.22 (0.14, 0.30) \\ + ## 4 & 11 & 0.32 (0.19, 0.45) \\ + ## 5 & 12 & 0.55 (0.36, 0.75) \\ + ## 6 & 13 & 0.64 (0.45, 0.84) \\ + ## 7 & 14 & 0.84 (0.57, 1.11) \\ + ## 8 & 15 & 1.02 (0.73, 1.31) \\ ## \hline ## \end{tabular} ## \end{table} @@ -573,12 +573,12 @@ tail(result[["arrivals"]]) ``` ## name start_time end_time activity_time resource replication - ## 160 patient128 62.63725 NA NA nurse 0 - ## 161 patient149 74.50965 NA NA nurse 0 - ## 162 patient151 75.76834 NA NA nurse 0 - ## 163 patient114 55.80323 NA NA nurse 0 - ## 164 patient153 76.74319 NA NA nurse 0 - ## 165 patient155 77.41953 NA NA nurse 0 + ## 160 patient97 47.84534 NA NA nurse 0 + ## 161 patient155 77.41953 NA NA nurse 0 + ## 162 patient66 35.72669 NA NA nurse 0 + ## 163 patient52 27.21852 NA NA nurse 0 + ## 164 patient156 77.59569 NA NA nurse 0 + ## 165 patient160 78.41585 NA NA nurse 0 ## Example run with logs diff --git a/rmarkdown/choosing_parameters.Rmd b/rmarkdown/choosing_parameters.Rmd index b0433b7..68dee33 100644 --- a/rmarkdown/choosing_parameters.Rmd +++ b/rmarkdown/choosing_parameters.Rmd @@ -69,11 +69,11 @@ When selecting the number of replications you should repeat the analysis for all #' @param desired_precision Desired mean deviation from confidence interval. #' @param metric Name of performance metric to assess. #' @param yaxis_title Label for y axis. -#' @param file Filename to save figure to. +#' @param path Path inc. filename to save figure to. #' @param min_rep A suggested minimum number of replications (default=NULL). confidence_interval_method <- function(replications, desired_precision, metric, - yaxis_title, file, min_rep = NULL) { + yaxis_title, path, min_rep = NULL) { # Run model for specified number of replications param_class <- defaults() param_class[["update"]](list(number_of_runs = replications)) @@ -164,63 +164,73 @@ confidence_interval_method <- function(replications, desired_precision, metric, theme_minimal() # Save the plot - full_path <- file.path(output_dir, file) - ggsave(filename = full_path, width = 6.5, height = 4L, bg = "white") + ggsave(filename = path, width = 6.5, height = 4L, bg = "white") - # View the plot - include_graphics(full_path) + return(cumulative) } ``` +It's important to check ahead, to check that the 5% precision is maintained - which is fine in this case - it doesn't go back up to future deviation. + ```{r} -confidence_interval_method( - replications = 70L, +path <- file.path(output_dir, "choose_param_conf_int_1.png") + +# Run calculations and produce plot +ci_df <- confidence_interval_method( + replications = 150L, desired_precision = 0.05, metric = "mean_activity_time_nurse", yaxis_title = "Mean time with nurse", - file = "choose_param_conf_int_1.png", - min_rep = 66L + path = path, + min_rep = 98L ) -``` -It's important to check ahead, to check that the 5% precision is maintained. +# View first ten rows were percentage deviation is below 5 +ci_df %>% + filter(perc_deviation < 5L) %>% + head(10L) -```{r} -confidence_interval_method( - replications = 100L, - desired_precision = 0.05, - metric = "mean_activity_time_nurse", - yaxis_title = "Mean time with nurse", - file = "choose_param_conf_int_2.png", - min_rep = 66L -) +# View plot +include_graphics(path) ``` -Also, to check across multiple metrics. +It is also important to check across multiple metrics. ```{r} -confidence_interval_method( +path <- file.path(output_dir, "choose_param_conf_int_3.png") + +# Run calculations and produce plot +ci_df <- confidence_interval_method( replications = 200L, desired_precision = 0.05, metric = "utilisation_nurse", yaxis_title = "Mean nurse utilisation", - file = "choose_param_conf_int_3.png", - min_rep = 136L + path = path, + min_rep = 148L ) + +# View first ten rows were percentage deviation is below 5 +ci_df %>% + filter(perc_deviation < 5L) %>% + head(10L) + +# View plot +include_graphics(path) ``` ## Run time with varying number of CPU cores ```{r} -#' Run model with 1 to 8 CPU cores and examine run times +#' Run model with varying number of CPU cores and examine run times #' +#' @param n_cores Number of cores to test up to #' @param file Filename to save figure to. #' @param model_param List of parameters for the model. -run_cores <- function(file, model_param = NULL) { +run_cores <- function(n_cores, file, model_param = NULL) { # Run model with 1 to 8 cores speed <- list() - for (i in 1L:8L){ + for (i in 1L:n_cores){ print(paste("Running with cores:", i)) cores_start <- Sys.time() @@ -233,8 +243,9 @@ run_cores <- function(file, model_param = NULL) { param_class[["update"]](list(cores = i)) invisible(trial(param_class)) - # Record time taken - cores_time <- as.numeric(Sys.time() - cores_start, units = "secs") + # Record time taken, rounded to nearest .5 dp by running round(x*2)/2 + cores_time <- round( + as.numeric(Sys.time() - cores_start, units = "secs")*2)/2 speed[[i]] <- list(cores = i, run_time = round(cores_time, 3L)) } @@ -244,7 +255,7 @@ run_cores <- function(file, model_param = NULL) { # Generate plot p <- ggplot(speed_df, aes(x = .data[["cores"]], y = .data[["run_time"]])) + geom_line() + - labs(x = "Cores", y = "Run time (seconds)") + + labs(x = "Cores", y = "Run time (rounded to nearest .5 seconds)") + theme_minimal() # Save plot @@ -260,17 +271,17 @@ run_cores <- function(file, model_param = NULL) { Setting up and managing a parallel cluster takes extra time. For small tasks or few iterations, this extra time can be more than the time saved by running in parallel. ```{r} -run_cores("cores1.png") +run_cores(5L, "cores1.png") ``` -Having increased the simulation length, we now see that parallelisation is increasing the model run time. +Having increased the simulation length, we now see that parallelisation is decreasing the model run time. However, when you use more cores, the data needs to be divided and sent to more workers. For small tasks, this extra work is small, but as the number of workers increases, the time spent managing and communicating with them can grow too much. At some point, this overhead becomes larger than the time saved by using more cores. The optimal number of cores will vary depending on your model parameters and machine. ```{r} -run_cores("cores2.png", list(data_collection_period = 10000L)) +run_cores(5L, "cores2.png", list(data_collection_period = 100000L)) ``` ## Run time diff --git a/rmarkdown/choosing_parameters.md b/rmarkdown/choosing_parameters.md index 3cd6c8c..eb17b4e 100644 --- a/rmarkdown/choosing_parameters.md +++ b/rmarkdown/choosing_parameters.md @@ -40,12 +40,12 @@ devtools::install() ## ─ preparing ‘simulation’: ## checking DESCRIPTION meta-information ... ✔ checking DESCRIPTION meta-information ## ─ checking for LF line-endings in source and make files and shell scripts - ## ─ checking for empty or unneeded directories - ## Omitted ‘LazyData’ from DESCRIPTION + ## ─ checking for empty or unneeded directories + ## Omitted ‘LazyData’ from DESCRIPTION ## ─ building ‘simulation_0.1.0.tar.gz’ ## ## Running /opt/R/4.4.1/lib/R/bin/R CMD INSTALL \ - ## /tmp/RtmpeOwgfX/simulation_0.1.0.tar.gz --install-tests + ## /tmp/RtmpaECLM8/simulation_0.1.0.tar.gz --install-tests ## * installing to library ‘/home/amy/.cache/R/renv/library/rap_template_r_des-cd7d6844/linux-ubuntu-noble/R-4.4/x86_64-pc-linux-gnu’ ## * installing *source* package ‘simulation’ ... ## ** using staged installation @@ -134,11 +134,11 @@ of replications. #' @param desired_precision Desired mean deviation from confidence interval. #' @param metric Name of performance metric to assess. #' @param yaxis_title Label for y axis. -#' @param file Filename to save figure to. +#' @param path Path inc. filename to save figure to. #' @param min_rep A suggested minimum number of replications (default=NULL). confidence_interval_method <- function(replications, desired_precision, metric, - yaxis_title, file, min_rep = NULL) { + yaxis_title, path, min_rep = NULL) { # Run model for specified number of replications param_class <- defaults() param_class[["update"]](list(number_of_runs = replications)) @@ -229,76 +229,115 @@ confidence_interval_method <- function(replications, desired_precision, metric, theme_minimal() # Save the plot - full_path <- file.path(output_dir, file) - ggsave(filename = full_path, width = 6.5, height = 4L, bg = "white") + ggsave(filename = path, width = 6.5, height = 4L, bg = "white") - # View the plot - include_graphics(full_path) + return(cumulative) } ``` +It’s important to check ahead, to check that the 5% precision is +maintained - which is fine in this case - it doesn’t go back up to +future deviation. + ``` r -confidence_interval_method( - replications = 70L, +path <- file.path(output_dir, "choose_param_conf_int_1.png") + +# Run calculations and produce plot +ci_df <- confidence_interval_method( + replications = 150L, desired_precision = 0.05, metric = "mean_activity_time_nurse", yaxis_title = "Mean time with nurse", - file = "choose_param_conf_int_1.png", - min_rep = 66L + path = path, + min_rep = 98L ) ``` - ## [1] "Reached desired precision (0.05) in 66 replications." + ## [1] "Reached desired precision (0.05) in 98 replications." -![](../outputs/choose_param_conf_int_1.png) +``` r +# View first ten rows were percentage deviation is below 5 +ci_df %>% + filter(perc_deviation < 5L) %>% + head(10L) +``` -It’s important to check ahead, to check that the 5% precision is -maintained. + ## replications cumulative_mean cumulative_std ci_lower ci_upper perc_deviation + ## 1 98 8.461235 2.106669 8.038875 8.883596 4.991712 + ## 2 99 8.475054 2.100398 8.056137 8.893971 4.942943 + ## 3 100 8.468351 2.090838 8.053483 8.883219 4.899036 + ## 4 101 8.473309 2.080954 8.062503 8.884115 4.848241 + ## 5 102 8.478815 2.071373 8.071959 8.885671 4.798504 + ## 6 103 8.485316 2.062250 8.082270 8.888361 4.749915 + ## 7 104 8.490698 2.052949 8.091450 8.889945 4.702173 + ## 8 105 8.477837 2.047301 8.081634 8.874040 4.673399 + ## 9 106 8.456515 2.049320 8.061841 8.851190 4.667105 + ## 10 107 8.459470 2.039859 8.068501 8.850440 4.621677 ``` r -confidence_interval_method( - replications = 100L, - desired_precision = 0.05, - metric = "mean_activity_time_nurse", - yaxis_title = "Mean time with nurse", - file = "choose_param_conf_int_2.png", - min_rep = 66L -) +# View plot +include_graphics(path) ``` - ## [1] "Reached desired precision (0.05) in 66 replications." - -![](../outputs/choose_param_conf_int_2.png) +![](../outputs/choose_param_conf_int_1.png) -Also, to check across multiple metrics. +It is also important to check across multiple metrics. ``` r -confidence_interval_method( +path <- file.path(output_dir, "choose_param_conf_int_3.png") + +# Run calculations and produce plot +ci_df <- confidence_interval_method( replications = 200L, desired_precision = 0.05, metric = "utilisation_nurse", yaxis_title = "Mean nurse utilisation", - file = "choose_param_conf_int_3.png", - min_rep = 136L + path = path, + min_rep = 148L ) ``` - ## [1] "Reached desired precision (0.05) in 136 replications." + ## [1] "Reached desired precision (0.05) in 148 replications." + +``` r +# View first ten rows were percentage deviation is below 5 +ci_df %>% + filter(perc_deviation < 5L) %>% + head(10L) +``` + + ## replications cumulative_mean cumulative_std ci_lower ci_upper perc_deviation + ## 1 148 45.73420 14.04814 43.45214 48.01625 4.989822 + ## 2 149 45.89021 14.12952 43.60278 48.17764 4.984574 + ## 3 150 45.90075 14.08261 43.62865 48.17286 4.950028 + ## 4 151 45.84563 14.05193 43.58612 48.10513 4.928512 + ## 5 152 45.92331 14.03803 43.67359 48.17302 4.898849 + ## 6 153 45.84086 14.02890 43.60008 48.08163 4.888154 + ## 7 154 45.71614 14.06836 43.47650 47.95579 4.899034 + ## 8 155 45.85137 14.12331 43.61035 48.09239 4.887567 + ## 9 156 45.87804 14.08162 43.65092 48.10515 4.854423 + ## 10 157 46.12548 14.37477 43.85937 48.39160 4.912928 + +``` r +# View plot +include_graphics(path) +``` ![](../outputs/choose_param_conf_int_3.png) ## Run time with varying number of CPU cores ``` r -#' Run model with 1 to 8 CPU cores and examine run times +#' Run model with varying number of CPU cores and examine run times #' +#' @param n_cores Number of cores to test up to #' @param file Filename to save figure to. #' @param model_param List of parameters for the model. -run_cores <- function(file, model_param = NULL) { +run_cores <- function(n_cores, file, model_param = NULL) { # Run model with 1 to 8 cores speed <- list() - for (i in 1L:8L){ + for (i in 1L:n_cores){ print(paste("Running with cores:", i)) cores_start <- Sys.time() @@ -311,8 +350,9 @@ run_cores <- function(file, model_param = NULL) { param_class[["update"]](list(cores = i)) invisible(trial(param_class)) - # Record time taken - cores_time <- as.numeric(Sys.time() - cores_start, units = "secs") + # Record time taken, rounded to nearest .5 dp by running round(x*2)/2 + cores_time <- round( + as.numeric(Sys.time() - cores_start, units = "secs")*2)/2 speed[[i]] <- list(cores = i, run_time = round(cores_time, 3L)) } @@ -322,7 +362,7 @@ run_cores <- function(file, model_param = NULL) { # Generate plot p <- ggplot(speed_df, aes(x = .data[["cores"]], y = .data[["run_time"]])) + geom_line() + - labs(x = "Cores", y = "Run time (seconds)") + + labs(x = "Cores", y = "Run time (rounded to nearest .5 seconds)") + theme_minimal() # Save plot @@ -340,7 +380,7 @@ tasks or few iterations, this extra time can be more than the time saved by running in parallel. ``` r -run_cores("cores1.png") +run_cores(5L, "cores1.png") ``` ## [1] "Running with cores: 1" @@ -348,14 +388,11 @@ run_cores("cores1.png") ## [1] "Running with cores: 3" ## [1] "Running with cores: 4" ## [1] "Running with cores: 5" - ## [1] "Running with cores: 6" - ## [1] "Running with cores: 7" - ## [1] "Running with cores: 8" ![](../outputs/cores1.png) Having increased the simulation length, we now see that parallelisation -is increasing the model run time. +is decreasing the model run time. However, when you use more cores, the data needs to be divided and sent to more workers. For small tasks, this extra work is small, but as the @@ -367,7 +404,7 @@ The optimal number of cores will vary depending on your model parameters and machine. ``` r -run_cores("cores2.png", list(data_collection_period = 10000L)) +run_cores(5L, "cores2.png", list(data_collection_period = 100000L)) ``` ## [1] "Running with cores: 1" @@ -375,9 +412,6 @@ run_cores("cores2.png", list(data_collection_period = 10000L)) ## [1] "Running with cores: 3" ## [1] "Running with cores: 4" ## [1] "Running with cores: 5" - ## [1] "Running with cores: 6" - ## [1] "Running with cores: 7" - ## [1] "Running with cores: 8" ![](../outputs/cores2.png) @@ -394,4 +428,4 @@ seconds <- as.integer(runtime %% 60L) print(sprintf("Notebook run time: %dm %ds", minutes, seconds)) ``` - ## [1] "Notebook run time: 2m 41s" + ## [1] "Notebook run time: 1m 19s" diff --git a/rmarkdown/generate_exp_results.md b/rmarkdown/generate_exp_results.md index 088507d..4e56909 100644 --- a/rmarkdown/generate_exp_results.md +++ b/rmarkdown/generate_exp_results.md @@ -38,7 +38,7 @@ devtools::install() ## ─ building ‘simulation_0.1.0.tar.gz’ ## ## Running /opt/R/4.4.1/lib/R/bin/R CMD INSTALL \ - ## /tmp/RtmpfNXwLp/simulation_0.1.0.tar.gz --install-tests + ## /tmp/RtmpVSwa65/simulation_0.1.0.tar.gz --install-tests ## * installing to library ‘/home/amy/.cache/R/renv/library/rap_template_r_des-cd7d6844/linux-ubuntu-noble/R-4.4/x86_64-pc-linux-gnu’ ## * installing *source* package ‘simulation’ ... ## ** using staged installation @@ -101,12 +101,12 @@ head(results) ## # A tibble: 6 × 5 ## replication arrivals mean_waiting_time_nurse mean_activity_time_nurse ## - ## 1 1 15 0 8.92 - ## 2 2 16 0 7.73 - ## 3 3 14 0 7.56 - ## 4 4 19 1.54 8.06 - ## 5 5 21 0.0520 9.35 - ## 6 6 29 0.384 8.21 + ## 1 1 21 0.173 10.7 + ## 2 2 16 0 7.10 + ## 3 3 13 0 6.89 + ## 4 4 16 0.0177 9.29 + ## 5 5 17 0 4.79 + ## 6 6 18 0.393 8.12 ## # ℹ 1 more variable: utilisation_nurse ``` r diff --git a/tests/testthat/testdata/results.csv b/tests/testthat/testdata/results.csv index 15dba47..b807aa5 100644 --- a/tests/testthat/testdata/results.csv +++ b/tests/testthat/testdata/results.csv @@ -1,11 +1,11 @@ "replication","arrivals","mean_waiting_time_nurse","mean_activity_time_nurse","utilisation_nurse" -1,15,0,8.92266571894842,0.371528668451366 -2,16,0,7.7255587739498,0.399865009965906 -3,14,0,7.55613765895507,0.34983454158788 -4,19,1.53581418282632,8.0588103472346,0.604995664123677 -5,21,0.0519866784761905,9.35135333211744,0.573584434011877 -6,29,0.384083299862069,8.20993074704441,0.611272646882443 -7,16,0,9.54055185249263,0.401321053724437 -8,19,0.497575033078947,8.57860709328252,0.448350482951233 -9,19,0,10.0769710427457,0.627360482435886 -10,15,0,8.01416495697315,0.336149156108189 +1,21,0.172634907604762,10.7405465431143,0.6857207386012 +2,16,0,7.10057623127618,0.339653664237136 +3,13,0,6.88747631698383,0.264798148560583 +4,16,0.01773150233125,9.28954224746827,0.454079770130564 +5,17,0,4.78654797563128,0.266413092811991 +6,18,0.393059339322222,8.11718767381037,0.58554969502398 +7,25,0.111893570592,8.41810983409961,0.55033912691785 +8,16,0,6.66989735996729,0.346967751006821 +9,22,1.94776083503636,9.00372743303383,0.644303386268358 +10,22,0.0487056471818182,8.99915934342635,0.566310989374246