Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

set_source does not deactivate the old source #322

Closed
jr1234567 opened this issue Sep 26, 2024 · 3 comments
Closed

set_source does not deactivate the old source #322

jr1234567 opened this issue Sep 26, 2024 · 3 comments
Labels

Comments

@jr1234567
Copy link

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) :

  • an additional single entity is generated by Gen1 after t = 75, at a time where it should be stopped
  • in the second run Gen1 remains stopped and does not generate entities despite the reset() should have restarted it

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
Copy link
Member

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.

@Enchufa2 Enchufa2 added the bug label Sep 26, 2024
@Enchufa2 Enchufa2 changed the title set_source bugs ? set_source does not deactivate the old source Sep 26, 2024
@jr1234567
Copy link
Author

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
Copy link
Member

Enchufa2 commented Sep 26, 2024

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants