∇ProvenDecisions
Ctrl K

Transportation problem in Python: a linear programming example, solved and checked

Articles · 2026-11-24

Transportation problem in Python: a linear programming example, solved and checked

The transportation problem is a classic linear program (LP). You have places that can supply goods, places that need goods, and a price per unit for every route. You want the cheapest shipping plan. This article builds a small one, solves it in Python, and then checks the answer two ways: by testing every rule, and by proving that no cheaper plan exists.

The problem

Two plants can ship 20 and 30 units. Three markets need 10, 25 and 15 units. Shipping one unit costs the following, in dollars:

From \ To Market A Market B Market C Capacity (units)
Plant 1 $8 $6 $10 20
Plant 2 $9 $12 $13 30
Demand (units) 10 25 15

Find the shipping plan with the lowest total cost in dollars.

Total capacity is 20 + 30 = 50 units. Total demand is 10 + 25 + 15 = 50 units. Supply and demand match exactly, so every plant will ship everything it can, and every market will get exactly what it asks for. This matters later.

The model in words

The model in math

Write x1A for the units shipped from plant 1 to market A, and so on.

minimize 8*x1A + 6*x1B + 10*x1C + 9*x2A + 12*x2B + 13*x2C
# total cost in dollars: units on each route times the dollars per unit

subject to
x1A + x1B + x1C <= 20   # plant 1 ships at most 20 units
x2A + x2B + x2C <= 30   # plant 2 ships at most 30 units
x1A + x2A >= 10         # market A receives at least 10 units
x1B + x2B >= 25         # market B receives at least 25 units
x1C + x2C >= 15         # market C receives at least 15 units
x1A, x1B, x1C, x2A, x2B, x2C >= 0   # no negative shipments

That is 5 rules plus 6 non-negativity rules, 11 constraints in all. Everything is a straight line (no squares, no products of variables), which is what makes it an LP.

The Python code

This uses cvxpy with the HiGHS solver. It builds exactly the model above.

import cvxpy as cp

# Units shipped on each route (units), all must be >= 0
x1A = cp.Variable(nonneg=True)  # plant 1 -> market A
x1B = cp.Variable(nonneg=True)  # plant 1 -> market B
x1C = cp.Variable(nonneg=True)  # plant 1 -> market C
x2A = cp.Variable(nonneg=True)  # plant 2 -> market A
x2B = cp.Variable(nonneg=True)  # plant 2 -> market B
x2C = cp.Variable(nonneg=True)  # plant 2 -> market C

# Total shipping cost in dollars
cost = 8*x1A + 6*x1B + 10*x1C + 9*x2A + 12*x2B + 13*x2C

constraints = [
    x1A + x1B + x1C <= 20,  # plant 1 capacity (units)
    x2A + x2B + x2C <= 30,  # plant 2 capacity (units)
    x1A + x2A >= 10,        # market A demand (units)
    x1B + x2B >= 25,        # market B demand (units)
    x1C + x2C >= 15,        # market C demand (units)
]

prob = cp.Problem(cp.Minimize(cost), constraints)
prob.solve(solver=cp.HIGHS)

print(prob.status, prob.value)

The solver reports status optimal and a value of 465.0.

The answer

The cheapest plan costs $465 in total.

Route Units shipped
Plant 1 to A 0
Plant 1 to B 20
Plant 1 to C 0
Plant 2 to A 10
Plant 2 to B 5
Plant 2 to C 15

In words: plant 1 sends all 20 of its units to market B, because B is where plant 1 has its best price ($6 per unit). Plant 2 covers all of A (10 units), the remaining 5 units of B, and all of C (15 units).

Cost by route, in dollars:

20 units * $6  = $120   # plant 1 to B
10 units * $9  = $90    # plant 2 to A
 5 units * $12 = $60    # plant 2 to B
15 units * $13 = $195   # plant 2 to C
120 + 90 + 60 + 195 = $465   # total shipping cost

How to check it

A solver says "optimal", but you should not have to take that on faith. Two checks are enough: the plan obeys every rule, and no cheaper plan can exist.

Check 1: every rule holds

x1A + x1B + x1C = 0 + 20 + 0  = 20 units <= 20   # plant 1: OK (full)
x2A + x2B + x2C = 10 + 5 + 15 = 30 units <= 30   # plant 2: OK (full)
x1A + x2A = 0 + 10  = 10 units >= 10             # market A: OK (exact)
x1B + x2B = 20 + 5  = 25 units >= 25             # market B: OK (exact)
x1C + x2C = 0 + 15  = 15 units >= 15             # market C: OK (exact)
all six values are 0, 20, 0, 10, 5, 15           # none is negative: OK

All 11 constraints hold, and the largest violation is 0. Because total supply equals total demand (50 units), all five main rules are exactly tight.

Check 2: the dual proves optimality

Passing the rules shows the plan is feasible, not that it is the best. For an LP, the proof comes from the dual problem. The idea is to put a price on each rule. If those prices give a lower bound on cost that equals the cost of our plan, nothing cheaper can exist.

Here is a set of dual values that works. Let a1, a2 be prices on the plant capacity rules (dollars per unit of capacity), and pA, pB, pC be prices on the market demand rules (dollars per unit delivered). I found one valid set by hand, using the four routes that carry shipments:

pB - a1 = 6     # route 1->B is used, so its price gap equals its cost ($6)
pA - a2 = 9     # route 2->A is used: gap equals cost ($9)
pB - a2 = 12    # route 2->B is used: gap equals cost ($12)
pC - a2 = 13    # route 2->C is used: gap equals cost ($13)

Choose a2 = 0. Then pB = 12, pA = 9, pC = 13, and a1 = 12 - 6 = 6. Now check the two unused routes. Their price gap must not exceed their cost, otherwise they would be worth using:

pA - a1 = 9 - 6  = 3 <= 8    # route 1->A: OK
pC - a1 = 13 - 6 = 7 <= 10   # route 1->C: OK

All six routes pass, and a1, a2, pA, pB, pC are all non-negative, so these prices are a valid dual solution. Its value is the demand-weighted prices minus the capacity-weighted prices:

10*9 + 25*12 + 15*13 - 20*6 - 30*0
= 90 + 300 + 195 - 120 - 0
= $465

The dual objective is $465, the primal cost is $465, so the gap is $0. Any feasible plan must cost at least the dual value, so no plan can cost less than $465. The solver's own dual check agrees: it holds, with a dual objective of 465.0 and a gap of 0.0.

The dual values also have a plain meaning. Plant 1 capacity is worth $6 per unit: one more unit there would let plant 1 take a unit of B's demand and save plant 2 the $12 route, a saving of $12 − $6 = $6.

Takeaway

You can see this example solved and checked step by step in ProvenDecisions, a web app I build that solves and checks problems like this from typed math, LaTeX, a photo or a spoken problem.

This article was written with AI assistance; every number in it comes from the solver run shown above.

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: