如何在 R 中进行数字分配(一台机器 n 个作业)



我正在研究R中的赋值问题。我在 r 中有以下数据帧

cycle_time   TAT  ready_for_next  ITV_no    
2      10        12          0           
4      12        16          0           
6      13        19          0           
8      11        19          0           
10      15        25          0           
12      17        29          0           
14      13        27          0           
16      13        29          0           
18      12        30          0             
20      16        36          0
22      13        35          0
24      12        36          0
26      15        41          0
28      14        42          0
30      17        47          0

我想要的数据帧是

cycle_time   TAT  ready_for_next  ITV_no     wait_time
2      10        12          1            0
4      12        16          2            0
6      13        19          3            0
8      11        19          4            0
10      15        25          5            0
12      17        29          1            0 
14      13        27          6            0
16      13        29          2            0
18      12        30          3            1
20      16        36          4            1
22      13        35          5            3
24      12        36          6            3
26      15        41          2            3 
28      14        42          3            2 
30      17        47          5            5
cycle_time = crane cycle time
TAT(in mins) = turn around time of truck
ready_for_next(in mins) = ready to take next container
ITV_no = ITV no to be assigned for that job
***There are only 6 unique trucks available***

这里的想法是分配卡车,以便等待时间最短。 在前五个观测值中,分配了所有 5 辆卡车。

对于下一个容器,即第 6 行(第 12 分钟),ITV_no 1 从其作业返回,因此将被分配给此作业。 第 7 次观察(即第 14 分钟)没有可用的卡车,因此我们将不得不分配新卡车(即 ITV_no 6) 第 8 次观察(16 分钟) ITV_no 2 从其作业中返回,因此将被分配给此作业,依此类推。

如果没有可用的卡车,那么它必须等到最近的卡车下班回来。

如何在 R 中实现这一点?

我已经建立了一些逻辑

cycle_time <- c(2,4,6,8,10,12,14,16,18,20,22,24,26,28,30)
ITV_no <- c(1,2,3,4,5,6,7)
temp <- c()
TAT <- c(10,12,13,11,15,17,13,13,12,16,13,12,15,14,17)
ready_for_next <- cycle_time + TAT
assignment <- data.frame(cycle_time,TAT,ready_for_next)
assignment$ITV_no <- 0
for(i in 1:nrow(assignment)) {
for(j in 1:length(ITV_no)){
assignment$ITV_no[i] <- ifelse(assignment$cycle_time <= assignment$ready_for_next,ITV_no[j],
ifelse())
## I am not able to update the count of trucks which are already assigned
# and which are free to be assigned
}
}
Logic
1. first row increment ITV_no by 1. directly assign truck to that job
2. check if cycle_time <= previous all ready_for_next(i.e 12), if yes then increment ITV_no by 1,if no then assign previous ITV_no for that job(i.e 1)
e.g 
for row 6, cycle time will get compared to all previous ready_for_next column values (25,19,19,16,12) it finds the match at first row then that ITV_no(i.e 2) is assigned to 6th row
for row 7, cycle time will get compared to all previous ready_for_next column values (25,19,19,16) **12 should be removed from comparison because the truck is already assigned to the job** match at first row then that ITV_no(i.e 2) is assigned to 6th row. No match,so new truck is assigned to that job

我想出了一些解决方案... 它正在使用示例数据

rm(list=ls())
df <- data.frame(qc_time =   seq(2,40,2),itv_tat=c(10,15,12,18,25,19,18,16,14,10,12,15,17,19,13,12,8,15,9,14))
itv_number_vec <- vector()
itv_number_vec <- 0
itvno_time <- list()
for (i in 1:nrow(df))
{
####  Initialisation ####
if (i==1)
{
df$itv_available_time[i] <- sum(df$qc_time[i] + df$itv_tat[i])
itvno_time[[i]] <- df$itv_available_time[i]
df$delay[i] <- 0
df$itv_number[i] <- 1
itv_number_vec <- 1
}
if(i!=1)
{
if (df$qc_time[i] >= min(unlist(itvno_time)))
{
for (j in 1:length(itvno_time))
{
if (itvno_time[[j]] <= df$qc_time[i])
{
df$itv_number[i] <- j
df$itv_available_time[i] <- sum(df$qc_time[i] + df$itv_tat[i])
itvno_time[[j]] <- df$itv_available_time[i]
break
}
}
}else{
if (max(itv_number_vec)<7)
{
df$itv_number[i] <- max(itv_number_vec) + 1
itv_number_vec <- c(itv_number_vec,(max(itv_number_vec) + 1))
df$delay[i] <- 0
df$itv_available_time[i] <- sum(df$qc_time[i] + df$itv_tat[i])
itvno_time[[max(itv_number_vec)]] <- df$itv_available_time[i]
}else{
df$delay[i] <- (min(unlist(itvno_time)) - df$qc_time[i])
df$itv_number[i] <- which.min(itvno_time)
df$itv_available_time[i] <- sum(df$qc_time[i], df$itv_tat[i] ,df$delay[i])
itvno_time[[which.min(itvno_time)]] <- df$itv_available_time[i] 
}
}
}
}

相关内容

最新更新