Diet Planning
Intermediate
Binary Variables
Multiple Constraints
Lower+Upper Bounds
MINIMIZE
THE PROBLEM
A meal planning service needs to assemble a daily meal plan from available options. Each meal provides protein, carbs, fat, and calories at a given cost. The plan must meet minimum protein (50g) and carbs (100g) requirements, stay under maximum fat (50g) and calorie (1500 cal) limits, and minimize total cost. This multi-constraint problem shows how PackDB handles both lower-bound (≥) and upper-bound (≤) constraints simultaneously.
SAMPLE DATA
CREATE TABLE Meals (
id INTEGER, name VARCHAR, protein INTEGER, carbs INTEGER,
fat INTEGER, calories INTEGER, cost DOUBLE
);
INSERT INTO Meals VALUES
(1, 'Grilled Chicken', 35, 20, 12, 420, 8.50),
(2, 'Caesar Salad', 10, 15, 8, 220, 6.00),
(3, 'Beef Stew', 30, 25, 18, 480, 9.00),
(4, 'Veggie Stir Fry', 15, 40, 10, 350, 5.50),
(5, 'Fish Tacos', 28, 30, 14, 400, 7.50),
(6, 'Greek Yogurt Bowl', 20, 30, 8, 280, 4.00),
(7, 'Turkey Sandwich', 25, 35, 10, 380, 5.00),
(8, 'Lentil Soup', 18, 45, 6, 320, 4.50);
THE QUERY
SELECT id, name, protein, carbs, fat, calories, cost, x as selected
FROM Meals
DECIDE x IS BOOLEAN
SUCH THAT
SUM(x * protein) >= 50 AND
SUM(x * carbs) >= 100 AND
SUM(x * fat) <= 50 AND
SUM(x * calories) <= 1500
MINIMIZE SUM(x * cost);
QUERY BREAKDOWN
1
DECIDE x IS BOOLEAN — For each meal: include (x=1) or skip (x=0).
2
SUM(x * protein) >= 50 — Selected meals must provide at least 50g protein.
3
SUM(x * carbs) >= 100 — At least 100g carbs.
4
SUM(x * fat) <= 50 — No more than 50g fat.
5
SUM(x * calories) <= 1500 — Stay under 1500 calories.
6
MINIMIZE SUM(x * cost) — Find the cheapest combination meeting all nutritional requirements.RESULT
| id | name | protein | carbs | fat | calories | cost | selected |
|---|---|---|---|---|---|---|---|
| 1 | Grilled Chicken | 35 | 20 | 12 | 420 | 8.50 | 0 |
| 2 | Caesar Salad | 10 | 15 | 8 | 220 | 6.00 | 0 |
| 3 | Beef Stew | 30 | 25 | 18 | 480 | 9.00 | 0 |
| 4 | Veggie Stir Fry | 15 | 40 | 10 | 350 | 5.50 | 0 |
| 5 | Fish Tacos | 28 | 30 | 14 | 400 | 7.50 | 0 |
| 6 | Greek Yogurt Bowl | 20 | 30 | 8 | 280 | 4.00 | 1 |
| 7 | Turkey Sandwich | 25 | 35 | 10 | 380 | 5.00 | 1 |
| 8 | Lentil Soup | 18 | 45 | 6 | 320 | 4.50 | 1 |
INTERPRETATION
The solver selects Greek Yogurt Bowl ($4.00), Turkey Sandwich ($5.00), and Lentil Soup ($4.50) for a total cost of $13.50. This combination provides 63g protein (≥50), 110g carbs (≥100), 24g fat (≤50), and 980 calories (≤1500) — meeting all four constraints at the lowest possible cost.