R simmer:选择服务器的自定义逻辑



我正在构建一个simmer模拟,用于通过无人机运送疫苗。模拟部分的伪代码为:

  1. 生成N个"需求点"在一个地理位置中,代表需要接种疫苗的地点。生成一个数据框架。将到达时间添加为数据框列。增加优先级列-先到先得。
  2. 使用kmeans聚类查找K个无人机站的位置,横跨地理
  3. 生成一个N x K矩阵,表示从每个无人机站到每个需求点的旅行时间

在模拟中,疫苗交付是到达,无人机是资源(服务器容量为1,无限队列容量)。我希望模拟使用以下资源选择逻辑:

  1. 当到达时,确定哪些无人机可用。其中,根据飞行时间矩阵选择飞行时间最短的无人机。
  2. 如果当前所有的无人机都被使用,新到的将被放入一个公共队列。只要有无人机可用,公共队列中的到达者优先,队列中最老的到达者优先。这可能意味着疫苗没有从最近的无人机站运送。
  3. 一旦到达seize_selected选定的无人机,timeout为旅行时间,然后release_selected该无人机。

我正在尝试在使用simmer包(或替代方案)调度资源时使用路由逻辑,但不像预期的那样工作。

任何帮助都是感激的。对我来说,真正棘手的部分是把到达的人放在一个共同的队列中,然后选择最快的无人机。
我当前的模拟代码是:

delivery_env <- simmer()
delivery_traj <- trajectory("delivery") %>%

set_attribute(c("min_drone_index", "min_drone_delay"), function() {
# find available resources
server_count <- numeric(drone_count)

for (i in 1:length(server_count)){server_count[i] <- get_server_count(delivery_env, paste0("drone", i))   }

#find index of minimum travel time, inclusive of server_count
#since the capacity of each drone is 1, we want to find the drones
#that have server_count == 1 and set them "very very far away" from the deliverypoint
#so the ranking system puts them last

#identify row of traveltime_matrix that corresponds to the delivery point
#in traveltime_matrix, rows are vaccines, columns are drones
k <- get_attribute(delivery_env, "arrival_index_index1")
traveltime_vec <- traveltime_matrix[k, ]

#make the currently-occupied drones, "very very far away"
traveltime_vec[which(server_count==1)] <- traveltime_vec[which(server_count==1)]+ 9999999999

#identify a single value for the minimum distance - more than 1 drone index may be the minimum
#identify closest available. randomly sample if more than 1 is closest
k <- which.min(traveltime_vec)
min_drone_index <- sample(k,1)
#the drone (resource) is seized for 2x the one-way travel time, plus time on the ground.
min_drone_delay <- 2*traveltime_vec[min_drone_index] + delivery_ontheground_time_minutes 
# take the nearest available resource. 
return(c(min_drone_index, min_drone_delay))
}) %>%

simmer::select(function() paste0("drone", get_attribute(delivery_env, "min_drone_index"))) %>%
seize_selected() %>%
timeout_from_attribute("min_drone_delay") %>%
release_selected() %>%
#release("drone") %>%
log_("Delivery Finished")


delivery_env <-
simmer("drone") %>%
add_resource(name= paste0("drone",seq(1,drone_count,1)), capacity=1) %>%
add_dataframe(name_prefix='delivery',trajectory = delivery_traj, data=pointsdf,mon=2,batch=50,col_priority="priority",
col_time = "absolute_time", time ="absolute",col_attributes = c("longitude","latitude","arrival_index_index1","arrival_index_index0"))

sim_out <- delivery_env %>% run()

你需要一个额外的资源,容量等于无人机的数量。这是常见的队列。如果你需要最老的先到,那就是后进先出。根据计数器函数设置优先级值就可以达到这个目的(或者,如果在源数据帧中设置优先级,也可以)。把所有的东西放在一起:

prio_counter <- function() {
i <- 0
function() {
i <<- i + 1
c(i, NA, NA)
}
}
delivery_traj <- trajectory("delivery") %>%
set_prioritization(prio_counter()) %>%
seize("common_queue") %>%
set_attribute(c("min_drone_index", "min_drone_delay"), function() {
...
}) %>%
simmer::select(...) %>%
seize_selected() %>%
timeout_from_attribute("min_drone_delay") %>%
release_selected() %>%
release("common_queue") %>%
log_("Delivery Finished")

顺便说一句:get_server_count()(以及所有其他getter)都是矢量化的,因此在那里不需要循环。

最新更新