動態規划-流水線調度(Assembly Line Scheduling, ALS)


流水線調度問題,直接上圖:

e為開始為進入流水線花費時間,a為station花費的時間,t為切換流水線花費時間,x為出流水線的時間,

代碼中用total[0][i]和total[1][i]分別表示第0條和第1條流水線到station i 所花費的最短時間

代碼(C語言寫的心累):

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#include<string.h>
#include<fstream>
#define MAX 200000
int assembleLineSequence(int *e, int t[][MAX], int a[][MAX],int *x, int n)
{
    int *total[2];    //記錄到達每個station最小的時間花費
    total[0] = (int *)malloc(sizeof(int)*n);
    total[1] = (int *)malloc(sizeof(int)*n);
    total[0][0] = e[0] + a[0][0];
    total[1][0] = e[1] + a[1][0];
    for(int i=1; i<n; i++){
        if(total[0][i-1] + a[0][i] < total[1][i-1] + t[1][i-1] + a[0][i])
            total[0][i] = total[0][i-1] + a[0][i];
        else
            total[0][i] = total[1][i-1] + t[1][i-1] + a[0][i];
        if(total[1][i-1] + a[1][i] < total[0][i-1] + t[0][i-1] + a[1][i])
            total[1][i] = total[1][i-1] + a[1][i];
        else
            total[1][i] = total[0][i-1] + t[0][i-1] + a[1][i];
    }
    if(total[0][n-1] + x[0] < total[1][n-1] + x[1])
        return total[0][n-1] + x[0];
    return total[1][n-1] + x[1];
}

int main()
{
    FILE *fp;
    int result;
    clock_t start, end;    
    for(int i=1; i<=10; i++){
        start = clock();
        char data[20] = "";  //給足夠大的長度才不會出現段錯誤 char s[2];
        sprintf(s,"%d",i);
        strcat(data,s);
        strcat(data, "組.txt");
        if((fp=fopen(data,"r"))==NULL){
            printf("Cannot open file strike any key exit!");
            return -1;
        }
        bool flag = false;    //記錄當前字符是否與上一字符拼接成新數字
        int num,j=0,n=0,line=1;    //n記錄數據個數
        int t[2][MAX],a[2][MAX];
        int e[2] = {0,0},x[2] = {0,0};
        char nc;
        while(!feof(fp)){
            nc = fgetc(fp);
            if(isdigit(nc)){
                if(flag == true){
                    num = num * 10 + (nc - '0');
                }else{
                    num = nc - '0';
                }
                flag = true;
            }else {                
                if(nc=='\n'){
                    n = j;
                    j = 0;                    
                    line++;
                }
                if(nc == ' '){
                    switch(line){
                        case 1:
                            t[0][j++] = num;
                            break;
                        case 2:
                            a[0][j++] = num;
                            break;
                        case 3:
                            t[1][j++] = num;
                            break;
                        case 4:
                            a[1][j++] = num;
                            break;
                        default:
                            printf("第%d行default\n",line);
                            break;
                    }
                }
                flag = false;
            }
        }
        end = clock();
        printf("花費時間:%fs\t",(double)(end - start)/CLOCKS_PER_SEC);
        result = assembleLineSequence(e,t,a,x,n);
        printf("第%d組:%d\n",i,result);
        fclose(fp);
    }
        
    return 0;
}

測試數據:

http://yunpan.cn/c3weMQSFtyvaw (提取碼:625f)

第一行表示流水線0切換到流水線1的切換時間

第二行表示流水線0中station的處理時間

第三行表示流水線1切換到流水線0的切換時間

第四行表示流水線1中station的處理時間

結果:

花費時間:0.020000s	第1組:996644
花費時間:0.020000s	第2組:1092030
花費時間:0.030000s	第3組:1179822
花費時間:0.020000s	第4組:1268183
花費時間:0.020000s	第5組:1363569
花費時間:0.030000s	第6組:1452373
花費時間:0.030000s	第7組:1543059
花費時間:0.040000s	第8組:1631089
花費時間:0.030000s	第9組:1722228
花費時間:0.030000s	第10組:1813060

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM