近似算法求解调度问题

来源:互联网 发布:回力鞋怎么知是正品 编辑:程序博客网 时间:2024/05/19 20:01

        给定n个任务J1, J2,……, Jn和M台机器M1, M2……,Mm。对于每个任务Ji,其处理时间为ti > 0,而且必须由一台不间断底处理,每台机器在一个时间段里最多处理一个任务。并行机器调度问题就是如何将任务分配给机器,使得处理完成任务的完成时间(makespan)最短。

      对于这个NP问题,没有多项式时间的精确算法,用近似算法,效果好,速度快,真是难以置信啊,我刚5小时前还觉得JB近似算法有什么好研究的。


ListScheduling(I)while the list is not empty dotake the next job j from the head of the listdelete j from the listfind the machine m with the least amount of work assigned to it so farassign j to mreturn the schedule so obtainedLPT(I)sort the jobs in order of decreasing processing times: t1 >= t2 >= t3 .... >= tnexecute list scheduling on the sorted listreturn the schedule so obtained



算法如下:

#include <iostream>#include <algorithm>#include <numeric>#include <string.h>using namespace std;const int N = 1000000;const int M = 50; // the No. of machineintListScheduling(int data[], int len);bool max(const int a, const int b){return a > b;}int main(int argc, char* argv[]){srand(time(0));int data[N];int i;int sum = 0;for (i = 0; i < N; i++) {data[i] = rand() % 10;}sort(data, data + N);sum = accumulate(data, data + sizeof(data)/sizeof(data[0]), 0);cout <<"sum = " << sum <<  " sum/" << M  <<" = " << sum / M << endl;cout << "the result of list scheduling= " << ListScheduling(data, sizeof(data)/sizeof(data[0])) << endl;return 0;}intListScheduling(int data[], int len){int i, j;int M1 = 0, M2 = 0;int machine[M] = { 0 };int minIndex;for (i = 0; i < len; i++) {minIndex = 0;for (j = 1; j < M; j++) {if (machine[minIndex] > machine[j]) {minIndex = j;}}machine[minIndex] += data[i];}int maxIndex = 0;for (j = 1; j < M; j++) {if (machine[maxIndex] < machine[j]) {maxIndex = j;}}return machine[maxIndex];}

测试结果:


原创粉丝点击