Bin packing problem: an integer programming example, solved and checked
The bin packing problem is a classic integer programming problem. Here it is solved by a real solver (HIGHS) and checked: every constraint, the dual, and the optimality certificate. Change the numbers below and solve your own.
Solve your own version free, no account needed
The problem
Pack items of sizes 4, 8, 1, 4, 2, 1 into bins of capacity 10 using as few bins as possible.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- yj = 1 if bin j is used; xij = 1 if item i goes in bin j.
- The first lines put each item in exactly one bin: for item 1, x11 + x12 + … = 1.
- 4x_11 + 8x_21 + … ≤ 10 y1: the items in bin 1 have total size at most 10, and nothing goes in a bin that is not used.
The model
Each line has a plain-language note on the right.
| make this as small as possible | |
| limit 1: left side must equal 1 | |
| limit 2: left side must equal 1 | |
| limit 3: left side must equal 1 | |
| limit 4: left side must equal 1 | |
| limit 5: left side must equal 1 | |
| limit 6: left side must equal 1 | |
| limit 7: left side cannot exceed the right side | |
| limit 8: left side cannot exceed the right side | |
| limit 9: left side cannot exceed the right side | |
| yes (1) or no (0) |
What is being decided
There are 21 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| x11 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 1, j = 1) |
| x12 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 1, j = 2) |
| x13 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 1, j = 3) |
| x21 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 2, j = 1) |
| x22 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 2, j = 2) |
| x23 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 2, j = 3) |
| x31 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 3, j = 1) |
| x32 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 3, j = 2) |
| x33 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 3, j = 3) |
| x41 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 4, j = 1) |
| x42 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 4, j = 2) |
| x43 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 4, j = 3) |
| x51 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 5, j = 1) |
| x52 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 5, j = 2) |
| x53 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 5, j = 3) |
| x61 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 6, j = 1) |
| x62 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 6, j = 2) |
| x63 | binary (0 or 1) | 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 6, j = 3) |
| y1 | binary (0 or 1) | 1 if bin j is used (yes/no) (j = 1) |
| y2 | binary (0 or 1) | 1 if bin j is used (yes/no) (j = 2) |
| y3 | binary (0 or 1) | 1 if bin j is used (yes/no) (j = 3) |
Goal and limits
Minimize the objective subject to 9 constraints.
Problem type
ILP, complexity level 5: integer variables with linear objective and constraints. Solved with HIGHS at the Medium solver tier.
The answer at a glance
The optimal value of each decision, as the solver returned it.
Which constraints hold the answer in place
Binding (no slack left): all 9 constraints (see the table). Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | x11 + x12 + x13 = 1 | 1 | 1 | 0 | yes |
| 2 | x21 + x22 + x23 = 1 | 1 | 1 | 0 | yes |
| 3 | x31 + x32 + x33 = 1 | 1 | 1 | 0 | yes |
| 4 | x41 + x42 + x43 = 1 | 1 | 1 | 0 | yes |
| 5 | x51 + x52 + x53 = 1 | 1 | 1 | 0 | yes |
| 6 | x61 + x62 + x63 = 1 | 1 | 1 | 0 | yes |
| 7 | 4*x11 + 8*x21 + x31 + 4*x41 + 2*x51 + x61 <= 10*y1 | 10 | 10 | 0 | yes |
| 8 | 4*x12 + 8*x22 + x32 + 4*x42 + 2*x52 + x62 <= 10*y2 | 0 | 0 | 0 | yes |
| 9 | 4*x13 + 8*x23 + x33 + 4*x43 + 2*x53 + x63 <= 10*y3 | 10 | 10 | 0 | yes |
Algebraic model (low level)
Decision variables, objective and every constraint written out; this is exactly what the solver receives.
Standard form
As a linear program: minimize cᵀx subject to A x ≤ b (the inequality rows) and A_eq x = b_eq (the equations); a maximization is written as minimizing the negative.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
x11 = cp.Variable(name='x11', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 1, j = 1)
x12 = cp.Variable(name='x12', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 1, j = 2)
x13 = cp.Variable(name='x13', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 1, j = 3)
x21 = cp.Variable(name='x21', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 2, j = 1)
x22 = cp.Variable(name='x22', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 2, j = 2)
x23 = cp.Variable(name='x23', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 2, j = 3)
x31 = cp.Variable(name='x31', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 3, j = 1)
x32 = cp.Variable(name='x32', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 3, j = 2)
x33 = cp.Variable(name='x33', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 3, j = 3)
x41 = cp.Variable(name='x41', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 4, j = 1)
x42 = cp.Variable(name='x42', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 4, j = 2)
x43 = cp.Variable(name='x43', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 4, j = 3)
x51 = cp.Variable(name='x51', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 5, j = 1)
x52 = cp.Variable(name='x52', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 5, j = 2)
x53 = cp.Variable(name='x53', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 5, j = 3)
x61 = cp.Variable(name='x61', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 6, j = 1)
x62 = cp.Variable(name='x62', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 6, j = 2)
x63 = cp.Variable(name='x63', boolean=True) # 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no) (i = 6, j = 3)
y1 = cp.Variable(name='y1', boolean=True) # 1 if bin j is used (yes/no) (j = 1)
y2 = cp.Variable(name='y2', boolean=True) # 1 if bin j is used (yes/no) (j = 2)
y3 = cp.Variable(name='y3', boolean=True) # 1 if bin j is used (yes/no) (j = 3)
objective = cp.Minimize(y1 + y2 + y3)
constraints = [
x11 + x12 + x13 == 1,
x21 + x22 + x23 == 1,
x31 + x32 + x33 == 1,
x41 + x42 + x43 == 1,
x51 + x52 + x53 == 1,
x61 + x62 + x63 == 1,
4*x11 + 8*x21 + x31 + 4*x41 + 2*x51 + x61 <= 10*y1,
4*x12 + 8*x22 + x32 + 4*x42 + 2*x52 + x62 <= 10*y2,
4*x13 + 8*x23 + x33 + 4*x43 + 2*x53 + x63 <= 10*y3,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('x11 =', x11.value)
print('x12 =', x12.value)
print('x13 =', x13.value)
print('x21 =', x21.value)
print('x22 =', x22.value)
print('x23 =', x23.value)
print('x31 =', x31.value)
print('x32 =', x32.value)
print('x33 =', x33.value)
print('x41 =', x41.value)
print('x42 =', x42.value)
print('x43 =', x43.value)
print('x51 =', x51.value)
print('x52 =', x52.value)
print('x53 =', x53.value)
print('x61 =', x61.value)
print('x62 =', x62.value)
print('x63 =', x63.value)
print('y1 =', y1.value)
print('y2 =', y2.value)
print('y3 =', y3.value)The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. Integer variables are relaxed: this is the dual of the continuous (LP) relaxation, which gives a bound on the integer optimum, not its exact value. Only weak duality: the dual of the continuous relaxation gives an lower bound of 2 on the integer optimum 2, a duality gap of 0. Integer problems generally have a gap.
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'x11 + x12 + x13 = 1' (objective change per extra unit of its right-hand side) | 0.4 |
| y2 | price of constraint 'x21 + x22 + x23 = 1' (objective change per extra unit of its right-hand side) | 0.8 |
| y3 | price of constraint 'x31 + x32 + x33 = 1' (objective change per extra unit of its right-hand side) | 0.1 |
| y4 | price of constraint 'x41 + x42 + x43 = 1' (objective change per extra unit of its right-hand side) | 0.4 |
| y5 | price of constraint 'x51 + x52 + x53 = 1' (objective change per extra unit of its right-hand side) | 0.2 |
| y6 | price of constraint 'x61 + x62 + x63 = 1' (objective change per extra unit of its right-hand side) | 0.1 |
| y7 | price of constraint '4*x11 + 8*x21 + x31 + 4*x41 + 2*x51 + x61 <= 10*y1' (objective change per extra unit of its right-hand side) | -0.1 |
| y8 | price of constraint '4*x12 + 8*x22 + x32 + 4*x42 + 2*x52 + x62 <= 10*y2' (objective change per extra unit of its right-hand side) | -0.1 |
| y9 | price of constraint '4*x13 + 8*x23 + x33 + 4*x43 + 2*x53 + x63 <= 10*y3' (objective change per extra unit of its right-hand side) | -0.1 |
| y10 | price of constraint 'x11 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y11 | price of constraint 'x12 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y12 | price of constraint 'x13 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y13 | price of constraint 'x21 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y14 | price of constraint 'x22 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y15 | price of constraint 'x23 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y16 | price of constraint 'x31 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y17 | price of constraint 'x32 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y18 | price of constraint 'x33 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y19 | price of constraint 'x41 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y20 | price of constraint 'x42 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y21 | price of constraint 'x43 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y22 | price of constraint 'x51 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y23 | price of constraint 'x52 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y24 | price of constraint 'x53 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y25 | price of constraint 'x61 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y26 | price of constraint 'x62 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y27 | price of constraint 'x63 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y28 | price of constraint 'y1 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y29 | price of constraint 'y2 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y30 | price of constraint 'y3 <= 1' (objective change per extra unit of its right-hand side) | 0 |
Solve your own
This is the model as the app reads it. Change any number and solve it in the free box on the front page (no account needed).
minimize y1 + y2 + y3
subject to
x11 + x12 + x13 = 1
x21 + x22 + x23 = 1
x31 + x32 + x33 = 1
x41 + x42 + x43 = 1
x51 + x52 + x53 = 1
x61 + x62 + x63 = 1
4 x11 + 8 x21 + 1 x31 + 4 x41 + 2 x51 + 1 x61 <= 10 y1
4 x12 + 8 x22 + 1 x32 + 4 x42 + 2 x52 + 1 x62 <= 10 y2
4 x13 + 8 x23 + 1 x33 + 4 x43 + 2 x53 + 1 x63 <= 10 y3
binary: x11, x12, x13, x21, x22, x23, x31, x32, x33, x41, x42, x43, x51, x52, x53, x61, x62, x63, y1, y2, y3
# xij = 1 if item i (sizes 4, 8, 1, 4, 2, 1 for i=1..6) is placed in bin j (yes/no)
# yj = 1 if bin j is used (yes/no)Checked: the app's answer matches the reference answer in our test set, which two solvers (HIGHS, SCIP) agree on. Built 2026-11-10.
Related examples
Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.