Skip to content

Instantly share code, notes, and snippets.

@bart6114
Created June 15, 2016 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bart6114/b51b5de0a15e4d701173874f1f510b1d to your computer and use it in GitHub Desktop.
Save bart6114/b51b5de0a15e4d701173874f1f510b1d to your computer and use it in GitHub Desktop.
library(R6)
library(simmer)
BatchGate <- R6Class("BatchGate",
public = list(
queue_length = 0,
opens_at_length = NA,
open = FALSE,
allowed_to_pass = 0,
initialize = function(opens_at_length){
self$opens_at_length = opens_at_length
},
join_queue = function(){
self$queue_length <- self$queue_length + 1
},
is_open = function(){
if(self$queue_length >= self$opens_at_length && self$open == FALSE){
self$open <- TRUE
self$allowed_to_pass <- self$opens_at_length
}
return(self$open)
},
pass_gate = function(){
self$queue_length <- self$queue_length - 1
self$allowed_to_pass <- self$allowed_to_pass - 1
if(self$allowed_to_pass == 0) self$open <- FALSE
}
))
my_gate <- BatchGate$new(3)
env<-simmer()
t0 <- create_trajectory() %>%
timeout(function() { # dummy to join queue
my_gate$join_queue()
0
}) %>%
timeout(.001) %>% # dummy to simulate wait in queue, dislike that we need to enter a number heer
rollback(1, check=function(){
if(my_gate$is_open()){
# pass the gate
my_gate$pass_gate()
return(FALSE) # i.e. don't roll back
} else return(TRUE)
}) %>%
timeout(function(){
print("Passed the gate! Let's roll!")
0
})
env <- env %>%
add_generator("ships", t0, at(0, 5, 10, 10, 15, 20)) %>%
run()
get_mon_arrivals(env)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment