∇ProvenDecisions
Ctrl K

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.

Answer. The smallest objective value is 2, at x11 = 1, x12 = 0, x13 = 0, x21 = 0, x22 = 0, x23 = 1, x31 = 1, x32 = 0, x33 = 0, x41 = 1, x42 = 0, x43 = 0, x51 = 0, x52 = 0, x53 = 1, x61 = 1, x62 = 0, x63 = 0, y1 = 1, y2 = 0, y3 = 1. All 9 constraints are satisfied exactly. Optimal within gap tolerance.

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:

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.

VariableTypeMeaning
x11binary (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)
x12binary (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)
x13binary (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)
x21binary (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)
x22binary (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)
x23binary (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)
x31binary (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)
x32binary (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)
x33binary (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)
x41binary (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)
x42binary (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)
x43binary (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)
x51binary (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)
x52binary (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)
x53binary (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)
x61binary (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)
x62binary (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)
x63binary (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)
y1binary (0 or 1)1 if bin j is used (yes/no) (j = 1)
y2binary (0 or 1)1 if bin j is used (yes/no) (j = 2)
y3binary (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.

x111x120x130x210x220x231x311x320x330x411x420x430x510x520x531x611x620x630y11y20y31

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.

#ConstraintValueLimitSlackBinding
1x11 + x12 + x13 = 1110yes
2x21 + x22 + x23 = 1110yes
3x31 + x32 + x33 = 1110yes
4x41 + x42 + x43 = 1110yes
5x51 + x52 + x53 = 1110yes
6x61 + x62 + x63 = 1110yes
74*x11 + 8*x21 + x31 + 4*x41 + 2*x51 + x61 <= 10*y110100yes
84*x12 + 8*x22 + x32 + 4*x42 + 2*x52 + x62 <= 10*y2000yes
94*x13 + 8*x23 + x33 + 4*x43 + 2*x53 + x63 <= 10*y310100yes

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 variablePricesValue
y1price of constraint 'x11 + x12 + x13 = 1' (objective change per extra unit of its right-hand side)0.4
y2price of constraint 'x21 + x22 + x23 = 1' (objective change per extra unit of its right-hand side)0.8
y3price of constraint 'x31 + x32 + x33 = 1' (objective change per extra unit of its right-hand side)0.1
y4price of constraint 'x41 + x42 + x43 = 1' (objective change per extra unit of its right-hand side)0.4
y5price of constraint 'x51 + x52 + x53 = 1' (objective change per extra unit of its right-hand side)0.2
y6price of constraint 'x61 + x62 + x63 = 1' (objective change per extra unit of its right-hand side)0.1
y7price 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
y8price 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
y9price 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
y10price of constraint 'x11 <= 1' (objective change per extra unit of its right-hand side)0
y11price of constraint 'x12 <= 1' (objective change per extra unit of its right-hand side)0
y12price of constraint 'x13 <= 1' (objective change per extra unit of its right-hand side)0
y13price of constraint 'x21 <= 1' (objective change per extra unit of its right-hand side)0
y14price of constraint 'x22 <= 1' (objective change per extra unit of its right-hand side)0
y15price of constraint 'x23 <= 1' (objective change per extra unit of its right-hand side)0
y16price of constraint 'x31 <= 1' (objective change per extra unit of its right-hand side)0
y17price of constraint 'x32 <= 1' (objective change per extra unit of its right-hand side)0
y18price of constraint 'x33 <= 1' (objective change per extra unit of its right-hand side)0
y19price of constraint 'x41 <= 1' (objective change per extra unit of its right-hand side)0
y20price of constraint 'x42 <= 1' (objective change per extra unit of its right-hand side)0
y21price of constraint 'x43 <= 1' (objective change per extra unit of its right-hand side)0
y22price of constraint 'x51 <= 1' (objective change per extra unit of its right-hand side)0
y23price of constraint 'x52 <= 1' (objective change per extra unit of its right-hand side)0
y24price of constraint 'x53 <= 1' (objective change per extra unit of its right-hand side)0
y25price of constraint 'x61 <= 1' (objective change per extra unit of its right-hand side)0
y26price of constraint 'x62 <= 1' (objective change per extra unit of its right-hand side)0
y27price of constraint 'x63 <= 1' (objective change per extra unit of its right-hand side)0
y28price of constraint 'y1 <= 1' (objective change per extra unit of its right-hand side)0
y29price of constraint 'y2 <= 1' (objective change per extra unit of its right-hand side)0
y30price 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)

Change the numbers and solve it

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

All 50 solved examples

Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.

What do you want to optimize?

Type the math, describe it in words, snap a photo or say it. ProvenDecisions builds the model, solves it, checks it and walks you through it.

One constraint per line, or separated by commas. Add integer: x, y or binary: z for whole-number decisions. LaTeX works too.

Try: