Integer Programming
Many decisions are discrete: build a warehouse or not, assign a worker to a shift, choose 3 of 20 projects, route a truck through a city. A mixed-integer linear program (MILP) is an LP in which some variables must be integers, often binary. Integrality makes the problem NP-hard in general, yet modern solvers routinely handle models with hundreds of thousands of variables.
Rounding does not work
The natural idea, solve the LP and round, can give infeasible or very poor answers:
import numpy as np
from scipy.optimize import linprog, milp, LinearConstraint, Bounds
# maximize x + 5y s.t. x + 10y <= 20, x <= 2, x, y >= 0 integer
c = np.array([-1.0, -5.0])
A = np.array([[1.0, 10.0], [1.0, 0.0]]); b = np.array([20.0, 2.0])
lp = linprog(c, A_ub=A, b_ub=b, bounds=[(0, None)] * 2, method="highs")
print("LP relaxation:", np.round(lp.x, 4), " value", -lp.fun)
rounded = np.round(lp.x)
print("rounded:", rounded, " feasible:", bool(np.all(A @ rounded <= b)), " value", float(-c @ rounded))
ip = milp(c, constraints=LinearConstraint(A, ub=b), integrality=[1, 1], bounds=Bounds(0, np.inf))
print("true integer optimum:", ip.x, " value", -ip.fun)LP relaxation: [2. 1.8] value 11.0
rounded: [2. 2.] feasible: False value 12.0
true integer optimum: [0. 2.] value 10.0
The LP optimum rounds to , which violates . The integer optimum is with value 10.
Modelling with binary variables
Binary variables express logic. The key constants are "big-M" bounds that switch constraints on and off.
| Requirement | Constraints |
|---|---|
| choose at most of the options | |
| if then | |
| fixed cost: pay if , with | , cost |
| either or | , |
| takes one of the values | , |
Choose as small as is valid: a huge makes the LP relaxation weak and the solver slow.
Example: facility location
Open warehouses (fixed cost each, limited capacity) to serve customers at minimum total cost:
import numpy as np
from scipy.optimize import milp, LinearConstraint, Bounds
rng = np.random.default_rng(3)
n_w, n_c = 4, 8
fixed = np.array([30.0, 25.0, 40.0, 20.0]); cap = np.array([12.0, 10.0, 15.0, 8.0])
demand = rng.integers(1, 5, size=n_c).astype(float)
ship = rng.uniform(1, 6, size=(n_w, n_c)) # cost per unit shipped
# variables: open_j (binary, 4), flow_jk (continuous, 32)
c = np.r_[fixed, ship.ravel()]
rows, lb, ub = [], [], []
for k in range(n_c): # demand met
r = np.zeros(n_w + n_w * n_c); r[n_w + np.arange(n_w) * n_c + k] = 1
rows.append(r); lb.append(demand[k]); ub.append(demand[k])
for j in range(n_w): # capacity only if open
r = np.zeros(n_w + n_w * n_c); r[n_w + j * n_c: n_w + (j + 1) * n_c] = 1; r[j] = -cap[j]
rows.append(r); lb.append(-np.inf); ub.append(0)
res = milp(c, constraints=LinearConstraint(np.array(rows), lb, ub),
integrality=np.r_[np.ones(n_w), np.zeros(n_w * n_c)], bounds=Bounds(0, np.r_[np.ones(n_w), np.full(n_w * n_c, np.inf)]))
print("total demand:", demand.sum(), " open warehouses:", np.nonzero(res.x[:n_w] > 0.5)[0], " cost:", round(res.fun, 3))total demand: 19.0 open warehouses: [0 3] cost: 92.526
Why integer programs are hard, and why solvers work anyway
Feasible sets of integer programs are not convex, so no local optimality condition certifies a global optimum. Solvers combine three ideas, each the subject of a lesson:
- Relaxation: drop integrality to get an LP whose value bounds the optimum.
- Branching: split the problem into subproblems (Branch and Bound).
- Cutting planes: add inequalities valid for all integer points that tighten the relaxation (Cutting Planes).
Plus presolve, heuristics that find good solutions early, and symmetry handling. A good formulation (tight relaxation, small big-M) often matters more than the solver.
In software
scipy.optimize.milp(HiGHS), OR-Tools (CP-SAT for scheduling-type problems), PuLP with CBC, Pyomo; commercial Gurobi, CPLEX, Xpress.- Typical uses: crew and shift scheduling, vehicle routing, supply-chain network design, unit commitment in power grids, and sports schedules.
Exercises
Exercise 1: Logic constraint
Binary select projects. Model "project 3 can only be chosen if both 1 and 2 are".
Answer
and .
Solution
If either or is 0, is forced to 0; if both are 1, is free.
Exercise 2: Fixed cost
Production costs 3 per unit plus a set-up cost 50 if anything is produced. Write the cost and constraints.
Answer
Cost with , .
Solution
forces and no set-up cost; allows any and charges 50. Minimization never sets with , since that only adds cost.
Exercise 3: A tiny knapsack (code)
Values , weights , capacity 10. Solve with milp and by enumerating all 16 subsets.
Answer
Take the second and fourth items (indices 1 and 3) for a value of 21; both methods agree.
Solution
import numpy as np
from itertools import product
from scipy.optimize import milp, LinearConstraint, Bounds
v = np.array([10.0, 13.0, 7.0, 8.0]); w = np.array([5.0, 6.0, 3.0, 4.0])
res = milp(-v, constraints=LinearConstraint(w, ub=10), integrality=np.ones(4), bounds=Bounds(0, 1))
best = max((float(v @ s), s) for s in product([0, 1], repeat=4) if w @ s <= 10)
print("milp:", res.x.astype(int), -res.fun, " brute force:", best)milp: [0 1 0 1] 21.0 brute force: (21.0, (0, 1, 0, 1))
Test yourself
Why is rounding the LP relaxation's solution unreliable?
- The rounded point may be infeasible or far from optimal
- LPs have no solutions
- Rounding is too slow
- It always works
A fixed cost for producing any (with ) is modelled by…
- and cost with binary
- a quadratic term
- it cannot be modelled
Why should big-M constants be as small as possible?
- Large weakens the LP relaxation and slows the solver
- Large makes the problem infeasible
- Solvers reject
- It does not matter