Skip to content

Commit 8ab1284

Browse files
committed
Put in some visualization of the solution. Seems to be a bug with the idle time computation.
1 parent 20f4bd8 commit 8ab1284

1 file changed

Lines changed: 61 additions & 17 deletions

File tree

flow-shop/flow.py

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import sys, os, time, random
22

33

4+
##############
5+
## Settings ##
6+
##############
7+
TIME_LIMIT = 10
8+
49

510
################
611
## Heuristics ##
@@ -24,41 +29,80 @@ def parse_problem(filename):
2429
return zip(*data)
2530

2631

27-
def makespan(data, njobs, nmach, perm):
32+
def makespan(data, perm):
33+
return compile_solution(data, perm)[-1][-1] + data[perm[-1]][-1]
34+
35+
36+
def compile_solution(data, perm):
37+
38+
nmach = len(data[0])
39+
40+
mach_times = [[] for i in range(nmach)]
2841

29-
mach_time = [0] * nmach
42+
# Assign the initial job to the machines
43+
mach_times[0].append(0)
44+
for mach in range(1,nmach):
45+
mach_times[mach].append(mach_times[mach-1][0] + data[perm[0]][mach-1])
3046

31-
for job in perm:
32-
mach_time[0] += data[job][0]
47+
# Assign the remaining jobs
48+
for i in range(1, len(perm)):
49+
job = perm[i]
50+
mach_times[0].append(mach_times[0][-1] + data[perm[i-1]][0])
3351
for mach in range(1, nmach):
34-
mach_time[mach] = max(mach_time[mach-1], mach_time[mach]) + data[job][mach]
52+
mach_times[mach].append(max(mach_times[mach-1][i] + data[i][mach-1],
53+
mach_times[mach][i-1] + data[perm[i-1]][mach]))
3554

36-
return mach_time[-1]
55+
return mach_times
3756

3857

3958
def solve(data, heuristic = _heur_random):
40-
njobs = len(data)
41-
nmach = len(data[0])
42-
perm = range(njobs)
4359

44-
best_make = makespan(data, njobs, nmach, perm)
60+
perm = range(len(data))
61+
62+
best_make = makespan(data, perm)
4563
best_perm = perm
4664

47-
time_limit = time.time() + 10
65+
time_limit = time.time() + TIME_LIMIT
4866
while time.time() < time_limit:
4967

5068
perm = heuristic(perm)
51-
res = makespan(data, njobs, nmach, perm)
69+
res = makespan(data, perm)
5270

5371
if res < best_make:
5472
best_make = res
5573
best_perm = perm[:]
5674

57-
print (best_perm, best_make)
75+
return (best_perm, best_make)
5876

5977

60-
def print_solution(data, sol):
61-
pass
78+
def print_solution(data, perm):
79+
80+
sol = compile_solution(data, perm)
81+
82+
print "\nPermutation: %s\n" % str([i+1 for i in perm])
83+
84+
print "Makespan: %d\n" % makespan(data, perm)
85+
86+
row_format ="{:>15}" * 4
87+
print row_format.format('Machine', 'Start Time', 'Finish Time', 'Idle Time')
88+
for mach in range(len(data[0])):
89+
finish_time = sol[mach][-1] + data[perm[-1]][-1]
90+
idle_time = (finish_time - sol[mach][0]) - sum([job[mach] for job in data])
91+
print row_format.format(mach+1, sol[mach][0], finish_time, idle_time)
92+
93+
print "\n"
94+
print row_format.format('Job', 'Start Time', 'Finish Time', 'Idle Time')
95+
results = []
96+
for i in range(len(data)):
97+
finish_time = sol[-1][i] + data[perm[i]][-1]
98+
idle_time = (finish_time - sol[0][i]) - sum([time for time in data[perm[i]]])
99+
results.append((perm[i]+1, sol[0][i], finish_time, idle_time))
100+
101+
for r in sorted(results):
102+
print row_format.format(*r)
103+
104+
105+
print "\n\nNote: Idle time does not include initial or final wait time.\n"
62106

63107

64108

@@ -67,5 +111,5 @@ def print_solution(data, sol):
67111

68112
if __name__ == '__main__':
69113
data = parse_problem(sys.argv[1])
70-
sol = solve(data)
71-
print_solution(data, sol)
114+
(perm, ms) = solve(data)
115+
print_solution(data, perm)

0 commit comments

Comments
 (0)