Recycling trajectory issues when using 'now' function #301
-
Any help/advice much appreciated! I have a simmer model for a patient waiting list, and depending how long a patient has been waiting they are assigned to a different capacity pot. E.g. a patient waiting for treatment at 44 days is added to the 40-80 capacity queue, a patient at 85 days is added to the 80-120 day capacity queue, and so on. This is all done using the now() function to compare the current simulation time with the patient's arrival time. It's to help quantify the impact of not always treating patients strictly from longest wait to shortest. The trajectory the patient follows is dynamically created using some trajectory blocks stored as strings, and it takes 50-60 seconds to parse together. I would like to only do this once, and then recycle that trajectory across however many iterations I simulate. But re-using the trajectory poses difficulties when trying to functionalise the process, I presume because of the different environments in play. See code examples below, which are a simplified version just printing the current simulation time in different ways:
In the last example, even when the function environment contains an object called 'sim3', the trajectory doesn't think it exists when it tries to run now(sim3). The middle example works fine, but in my actual model this would mean I'd have to recreate the trajectory inside every function call, at a cost of 50-60 seconds a time. Any suggestions on how I could share the trajectory across these environments, or if there's a better way to achieve this would be gratefully received :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Yes, functions in trajectories are tied to the environment where the trajectory was defined, and there's also some cleaning involved to avoid reference loops that could make your memory grow indefinitely. A simple solution to your third example would be something like this: sim3 <- NULL # <---
t3 <- trajectory() %>%
timeout(1) %>%
log_(function(){as.character(simmer::now(sim3))})
runASim2 <- function(traj){
sim3 <<- simmer() # <---
sim3 %>%
add_generator("test",traj,at(0,1,2,3,4)) %>%
run()
}
runASim2(t3) Note that the first line is not strictly needed, but it helps if you define the trajectory in another place different from the global environment. The best solution also depends on whether you want different simulations, or multiple replications of the same simulation (in this case, I would just define a single simulation and then call BTW, I'm curious why you have the trajectories as strings. What's the application? |
Beta Was this translation helpful? Give feedback.
Yes, functions in trajectories are tied to the environment where the trajectory was defined, and there's also some cleaning involved to avoid reference loops that could make your memory grow indefinitely.
A simple solution to your third example would be something like this:
Note that the first line is not strictly needed, but it helps if you define the trajectory in another place different from the global environment. The best solutio…