R Simmer立即进行库存补充

时间:2018-08-03 21:13:33

标签: r system simulation inventory

对于Simmer库和R来说,我都是新手,所以我希望有人可以帮助我解决这个问题。

Simmer文档中的示例显示了一个重新库存监视器,该监视器按设定的时间间隔检查库存,但是我希望在客户检索到一定数量的库存后立即进行重新库存。我尝试创建一个不确定的开始时间的补货生成器,并使用set_source("Restock", at(now(env))重新计划在当前模拟时间发生的补货,但这似乎行不通。有什么建议吗?

# Inv. properties
INV_CAP      <- 100
INV_TRIG     <- 30
INV_LEVEL    <- INV_CAP

# Environment rates
SERVE_RATE   <- 1/10
RESTOCK_RATE <- 1/10

env <- simmer()

restock <- trajectory() %>%
  # Restock only beneath INV_TRIG threshold
  branch(function() INV_LEVEL <= INV_TRIG,
    continue = TRUE,
      trajectory() %>%
        log_("Begin restock") %>%
        timeout(function() rexp(1, RESTOCK_RATE)) %>%
        log_(function(){
          INV_LEVEL <<- INV_CAP
          paste("Restock finished")
        }))

serve <- trajectory() %>%
  log_("Begin serving") %>%
  timeout(function() rexp(1, SERVE_RATE)) %>%
  # Decrease inv. level and cust. demand
  set_attribute("demand", function() {
    amount <- min(INV_LEVEL, get_attribute(env, "demand"))
    INV_LEVEL <<- INV_LEVEL - amount
    return(-amount)
  }, mod = "+")

customer <- trajectory() %>%
  set_attribute("demand", function() sample(DEMAND_MIN:DEMAND_MAX, 1)) %>%
  log_("Entering queue") %>%
  seize("Server") %>%
  join(serve) %>%
  release("Server") %>%
  log_("Finished")

env %>%
  add_resource("Server", capacity = SERVER_CAP, queue_size = QUEUE_CAP) %>%
  add_generator("Customer", customer, function() rexp(1, CUST_RATE), mon = 2) %>%
  run(until = 500)

1 个答案:

答案 0 :(得分:0)

要设置“重新库存”源,首先需要定义一个无效的“重新库存”生成器。考虑到它是不活动的,您需要在设置新源之后将其激活。我给你举个小例子:

library(simmer)

env <- simmer()

restock <- trajectory() %>%
  log_("restock")

serve <- trajectory() %>%
  log_("serve") %>%
  set_source("Restock", at(0)) %>%
  activate("Restock")

env %>%
  add_generator("Customer", serve, at(1, 2, 3)) %>%
  add_generator("Restock", restock, at(-1)) %>%
  run()

编辑:如果定义以下便利功能(我想在下一个版本中将其添加到simmer中)

when_activated <- function(n=1) {
  first <- TRUE
  function() {
    if (first) {
      first <<- FALSE
      return(-1)
    }
    c(rep(0, n), -1)
  }
}

然后

library(simmer)

env <- simmer()

restock <- trajectory() %>%
  log_("restock")

serve <- trajectory() %>%
  log_("serve") %>%
  activate("Restock")

env %>%
  add_generator("Customer", serve, at(1, 2, 3)) %>%
  add_generator("Restock", restock, when_activated()) %>%
  run()

就是这样。