r-simmer / simmer

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

synchronize() not work as expected #275

Closed vohai611 closed 2 years ago

vohai611 commented 2 years ago
library(simmer)
end = trajectory() %>% 
  synchronize(wait = FALSE) %>% 
  log_('leave')

p = trajectory() %>% 
  clone(n = 2,
        trajectory() %>% renege_in(t = 1, out = trajectory() %>% 
                                     join(end) ) %>% timeout(5) ,
        trajectory() %>% timeout(3) %>%
          join(end)) 

simmer() %>% 
  add_generator("p", p, distribution = at(c(1))) %>% 
  run(until = 10)
#> 2: p0: leave
#> 4: p0: leave
#> simmer environment: anonymous | now: 4 | next: 
#> { Monitor: in memory }
#> { Source: p | monitored: 1 | n_generated: 1 }

Created on 2022-03-04 by the reprex package (v2.0.1)

I expect that with wait=FALSE, only the first arrival can reach log_("leave"). Anyway, thank you guys for an amazing pacakges!

Enchufa2 commented 2 years ago

Well, it is kind of expected. This trajectory is equivalent to the one above:

p = trajectory() %>% 
  clone(
    n = 2,
    trajectory() %>%
      renege_in(
        t = 1, out = trajectory() %>%
          synchronize(wait = FALSE) %>% 
          log_('leave')) %>%
      timeout(5),
    trajectory() %>%
      timeout(3) %>%
      synchronize(wait = FALSE) %>%
      log_('leave')
  ) 

Note what I did: I just replaced the joins with the part they are copying. Removing the renege_in, for better clarity:

p = trajectory() %>% 
  clone(
    n = 2,
    trajectory() %>%
      timeout(1) %>%
      synchronize(wait = FALSE) %>% 
      log_('leave'),
    trajectory() %>%
      timeout(3) %>%
      synchronize(wait = FALSE) %>%
      log_('leave')
  ) 

In essence, there are two distinct synchronize activities, and they are not aware of each other, so arrivals cannot be synchronized.

vohai611 commented 2 years ago

Oh I see. Thank you!

Enchufa2 commented 2 years ago

Let me explore the possibility of setting a global storage for sync activities. Otherwise, I see no way of reneging and synchronizing.