Solving the Diet Problem with PuLP

An Example of Cost-Effective Nutrition Optimization

Example: Diet Problem

The diet problem is a classic $optimization$ $problem$ that aims to find the most $cost$-$effective$ way to meet nutritional requirements using a selection of foods.

Let’s solve a simple example using the $PuLP$ library in $Python$.

Problem Statement:

You are trying to meet your daily nutritional needs using three different foods.

Each food has a $cost$ and provides a certain amount of $calories$, $protein$, and $fat$.

Your goal is to minimize the cost while ensuring that you meet the minimum daily requirements for $calories$, $protein$, and $fat$.

Data:

  • Foods: Chicken, Beef, Vegetables
  • Cost per unit:
    • Chicken: $2.50
    • Beef: $3.00
    • Vegetables: $1.00
  • Nutritional content per unit:
    • Chicken: $250$ calories, $30$g protein, $10$g fat
    • Beef: $300$ calories, $20$g protein, $20$g fat
    • Vegetables: $50$ calories, $5$g protein, $0$g fat
  • Minimum daily requirements:
    • Calories: $2000$
    • Protein: $60$g
    • Fat: $50$g

Solution using PuLP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pulp

# Define the problem
prob = pulp.LpProblem("Diet_Problem", pulp.LpMinimize)

# Define the decision variables: the amount of each food to consume
food_vars = {
"Chicken": pulp.LpVariable("Chicken", lowBound=0, cat='Continuous'),
"Beef": pulp.LpVariable("Beef", lowBound=0, cat='Continuous'),
"Vegetables": pulp.LpVariable("Vegetables", lowBound=0, cat='Continuous')
}

# Define the cost of each food
costs = {
"Chicken": 2.50,
"Beef": 3.00,
"Vegetables": 1.00
}

# Objective function: Minimize the total cost
prob += pulp.lpSum(food_vars[food] * costs[food] for food in food_vars)

# Define the nutritional content of each food
calories = {
"Chicken": 250,
"Beef": 300,
"Vegetables": 50
}

protein = {
"Chicken": 30,
"Beef": 20,
"Vegetables": 5
}

fat = {
"Chicken": 10,
"Beef": 20,
"Vegetables": 0
}

# Add constraints for minimum daily nutritional requirements
prob += pulp.lpSum(food_vars[food] * calories[food] for food in food_vars) >= 2000, "CaloriesRequirement"
prob += pulp.lpSum(food_vars[food] * protein[food] for food in food_vars) >= 60, "ProteinRequirement"
prob += pulp.lpSum(food_vars[food] * fat[food] for food in food_vars) >= 50, "FatRequirement"

# Solve the problem
prob.solve()

# Print the results
print("Optimal diet:")
for food in food_vars:
print(f"{food}: {food_vars[food].varValue:.2f} units")

print(f"Total cost: ${pulp.value(prob.objective):.2f}")

Explanation:

  • Decision Variables: food_vars represent the amount of each food to consume.
  • Objective Function: Minimize the total cost of the diet.
  • Constraints: Ensure that the total intake of $calories$, $protein$, and $fat$ meets or exceeds the minimum daily requirements.

Output:

1
2
3
4
5
Optimal diet:
Chicken: 0.00 units
Beef: 6.67 units
Vegetables: 0.00 units
Total cost: $20.00

Explanation of the Result:

The optimal solution suggests that the most $cost$-$effective$ way to meet the daily nutritional requirements is to consume $6.67$ units of Beef and no units of Chicken or Vegetables.

The total cost of this diet is $20.00.

Key Points:

  1. Beef-only diet: The solution indicates that Beef alone is sufficient to meet the required minimums for calories, protein, and fat, making it the $cheapest$ option at $3.00 per unit.

  2. No Chicken or Vegetables: Since Beef provides a higher concentration of calories and fat compared to Chicken and Vegetables, the solver determined that only Beef is needed to satisfy the constraints without incurring extra costs.

  3. Total Cost: By consuming $6.67$ units of Beef, the total expenditure on this diet is minimized to $20.00.

This result demonstrates how optimization can find the most economical way to satisfy nutritional needs based on the available options.