Diet problem: a linear programming example, solved and checked
The diet 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
Say it or snap it
You can give ProvenDecisions this problem out loud or as a photo. The app wrote down both, solved them, and got the same checked answer as the typed version.
What the app heard: “The price is $1 per serving, oats cost $0.30, milk $0.25, and bean $0.50. A serving of oats has 5 grams of protein, 1% of the daily value of calcium and 4% of the daily value of iron. A serving of milk has 8 grams of protein, 30% of the daily calcium and no iron. A serving of beans has 9 grams of protein, 4% of the daily calcium and 3% of the daily iron. Each day needs at least 40 grams of protein, 60% of the daily value of calcium and 20% of the daily value of iron. Find the cheapest diet.”

The problem
Oats, milk and beans cost $0.30, $0.25 and $0.50 per serving. Per serving, protein (grams): 5, 8, 9; calcium (% of daily value): 1, 30, 4; iron (% of daily value): 4, 0, 3. A day needs at least 40 grams of protein, 60% of the daily value of calcium and 20% of the daily value of iron. Find the cheapest diet.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
Assumptions:
- servings may be fractional
The model
Each line has a plain-language note on the right.
| make as small as possible: total daily diet cost (dollars) | |
| limit 1: left side must reach at least 40 | |
| limit 2: left side must reach at least 60 | |
| limit 3: left side must reach at least 20 | |
| servings of oats per day (servings) cannot be negative | |
| servings of milk per day (servings) cannot be negative | |
| servings of beans per day (servings) cannot be negative |
In matrix form
With its numbers:
What is being decided
There are 3 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| oats | real number | servings of oats per day (servings) |
| milk | real number | servings of milk per day (servings) |
| beans | real number | servings of beans per day (servings) |
Goal and limits
Minimize total daily diet cost (dollars) 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) 9*beans + 8*milk + 5*oats >= 40; (3) 3*beans + 4*oats >= 20; (6) beans >= 0. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | 9*beans + 8*milk + 5*oats >= 40 | 40 | 40 | 0 | yes |
| 2 | 4*beans + 30*milk + oats >= 60 | 61.25 | 60 | 1.25 | |
| 3 | 3*beans + 4*oats >= 20 | 20 | 20 | 0 | yes |
| 4 | oats >= 0 | 5 | 0 | 5 | |
| 5 | milk >= 0 | 1.875 | 0 | 1.875 | |
| 6 | beans >= 0 | 0 | 0 | 0 | yes |
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. Every step was checked in exact fractions, not rounded decimals.
| # | Constraint | Weight |
|---|---|---|
| 1 | 9·beans + 8·milk + 5·oats ≥ 40 | 1/32 |
| 3 | 3·beans + 4·oats ≥ 20 | 23/640 |
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 (and A x = b for equalities); a maximization is written as minimizing the negative.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
oats = cp.Variable(name='oats') # servings of oats per day (servings)
milk = cp.Variable(name='milk') # servings of milk per day (servings)
beans = cp.Variable(name='beans') # servings of beans per day (servings)
objective = cp.Minimize(0.5*beans + 0.25*milk + 0.3*oats)
constraints = [
9*beans + 8*milk + 5*oats >= 40,
4*beans + 30*milk + oats >= 60,
3*beans + 4*oats >= 20,
oats >= 0,
milk >= 0,
beans >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('oats =', oats.value)
print('milk =', milk.value)
print('beans =', beans.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 (1.96875).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint '9*beans + 8*milk + 5*oats >= 40' (objective change per extra unit of its right-hand side) | 0.03125 |
| y2 | price of constraint '4*beans + 30*milk + oats >= 60' (objective change per extra unit of its right-hand side) | 0 |
| y3 | price of constraint '3*beans + 4*oats >= 20' (objective change per extra unit of its right-hand side) | 0.0359375 |
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 0.30*oats + 0.25*milk + 0.50*beans
subject to
5*oats + 8*milk + 9*beans >= 40
1*oats + 30*milk + 4*beans >= 60
4*oats + 0*milk + 3*beans >= 20
oats, milk, beans >= 0
# oats = servings of oats per day (servings)
# milk = servings of milk per day (servings)
# beans = servings of beans per day (servings)
# objective = total daily diet cost (dollars)
# assume: servings may be fractionalChecked: 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.