【发布时间】:2016-01-08 02:49:27
【问题描述】:
我们正在研究只能同时照顾一个客户的服务器的延迟。假设我们有两个数据框:agg_data 和 ind_data。
> agg_data
minute service_minute
1 0 1
2 60 3
3 120 2
4 180 3
5 240 2
6 300 4
agg_data 每 小时 提供两个连续客户之间的服务时间。例如,在 60 到 120 之间(从开始算起的第二个小时),我们可以每 3 分钟为一位新客户提供服务,并且在该给定小时内我们总共可以为 20 位客户提供服务。
ind_data 提供每位客户的到达分钟数:
Arrival
1 51
2 63
3 120
4 121
5 125
6 129
我需要为受agg_data 中的service_minute 影响的客户生成出发时间。
输出如下:
Arrival Dep
1 51 52
2 63 66
3 120 122
4 121 124
5 125 127
6 129 131
这是我目前的代码,正确但效率很低:
ind_data$Dep = rep(0,now(ind_data))
# After the service time, the first customer can leave the system with no delay
# Service time is taken as that of the hour when the customer arrives
ind_data$Dep[1] = ind_data$Arrival[1] + agg_data[max(which(agg_data$minute<=ind_data$Arrival[1])),'service_minute']
# For customers after the first one,
# if they arrive when there is no delay (arrival time > departure time of the previous customer),
# then the service time is that of the hour when the arrive and
# departure time is arrival time + service time;
# if they arrive when there is delay (arrival time < departure time of the previous customer),
# then the service time is that of the hour when the previous customer leaves the system and
# the departure time is the departure time of the previous customer + service time.
for (i in 2:nrow(ind_data)){
ind_data$Dep[i] = max(
ind_data$Dep[i-1] + agg_data[max(which(agg_data$minute<=ind_data$Dep[i-1])),'service_minute'],
ind_data$Arrival[i] + agg_data[max(which(agg_data$minute<=ind_data$Arrival[i])),'service_minute']
)
}
我认为这是我们在agg_data 中寻找合适的服务时间的步骤需要很长时间。有没有更高效的算法?
谢谢。
【问题讨论】:
-
如果在 60-120 之间出现 20 个或更多到达会怎样?
-
第 4 位和第 5 位客户的情况相同。会有一个队列(延迟)。顾客将按照到达的顺序得到服务。服务的开始时间是客户的到达时间和前一个客户的离开时间的最大值。该客户的服务时间为开始时的服务时间。