Project scheduling¶
A project is a set of activities, each with a known duration. Some activities must wait until others have ended: the walls go up only after the foundation is done. Each activity also needs resources while it runs, such as workers or machines, and each resource has a fixed capacity per day. The goal is the shortest makespan: the day when the last activity ends. This is the resource-constrained project scheduling problem (RCPSP). Building projects, product development, software releases and plant shutdowns for maintenance all look like this.
This example solves instance j6021_4 of PSPLIB, the standard benchmark library of Kolisch and Sprecher (1996): 60 activities and 4 resources with capacities of 13, 15, 15 and 14 units per day.
| Instance | Activities | Resources | Critical path | Optimal makespan |
|---|---|---|---|---|
| j6021_4 | 60 | 4 | 64 days | 95 days |
The critical path is the length of the project with precedence only, as if resources were unlimited. The resources stretch the project by almost half. PSPLIB lists 95 both as the best known makespan and as a lower bound (found by Schutt, Feydy, Stuckey and Wallace), so 95 is optimal. The RCPLIB best known solutions of Vanhoucke and Coelho give the same value. The data come from PSPLIB (file j6021_4.sm of the j60 set).
The model¶
A solution is an activity list: one Permutation of the 60 activities. The objective turns it into a schedule with the serial schedule generation scheme (Kolisch 1996). It schedules the activities one at a time. The next one is the first activity in the list whose predecessors are all scheduled, and it starts on the earliest day when its predecessors have ended and enough of every resource is free for its whole duration. So every list gives a feasible schedule: precedence and resource limits hold by construction, and no constraints are needed. Some list always leads to an optimal schedule.
Two additions, used in many of the best heuristics for this problem, make the search stronger:
- Forward and backward improvement (Li and Willis 1992, Valls, BallestÃn and Quintanilla 2005). After the first pass, the objective schedules the activities again from the end, the latest finish first, which moves each one as late as possible. Then it schedules them from the start once more, the earliest start first. This never makes a schedule longer, and on random lists it made 1994 of 2000 shorter.
- A good start. Instead of random lists, the first population holds 100 lists from biased random sampling (Kolisch 1995). Each list is built one activity at a time, and an activity with an early latest finish time (from the critical path) is more likely to come next. GA then recombines these lists.
Classical methods do not do well here. Six classic priority rules (latest finish first, least slack first and others) give 109 to 116 days with the same improvement passes. A time-indexed integer program with an open source MILP solver (HiGHS) found no schedule better than 109 days in two minutes, with a lower bound of only 81. Methods made for this problem proved the optimum of 95, with constraint programming built for scheduling.
import heapq
import numpy as np
import ypea
# PSPLIB j6021_4, one row per activity: number, duration (days), units needed of
# resources 1 to 4, successors. Activities 1 and 62 mark the start and the end.
J6021_4 = """
1 0 0 0 0 0 2 3 4
2 3 7 0 0 0 5 9 11
3 5 0 0 10 6 6 29 32
4 8 8 0 7 0 13 21 33
5 5 2 0 0 2 12 22
6 7 0 10 10 0 7 16 17
7 1 0 0 4 1 8 10 23
8 8 0 2 0 5 14 36
9 2 1 0 0 0 48
10 3 0 10 0 0 60
11 7 0 2 10 5 33
12 9 0 0 4 4 14
13 8 0 7 10 2 15 19
14 10 9 0 0 0 27 30
15 1 5 0 5 0 20 23 40
16 7 0 6 8 0 22 37 46
17 4 3 4 0 0 18 39 49
18 3 8 10 1 9 27 31
19 2 0 0 0 3 27 34
20 7 5 8 0 4 22 47
21 6 0 0 0 9 41
22 8 0 0 0 4 43 45
23 3 0 0 6 0 24 28 54
24 7 3 0 0 0 25 26 31
25 7 0 4 0 10 43 57
26 7 0 0 3 6 44 45 55
27 5 5 6 5 0 37 46 53
28 4 10 0 0 3 39
29 1 0 10 8 0 38
30 5 0 4 0 6 35 38 49
31 2 4 8 4 3 43
32 6 10 4 2 0 52
33 10 0 0 7 5 35
34 3 4 10 0 0 42 59
35 1 7 0 0 5 53
36 6 0 6 2 0 37 38 50
37 2 0 1 3 0 40 54
38 8 7 0 0 10 54
39 7 10 0 10 0 48 51
40 1 8 8 0 5 41 45 47
41 1 0 1 5 8 42 51 55
42 2 0 0 0 8 52
43 4 0 3 0 0 44
44 7 0 0 2 0 48 53
45 4 0 0 3 2 51 56
46 3 5 0 10 10 50 56
47 3 6 10 10 0 58
48 7 9 0 0 4 50
49 4 0 0 9 6 58
50 1 2 0 0 8 61
51 7 4 0 5 1 61
52 3 3 0 0 10 56
53 1 0 3 0 8 59
54 9 0 7 0 0 60
55 8 0 0 4 6 57 59
56 3 0 0 0 2 57 58
57 9 1 0 0 3 61
58 3 0 8 0 8 60
59 1 0 2 0 1 62
60 10 0 2 0 2 62
61 7 0 6 6 6 62
62 0 0 0 0 0
"""
capacity = [13, 15, 15, 14]
duration = []
demand = []
successors = []
for line in J6021_4.strip().splitlines():
numbers = line.split()
duration.append(int(numbers[1]))
demand.append([int(numbers[2]), int(numbers[3]), int(numbers[4]), int(numbers[5])])
after = []
for number in numbers[6:]:
after.append(int(number) - 1) # activity k is index k - 1
successors.append(after)
n = len(duration)
predecessors = []
for _ in range(n):
predecessors.append([])
for j in range(n):
for k in successors[j]:
predecessors[k].append(j)
def serial_sgs(priority, before, after):
"""Schedule the activities one at a time, the eligible one with the lowest priority first,
each as early as the activities before it and the free resources allow."""
free = [] # free[r][day]: units of resource r still free on that day
for units in capacity:
free.append([units] * sum(duration))
start = [0] * n
waiting = []
eligible = [] # a heap of (priority, activity) whose activities before are all scheduled
for j in range(n):
waiting.append(len(before[j]))
if waiting[j] == 0:
heapq.heappush(eligible, (priority[j], j))
while eligible:
_, j = heapq.heappop(eligible)
t = 0
for i in before[j]:
t = max(t, start[i] + duration[i])
day = t
while day < t + duration[j]: # check each day, and start again after a shortage
fits = True
for r in range(len(free)):
if demand[j][r] > free[r][day]:
fits = False
break
if fits:
day += 1
else:
t = day + 1
day = t
for r in range(len(free)):
for day in range(t, t + duration[j]):
free[r][day] -= demand[j][r]
start[j] = t
for k in after[j]:
waiting[k] -= 1
if waiting[k] == 0:
heapq.heappush(eligible, (priority[k], k))
return start
def schedule(order):
"""Start times from an order of the 60 activities, then one forward and backward improvement."""
priority = [0] * n
for position, item in enumerate(order):
priority[item + 1] = position + 1 # item i is activity i + 2
priority[n - 1] = n
start = serial_sgs(priority, predecessors, successors)
latest_finish_first = [] # shift right: schedule backward, the latest finish first
for j in range(n):
latest_finish_first.append(-(start[j] + duration[j]))
reversed_start = serial_sgs(latest_finish_first, successors, predecessors)
end = max(reversed_start)
earliest_start_first = [] # shift left again: the earliest start first
for j in range(n):
earliest_start_first.append(end - reversed_start[j] - duration[j])
return serial_sgs(earliest_start_first, predecessors, successors)
def makespan(sol):
start = schedule(sol.order.tolist())
return start[-1] # the start of the end activity
# latest finish times with the critical path as the deadline
earliest_start = [0] * n
for j in range(n):
for i in predecessors[j]:
earliest_start[j] = max(earliest_start[j], earliest_start[i] + duration[i])
latest_finish = [earliest_start[-1]] * n
for j in range(n - 1, -1, -1):
for k in successors[j]:
latest_finish[j] = min(latest_finish[j], latest_finish[k] - duration[k])
def sampled_orders(count, seed):
"""Random orders that tend to put activities with an early latest finish first."""
rng = np.random.default_rng(seed)
orders = []
for _ in range(count):
waiting = []
for j in range(n):
waiting.append(len(predecessors[j]))
candidates = [0]
order = []
while candidates:
weights = []
for j in candidates:
weights.append(latest_finish[j])
weights = max(weights) - np.array(weights) + 1
weights = weights / weights.sum()
j = candidates.pop(rng.choice(len(candidates), p=weights))
if 0 < j < n - 1:
order.append(j - 1)
for k in successors[j]:
waiting[k] -= 1
if waiting[k] == 0:
candidates.append(k)
orders.append({"order": np.array(order)})
return orders
problem = ypea.Problem(
ypea.Permutation("order", n=n - 2),
makespan,
name="j6021_4",
optimum=95,
)
start = sampled_orders(100, seed=0)
alg = ypea.GA(space="native", x0=start, max_nfe=20_000)
result = alg.solve(problem, seed=3)
print(result.summary())
GA on j6021_4
best objective value: 96
error: 1
feasible: True (violation 0)
evaluations: 20000, iterations: 199, time: 44.5 s, stopped by: max_nfe
Differs from Python: MATLAB draws its own random numbers, so the 100 starting orders and the result differ from Python's. Activities are numbered from 1.
% PSPLIB j6021_4, one row per activity: number, duration (days), units needed of
% resources 1 to 4, successors. Activities 1 and 62 mark the start and the end.
j6021_4 = [
" 1 0 0 0 0 0 2 3 4"
" 2 3 7 0 0 0 5 9 11"
" 3 5 0 0 10 6 6 29 32"
" 4 8 8 0 7 0 13 21 33"
" 5 5 2 0 0 2 12 22"
" 6 7 0 10 10 0 7 16 17"
" 7 1 0 0 4 1 8 10 23"
" 8 8 0 2 0 5 14 36"
" 9 2 1 0 0 0 48"
" 10 3 0 10 0 0 60"
" 11 7 0 2 10 5 33"
" 12 9 0 0 4 4 14"
" 13 8 0 7 10 2 15 19"
" 14 10 9 0 0 0 27 30"
" 15 1 5 0 5 0 20 23 40"
" 16 7 0 6 8 0 22 37 46"
" 17 4 3 4 0 0 18 39 49"
" 18 3 8 10 1 9 27 31"
" 19 2 0 0 0 3 27 34"
" 20 7 5 8 0 4 22 47"
" 21 6 0 0 0 9 41"
" 22 8 0 0 0 4 43 45"
" 23 3 0 0 6 0 24 28 54"
" 24 7 3 0 0 0 25 26 31"
" 25 7 0 4 0 10 43 57"
" 26 7 0 0 3 6 44 45 55"
" 27 5 5 6 5 0 37 46 53"
" 28 4 10 0 0 3 39"
" 29 1 0 10 8 0 38"
" 30 5 0 4 0 6 35 38 49"
" 31 2 4 8 4 3 43"
" 32 6 10 4 2 0 52"
" 33 10 0 0 7 5 35"
" 34 3 4 10 0 0 42 59"
" 35 1 7 0 0 5 53"
" 36 6 0 6 2 0 37 38 50"
" 37 2 0 1 3 0 40 54"
" 38 8 7 0 0 10 54"
" 39 7 10 0 10 0 48 51"
" 40 1 8 8 0 5 41 45 47"
" 41 1 0 1 5 8 42 51 55"
" 42 2 0 0 0 8 52"
" 43 4 0 3 0 0 44"
" 44 7 0 0 2 0 48 53"
" 45 4 0 0 3 2 51 56"
" 46 3 5 0 10 10 50 56"
" 47 3 6 10 10 0 58"
" 48 7 9 0 0 4 50"
" 49 4 0 0 9 6 58"
" 50 1 2 0 0 8 61"
" 51 7 4 0 5 1 61"
" 52 3 3 0 0 10 56"
" 53 1 0 3 0 8 59"
" 54 9 0 7 0 0 60"
" 55 8 0 0 4 6 57 59"
" 56 3 0 0 0 2 57 58"
" 57 9 1 0 0 3 61"
" 58 3 0 8 0 8 60"
" 59 1 0 2 0 1 62"
" 60 10 0 2 0 2 62"
" 61 7 0 6 6 6 62"
" 62 0 0 0 0 0"
];
capacity = [13 15 15 14];
n = numel(j6021_4);
duration = zeros(1, n);
demand = zeros(n, 4);
successors = cell(1, n);
for j = 1:n
numbers = sscanf(char(j6021_4(j)), "%d")';
duration(j) = numbers(2);
demand(j, :) = numbers(3:6);
successors{j} = numbers(7:end);
end
predecessors = cell(1, n);
for j = 1:n
predecessors{j} = [];
end
for j = 1:n
for k = successors{j}
predecessors{k}(end + 1) = j;
end
end
project = struct("duration", duration, "demand", demand, "capacity", capacity);
project.predecessors = predecessors;
project.successors = successors;
% latest finish times with the critical path as the deadline
earliest_start = zeros(1, n);
for j = 1:n
for i = predecessors{j}
earliest_start(j) = max(earliest_start(j), earliest_start(i) + duration(i));
end
end
latest_finish = repmat(earliest_start(n), 1, n);
for j = n:-1:1
for k = successors{j}
latest_finish(j) = min(latest_finish(j), latest_finish(k) - duration(k));
end
end
problem = ypea.Problem( ...
vars=ypea.Permutation("order", n=n - 2), ...
objective=@(sol) makespan(sol.order, project), ...
name="j6021_4", ...
optimum=95);
start = sampled_orders(100, 0, project, latest_finish);
alg = ypea.GA(space="native", x0=start, max_nfe=20000);
result = alg.solve(problem, seed=3);
disp(result.summary())
function span = makespan(order, project)
start = schedule(order, project);
span = start(end); % the start of the end activity
end
function start = schedule(order, project)
% Start times from an order of the 60 activities, then one forward and backward improvement.
n = numel(project.duration);
priority = zeros(1, n);
for position = 1:numel(order)
priority(order(position) + 1) = position; % item i is activity i + 1
end
priority(n) = n;
start = serial_sgs(priority, project.predecessors, project.successors, project);
% shift right: schedule backward, the latest finish first
latest_finish_first = -(start + project.duration);
reversed_start = serial_sgs(latest_finish_first, project.successors, project.predecessors, project);
% shift left again: the earliest start first
earliest_start_first = max(reversed_start) - reversed_start - project.duration;
start = serial_sgs(earliest_start_first, project.predecessors, project.successors, project);
end
function start = serial_sgs(priority, before, after, project)
% Schedule the activities one at a time, the eligible one with the lowest priority first,
% each as early as the activities before it and the free resources allow.
duration = project.duration;
n = numel(duration);
free = repmat(project.capacity(:), 1, sum(duration)); % free(r, day + 1): units of resource r still free on that day
start = zeros(1, n);
waiting = zeros(1, n);
eligible = []; % activities whose activities before are all scheduled
for j = 1:n
waiting(j) = numel(before{j});
if waiting(j) == 0
eligible(end + 1) = j;
end
end
while ~isempty(eligible)
% the lowest priority first, and the lower number on a tie
pick = 1;
for i = 2:numel(eligible)
smaller = priority(eligible(i)) < priority(eligible(pick));
tie = priority(eligible(i)) == priority(eligible(pick)) && eligible(i) < eligible(pick);
if smaller || tie
pick = i;
end
end
j = eligible(pick);
eligible(pick) = [];
t = 0;
for i = before{j}
t = max(t, start(i) + duration(i));
end
day = t;
while day < t + duration(j) % check each day, and start again after a shortage
if all(project.demand(j, :)' <= free(:, day + 1))
day = day + 1;
else
t = day + 1;
day = t;
end
end
days = t + 1:t + duration(j);
free(:, days) = free(:, days) - project.demand(j, :)';
start(j) = t;
for k = after{j}
waiting(k) = waiting(k) - 1;
if waiting(k) == 0
eligible(end + 1) = k;
end
end
end
end
function orders = sampled_orders(count, seed, project, latest_finish)
% Random orders that tend to put activities with an early latest finish first.
generator = ypea.Rng(seed);
n = numel(project.duration);
orders = cell(1, count);
for c = 1:count
waiting = zeros(1, n);
for j = 1:n
waiting(j) = numel(project.predecessors{j});
end
candidates = 1;
order = [];
while ~isempty(candidates)
weights = latest_finish(candidates);
weights = max(weights) - weights + 1;
weights = weights / sum(weights);
pick = generator.choice(numel(candidates), 1, weights);
j = candidates(pick);
candidates(pick) = [];
if j > 1 && j < n
order(end + 1) = j - 1;
end
for k = project.successors{j}
waiting(k) = waiting(k) - 1;
if waiting(k) == 0
candidates(end + 1) = k;
end
end
end
orders{c} = struct("order", order);
end
end
GA on j6021_4
best objective value: 98
error: 3
feasible: true (violation 0)
evaluations: 20000, iterations: 199, time: 98.6 s, stopped by: max_nfe
The genetic algorithm finds a schedule of 96 days, 1.1 percent above the optimum. The full script, examples/applications/project_scheduling.py, holds the same model and draws the resource use of the schedule.
Results¶
Four setups, each with 10 seeded runs of 20 000 evaluations (about a minute a run). GA and SA use the native permutation operators (space="native"). "Sampled start" means the 100 sampled lists above as the first population, and their evaluations count in the budget. The other setups start from random lists. The gap is the distance of the best run from the optimum.
| Algorithm | Best | Mean | Worst | Runs within 3% | Gap of best |
|---|---|---|---|---|---|
| GA, sampled start | 95 | 97.4 | 99 | 4 of 10 | 0.0% |
| GA | 96 | 99.5 | 101 | 1 of 10 | 1.1% |
| SA | 95 | 99.0 | 102 | 1 of 10 | 0.0% |
| ILS | 95 | 99.3 | 102 | 1 of 10 | 0.0% |
GA from sampled lists does best: on average 97.4 days, 2.5 percent above the optimum, within 3 percent in 4 of 10 runs, and its best run found the optimum of 95. SA and ILS from random lists found the optimum once each in 10 runs, but like GA from random lists, they average 99 to 99.5 days.
On the same model and decoder, the classical methods fall short:
- the best of six priority rules (latest start first): 109 days, 14.7 percent above the optimum,
- biased random sampling alone, 20 000 lists per run: 98 or 99 days in 5 runs, 3.2 percent above at best,
- a time-indexed MILP with HiGHS: after 2 minutes, no schedule shorter than the 109 days of the best rule, and a lower bound of 81.
Sampling knows which activities are urgent, and the genetic algorithm recombines the good parts of many sampled lists. Together they are better than either one alone.
Try it on your own project¶
Replace the rows of the data with your activities: number, duration, the units of each resource it needs, and its successors. Keep a start activity first and an end activity last, both with duration 0, and number the activities so that each one comes after its predecessors (latest_finish relies on it). For another number of resources, change the capacity list and the line that reads the demands.
Durations are whole days, because the schedule keeps the free units of each resource per day. For hours, use whole hours. Other goals fit in the objective, because it has the start time of every activity: for example, the total delay past due dates.
Start from sampled lists. On this instance they save about two days on average against random lists, at a cost of 100 evaluations. A run of 20 000 evaluations takes about a minute for 60 activities, because the decoder is plain Python. For larger projects, give the search more evaluations.