Scheduling and Assignment
Scheduling is where optimization meets daily operations: who works which shift, which machine runs which job, which lecture goes in which room. These problems are discrete and usually modelled as integer programs, but some important cases have special structure that makes them easy.
The assignment problem
Assign workers to tasks, one each, minimizing total cost with , each row and column summing to 1. Its constraint matrix is totally unimodular, so the LP relaxation already has integer vertices, and the Hungarian algorithm solves it in :
import numpy as np
from scipy.optimize import linear_sum_assignment, linprog
rng = np.random.default_rng(0)
C = rng.integers(1, 20, size=(5, 5)).astype(float)
rows, cols = linear_sum_assignment(C)
print("cost matrix:\n", C.astype(int))
print("assignment:", list(zip(rows.tolist(), cols.tolist())), " total cost:", C[rows, cols].sum())
# the LP relaxation gives the same, integral answer
n = 5
A_eq = np.vstack([np.kron(np.eye(n), np.ones(n)), np.kron(np.ones(n), np.eye(n))])
lp = linprog(C.ravel(), A_eq=A_eq, b_eq=np.ones(2 * n), bounds=(0, 1), method="highs")
print("LP relaxation value:", lp.fun, " integral:", bool(np.allclose(lp.x, np.round(lp.x))))cost matrix:
[[17 13 10 6 6]
[ 1 2 1 4 16]
[13 18 10 12 19]
[14 13 11 11 18]
[ 6 16 13 1 8]]
assignment: [(0, 4), (1, 0), (2, 2), (3, 1), (4, 3)] total cost: 31.0
LP relaxation value: 31.0 integral: True
Shift scheduling: covering demand
A call centre needs at least staff in each hour ; workers start at any hour and work 8 consecutive hours. Variables = number of workers starting at hour . Minimize total staff s.t. coverage. This is an integer program. If no shift crossed midnight, each column of the coverage matrix would hold one block of consecutive ones (an interval matrix, with the consecutive-ones property), which is totally unimodular, so the LP would solve it exactly. Here shifts wrap around the 24-hour day, so we solve the LP and check the answer:
import numpy as np
from scipy.optimize import linprog
demand = np.array([3, 3, 2, 2, 2, 4, 6, 9, 11, 12, 12, 10, 10, 11, 12, 11, 9, 8, 7, 6, 5, 4, 4, 3]) # 24 hours
A = np.zeros((24, 24))
for s in range(24):
for h in range(8):
A[(s + h) % 24, s] = 1 # a worker starting at s covers hours s..s+7 (wrapping)
res = linprog(np.ones(24), A_ub=-A, b_ub=-demand, bounds=[(0, None)] * 24, method="highs")
x = np.round(res.x).astype(int)
print(f"workers needed: {int(x.sum())} (LP value {res.fun:.3f})")
print("start times with workers:", {s: int(v) for s, v in enumerate(x) if v})
print("over-coverage per hour:", (A @ x - demand).astype(int).tolist())workers needed: 23 (LP value 23.000)
start times with workers: {0: 3, 3: 1, 6: 3, 7: 2, 8: 5, 9: 1, 10: 1, 14: 3, 15: 1, 16: 3}
over-coverage per hour: [0, 0, 1, 2, 2, 0, 1, 0, 0, 0, 1, 2, 2, 1, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0]
(With wrap-around shifts the columns are circular ones, and this matrix is not totally unimodular: the submatrix of start hours 0, 5, 10, 15, 20 and hours 1, 5, 10, 15, 20 is the incidence matrix of an odd cycle, with determinant 2. So integrality is not guaranteed; the LP happened to have an integral optimum for this demand. In general use scipy.optimize.milp with integer variables, or the method of Bartholdi, Orlin and Ratliff (1980), who showed that cyclic staffing with circular-ones columns reduces to a bounded sequence of network-flow problems, or to the LP followed by a special rounding.)
Machine scheduling
Ordering jobs on a single machine to minimize the total (or weighted) completion time has a greedy optimal rule: shortest processing time first (weighted: sort by , Smith's rule). Most other variants (deadlines, multiple machines, setup times) are NP-hard and solved with MILP or constraint programming. A classic MILP formulation uses start times and binary order variables with big-M disjunctions: , .
import numpy as np
from itertools import permutations
p = np.array([4, 2, 7, 1, 3]); w = np.array([1, 3, 2, 2, 1]) # processing times and weights
def weighted_completion(order):
t, total = 0, 0
for j in order:
t += p[j]; total += w[j] * t
return total
smith = np.argsort(p / w)
best = min(permutations(range(5)), key=weighted_completion)
print("Smith's rule order:", smith.tolist(), " cost", weighted_completion(smith))
print("brute force best: ", list(best), " cost", weighted_completion(best))Smith's rule order: [3, 1, 4, 2, 0] cost 60
brute force best: [3, 1, 4, 2, 0] cost 60
Constraint programming
For rich scheduling problems (precedences, resources, calendars, no-overlap), constraint programming solvers such as OR-Tools CP-SAT are often stronger than MILP. They reason directly about intervals and propagate constraints, then search, and modern ones learn conflicts like SAT solvers.
In software
scipy.optimize.linear_sum_assignment(Hungarian/Jonker-Volgenant), OR-Tools (CP-SAT, routing library), Timefold, Gurobi/HiGHS for MILP models.- Used for nurse rostering, airline crew pairing (column generation), sports leagues, cloud job placement and exam timetabling.
Exercises
Exercise 1: Greedy assignment fails
For costs , what does "give each worker in turn their cheapest free task" produce, and what is optimal?
Answer
Greedy: cost . Optimal: .
Solution
Worker 1 grabs task 1 (cost 1), leaving worker 2 the expensive task 2. Swapping gives both workers cost 2.
Exercise 2: SPT rule
Jobs with times 5, 1, 3 on one machine. In which order should they run to minimize total completion time, and what is it?
Answer
1, 3, 5: total completion time .
Solution
Completion times in SPT order are 1, and . Any other order delays more jobs behind the long one.
Exercise 3: Unbalanced assignment (code)
Assign 3 workers to 5 tasks (each worker one task, tasks used at most once) with linear_sum_assignment on a matrix.
Answer
It handles rectangular matrices directly, leaving two tasks unassigned.
Solution
import numpy as np
from scipy.optimize import linear_sum_assignment
C = np.array([[4, 2, 8, 5, 3], [7, 6, 1, 9, 4], [3, 8, 5, 2, 6]])
r, c = linear_sum_assignment(C)
print(list(zip(r.tolist(), c.tolist())), int(C[r, c].sum()))[(0, 1), (1, 2), (2, 3)] 5
Test yourself
Why can the assignment problem be solved as an LP?
- Its constraint matrix is totally unimodular, so LP vertices are integral
- It has no constraints
- Solvers round the result
- It is convex in the integers
To minimize total completion time on one machine, schedule jobs…
- longest first
- shortest processing time first
- in random order
- by arrival time only
Which tool is often strongest for scheduling with intervals, precedences and resources?
- Constraint programming (e.g. CP-SAT)
- Gradient descent
- The simplex method alone
- Newton's method