近似算法求解调度问题

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

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


ListScheduling(I)
	while the list is not empty do
		take the next job j from the head of the list
		delete j from the list
		find the machine m with the least amount of work assigned to it so far
		assign j to m
	return the schedule so obtained

LPT(I)
sort the jobs in order of decreasing processing times: t1 >= t2 >= t3 .... >= tn
execute list scheduling on the sorted list
return 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 machine


int	ListScheduling(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;
}


int	ListScheduling(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];
}

测试结果:

近似算法求解调度问题_第1张图片

你可能感兴趣的:(算法,list,测试,processing,任务,jobs)