forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssembly_Line_Scheduling.php
executable file
·261 lines (214 loc) · 8.84 KB
/
Assembly_Line_Scheduling.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/*
Author: Vedant Wakalkar
PHP Program for Assembly Line Scheduling (Dynamic Programming).
Consider two assembly lines 'A' and 'B'
numbered from 'A1 to An' and 'B1 to Bn' respectively.
'In1' and 'In2' are two possible where an objects enters
and 'Out1' and 'Out2' are two exits after assembling parts.
In1 --> A1 --> A2 --> .... --> An-2 --> An-1 --> An --> Out1
\ * \ * \ *
tA1,2 \ / tAn-2,n-1 \ / \ / tBn-1,n
\/ \/ \/
/\ /\ /\
tB1,2 / \ tBn-2,n-1 / \ / \ tAn-1,n
/ * / * / *
In2 --> B1 --> B2 --> .... --> Bn-2 --> Bn-1 --> Bn --> Out2
*(arrow) = points toward
Let 'e1' and 'e2' be entry time from 'In1' and 'In2'
and 'ex1' and 'ex2' be out time for 'Out1' and 'Out2'
Let 'tAn' be the time taken at 'An' node of assembly line A and
'tBn' at 'Bn' node of assembly line B.
Let transiton time from one assembly line to other be tAi,j
and tBi,j for line 'A' and 'B' resp.
tAi,j -> Translating from Ai to Bj
tBi,j -> Translating from Bi to Aj
j = i + 1 for most of cases.
*/
$ASSEMBLY_LINES = 2; // Declaring No. of Assembly Lines
function assemblyLineScheduling($line, $transactionTime, $inTime, $outTime)
{
# line -> containing assembly line with its node values
# transactionTime -> containing lists of Extra cost incurred to change the assembly line
# inTime -> list of values of In time for assembly lines
# outTime -> list of values of Out time for assembly lines
$noOfNodes = sizeof($line[0]);
# line1 and line2 for storing the calculated cost at each node
$line1 = array();
$line2 = array();
# Adding In time and cost of first node to respective list
$line1[0] = ($inTime[0]);
$line2[0] = ($inTime[1]);
$line1[1] = ($inTime[0] + $line[0][0]);
$line2[1] = ($inTime[1] + $line[1][0]);
for ($nNode = 1; $nNode < $noOfNodes; $nNode++)
{
# tempS -> node selected on same line
# tempC -> node selected on different line
# temp -> minimum of tempS and tempC
# for Line_1
$tempS = $line1[$nNode] + $line[0][$nNode];
$tempC = $line2[$nNode] + $transactionTime[1][$nNode] + $line[0][$nNode];
$temp = min($tempS, $tempC);
$line1[$nNode + 1] = $temp;
# for Line_2
$tempS = $line2[$nNode] + $line[1][$nNode];
$tempC = $line1[$nNode] + $transactionTime[0][$nNode] + $line[1][$nNode];
$temp = min($tempS, $tempC);
$line2[$nNode + 1] = $temp;
}
# Adding Out time for respective line
$line1[sizeof($line1)] = ($line1[sizeof($line1) - 1] + $outTime[0]);
$line2[sizeof($line2)] = ($line2[sizeof($line2) - 1] + $outTime[1]);
if (min($line1[sizeof($line1) - 1], $line2[sizeof($line2) - 1]) == $line1[sizeof($line1) - 1])
{
$pathC = '1';
}
else
{
$pathC = '2';
}
$path = array();
for ($i = $noOfNodes + 1; $i >= 0; $i--)
{
if ($i == $noOfNodes+1)
{
array_splice( $path, sizeof($path), 0, 'Out'.$pathC.'('.$outTime[(int)$pathC - 1].')');
}
elseif ($i == 0)
{
array_splice( $path, 0, 0, 'In'.$pathC.'('.$inTime[(int)$pathC - 1].')');
}
else
{
array_splice( $path, 0, 0, 'Node'.$pathC.'('.$line[(int)$pathC - 1][$i - 1].')');
if ($pathC == '1')
{
$val = $line1[$i] - $line1[$i - 1];
if ($val != $line[(int)$pathC - 1][$i - 1])
{
$pathC = '2';
}
}
else
{
$val = $line2[$i] - $line2[$i - 1];
if ($val != $line[(int)$pathC - 1][$i - 1])
{
$pathC = '1';
}
}
}
}
# Finding minimum cost and return
if (min($line1[sizeof($line1) - 1], $line2[sizeof($line2) - 1]) == $line1[sizeof($line1) - 1])
{
return [$path, $line1[sizeof($line1) - 1]];
}
else
{
return [$path, $line2[sizeof($line2) - 1]];
}
}
/*
Example :
30(In1) --> 80 --> 40 --> 60 --> 30(Out1)
\ * \ *
20 \ / \ / 30
\/ \/
/\ /\
10 / \ / \ 20
/ * / *
10(In2) --> 50 --> 70 --> 50 --> 30(Out2)
*/
# lineA, lineB time required at each node on assembly line Here, lineA = line[0], lineB = line[1]
# aToB, bToA time required at change assembly line from Ai to Bj or Bi to Aj where j = i+1 Here, aToB = transactionTime[0], bToA = transactionTime[1]
# inA, inB Time required to enter Assembly line Here, inA = inTime[0], inB = inTime[1]
# outA, outB Time required to exit from Assembly line Here, outA = outTime[0], outB = outTime[1]
$line = array();
$inTime = array();
$outTime = array();
$transactionTime = array();
$fr = fopen("php://stdin","r"); // open our file pointer to read from stdin
echo "Enter the no. of nodes on Assembly line : ";
$nodes = rtrim(fgets($fr,128));
echo "\n-- In Time --".PHP_EOL;
for ($lineN = 0; $lineN < $ASSEMBLY_LINES; $lineN++)
{
echo "For Line".($lineN + 1).": ";
$inTime[$lineN] = rtrim(fgets($fr,128));
}
echo "\n-- Out Time --".PHP_EOL;
for ($lineN = 0; $lineN < $ASSEMBLY_LINES; $lineN++)
{
echo "For Line".($lineN + 1).": ";
$outTime[$lineN] = rtrim(fgets($fr,128));
}
echo "\n-- Time required at each node --".PHP_EOL;
for ($lineN = 0; $lineN < $ASSEMBLY_LINES; $lineN++)
{
echo "\nFor Assembly Line".($lineN + 1).": ".PHP_EOL;
for ($nThNode = 0; $nThNode < $nodes; $nThNode++)
{
echo "Node".$nThNode.": ";
$line[$lineN][$nThNode] = rtrim(fgets($fr,128));
}
}
echo "\n-- Transaction Time --".PHP_EOL;
for ($lineN = 0; $lineN < $ASSEMBLY_LINES; $lineN++)
{
for ($nThNode = 0; $nThNode < $nodes; $nThNode++)
{
if ($nThNode == 0)
{
$transactionTime[$lineN][$nThNode] = 0;
echo "\n";
}
else
{
if ($lineN == 0)
{
echo "Node".($lineN + 1)."(".($line[$lineN][$nThNode - 1]).") to Node".($lineN + 2)."(".($line[$lineN + 1][$nThNode]).") : ";
$transactionTime[$lineN][$nThNode] = rtrim(fgets($fr,128));
}
else
{
echo "Node".($lineN + 1)."(".($line[$lineN][$nThNode - 1]).") to Node".($lineN)."(".($line[$lineN - 1][$nThNode]).") : ";
$transactionTime[$lineN][$nThNode] = rtrim(fgets($fr,128));
}
}
}
}
fclose ($fr); // close the file handle
$op = assemblyLineScheduling($line, $transactionTime, $inTime, $outTime);
$path = $op[0];
$val = $op[1];
echo "\nMinimum time = ".$val.PHP_EOL;
echo "Path : ".implode(' --> ', $path).PHP_EOL;
/*
Output:
Enter the no. of nodes on Assembly line : 3
-- In Time --
For Line1: 30
For Line2: 10
-- Out Time --
For Line1: 30
For Line2: 30
-- Time required at each node --
For Assembly Line1:
Node0: 80
Node1: 40
Node2: 60
For Assembly Line2:
Node0: 50
Node1: 70
Node2: 50
-- Transaction Time --
Node1(80) to Node2(70) : 20
Node1(40) to Node2(50) : 20
Node2(50) to Node1(40) : 10
Node2(70) to Node1(60) : 30
Minimum time = 200
Path : In2(10) --> Node2(50) --> Node1(40) --> Node1(60) --> Out1(30)
*/
?>