批处理作业调度
1、问题描述
每一个作业Ji都有两项任务分别在2台机器上完成。每个作业必须先有机器1处理,然后再由机器2处理。作业Ji需要机器j的处理时间为tji。对于一个确定的作业调度,设Fji是作业i在机器j上完成处理时间。则所有作业在机器2上完成处理时间和f=F2i,称为该作业调度的完成时间和
对于给定的n个作业,指定最佳作业调度方案,使其完成时间和达到最小。
区别于流水线调度问题:批处理作业调度旨在求出使其完成时间和达到最小的最佳调度序列;
// PermutationTree.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
template<class Type>
class Flowshop
{
friend void Flow(int**,int);
private:
void OutPut();
void BackTrack(Type t);
Type** M, // 两台机器分别的时间
* x,//排列的顺序
f1,//机器1的时间
* f2,//机器2的时间
f,//时间的综合
bestf,//当前最优的时间
n;//任务数量
};
int n = 3;
void swap(char* c, char* v)
{
char a = *c;
*c = *v;
*v = a;
}
void test(char *ch,int t)
{
if (t >=n)
{
for (int i = 0; i < n; i++)
std::cout << ch[i];
std::cout << std::endl;
}
else
{
for (int i = t; i < n; i++)
{
swap(&ch[i], &ch[t]);
test(ch,t + 1);
swap(&ch[i], &ch[t]);
}
}
}
void swap(int &t, int &p)
{
int temp = t;
t = p;
p = temp;
}
template<class Type>
void Flowshop<Type>::BackTrack(Type t)
{
if (t > n)
{
for (int i = 1; i <= n; i++)
{
std::cout << x[i] << "\t";
}
std::cout << std::endl;
bestf = f;
std::cout << bestf << std::endl;
std::cout << f2[t-1] << std::endl;
}
else
{
for (int i = t; i <= n; i++)
{
f1 += M[x[i]][0];
f2[t] = ((f2[t - 1] > f1 )? f2[t - 1] : f1) + M[x[i]][1];
f += f2[t];
if (f < bestf)//当完成时间小于最优时间时
{
swap(x[i], x[t]);
BackTrack(t + 1);
swap(x[i], x[t]);
}
f1-= M[x[i]][0];
f -= f2[t];
}
}
}
void Flow(int **M,int n)
{
Flowshop<int> flow;
flow.M = M;
flow.n = n;
flow.x = new int[n+1];
flow.f2 = new int[n+1];
for (int i = 0; i <=n; i++)
flow.x[i] = i, flow.f2[i] = 0;
flow.f = 0;
flow.f1=0;
flow.bestf = 10000;
flow.OutPut();
std::cout << "_________________________" << std::endl;
flow.BackTrack(1);
}
template<class Type>
void Flowshop<Type>::OutPut()
{
std::cout << "机器数量n:" << std::endl;
for (int i = 0; i <= n; i++)
{
std::cout << x[i] << "\t";
}
std::cout << std::endl;
for (int i = 0; i <= n; i++)
{
std::cout << M[i][0] << "\t";
}
std::cout << std::endl;
for (int i = 0; i <= n; i++)
{
std::cout << M[i][1] << "\t";
}
std::cout << std::endl;
}
int main()
{
int** M;
int n = 3;
M =new int*[n+1];
for (int i = 0; i <= n; i++)
M[i] = new int[2];
M[0][0] = 0, M[1][0] = 2, M[2][0] = 3, M[3][0] = 2;
M[0][1] = 0, M[1][1] = 1, M[2][1] = 1, M[3][1] = 3;
//M[0][0]=0,M[1][0] = 2, M[2][0] = 5, M[3][0] = 7, M[4][0] = 10, M[5][0] = 5, M[6][0] = 2;
//M[0][1]=0,M[1][1] = 3, M[2][1] = 8, M[3][1] = 4, M[4][1] = 11, M[5][1] = 3, M[6][1] = 4;
Flow(M,n);
return 0;
}