Loading solver…
GLOP - Stigler diet
Problem: choose the cheapest mix of foods that satisfies daily minimum nutrition requirements.
Show code
import { MPSolver } from 'or-tools-wasm/mp-solver';
const solver = MPSolver.createSolver('GLOP');
solver.setNumThreads(4);
const nutrients = [
{ name: 'Calories (kcal)', minimum: 3 },
{ name: 'Protein (g)', minimum: 70 },
{ name: 'Calcium (g)', minimum: 0.8 },
];
const data = [
{ name: 'Wheat Flour', nutrients: [44.7, 1411, 2] },
{ name: 'Macaroni', nutrients: [11.6, 418, 0.7] },
{ name: 'Milk', nutrients: [6.1, 310, 10.5] },
{ name: 'Eggs', nutrients: [2.9, 238, 1] },
{ name: 'Spinach', nutrients: [1.1, 106, 0] },
];
const foods = data.map((food) => solver.addNumVariable(0, solver.infinity(), food.name));
nutrients.forEach((nutrient, nutrientIndex) => {
const ct = solver.addConstraint(nutrient.minimum, solver.infinity());
data.forEach((food, foodIndex) => {
ct.setCoefficient(foods[foodIndex], food.nutrients[nutrientIndex]);
});
});
const objective = solver.objective();
foods.forEach((food) => objective.setCoefficient(food, 1));
objective.setMinimization();
await solver.solve({ executor: 'worker' });
- This is a continuous linear program: foods can be bought in fractional daily units.
- The objective is to minimize annual food cost.
- Each nutrient is a lower-bound constraint that the selected food mix must satisfy.
- Expand any food card to edit its label, unit, color, and nutrient contributions.
- Change the nutrient requirement inputs to make the diet easier, harder, or impossible.
- The result is a mathematically cheap diet, not a realistic meal plan.
Food Catalog
Nutrient Requirements
Receipt
Run the solver to view the annual diet cost and selected foods.
Status / Response: