您的位置:首页 > 移动开发 > Objective-C

动态规划--装配线调度问题

2010-11-05 16:34 459 查看
问题描述:一个找出通过工厂装配线的最快方式的制造问题。共有两条装配线,每条有n个装配站,装配线i的第j个装配站完成工作需要的时间为a[i][j] 。在通过i装配线的第j个装配站后,产品可以直接进入i装配线的j+1个装配站,当中不需要损耗时间;产品也可以进入另外一条装配线的j+1个装配站,这个过程需要损耗t[i][j]时间。产品从初始站进入装配线分别需要损耗时间为e[0],e[1] ,产品从装配线进入终点站分别需要时间为x[0],x[1]。求一个产品从初始站到达终点站的最快路径。

 

分析:令f0[j]代表从初始站到在0号线上完成第j站操作所需的最少时间。令f1[j]代表从初始站到在1号线上完成第j站操作所需的最少时间。他们存在这样的递推关系。



 

代码

#include <iostream>
#define MAXASSEMBLE 10000
using namespace std;
/**
* @brief Get the fast way in the assembly line schedule
*
* @param a[][MAXASSEMBLE]: a[i][j] means the time consumed in j'th assembly station of i'th AssemblyLine. The index of j is from 0 to n-1.
* @param t[][MAXASSEMBLE]: t[i][j] means the time consumed to move object from j'th station in i'th AssemblyLine to j+1'th station in the other AssemblyLine. The index of j is from 0 to n-2.
* @param e: e[i] means the time consumed to move object from start station to 0'th station in i'th AssemblyLine.
* @param x: x[i] means the time consumed to move object from n-1'th station in i'th AssemblyLine to end station.
* @param n: the station number in AssemblyLine.
* @param time: the least time consumed to move object from start station to end station.
* @param line[][MAXASSEMBLE]: line[i][j] means the pre-station's AssemblyLine number of station j+1 in AssemblyLine i.
* @param lastLine: the pre-station's AssemblyLine number of the end station.
*/
void FastestWay(double a[][MAXASSEMBLE], double t[][MAXASSEMBLE], double* e, double* x, int n, double &time, int line[][MAXASSEMBLE], int &lastLine)
{
double lastWay0 = e[0] + a[0][0];
double lastWay1 = e[1] + a[1][0];
double Way0;
double Way1;
for (int i = 1; i < n; ++i)
{
//choose the way according last way
if (lastWay0 < lastWay1 + t[1][i - 1])
{
Way0 = lastWay0 + a[0][i];
line[0][i - 1] =  0;
}
else
{
Way0 = lastWay1 + t[1][i - 1] + a[0][i];
line[0][i - 1] = 1;
}
if (lastWay1 < lastWay0 + t[0][i - 1])
{
Way1 = lastWay1 + a[1][i];
line[1][i - 1] = 1;
}
else
{
Way1 = lastWay0 + t[0][i - 1] + a[1][i];
line[1][i - 1] = 0;
}
lastWay0 = Way0;
lastWay1 = Way1;
}
//choose the last step
if (lastWay0 + x[0] < lastWay1 + x[1])
{
time = lastWay0 + x[0];
lastLine = 0;
}
else
{
time = lastWay1 + x[1];
lastLine = 1;
}
}
/**
* @brief Print the path
*
* @param lastLine: the pre-station's AssemblyLine number of the end station
* @param line[][MAXASSEMBLE]: the path matrix
* @param n: the number of stations in one AssemblyLine
*/
void PrintPath(int lastLine, int line[][MAXASSEMBLE], int n)
{
cout << n << "step: " << lastLine << endl;
while (--n > 0)
{
cout << n << "step: " << line[lastLine][n - 1] << endl;
lastLine = line[lastLine][n - 1];
}
}
int main()
{
int n;
double a[2][MAXASSEMBLE];
double t[2][MAXASSEMBLE];
int line[2][MAXASSEMBLE];
int lastline;
double time;
double e[2];
double x[2];
while (cin >> n, n != 0)
{
for (int i = 0; i < n; ++i)
{
cin >> a[0][i];
}
for (int i = 0; i < n; ++i)
{
cin >> a[1][i];
}
for (int i = 0; i + 1 < n; ++i)
{
cin >> t[0][i];
}
for (int i = 0; i + 1 < n; ++i)
{
cin >> t[1][i];
}
cin >> e[0] >> e[1] >> x[0] >> x[1];
FastestWay(a, t, e, x, n, time, line, lastline);
cout << "Least time: " << time << endl;
PrintPath(lastline, line, n);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息