r-simmer / simmer

Discrete-Event Simulation for R
https://r-simmer.org
GNU General Public License v2.0
222 stars 42 forks source link

set_source does not deactivate the old source #322

Closed jr1234567 closed 1 month ago

jr1234567 commented 1 month ago

I am learning and experimenting with Simmer. In the following code, an entity in traj2 will deactivate at t= 75 a generator for traj1 (Gen1). This deactivation can be made using deactivate("Gen1") or set_source("Gen1", function() -1). I run the simulation two times until t = 300. With deactivate("Gen1") it works fine : Gen1 stops producing entities at t = 75 and both runs show similar behaviours. There are two issues when using set_source("Gen1", function() -1) :

Are these issues bugs or am I missing something ? Thanks

rm(list=ls())
library(simmer)

sim <- simmer("Simulation")

traj1 <- trajectory("Traj1") %>%
  set_attribute("TIME", now(sim)) 

traj2 <- trajectory("Traj2")%>%
  # set_source("Gen1", function() -1)
  deactivate("Gen1")

################################################################################

sim %>%
  add_generator("Gen2", traj1, from(0, function() rexp(1, 0.2)), mon=2) %>%
  add_generator("Gen1", traj1, from(0, function() rexp(1, 2.1)), mon=2) %>%
  add_generator("STOPPER", traj2, at(75), mon=2)

sim %>%  run(300) 

arrivals  <- get_mon_arrivals(sim) 
mon  <- get_mon_attributes(sim) 
time  <- mon[mon$key == "TIME", ]
time <- time[order(time$value),]
time$N <- 1
time$cumul <- cumsum(time$N)

plot(time$time, time$cumul, lwd=4, type="l", col=rgb(0.9, 0.3, 0.9, .8), ylim=c(0, 300), xlim=c(0, 300)) 

#################################################################
#### second run
#################################################################
reset(sim)

sim %>%  run(300) 

arrivals  <- get_mon_arrivals(sim) 
mon  <- get_mon_attributes(sim) 
time  <- mon[mon$key == "TIME", ]
time <- time[order(time$value),]
time$N <- 1
time$cumul <- cumsum(time$N)

lines(time$time, time$cumul, lwd=3, col=rgb(0.2, 0.3, 0.9, .9))
Enchufa2 commented 1 month ago

Thanks for the report. Please do not open several issues/discussions for the same thing in the future. I've closed the two other discussions in favour of this issue.

Now, here's a simpler reproducible example:

library(simmer)

set.seed(123)
sim <- simmer(" Simulation")

traj1 <- trajectory("traj1") %>%
  timeout(function() 100)

traj2 <- trajectory("traj2") %>%
  deactivate("Gen")
# set_source("Gen", function() -10)

env <- sim %>%
  add_generator("Gen", traj1, from(0, function() 1)) %>%
  add_generator("Stopper", traj2, at(1.5), mon=2)  %>%
  run(600) 

arrivals <- get_mon_arrivals(env) 
arrivals[order(arrivals$start_time), ]
#>       name start_time end_time activity_time finished replication
#> 2     Gen0        0.0    100.0           100     TRUE           1
#> 3     Gen1        1.0    101.0           100     TRUE           1
#> 1 Stopper0        1.5      1.5             0     TRUE           1

vs.

library(simmer)

set.seed(123)
sim <- simmer(" Simulation")

traj1 <- trajectory("traj1") %>%
  timeout(function() 100)

traj2 <- trajectory("traj2") %>%
# deactivate("Gen")
  set_source("Gen", function() -10)

env <- sim %>%
  add_generator("Gen", traj1, from(0, function() 1)) %>%
  add_generator("Stopper", traj2, at(1.5), mon=2)  %>%
  run(600) 

arrivals <- get_mon_arrivals(env) 
arrivals[order(arrivals$start_time), ]
#>       name start_time end_time activity_time finished replication
#> 2     Gen0        0.0    100.0           100     TRUE           1
#> 3     Gen1        1.0    101.0           100     TRUE           1
#> 1 Stopper0        1.5      1.5             0     TRUE           1
#> 4     Gen2        2.0    102.0           100     TRUE           1

The main difference is that set_source does not deactivate the source before changing it, and therefore the pending arrivals are still processed. Maybe this is counterintuitive, so I could look into deactivating it first.

jr1234567 commented 1 month ago

Thank you for looking into this and your work on this great package

What about the second issue that when the generator is stopped using set_source(), it remains permanently stopped in next runs despite the simulation being resetted, whereas when stopped with deactivate() it is re-started at each run of the simulation ?

Enchufa2 commented 1 month ago

Please open a separate issue for this second problem. Thanks. See link below.