Skip to content

Instantly share code, notes, and snippets.

@zedchance
Last active December 3, 2021 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zedchance/808c480b34f6e0fd15d76339dc11a47a to your computer and use it in GitHub Desktop.
Save zedchance/808c480b34f6e0fd15d76339dc11a47a to your computer and use it in GitHub Desktop.
Fastest path thru assembly line, with consecutive station bonus
# 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)
@zedchance
Copy link
Author

zedchance commented Dec 3, 2021

Output with C_FACTOR = 1:

[[9, inf, inf, inf, inf], [23, 17, inf, inf, inf], [20, 25, 18, inf, inf], [26, 23, 27, 19, inf], [31, 33, 29, 32, 23]]
[[12, inf, inf, inf, inf], [16, 16, inf, inf, inf], [26, 21, 20, inf, inf], [23, 29, 23, 21, inf], [27, 27, 32, 25, 22]]
shortest path total weight 26
line 1 station 1 
line 1 station 2 bonus of 1
line 1 station 3 bonus of 2
line 1 station 4 bonus of 3
line 1 station 5 bonus of 4

Output with C_FACTOR = 0.1:

[[9, inf, inf, inf, inf], [23, 17.9, inf, inf, inf], [20, 25.9, 20.7, inf, inf], [27.9, 23.9, 29.7, 24.4, inf], [35, 35.8, 31.7, 37.400000000000006, 32.0]]
[[12, inf, inf, inf, inf], [16, 16.9, inf, inf, inf], [26.9, 21.9, 22.7, inf, inf], [25, 30.799999999999997, 25.7, 26.4, inf], [31.9, 29.9, 35.599999999999994, 30.4, 31.0]]
shortest path total weight 34.7
line 1 station 1 
line 2 station 2 
line 1 station 3 
line 1 station 4 bonus of 1
line 1 station 5 bonus of 2

@zedchance
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment