Job shop scheduling¶
A workshop has several machines and a list of jobs. Each job is a fixed sequence of operations, and each operation needs one machine for a known time. A machine does one operation at a time, and a job does one operation at a time. The goal is the shortest makespan: the time when the last job ends.
Job shop scheduling is one of the hardest classic combinatorial problems. This example solves two well known instances of Fisher and Thompson (1963):
| Instance | Jobs | Machines | Operations | Optimal makespan |
|---|---|---|---|---|
| ft06 | 6 | 6 | 36 | 55 |
| ft10 | 10 | 10 | 100 | 930 (proved by Carlier and Pinson, 1989) |
The data come from OR-Library (J. E. Beasley, MIT license).
The model¶
A solution is an order of all operations: one Permutation variable with one item per operation. Item i belongs to job i % n_jobs, and the k-th item of a job in the order is its k-th operation. So every permutation gives a valid schedule, and no constraints are needed.
The objective builds the schedule by going through the order. Each operation starts as early as possible: after the previous operation of its job, in the first idle gap on its machine that is long enough. Filling the gaps gives much shorter schedules than adding each operation at the end of its machine's queue.
import numpy as np
import ypea
# ft06: each row is one job, as pairs of (machine, processing time) in the order the job needs them
FT06 = """
2 1 0 3 1 6 3 7 5 3 4 6
1 8 2 5 4 10 5 10 0 10 3 4
2 5 3 4 5 8 0 9 1 1 4 7
1 5 0 5 2 5 3 3 4 8 5 9
2 9 1 3 4 5 5 4 0 3 3 1
1 3 3 3 5 9 0 10 4 4 2 1
"""
rows = []
for line in FT06.strip().splitlines():
rows.append(line.split())
rows = np.array(rows, dtype=int)
machines = rows[:, 0::2]
times = rows[:, 1::2]
n_jobs, n_machines = machines.shape
def makespan(sol):
job_ready = np.zeros(n_jobs)
next_op = np.zeros(n_jobs, dtype=int)
busy = [] # for each machine, its (start, end) intervals in time order
for _ in range(n_machines):
busy.append([])
for item in sol.order:
job = item % n_jobs
op = next_op[job]
machine = machines[job, op]
time = times[job, op]
start = job_ready[job]
position = len(busy[machine])
for i, (busy_start, busy_end) in enumerate(busy[machine]):
if start + time <= busy_start: # it fits in the idle gap before this interval
position = i
break
start = max(start, busy_end)
busy[machine].insert(position, (start, start + time))
job_ready[job] = start + time
next_op[job] += 1
return job_ready.max()
problem = ypea.Problem(
ypea.Permutation("order", n=n_jobs * n_machines),
makespan,
name="ft06",
optimum=55,
)
result = ypea.ILS(max_nfe=20_000).solve(problem, seed=1)
print(result.summary())
ILS on ft06
best objective value: 55
error: 0
feasible: True (violation 0)
evaluations: 20000, iterations: 16, time: 2.28 s, stopped by: max_nfe
Differs from Python: MATLAB draws its own random numbers, so the results differ from Python's. Machines and jobs are numbered from 1.
% ft06: each row is one job, as pairs of (machine, processing time) in the order the job needs them
ft06 = [
2 1 0 3 1 6 3 7 5 3 4 6
1 8 2 5 4 10 5 10 0 10 3 4
2 5 3 4 5 8 0 9 1 1 4 7
1 5 0 5 2 5 3 3 4 8 5 9
2 9 1 3 4 5 5 4 0 3 3 1
1 3 3 3 5 9 0 10 4 4 2 1
];
machines = ft06(:, 1:2:end) + 1; % the data number machines from 0
times = ft06(:, 2:2:end);
[n_jobs, n_machines] = size(machines);
problem = ypea.Problem( ...
vars=ypea.Permutation("order", n=n_jobs * n_machines), ...
objective=@(sol) makespan(sol.order, machines, times), ...
name="ft06", ...
optimum=55);
result = ypea.ILS(max_nfe=20000).solve(problem, seed=1);
disp(result.summary())
function span = makespan(order, machines, times)
[n_jobs, n_machines] = size(machines);
job_ready = zeros(1, n_jobs);
next_op = ones(1, n_jobs);
busy = cell(1, n_machines); % for each machine, its [start end] intervals in time order
for m = 1:n_machines
busy{m} = zeros(0, 2);
end
for item = order
job = mod(item - 1, n_jobs) + 1;
op = next_op(job);
machine = machines(job, op);
time = times(job, op);
start = job_ready(job);
intervals = busy{machine};
position = size(intervals, 1) + 1;
for i = 1:size(intervals, 1)
if start + time <= intervals(i, 1) % it fits in the idle gap before this interval
position = i;
break
end
start = max(start, intervals(i, 2));
end
busy{machine} = [intervals(1:position - 1, :); start, start + time; intervals(position:end, :)];
job_ready(job) = start + time;
next_op(job) = op + 1;
end
span = max(job_ready);
end
ILS on ft06
best objective value: 55
error: 0
feasible: true (violation 0)
evaluations: 20000, iterations: 16, time: 15.3 s, stopped by: max_nfe
Iterated local search finds the optimal makespan of 55 in about two seconds. The full script, examples/applications/job_shop.py, also solves ft10 and draws the Gantt chart below.
Results¶
Five algorithms that suit permutations, each with 10 seeded runs: 20 000 evaluations on ft06 and 300 000 on ft10. GA and SA use the native permutation operators (space="native"). The gap is the distance of the best run from the optimum.
| Instance | Algorithm | Best | Mean | Worst | Gap of best |
|---|---|---|---|---|---|
| ft06 | GA | 55 | 55.0 | 55 | 0.0% |
| ft06 | SA | 55 | 55.2 | 57 | 0.0% |
| ft06 | HC | 55 | 55.0 | 55 | 0.0% |
| ft06 | ILS | 55 | 55.0 | 55 | 0.0% |
| ft06 | VNS | 55 | 55.3 | 58 | 0.0% |
| ft10 | GA | 972 | 1010.4 | 1055 | 4.5% |
| ft10 | SA | 947 | 971.2 | 998 | 1.8% |
| ft10 | HC | 966 | 977.9 | 985 | 3.9% |
| ft10 | ILS | 945 | 964.1 | 978 | 1.6% |
| ft10 | VNS | 956 | 967.9 | 981 | 2.8% |
All five find the optimum of ft06, most of them in every run. ft10 is much harder. ILS does best, with a best run 1.6 percent above the optimum and an average about 4 percent above it, followed by SA and VNS. The local search methods (ILS, VNS, hill climbing) work well here because a small change to the order, such as swapping two operations, usually changes the schedule only a little. For the last few percent, specialized methods (tabu search on the critical path, constraint programming) are stronger: ypea is a general toolbox, and this example shows how far the general methods get.
Try it on your own data¶
Replace the rows of the data with your jobs: one row per job, pairs of (machine, time) in the order the job needs them, with machines numbered from 0. Every job must use every machine once. For a job that skips machines, give those operations a time of 0.