∇ProvenDecisions
Ctrl K

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.

Answer. Assumed: servings may be fractional. The smallest objective value is 1.96875, at oats = 5, milk = 1.875, beans = 0. All 6 constraints are satisfied exactly. Exactly proven optimal.

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.

🎙️ Spoken (37 seconds)

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.”

📷 Photographed
Photo of the diet problem written on a note

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:

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.

VariableTypeMeaning
oatsreal numberservings of oats per day (servings)
milkreal numberservings of milk per day (servings)
beansreal numberservings 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.

oats5milk1.875beans0

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.

#ConstraintValueLimitSlackBinding
19*beans + 8*milk + 5*oats >= 4040400yes
24*beans + 30*milk + oats >= 6061.25601.25
33*beans + 4*oats >= 2020200yes
4oats >= 0505
5milk >= 01.87501.875
6beans >= 0000yes

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.

#ConstraintWeight
19·beans + 8·milk + 5·oats ≥ 401/32
33·beans + 4·oats ≥ 2023/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 variablePricesValue
y1price of constraint '9*beans + 8*milk + 5*oats >= 40' (objective change per extra unit of its right-hand side)0.03125
y2price of constraint '4*beans + 30*milk + oats >= 60' (objective change per extra unit of its right-hand side)0
y3price 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 fractional

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: