2017-07-15 20 views
1

我在R.工作分配問題,我有以下r中數據幀的分配怎麼做的R值(一體機n個工件)

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 
+1

請奠定了充分的算法和你如何試圖實施它和你卡住的地方。 –

+0

另外,請用'dput'或'DF <共享重複性的數據(例如 - data.frame(...)' – JanLauGe

+0

重複性的數據在代碼 – Neil

回答

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] 
       } 
     } 

    } 
} 
+0

其完美的工作,因爲我已經打算共享我真的很欣賞你的時間努力。謝謝。 – Neil