我正在处理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
答案 0 :(得分:1)
我想出了一些解决方案...... 它正在处理样本数据
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]
}
}
}
}