Blending problem: a linear programming example, solved and checked
The blending 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
Blend three grains into 1 tonne of feed at least 12% protein and at most 5% fibre. Grain 1: $200/t, 10% protein, 3% fibre. Grain 2: $300/t, 18% protein, 4% fibre. Grain 3: $150/t, 8% protein, 8% fibre. Minimize cost.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- x1, x2 and x3 are the tonnes of grains 1, 2 and 3 in the feed, so x1 + x2 + x3 = 1 tonne.
- Protein: 0.1 x1 + 0.18 x2 + 0.08 x3 is the tonnes of protein in the blend (10% of grain 1, and so on). It must be at least 12% of the 1 tonne, 0.12 t. For example, 0.5 t each of grains 1 and 2 has 0.05 + 0.09 = 0.14 t of protein, which passes.
- Fibre: the same idea, at most 5% of the tonne (0.05 t).
- Cost = $200 x1 + $300 x2 + $150 x3: the price per tonne times the tonnes.
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 reach at least 0.12 | |
| limit 3: left side cannot exceed 0.05 | |
| tonnes of grain 1 in the blend (t) cannot be negative | |
| tonnes of grain 2 in the blend (t) cannot be negative | |
| tonnes of grain 3 in the blend (t) cannot be negative |
In matrix form
With its numbers:
What is being decided
There are 3 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| x1 | real number | tonnes of grain 1 in the blend (t) |
| x2 | real number | tonnes of grain 2 in the blend (t) |
| x3 | real number | tonnes of grain 3 in the blend (t) |
Goal and limits
Minimize the objective subject to 6 constraints, of which 3 are simple bounds on a single variable.
Problem type
LP, complexity level 1: a small 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): (1) x1 + x2 + x3 = 1; (2) 0.1*x1 + 0.18*x2 + 0.08*x3 >= 0.12; (3) 0.03*x1 + 0.04*x2 + 0.08*x3 <= 0.05. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | x1 + x2 + x3 = 1 | 1 | 1 | 0 | yes |
| 2 | 0.1*x1 + 0.18*x2 + 0.08*x3 >= 0.12 | 0.12 | 0.12 | 0 | yes |
| 3 | 0.03*x1 + 0.04*x2 + 0.08*x3 <= 0.05 | 0.05 | 0.05 | 0 | yes |
| 4 | x1 >= 0 | 0.333333 | 0 | 0.333333 | |
| 5 | x2 >= 0 | 0.333333 | 0 | 0.333333 | |
| 6 | x3 >= 0 | 0.333333 | 0 | 0.333333 |
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 |
|---|---|---|
| 1 | x1 + x2 + x3 = 1 | 250/3 |
| 2 | 0.1·x1 + 0.18·x2 + 0.08·x3 ≥ 0.12 | 27500/21 |
| 3 | 0.03·x1 + 0.04·x2 + 0.08·x3 ≤ 0.05 | -10000/21 |
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
x1 = cp.Variable(name='x1') # tonnes of grain 1 in the blend (t)
x2 = cp.Variable(name='x2') # tonnes of grain 2 in the blend (t)
x3 = cp.Variable(name='x3') # tonnes of grain 3 in the blend (t)
objective = cp.Minimize(200*x1 + 300*x2 + 150*x3)
constraints = [
x1 + x2 + x3 == 1,
0.1*x1 + 0.18*x2 + 0.08*x3 >= 0.120000000000000,
0.03*x1 + 0.04*x2 + 0.08*x3 <= 0.0500000000000000,
x1 >= 0,
x2 >= 0,
x3 >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('x1 =', x1.value)
print('x2 =', x2.value)
print('x3 =', x3.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 (216.667).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'x1 + x2 + x3 = 1' (objective change per extra unit of its right-hand side) | 83.3333 |
| y2 | price of constraint '0.1*x1 + 0.18*x2 + 0.08*x3 >= 0.12' (objective change per extra unit of its right-hand side) | 1309.52 |
| y3 | price of constraint '0.03*x1 + 0.04*x2 + 0.08*x3 <= 0.05' (objective change per extra unit of its right-hand side) | -476.19 |
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 200 x1 + 300 x2 + 150 x3
subject to
x1 + x2 + x3 = 1
0.10 x1 + 0.18 x2 + 0.08 x3 >= 0.12
0.03 x1 + 0.04 x2 + 0.08 x3 <= 0.05
x1, x2, x3 >= 0
# x1 = tonnes of grain 1 in the blend (t)
# x2 = tonnes of grain 2 in the blend (t)
# x3 = tonnes of grain 3 in the blend (t)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
Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.