∇ProvenDecisions
Ctrl K

Two-stage supply chain: a linear programming example, solved and checked

The two-stage supply chain 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.

Answer. The smallest objective value is 470, at p = 50, f1 = 10, f2 = 40, s1 = 10, s2 = 40. All 12 constraints are satisfied exactly. Exactly proven optimal.

Solve your own version free, no account needed

The problem

A factory makes up to 80 units at $5 each. Units go to warehouses W1 or W2 ($2 and $3 per unit), then to a store that needs 50 units ($4 from W1, $1 from W2). Each warehouse handles at most 40 units. Minimize total cost.

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
units produced at the factory (units) is at most 80
limit 2: left side must equal the right side
limit 3: left side must reach at least 50
limit 4: left side must equal the right side
limit 5: left side must equal the right side
units shipped from factory to warehouse W1 (units) is at most 40
units shipped from factory to warehouse W2 (units) is at most 40
units produced at the factory (units) cannot be negative
units shipped from factory to warehouse W1 (units) cannot be negative
units shipped from factory to warehouse W2 (units) cannot be negative
units shipped from W1 to the store (units) cannot be negative
units shipped from W2 to the store (units) cannot be negative

In matrix form

With its numbers:

What is being decided

There are 5 decision variables.

VariableTypeMeaning
preal numberunits produced at the factory (units)
f1real numberunits shipped from factory to warehouse W1 (units)
f2real numberunits shipped from factory to warehouse W2 (units)
s1real numberunits shipped from W1 to the store (units)
s2real numberunits shipped from W2 to the store (units)

Goal and limits

Minimize the objective subject to 12 constraints, of which 8 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.

p50f110f240s110s240

Which constraints hold the answer in place

Binding (no slack left): (2) f1 + f2 = p; (3) s1 + s2 >= 50; (4) s1 = f1; (5) s2 = f2; (7) f2 <= 40. Changing a binding constraint changes the answer; a slack one can move a little without effect.

#ConstraintValueLimitSlackBinding
1p <= 80508030
2f1 + f2 = p50500yes
3s1 + s2 >= 5050500yes
4s1 = f110100yes
5s2 = f240400yes
6f1 <= 40104030
7f2 <= 4040400yes
8p >= 050050
9f1 >= 010010
10f2 >= 040040
11s1 >= 010010
12s2 >= 040040

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.

#ConstraintWeight
2f1 + f2 = p-5
3s1 + s2 ≥ 5011
4s1 = f1-7
5s2 = f2-10
7f2 ≤ 40-2

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

p = cp.Variable(name='p')  # units produced at the factory (units)
f1 = cp.Variable(name='f1')  # units shipped from factory to warehouse W1 (units)
f2 = cp.Variable(name='f2')  # units shipped from factory to warehouse W2 (units)
s1 = cp.Variable(name='s1')  # units shipped from W1 to the store (units)
s2 = cp.Variable(name='s2')  # units shipped from W2 to the store (units)

objective = cp.Minimize(2*f1 + 3*f2 + 5*p + 4*s1 + s2)
constraints = [
    p <= 80,
    f1 + f2 == p,
    s1 + s2 >= 50,
    s1 == f1,
    s2 == f2,
    f1 <= 40,
    f2 <= 40,
    p >= 0,
    f1 >= 0,
    f2 >= 0,
    s1 >= 0,
    s2 >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('p =', p.value)
print('f1 =', f1.value)
print('f2 =', f2.value)
print('s1 =', s1.value)
print('s2 =', s2.value)

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 (470).

Dual variablePricesValue
y1price of constraint 'p <= 80' (objective change per extra unit of its right-hand side)0
y2price of constraint 'f1 + f2 = p' (objective change per extra unit of its right-hand side)-5
y3price of constraint 's1 + s2 >= 50' (objective change per extra unit of its right-hand side)11
y4price of constraint 's1 = f1' (objective change per extra unit of its right-hand side)-7
y5price of constraint 's2 = f2' (objective change per extra unit of its right-hand side)-10
y6price of constraint 'f1 <= 40' (objective change per extra unit of its right-hand side)0
y7price of constraint 'f2 <= 40' (objective change per extra unit of its right-hand side)-2

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 5*p + 2*f1 + 3*f2 + 4*s1 + 1*s2
subject to
p <= 80
f1 + f2 = p
s1 + s2 >= 50
s1 = f1
s2 = f2
f1 <= 40
f2 <= 40
p, f1, f2, s1, s2 >= 0
# p = units produced at the factory (units)
# f1 = units shipped from factory to warehouse W1 (units)
# f2 = units shipped from factory to warehouse W2 (units)
# s1 = units shipped from W1 to the store (units)
# s2 = units shipped from W2 to the store (units)

Change the numbers and solve it

Checked: 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

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: