Dual of a transportation problem: a linear programming example, solved and checked
The dual of a transportation problem is a classic linear 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
Two warehouses hold 30 and 25 units. Two shops need exactly 20 and 30 units. Shipping one unit costs $4 and $6 from warehouse 1 to shops 1 and 2, and $5 and $3 from warehouse 2. Minimize shipping cost. Write the dual problem and find the dual prices of supply and demand.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- xij is the number of units shipped from warehouse i to shop j. The ≤ lines are the warehouses' stock; the = lines are the shops' exact needs.
- A shadow price is the change in the cheapest cost when that line's right-hand side grows by 1 unit. A negative price on a warehouse means one more unit there would lower the cost; a price on a shop is the extra cost of supplying it one more unit.
Assumptions:
- warehouse supplies are upper limits, so not all stock must be shipped
The model
Each line has a plain-language note on the right.
| make as small as possible: total shipping cost (dollars) | |
| limit 1: left side cannot exceed 30 | |
| limit 2: left side cannot exceed 25 | |
| limit 3: left side must equal 20 | |
| limit 4: left side must equal 30 | |
| units shipped from warehouse 1 to shop 1 (units) cannot be negative | |
| units shipped from warehouse 1 to shop 2 (units) cannot be negative | |
| units shipped from warehouse 2 to shop 1 (units) cannot be negative | |
| units shipped from warehouse 2 to shop 2 (units) cannot be negative |
In matrix form
With its numbers:
The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. Strong duality: primal and dual optima are equal (185).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'x11 + x12 <= 30' (objective change per extra unit of its right-hand side) | 0 |
| y2 | price of constraint 'x21 + x22 <= 25' (objective change per extra unit of its right-hand side) | -3 |
| y3 | price of constraint 'x11 + x21 = 20' (objective change per extra unit of its right-hand side) | 4 |
| y4 | price of constraint 'x12 + x22 = 30' (objective change per extra unit of its right-hand side) | 6 |
What is being decided
There are 4 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| x11 | real number | units shipped from warehouse 1 to shop 1 (units) |
| x12 | real number | units shipped from warehouse 1 to shop 2 (units) |
| x21 | real number | units shipped from warehouse 2 to shop 1 (units) |
| x22 | real number | units shipped from warehouse 2 to shop 2 (units) |
Goal and limits
Minimize total shipping cost (dollars) subject to 8 constraints, of which 4 are simple bounds on a single variable.
Problem type
LP, complexity level 2: a linear program. 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): (2) x21 + x22 <= 25; (3) x11 + x21 = 20; (4) x12 + x22 = 30; (7) x21 >= 0. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | x11 + x12 <= 30 | 25 | 30 | 5 | |
| 2 | x21 + x22 <= 25 | 25 | 25 | 0 | yes |
| 3 | x11 + x21 = 20 | 20 | 20 | 0 | yes |
| 4 | x12 + x22 = 30 | 30 | 30 | 0 | yes |
| 5 | x11 >= 0 | 20 | 0 | 20 | |
| 6 | x12 >= 0 | 5 | 0 | 5 | |
| 7 | x21 >= 0 | 0 | 0 | 0 | yes |
| 8 | x22 >= 0 | 25 | 0 | 25 |
Why this is optimal
A proof you can check by hand. Multiply each constraint in the table by its weight and add them up: the result is a lower limit on the objective that holds for every plan meeting the constraints. Our answer reaches that limit exactly, so no plan does better. A negative weight turns a limit around (for example ≥ into ≤) before adding. Every step was checked in exact fractions, not rounded decimals.
| # | Constraint | Weight |
|---|---|---|
| 2 | x21 + x22 ≤ 25 | -3 |
| 3 | x11 + x21 = 20 | 4 |
| 4 | x12 + x22 = 30 | 6 |
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') # units shipped from warehouse 1 to shop 1 (units)
x12 = cp.Variable(name='x12') # units shipped from warehouse 1 to shop 2 (units)
x21 = cp.Variable(name='x21') # units shipped from warehouse 2 to shop 1 (units)
x22 = cp.Variable(name='x22') # units shipped from warehouse 2 to shop 2 (units)
objective = cp.Minimize(4*x11 + 6*x12 + 5*x21 + 3*x22)
constraints = [
x11 + x12 <= 30,
x21 + x22 <= 25,
x11 + x21 == 20,
x12 + x22 == 30,
x11 >= 0,
x12 >= 0,
x21 >= 0,
x22 >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('x11 =', x11.value)
print('x12 =', x12.value)
print('x21 =', x21.value)
print('x22 =', x22.value)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 4*x11 + 6*x12 + 5*x21 + 3*x22
subject to
x11 + x12 <= 30
x21 + x22 <= 25
x11 + x21 = 20
x12 + x22 = 30
x11, x12, x21, x22 >= 0
# x11 = units shipped from warehouse 1 to shop 1 (units)
# x12 = units shipped from warehouse 1 to shop 2 (units)
# x21 = units shipped from warehouse 2 to shop 1 (units)
# x22 = units shipped from warehouse 2 to shop 2 (units)
# objective = total shipping cost (dollars)
# assume: warehouse supplies are upper limits, so not all stock must be shipped
# asked: the dual problem and the dual prices of supply and demandChecked: the app's answer matches the reference answer in our test set, which two solvers (HIGHS, CLARABEL) 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.