Diet Planning

Intermediate
Binary Variables Multiple Constraints Lower+Upper Bounds MINIMIZE

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.

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);
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);
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.
id name protein carbs fat calories cost selected
1Grilled Chicken3520124208.500
2Caesar Salad101582206.000
3Beef Stew3025184809.000
4Veggie Stir Fry1540103505.500
5Fish Tacos2830144007.500
6Greek Yogurt Bowl203082804.001
7Turkey Sandwich2535103805.001
8Lentil Soup184563204.501
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.
← Item Filtering Next: Task Assignment →