-
|
Greetings! Long time enthusiast, but first time user of simmer here humbly seeking a bit of learned guidance. I'm digging into the Bank Tutorial Part 1 (https://r-simmer.org/articles/simmer-04-bank-1.html). I've ran through the entire thing and I can get it to work. Now I'm trying to test my (mis)understanding of the tutorial by trying to combine different aspects of it (i.e., getting attributions, using multiple resources and multiple runs) in the same simulation. Below is the code I'm trying to use that gives me the error noted in the title: library("parallel")
library("simmer")
customer<-
trajectory("Customer's path") %>%
set_attribute("start_time", function(){now(bank)}) %>%
log_("Here I am") %>%
select(c("counter1", "counter2", "counter3"), policy="shortest-queue") %>%
seize_selected() %>%
log_(function(){paste("Waited: ", now(bank) - get_attribute(bank,"start_time"))}) %>%
timeout(function(){rexp(1,1/12)}) %>%
release_selected() %>%
log_(function(){paste("Finished: ", now(bank))})
mclapply(c(393943, 100005, 777999555, 319999772), function(the_seed){ # Note that everything below is
set.seed(the_seed) # is encapsulated by the mclapply({})
bank<-
simmer("bank") %>%
add_resource("counter1",1) %>%
add_resource("counter2",1) %>%
add_resource("counter3",1) %>%
add_generator("Customer",customer,function(){c(0,rexp(100,1/10),-1)})
bank %>% run(until=1000)
results<-
bank %>%
get_mon_arrivals() %>%
transform(waiting_time=end_time-start_time-activity_time) %>%
.[order(.$start_time),]
paste("Average wait for ", sum(results$finished), " completions was ",
mean(results$waiting_time), "minutes.")
}) %>% unlist() # Here is the endHowever, as soon as I comment out the code associated with the mclapply and multiple runs, the code works: library("parallel")
library("simmer")
customer<-
trajectory("Customer's path") %>%
set_attribute("start_time", function(){now(bank)}) %>%
log_("Here I am") %>%
select(c("counter1", "counter2", "counter3"), policy="shortest-queue") %>%
seize_selected() %>%
log_(function(){paste("Waited: ", now(bank) - get_attribute(bank,"start_time"))}) %>%
timeout(function(){rexp(1,1/12)}) %>%
release_selected() %>%
log_(function(){paste("Finished: ", now(bank))})
#mclapply(c(393943, 100005, 777999555, 319999772), function(the_seed){ # Note that everything below is
#set.seed(the_seed) # is encapsulated by the mclapply({})
bank<-
simmer("bank") %>%
add_resource("counter1",1) %>%
add_resource("counter2",1) %>%
add_resource("counter3",1) %>%
add_generator("Customer",customer,function(){c(0,rexp(100,1/10),-1)})
bank %>% run(until=1000)
results<-
bank %>%
get_mon_arrivals() %>%
transform(waiting_time=end_time-start_time-activity_time) %>%
.[order(.$start_time),]
paste("Average wait for ", sum(results$finished), " completions was ",
mean(results$waiting_time), "minutes.")
#}) %>% unlist() # Here is the end
simmer environment: bank | now: 1000 | next: 1006.0309948087
{ Monitor: in memory }
{ Resource: counter1 | monitored: TRUE | server status: 1(1) | queue status: 0(Inf) }
{ Resource: counter2 | monitored: TRUE | server status: 1(1) | queue status: 0(Inf) }
{ Resource: counter3 | monitored: TRUE | server status: 0(1) | queue status: 0(Inf) }
{ Source: Customer | monitored: 1 | n_generated: 101 }
>
> results<-
+ bank %>%
+ get_mon_arrivals() %>%
+ transform(waiting_time=end_time-start_time-activity_time) %>%
+ .[order(.$start_time),]
>
> paste("Average wait for ", sum(results$finished), " completions was ",
+ mean(results$waiting_time), "minutes.")
[1] "Average wait for 89 completions was 0.53324398904408 minutes."So I guess my immediate question is what am I doing wrong with the mclapply? What is the correct way to use multiple resources in multiple parallel runs in the same simulation? Thank you in advance. Cheers! P |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Parallelization becomes tricky when something in your trajectories references the simulation environment ( |
Beta Was this translation helpful? Give feedback.
Parallelization becomes tricky when something in your trajectories references the simulation environment (
now(bank)in your case). This is mainly because, internally, both trajectories and the simmer environment are pointers to internal objects. My recommendation is to define a function (e.g.,simulate) encapsulating everything, and then pass that function tomclapply. You can find an example of this workflow in this vignette.