Last active
December 3, 2021 22:14
-
-
Save zedchance/808c480b34f6e0fd15d76339dc11a47a to your computer and use it in GitHub Desktop.
Fastest path thru assembly line, with consecutive station bonus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# assembly line scheduling problem, modified with consecutive station bonus | |
import math | |
C_FACTOR = 1 | |
def solve(e1, e2, a1, a2, t1, t2, x1, x2): | |
""" finds fastest route thru factory """ | |
assert len(a1) == len(a2) | |
n = len(a1) | |
f = [[[math.inf for i in range(n)] for i in range(n)] for i in range(2)] | |
# base case | |
f[0][0][0] = e1 + a1[0] | |
f[1][0][0] = e2 + a2[0] | |
# work left to right | |
for j in range(1, n): | |
# work front to back | |
for c in range(j + 1): | |
if c == 0: | |
# transferring from line 2 | |
f[0][j][c] = min(f[1][j - 1]) + t2[j - 1] + a1[j] | |
# transferring from line 1 | |
f[1][j][c] = min(f[0][j - 1]) + t1[j - 1] + a2[j] | |
else: | |
# continuing from line 1 | |
f[0][j][c] = f[0][j - 1][c - 1] + a1[j] - (c * C_FACTOR) | |
# continuing from line 2 | |
f[1][j][c] = f[1][j - 1][c - 1] + a2[j] - (c * C_FACTOR) | |
for line in f: | |
print(line) | |
# determine shortest, min value of last station's exit times, plus line exit time | |
a1_total, cs1 = min((a + x1, c) for (c, a) in enumerate(f[0][-1])) | |
a2_total, cs2 = min((a + x2, c) for (c, a) in enumerate(f[1][-1])) | |
if a1_total < a2_total: | |
fs = a1_total | |
ls = 1 | |
cs = cs1 | |
else: | |
fs = a2_total | |
ls = 2 | |
cs = cs2 | |
print("shortest path total weight", fs) | |
# print route thru assembly line | |
def _print_path(i, j, c): | |
if j < 0: | |
return | |
else: | |
# we're still on the same line | |
if c > 0: | |
_print_path(i, j - 1, c - 1) | |
# switching lines | |
else: | |
new_line = 1 if i == 1 else 0 | |
min_time = min(f[new_line][j - 1]) | |
_print_path(new_line, j - 1, f[new_line][j - 1].index(min_time)) | |
print("line", i, "station", j + 1, f'bonus of {c}' if c > 0 else '') | |
_print_path(ls, n - 1, cs) | |
if __name__ == '__main__': | |
e1 = 2 | |
e2 = 4 | |
a1 = [7, 9, 3, 4, 8] | |
a2 = [8, 5, 6, 4, 5] | |
t1 = [2, 3, 1, 3] | |
t2 = [2, 1, 2, 2] | |
x1 = 3 | |
x2 = 6 | |
solve(e1, e2, a1, a2, t1, t2, x1, x2) |
More visual on what each subproblem relies on:
https://www.figma.com/file/G3ugK4WQb4K1O2A8zvdUmn/assembly_mod?node-id=0%3A1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output with
C_FACTOR = 1
:Output with
C_FACTOR = 0.1
: