慢煮:从内部轨迹功能中读取资源

时间:2017-06-26 19:48:54

标签: r lubridate event-simulation

我希望能够根据队列长度修改轨迹内的资源容量。

以下(简化)代码不起作用。 - 当我尝试在函数内部调用get_mon_resources(simStore)时,代码崩溃并显示错误:

Error in run_(private$sim_obj, until) : 
   Expecting a single value: [extent=0].

感谢您的帮助。

simStore <- simmer()

fUpdateNumberOfCashiers <- function() {
  dtLastRes <- simStore %>% get_mon_resources  %>% tail(1) 
  nCapacityNow <- dtLastRes$capacity # same result with get_capacity(simStore),
  nQueueNow <- dtLastRes$queue       # same result with get_queue_count(simStore)  
  print(dtLastRes)                  # prints empty data-frame !
  return (5)  # crashes here ! (eventually 5 will be replaced with more meaningful formula
}

trajClient <- trajectory("Client's path") %>%    
  log_("Arrived to cashier") %>%
  set_capacity("Cashier", value = fUpdateNumberOfCashiers ) %>%
  seize("Cashier") %>%
  timeout(function() {rexp(1, 30)}) %>%    # One Cashier processes 30 clients / hour
  release("Cashier") %>% 
  log_(function(attr) { sprintf("In total spent %.2f", now(simStore) - attr["start_time"])})

simStore <- simmer("Store") %>% 
  add_resource("Cashier", 1) %>% 
  add_generator("Store Clients", trajClient,  function() {rexp(1, 120)}) %>% # 120 clients / hour
  run(until=nHoursObserved <- 1) ; simStore

1 个答案:

答案 0 :(得分:0)

请在此处查看与解决此问题相关的讨论:https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/simmer-devel/NgIikOpHpss

导致问题的原因是另一个包(lubridate)从“simmer”中屏蔽了对象,如下所示:

 Attaching package: ‘lubridate’
 The following objects are masked from ‘package:simmer’:
       now, rollback

一旦我更换了

 library(simmer); library(lubridate);

library(lubridate); library(simmer); 

问题消失了!