TExcelTAD
Home › Optimization › Integer Programming

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.

min⁡ cTxs.t.Ax≤b,xj∈Z for j∈I\min\ \mathbf{c}^T\mathbf{x} \quad \text{s.t.} \quad A\mathbf{x} \le \mathbf{b}, \quad x_j \in \mathbb{Z} \ \text{for } j \in I

Rounding does not work

The natural idea, solve the LP and round, can give infeasible or very poor answers:

python · try it yourself
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 (2,1.8)(2, 1.8) rounds to (2,2)(2, 2), which violates x+10y≤20x + 10y \le 20. The integer optimum is (0,2)(0, 2) with value 10.

Modelling with binary variables

Binary variables z∈{0,1}z \in \{0, 1\} express logic. The key constants are "big-M" bounds that switch constraints on and off.

Requirement Constraints
choose at most kk of the options ∑izi≤k\sum_iz_i \le k
if z1=1z_1 = 1 then z2=1z_2 = 1 z1≤z2z_1 \le z_2
fixed cost: pay FF if x>0x > 0, with x≤Ux \le U x≤Uzx \le Uz, cost FzFz
either aTx≤b\mathbf{a}^T\mathbf{x} \le b or dTx≤e\mathbf{d}^T\mathbf{x} \le e aTx≤b+Mz\mathbf{a}^T\mathbf{x} \le b + Mz, dTx≤e+M(1−z)\mathbf{d}^T\mathbf{x} \le e + M(1 - z)
xx takes one of the values v1,…,vkv_1, \dots, v_k x=∑ivizix = \sum_iv_iz_i, ∑izi=1\sum_iz_i = 1

Choose MM as small as is valid: a huge MM 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:

python · try it yourself
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:

  1. Relaxation: drop integrality to get an LP whose value bounds the optimum.
  2. Branching: split the problem into subproblems (Branch and Bound).
  3. 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 z1,z2,z3z_1, z_2, z_3 select projects. Model "project 3 can only be chosen if both 1 and 2 are".

Answer

z3≤z1z_3 \le z_1 and z3≤z2z_3 \le z_2.

Solution

If either z1z_1 or z2z_2 is 0, z3z_3 is forced to 0; if both are 1, z3z_3 is free.

Exercise 2: Fixed cost

Production x∈[0,100]x \in [0, 100] costs 3 per unit plus a set-up cost 50 if anything is produced. Write the cost and constraints.

Answer

Cost 3x+50z3x + 50z with x≤100zx \le 100z, z∈{0,1}z \in \{0, 1\}.

Solution

z=0z = 0 forces x=0x = 0 and no set-up cost; z=1z = 1 allows any x≤100x \le 100 and charges 50. Minimization never sets z=1z = 1 with x=0x = 0, since that only adds cost.

Exercise 3: A tiny knapsack (code)

Values (10,13,7,8)(10, 13, 7, 8), weights (5,6,3,4)(5, 6, 3, 4), 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
python · try it yourself
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?

  1. The rounded point may be infeasible or far from optimal
  2. LPs have no solutions
  3. Rounding is too slow
  4. It always works

A fixed cost FF for producing any x>0x > 0 (with x≤Ux \le U) is modelled by…

  1. x≤Uzx \le Uz and cost FzFz with binary zz
  2. x≥Fx \ge F
  3. a quadratic term
  4. it cannot be modelled

Why should big-M constants be as small as possible?

  1. Large MM weakens the LP relaxation and slows the solver
  2. Large MM makes the problem infeasible
  3. Solvers reject M>100M > 100
  4. It does not matter