【发布时间】:2013-11-21 20:19:43
【问题描述】:
我有一组船只(数据在外部 .txt 文件中给出:船号、到达时间、服务时间、到期时间、惩罚因素、等待因素),它们靠泊并一一服务。我需要找到成本最低的船只订单。我正在使用非常基本的方法 - 我生成所有可能的序列,然后比较每个序列的成本。一个容器只能服务一次,这意味着顺序中不能有重复。 每艘新来船的成本包括等待成本(排队等候的时间)和惩罚成本(到期后靠泊的时间)。 目前,我有一个包含 10 个嵌套循环的程序,用于 10 个容器。 代码
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<time.h>
void main()
{
FILE *f1;
int n = 10;/*The number of vessels*/
int arr[10];
int count = 0, s = 1;
int a, b, c, d, e, f, g, h, i, j;
int t[10];
int min;
int time[10];
struct vessel{
int name;/*the name of the vessel*/
int ar; /*arrival time*/
int ser; /*service time*/
int exp; /*due time*/
int df; /*delay factor*/
int wf; /*waiting factor*/
};
struct vessel x[10]; /* where number means the total number of vessels arriving*/
for (i = 0; i < n; i++)
{
s = s*(n-i) ;
}
printf("S is %d\n", s);/*The number of possible sequences. (n-1)! */
f1 = fopen("sample1.txt", "r");/*Read data from file*/
if (f1 == NULL){
printf("Can not open the file\n");
getch();
return 0;
}
for (i = 0; i < n; i++)
{
fscanf(f1, "%d, %d, %d, %d, %d, %d\n", &x[i].name, &x[i].ar, &x[i].ser, &x[i].exp, &x[i].df, &x[i].wf);
arr[i] = x[i].name;
}
/*f2 = fopen("results.txt", "w");*/
min = 9999999999;/*Set it to be big.*/
for (a = 0; a < n; a++)/*First step. Set time and cost.*/
{
t[a] = 0;/*As first vessel comes and goes, there are no penalt or waiting cost.*/
time[a] = x[a].ar + x[a].ser;/*The time vessel arrives + the service time.*/
for (b = 0; b < n; b++)/*LOOP START.*/
{
if (b != a)/*One vessel can be served only once.*/
{
time[b] = 0;
if (time[a] > x[b].ar)/*If vessel arrived during service time of the previous one*/
{
if ((time[a] + x[b].ser - x[b].exp) > 0)/*If the service of current vessel ends after the due time.*/
{
t[b] = t[a] + (time[a] - x[b].ar)*x[b].wf + (time[a] + x[b].ser - x[b].exp)*x[b].df;
}
else/*If the service ends before the due time.*/
{
t[b] = t[a] + (time[a] - x[b].ar)*x[b].wf;
}
time[b] = time[a] + x[b].ser;/*Update time. The time when the service of current vessel ends.*/
}
else/*If vessel arrives after the service time of the previous one.*/
{
t[b] = t[a] + 0;/*No penalty or waiting cost.*/
time[b] = x[b].ar + x[b].ser;/*Update time. Current vessel arrival time + its service time.*/
}
if (t[b] < min)/*Check if current cost is smaller than found minimum cost. If yes, continue. If not, go to the start of the loop. LOOP END.*/
{
for (c = 0; c < n; c++)
{
if (c != b && c != a)
{
time[c] = 0;
if (time[b] > x[c].ar)
{
if ((time[b] + x[c].ser - x[c].exp) > 0)
{
t[c] = t[b] + (time[b] - x[c].ar)*x[c].wf + (time[b] + x[c].ser - x[c].exp)*x[c].df;
}
else
{
t[c] = t[b] + (time[b] - x[c].ar)*x[c].wf;
}
time[c] = time[b] + x[c].ser;
}
else
{
t[c] = t[b] + 0;
time[c] = x[c].ar + x[c].ser;
}
if (t[c] < min)
{
for (d = 0; d < n; d++)
{
if (d != c && d != b && d != a)
{
time[d] = 0;
if (time[c] > x[d].ar)
{
if ((time[c] + x[d].ser - x[d].exp) > 0)
{
t[d] = t[c] + (time[c] - x[d].ar)*x[d].wf + (time[c] + x[d].ser - x[d].exp)*x[d].df;
}
else
{
t[d] = t[c] + (time[c] - x[d].ar)*x[d].wf;
}
time[d] = time[c] + x[d].ser;
}
else
{
t[d] = t[c] + 0;
time[d] = x[d].ar + x[d].ser;
}
if (t[d] < min)
{
for (e = 0; e < n; e++)
{
if (e != d && e != c && e != b && e != a)
{
time[e] = 0;
if (time[d] > x[e].ar)
{
if ((time[d] + x[e].ser - x[e].exp) > 0)
{
t[e] = t[d] + (time[d] - x[e].ar)*x[e].wf + (time[d] + x[e].ser - x[e].exp)*x[e].df;
}
else
{
t[e] = t[d] + (time[d] - x[e].ar)*x[e].wf;
}
time[e] = time[d] + x[e].ser;
}
else
{
t[e] = t[d] + 0;
time[e] = x[e].ar + x[e].ser;
}
if (t[e] < min)
{
for (f = 0; f < n; f++)
{
if (f != e && f != d && f != c && f != b && f != a)
{
time[f] = 0;
if (time[e] > x[f].ar)
{
if ((time[e] + x[f].ser - x[f].exp) > 0)
{
t[f] = t[e] + (time[e] - x[f].ar)*x[f].wf + (time[e] + x[f].ser - x[f].exp)*x[f].df;
}
else
{
t[f] = t[e] + (time[e] - x[f].ar)*x[f].wf;
}
time[f] = time[e] + x[f].ser;
}
else
{
t[f] = t[e] + 0;
time[f] = x[f].ar + x[f].ser;
}
if (t[f] < min)
{
for (g = 0; g < n; g++)
{
if (g != a && g != b && g != c && g != d && g != e && g != f)
{
time[g] = 0;
if (time[f] > x[g].ar)
{
if ((time[f] + x[g].ser - x[g].exp) > 0)
{
t[g] = t[f] + (time[f] - x[g].ar)*x[g].wf + (time[f] + x[g].ser - x[g].exp)*x[g].df;
}
else
{
t[g] = t[f] + (time[f] - x[g].ar)*x[g].wf;
}
time[g] = time[f] + x[g].ser;
}
else
{
t[g] = t[f] + 0;
time[g] = x[g].ar + x[g].ser;
}
if (t[g] < min)
{
for (h = 0; h < n; h++)
{
if (h != a && h != b && h != c && h != d && h != e && h != f && h != g)
{
time[h] = 0;
if (time[g] > x[h].ar)
{
if ((time[g] + x[h].ser - x[h].exp) > 0)
{
t[h] = t[g] + (time[g] - x[h].ar)*x[h].wf + (time[g] + x[h].ser - x[h].exp)*x[h].df;
}
else
{
t[h] = t[g] + (time[g] - x[h].ar)*x[h].wf;
}
time[h] = time[g] + x[h].ser;
}
else
{
t[h] = t[g] + 0;
time[h] = x[h].ar + x[h].ser;
}
if (t[h] < min)
{
for (i = 0; i < n; i++)
{
if (i != a && i != b && i != c && i != d && i != e && i != f && i != g && i != h)
{
time[i] = 0;
if (time[h] > x[i].ar)
{
if ((time[h] + x[i].ser - x[i].exp) > 0)
{
t[i] = t[h] + (time[h] - x[i].ar)*x[i].wf + (time[h] + x[i].ser - x[i].exp)*x[i].df;
}
else
{
t[i] = t[h] + (time[h] - x[i].ar)*x[i].wf;
}
time[i] = time[h] + x[i].ser;
}
else
{
t[i] = t[h] + 0;
time[i] = x[i].ar + x[i].ser;
}
if (t[i] < min)
{
for (j = 0; j < n; j++)
{
if (j != a && j != b && j != c && j != d && j != e && j != f && j != g && j != h && j != i)
{
time[j] = 0;
if (time[i] > x[j].ar)
{
if ((time[i] + x[j].ser - x[j].exp) > 0)
{
t[j] = t[i] + (time[i] - x[j].ar)*x[j].wf + (time[i] + x[j].ser - x[j].exp)*x[j].df;
}
else
{
t[j] = t[i] + (time[i] - x[j].ar)*x[j].wf;
}
time[j] = time[i] + x[j].ser;
}
else
{
t[j] = t[i] + 0;
time[j] = x[j].ar + x[j].ser;
}
if (t[j] < min)
{
min = t[j];
printf("%d\n", min);
/*printf("%d %d %d %d %d %d %d %d %d %d\n", t[a], t[b], t[c], t[d], t[e], t[f], t[g], t[h], t[i], t[j]);*/
arr[0] = a + 1; arr[1] = b + 1; arr[2] = c + 1; arr[3] = d + 1; arr[4] = e + 1; arr[5] = f + 1; arr[6] = g + 1; arr[7] = h + 1; arr[8] = i + 1; arr[9] = j + 1;
}
count++;/*How many sequences of n vessels were able to reach the end of loop.*/
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
printf("Final sequence is: ");
for (i = 0; i < n; i++)
{
printf("%d, ", arr[i]);
}
printf("\nFinal count is %d\n", count);
printf("Final cost is %d\n", min);
getch();
return 0;
}
此代码有效。但。 这是漫长而讨厌的。我不知道如何改变它。 对我来说唯一重要的部分是循环。 样本数据(以防万一)
1, 756, 207, 1148, 1160, 303
2, 660, 243, 1105, 847, 344
3, 1444, 225, 1857, 1006, 310
4, 1554, 199, 1941, 1004, 326
5, 1376, 186, 1737, 1112, 321
6, 1396, 170, 1680, 1053, 247
7, 1577, 158, 1917, 1005, 275
8, 629, 218, 1026, 976, 289
9, 807, 181, 1151, 1078, 299
10, 779, 157, 1088, 804, 254
非常感谢您的宝贵时间。
【问题讨论】:
-
两个想法,一个,为什么不粘贴一个可编译的版本?二、现在可以正常使用了吗?
-
这里需要更好地描述问题。我不确定它可以在没有 10 个循环的情况下解决。
-
对描述进行了一些更改。告诉我是否需要澄清其他内容。
-
如果你想制作一个包含所有血管组合的序列,递归不是更好的选择,这样你就有了回溯?我可能会尝试...
-
我正在考虑将递归作为我的第一选择。但我不知道它是如何工作的。
标签: c for-loop recursion nested-loops