流水作業調度的Johnson 演算法

來源:互聯網
上載者:User
     如果作業i和j滿足,則稱作業i和j滿足Johnson不等式。如果作業i和j不滿足Johnson不等式,則交換作業i和j的加工次序後,作業i和j滿足Johnson不等式。

流水作業調度的Johnson 演算法:

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
class Jobtype
{
  public:
  int time;     // 執行時間
  int index;    // 作業序號
  bool group;   // 作業所屬組,一共兩個機器,1為第一組,0為第二組

  int operator <= (Jobtype a) const // 重載<=號
  {
      return (time <= a.time);
  }

};

bool compare(Jobtype a, Jobtype b)      // sort使用
{
  return a.time<b.time;                 //升序排列,如果改為return a>b,則為降序
}

// 按照Johnson法則執行流水作業調度
/*
流水作業調度問題的Johnson演算法:

1. 令M={i|Mi<Ni},N={i|Mi>=Ni}

2. 將M中任務以Mi非遞減排序,將N中作業依Ni非遞增排序

3. M中任務接N中任務即為Johnson法則的最優調度

*/
int FlowShop(int n, int a[], int b[], int c[])
{
    Jobtype * d = new Jobtype[n];

    // 得到n個作業中,每個作業的最小加工時間

    for (int i = 0; i < n; i++)
    {
        d[i].time = a[i] > b[i] ? b[i] : a[i];       // 執行時間
        d[i].group = a[i] <= b[i];                   // 作業所屬組,一共兩個機器,1為第一組,0為第二組
        d[i].index = i;                              // 作業序號
    }

    sort(d, d + n, compare);    // 按d中作業時間增序排序
    int j = 0, k = n -1;
    for(int i = 0; i < n; i++)
    {
        if (d[i].group)        // 如果是第一組,則從0開始放入c[],c[]是最優調度序列
        {
            c[j++] = d[i].index;
        }
        else
        {
            c[k--] = d[i].index;
        }
    }

    j = a[c[0]];                // 計算最優調度序列下的消耗總時間
    k = j + b[c[0]];
    for (int i = 1; i < n; i++)
    {
        j += a[c[i]];
        k = j < k ? k + b[c[i]] : j + b[c[i]];  // 求得消耗總時間的最大值
    }
    delete d;
    return k;
}

int main()
{
    //cout << "Hello world!" << endl;
    int a[] = {2, 5, 9, 12};            // 第一台機器的加工時間
    int b[] = {7, 3, 10, 13};           // 第二台機器的加工時間
    int n = sizeof(a) / sizeof(int);    // 作業數
    int* c = new int[n];                // 最優調度序列
    int Result = FlowShop(n, a, b, c);  // 最優調度時間的總數
    cout << Result << endl;
    for (int i = 0; i < n; i++)         // 輸出最優調度序列
    {
        cout << c[i] << " ";
    }
    cout << endl;
    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.