Linear Programming and Healthy Diets — Part 2

Previously in this series:

Foods of the Father

My dad’s an interesting guy.

Every so often he picks up a health trend and/or weight loss goal that would make many people’s jaw drop. For example, we once went on a 5-day, 50-mile backpacking trip in the Grand Tetons, and my dad brought one of these per day for dinner, and had vitamin tablets for the rest of his sustenance. The rest of us planned for around 3,000 calories per day. He’s tried the “high fat” and “no fat” diets, and quite a few others. He’s concerned with losing weight, but also living longer, so he’s into caloric restriction among other things.

Recently he asked me to help him optimize his diet. He described a scheme he was performing by hand to minimize the number of calories he consumed per day while maintaining the minimum nutrients required by the FDA’s recommendations. He had a spreadsheet with the nutrients for each food, and a spreadsheet with the constraints on each nutrient. He wanted to come up with a collection of meals (or just throw all the food into a blender) that taste within reason but meet these criteria.

He was essentially solving a linear program by hand, roughly as best as one can, with a few hundred variables! After asking me whether there was “any kind of math” that could help him automate his laborious efforts, I decided to lend a hand. After all, it’s been over three years since I promised my readers I’d apply linear programming to optimize a diet (though it was optimizing for cost rather than calories).

Though it never went beyond what linear programming can handle, pretty quickly my dad’s requests specialized beyond what would interest a general reader. Perhaps this is the nature of math consulting, but it seems when you give someone what they want, they realize that’s not what they wanted.

But the basic ideas are still relevant enough. My solution is a hundred-ish lines of python to set up the input, using Google’s open source operations research tools as the core solver. Disclaimer: I work for Google but I don’t work on the team that wrote this tool. Also nothing in this post represents the views of my employer. It’s just me, and the scale of this problem is laughable for Google to care about anyway.

So this post is half tutorial showing how to use the or-tools python wrapper (it’s only somewhat documented), and half showing a realistic use case for linear programming.

However, don’t let this post dissuade you from the belief that linear programming is useful beyond dieting. People use linear programming to solve all kinds of interesting problems. Here are a few I came across in just the last few weeks:

And that’s not even to mention the ubiquitous applications in operations research (network flow, production optimization, economics) that every large company relies on. The applications seem endless!

As usual, all of the code and data we use in the making of this post is available at this blog’s Github page.

Update 2018-01-01: With this code my dad had tried a few inadvisable cooking techniques: take all the ingredients and throw them in an omelet, or blend them all together in a smoothie. Something about cooking the food alters the nutritional content, so he claims he needed to eat them more or less raw. The resulting “meals” were so unpalatable that he appears to have given up on the optimization techniques in this post. It seems the extreme end of the taste/health tradeoff is not where he wants to be. This suggests an open problem: find a good way to model (or lean from data) what foods taste good together, and in what quantities. One might be able to learn from a corpus of recipes, though I imagine that can only go so far for lightly-cooked ingredients. But with hypothetical constraints like, “penalize/prefer these foods being in the same meal”, one might be able to quantify the taste/health tradeoff in a way that makes my dad happy. Having an easy way to slide along the scale (rather than just naively optimize) would also potentially be useful.

Refresher

If you remember how linear programs work, you can safely skip this section.

As a refresher, let’s outline how to model the nutrition problem as a linear program and recall the basic notation. The variables are food in 100 gram increments. So $ x_1$ might be the amount of canned peas consumed, $ x_2$ lobster meat, etc. All variables would have to be nonnegative, of course. The objective is to minimize the total number of calories consumed. If $ c_1 \geq 0$ is the amount of calories in 100g of canned peas, then one would pay $ c_1x_1$ in calories contributed by peas. If we have $ n$ different variables, then the objective function is the linear combination

$ \textup{cost}(\mathbf{x}) = \sum_{j=1}^n c_j x_j$

We’re using boldface $ \mathbf{x}$ to represent the vector $ (x_1, \dots, x_n)$. Likewise, $ \mathbf{c}$ will represent the cost vector of foods $ (c_1, \dots, c_n)$. As we’ve seen many times, we can compactly write the sum above as an inner product $ \langle \mathbf{c}, \mathbf{x} \rangle$.

Finally, we require that the amount of each nutrient combined in the stuff we buy meets some threshold. So for each nutrient we have a constraint. The easiest one is calories; we require the total number of calories consumed is at least (say) 2,000. So if $ a_j$ represents the number of calories in food $ j$, we require $ \sum_{j=1}^n a_j x_j \geq 2000$. We might also want to restrict a maximum number of calories, but in general having a diet with more calories implies higher cost, and so when the linear program minimizes cost we should expect it not to produce a diet with significantly more than 2,000 calories.

Since we have one set of nutrient information for each pair of (nutrient, food), we need to get fancier with the indexing. I’ll call $ a_{i,j}$ the amount of nutrient $ i$ in food $ j$. Note that $ A = (a_{i,j})$ will be a big matrix, so I’m saying that nutrients $ i$ represent the rows of the matrix and foods $ j$ represent the columns. That is, each row of the matrix represents the amount of one specific nutrient in all the foods, and each column represents the nutritional content of a single food. We’ll always use $ n$ to denote the number of foods, and $ m$ to denote the number of nutrients.

Finally, we have a lower and upper bound for each nutrient, which behind the scenes are converted into lower bounds (possibly negating the variables). This isn’t required to write the program, as we’ll see. In notation, we require that for every $ 1 \leq i \leq m$, the nutrient constraint $ \sum_{j=1}^n a_{i,j} x_j \geq b_i$ is satisfied. If we again use vector notation for the constraints $ \mathbf{b}$, we can write the entire set of constraints as a “matrix equation”

$ A \mathbf{x} \geq \mathbf{b}$

And this means each entry of the vector you get from multiplying the left-hand-side is greater than the corresponding entry on the right-hand-side. So the entire linear program is summarized as follows

$ \displaystyle \begin{aligned} \textup{min } & \langle \mathbf{c} , \mathbf{x} \rangle  \\
\textup{such that } & A \mathbf{x}  \geq \mathbf{b} \\ & \mathbf{x}  \geq \mathbf{0} \end{aligned}$

That’s the syntactical form of our linear program. Now all (!) we need to do is pick a set of foods and nutrients, and fill in the constants for $ A, \mathbf{c}, \mathbf{b}$.

Nutrients and Foods

The easier of the two pieces of data is the nutrient constraints. The system used in the United States is called the Dietary Reference Intake system. It consists of five parts, which I’ve paraphrased from Wikipedia.

  • Estimated Average Requirements (EAR), expected to satisfy the needs of 50% of the people in an age group.
  • Recommended Dietary Allowances (RDA), the daily intake level considered sufficient to meet the requirements of 97.5% of healthy individuals (two standard deviations).
  • Adequate Intake (AI), where no RDA has been established. Meant to complement the RDA, but has less solid evidence.
  • Tolerable upper intake levels (UL), the highest level of daily consumption that have not shown harmful side effects.

While my dad come up with his own custom set of constraints, the ones I’ve posted on the github repository are essentially copy/paste from the current RDA/AI as a lower bound, with the UL as an upper bound. The values I selected are in a csv. Missing values in the upper bound column mean there is no upper bound. And sorry ladies, since it’s for my dad I chose the male values. Women have slightly different values due to different average size/weight.

Nutrient values for food are a little bit harder, because nutrient data isn’t easy to come by. There are a few databases out there, all of which are incomplete, and some of which charge for use. My dad spent a long time hunting down the nutrients (he wanted some additional special nutrients) for his top 200-odd foods.

Instead, in this post we’ll use the USDA’s free-to-use database of 8,000+ foods. It comes in a single, abbreviated, oddly-formatted text file which I’ve parsed into a csv and chosen an arbitrary subset of 800-ish foods to play around with.

Python OR Tools

Google’s OR tools can be installed via PIP:

python -m pip install ortools

Then in a python script, you can import the ortools library and create a simple linear program:

from ortools.linear_solver import pywraplp

solver = pywraplp.Solver('my_LP', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)

x = solver.NumVar(0, 10, 'my first variable')
y = solver.NumVar(0, 10, 'my second variable')

solver.Add(x + y <= 7)
solver.Add(x - 2 * y <= -2)

objective = solver.Objective()
objective.SetCoefficient(x, 3)
objective.SetCoefficient(y, 1)
objective.SetMaximization()

status = solver.Solve()

if status not in [solver.OPTIMAL, solver.FEASIBLE]:
    raise Exception('Unable to find feasible solution')

print(x.solution_value())
print(y.solution_value())

This provides the basic idea of the library. You can use python’s operator overloading (to an extent) to make the constraints look nice in the source code.

Setting up the food LP

The main file diet_optimizer.py contains a definition for a class, which, in addition to loading the data, encapsulates all the variables and constraints.

class DietOptimizer(object):
    def __init__(self, nutrient_data_filename='nutrients.csv',
                 nutrient_constraints_filename='constraints.csv'):

        self.food_table = # load data into a list of dicts
        self.constraints_data = # load data into a list of dicts

        self.solver = pywraplp.Solver('diet_optimizer', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
        self.create_variable_dict()
        self.create_constraints()

        self.objective = self.solver.Objective()
        for row in self.food_table:
            name = row['description']
            var = self.variable_dict[name]
            calories_in_food = row[calories_name]
            self.objective.SetCoefficient(var, calories_in_food)
        self.objective.SetMinimization()

We’ll get into the variables and constraints momentarily, but before that we can see the solve method

    def solve(self):
        '''
            Return a dictionary with 'foods' and 'nutrients' keys representing
            the solution and the nutrient amounts for the chosen diet
        '''
        status = self.solver.Solve()
        if status not in [self.solver.OPTIMAL, self.solver.FEASIBLE]:
            raise Exception('Unable to find feasible solution')

        chosen_foods = {
            food_name: var.solution_value()
            for food_name, var in self.variable_dict.items() if var.solution_value() >; 1e-10
        }

        self.chosen_foods = chosen_foods

        nutrients = {
            row['nutrient']: self.nutrients_in_diet(chosen_foods, row['nutrient'])
            for row in self.constraints_table
        }

        return {
            'foods': chosen_foods,
            'nutrients': nutrients,
        }

Here nutrients_in_diet is a helper function which, given a dictionary of foods and a nutrient, outputs the nutrient contents for that food. This can be used independently of the solver to evaluate the nutrient contents of a proposed diet.

Next we have the method to create the variables.

    def create_variable_dict(self):
        '''
            The variables are the amount of each food to include, denominated in units of 100g
        '''
        self.variable_dict = dict(
            (row['description'], self.solver.NumVar(0, 10, row['description']))
            for row in self.food_table
        )

Each food must be present in a nonnegative amount, and I’ve imposed a cap of 10 (1kg) for any individual food. The reason for this is that I originally had a “water” constraint, and the linear program decided to optimize for that by asking one to drink 2L of red wine per day. I neglected to put in an alcohol nutrient (because it was not already there and I’m lazy), so instead I limited the amount of any individual food. It still seems like a reasonable constraint to impose that nobody would want to eat more than 1kg of any single food on one day.

Finally, we can construct the constraints. The core method takes a nutrient and a lower and upper bound:

    def create_constraint(self, nutrient_name, lower, upper):
        '''
            Each constraint is a lower and upper bound on the
            sum of all food variables, scaled by how much of the
            relevant nutrient is in that food.
        '''
        if not lower:
            print('Warning! Nutrient %s has no lower bound!'.format(nutrient_name))
            return

        sum_of_foods = self.foods_for_nutrient(nutrient_name)
        constraint_lb = lower <= sum_of_foods
        self.solver.Add(constraint_lb)
        self.constraint_dict[nutrient_name + ' (lower bound)'] = constraint_lb

        if not upper:
            return  # no upper bound in the data

        constraint_ub = sum_of_foods <= upper
        self.solver.Add(constraint_ub)
        self.constraint_dict[nutrient_name + ' (upper bound)'] = constraint_ub

This method is mostly bookkeeping, while foods_for_nutrient does the individual nutrient lookup. Note that one is not allowed to do a double-ended inequality like self.solver.Add(lower <= sum_of_foods <= upper). If you try, ortools will ignore one end of the bound.

    def foods_for_nutrient(self, nutrient_name, scale_by=1.0):
        # a helper function that computes the scaled sum of all food variables
        # for a given nutrient
        relevant_foods = []
        for row in self.food_table:
            var = self.variable_dict[row['description']]
            nutrient_amount = row[nutrient_name]
            if nutrient_amount > 0:
                relevant_foods.append(scale_by * nutrient_amount * var)

        if len(relevant_foods) == 0:
            print('Warning! Nutrient %s has no relevant foods!'.format(nutrient_name))
            return

        return SumArray(relevant_foods)

Here we are a bit inefficient by iterating through the entire table, instead of just those foods containing the nutrient in question. But there are only a few hundred foods in our sample database (8,000 if you use the entire SR28 database), and so the optimization isn’t necessary.

Also note that while ortools allows one to use the sum method, it does so in a naive way, because sum([a, b, c]) becomes ((a + b) + c), which is a problem because if the list is too long their library exceeds Python’s default recursion limit. Instead we construct a SumArray by hand.

Finally, though we omitted it here for simplicity, throughout the code in the Github repository you’ll see references to percent_constraints. This exists because some nutrients, like fat, are recommended to be restricted to a percentage of calories, not an absolute amount. So we define a mechanism to specify a nutrient should be handled with percents, and a mapping from grams to calories. This ends up using the scale_by parameter above, both to scale fat by 9 calories per gram, and to scale calories to be a percentage. Cf. the special function for creating percent constraints.

Finally, we have methods just for pretty-printing the optimization problem and the solution, called summarize_optimization_problem and summarize_solution, respectively.

Running the solver

Invoking the solver is trivial.

if __name__ == "__main__":
    solver = DietOptimizer()
    # solver.summarize_optimization_problem()
    solution = solver.solve()
    solver.summarize_solution(solution)

With the example foods and constraints in the github repo, the result is:

Diet:
--------------------------------------------------

  298.9g: ALCOHOLIC BEV,WINE,TABLE,WHITE,MUSCAT
 1000.0g: ALFALFA SEEDS,SPROUTED,RAW
   38.5g: CURRY POWDER
    2.1g: CUTTLEFISH,MXD SP,CKD,MOIST HEAT
   31.3g: EGG,WHL,CKD,HARD-BOILED
   24.0g: LOTUS ROOT,CKD,BLD,DRND,WO/SALT
  296.5g: MACKEREL,JACK,CND,DRND SOL
  161.0g: POMPANO,FLORIDA,CKD,DRY HEAT
   87.5g: ROSEMARY,FRESH
  239.1g: SWEET POTATO,CKD,BKD IN SKN,FLESH,WO/ SALT

Nutrient totals
--------------------------------------------------

    1700.0 mg   calcium                   [1700.0, 2100.0]
     130.0 g    carbohydrate              [130.0, ]
     550.0 mg   choline                   [550.0, 3500.0]
       3.3 mg   copper                    [0.9, 10.0]
      60.5 g    dietary fiber             [38.0, ]
     549.7 μg   dietary folate            [400.0, 1000.0]
    1800.0 kcal energy                    [1800.0, 2100.0]
      32.4 mg   iron                      [18.0, 45.0]
     681.7 mg   magnesium                 [420.0, ]
       7.3 mg   manganese                 [2.3, 11.0]
      35.0 mg   niacin                    [16.0, 35.0]
      11.7 mg   pantothenic acid          [5.0, ]
    2554.3 mg   phosphorus                [1250.0, 4000.0]
      14.0 g    polyunsaturated fatty acids  [1.6, 16.0]
    4700.0 mg   potassium                 [4700.0, ]
     165.2 g    protein                   [56.0, ]
       2.8 mg   riboflavin                [1.3, ]
     220.8 μg   selenium                  [55.0, 400.0]
    1500.0 mg   sodium                    [1500.0, 2300.0]
       2.4 mg   thiamin                   [1.2, ]
      59.4 g    total fat                 [20.0, 35.0]        (29.7% of calories)
    3000.0 μg   vitamin a                 [900.0, 3000.0]
      23.0 μg   vitamin b12               [2.4, ]
       2.4 mg   vitamin b6                [1.7, 100.0]
     157.6 mg   vitamin c                 [90.0, 2000.0]
     893.0 iu   vitamin d                 [400.0, 4000.0]
      15.0 mg   vitamin e                 [15.0, 1000.0]
     349.4 μg   vitamin k                 [120.0, ]
      17.2 mg   zinc                      [11.0, 40.0]

Unfortunately, this asks for a kilo of raw alfalfa sprouts, which I definitely would not eat. The problem is that alfalfa is ridiculously nutritious. Summarizing the diet with the print_details flag set, we see they contribute a significant amount of nearly every important nutrient.

1000.0g: ALFALFA SEEDS,SPROUTED,RAW
	18.8% of calcium (mg)
	16.2% of carbohydrate (g)
	26.2% of choline (mg)
	47.3% of copper (mg)
	31.4% of dietary fiber (g)
	65.5% of dietary folate (μg)
	12.8% of energy (kcal)
	29.7% of iron (mg)
	39.6% of magnesium (mg)
	25.6% of manganese (mg)
	13.7% of niacin (mg)
	48.2% of pantothenic acid (mg)
	27.4% of phosphorus (mg)
	29.3% of polyunsaturated fatty acids (g)
	16.8% of potassium (mg)
	24.2% of protein (g)
	45.1% of riboflavin (mg)
	2.7% of selenium (μg)
	4.0% of sodium (mg)
	31.9% of thiamin (mg)
	11.6% of total fat (g)
	2.7% of vitamin a (μg)
	13.9% of vitamin b6 (mg)
	52.0% of vitamin c (mg)
	1.3% of vitamin e (mg)
	87.3% of vitamin k (μg)
	53.5% of zinc (mg)

But ignoring that, we have some reasonable sounding foods: fish, sweet potato, rosemary (okay that’s a ton of rosemary), egg and wine. I bet someone could make a tasty meal from those rough ingredients.

Extensions and Exercises

No tutorial would be complete without exercises. All of these are related to the actual linear program modeling problem.

Food groups: Suppose you had an additional column for each food called “food group.” You want to create a balanced meal, so you add additional constraint for each food group requiring some food, but not too much, from each group. Furthermore, for certain foods like spices, one could add a special constraint for each spice requiring not more than, say, 20g of any given spice. Or else, as one can see, the linear program can produce diets involving obscenely large amounts of spices.

Starting from a given set of foods: Supposing you have an idea for a recipe (or couple of recipes for a day’s meals), but you want to add whatever else is needed to make it meet the nutritional standards. Modify the LP to allow for this.

Integer variations: The ortools package supports integer programming as well. All you need to do to enable this is change the solver type to CBC_MIXED_INTEGER_PROGRAMMING. The solver will run as normal, and now you can create integer-valued variables using solver.IntVar instead of NumVar. Using binary variables, one can define logical OR constraints (figure out how this must work). Define a new binary variable for each food, and define a constraint that makes this variable 0 when the food is not used, and 1 when the food is used. Then add a term to the optimization problem that penalizes having too many different foods in a daily diet.

(Harder) Sampling: Part of the motivation for this project is to come up with a number of different dishes that are all “good” with respect to this optimization problem. Perhaps there is more than one optimal solution, or perhaps there are qualitatively different diets that are close enough to optimal. However, this implementation produces a deterministic output. Find a way to introduce randomness into the program, so that you can get more than one solution.

Feel free to suggest other ideas, and extend or rewrite the model to do something completely different. The sky’s the limit!

14 thoughts on “Linear Programming and Healthy Diets — Part 2

  1. Apologies in advance for my shallow hypocrisy. But if all you have is a hammer, then everything is a nail. For example, nutrition and economics are not linear systems. And because of climate change, they are not even games any more. Fortunately, there are ways to use computers to model nonlinear systems.

      • Biological systems in general are not linear. There are interactions between nutrients in gut flora. My intent is to encourage alternatives. I have no experience, but my understanding is that system modeling (stocks and flows, feedback loops, delays) can be effective.

      • I don’t think anything in this post suggests your gut flora are a linear system. We’re assuming that the important biological considerations were taken into account by whomever came up with the nutritional guidelines. I have never heard of a significant nutritional detriment to eating two specific foods in the same day (presumably because your gut does something different in the presence of both foods?), but please enlighten me if I’m wrong.

        It sounds like you’re just saying “X is not linear therefore we cannot use any linear methods.” Coupling that with a vague suggestion doesn’t exactly help, especially when it seems like anything beyond what I’ve done here is open research that depends on unvalidated theories of nutrition. So a model based on feedback loops and delays is much more likely to be completely wrong, while a linear model is more likely to be usable.

        To that end, the USDA has used linear programming to suggest affordable healthy diets for low-income families. Here’s one example: http://jn.nutrition.org/content/135/4/900.full And another from Denmark: http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0163411

  2. One customization point can be the upper limit for fiber intake. I don’t recall an official upper bound for fiber, but if I suddenly ramp up my insoluble fiber intake then it tends to lead to … unpleasant digestion. In a perfect world, the public databases would distinguish between soluble and insoluble fibers, but this is complicated by the problem of classifying fibers. Some candidates for fiber classification have the effects of both types of fiber.

  3. Indeed, the USDA guidelines do suggest that human nutrition is linear, but I’m skeptical that that suggestion is based on science. I have no scientific alternative, and it may be that the failure of USDA guidelines to prevent obesity is no fault of the USDA. I certainly cannot compete with the authority of the USDA, but its failure would seem more of an indictment than endorsement of its assumptions. One alternative to the assumption of linearity is non-linearity. Another alternative is external influences, like hunger. It seems to me, that a discussion of nutrition without consideration of the effects of various foods on hunger is as incomplete as such a discussion without consideration of gut flora. At any rate, consumption of food has some immediate, and some not so immediate, effects, so a systems approach might well be appropriate.

    • A simpler hypothesis is that the USDA guidelines have failed to “prevent obesity” because people don’t follow the guidelines. I am more inclined to believe that than the hypothesis that their science produces guidelines *making* people obese when they otherwise would not be.

    • Hunger (i.e., satiation) was accounted for by J L Balintfy in “The Cost of Decent Subsistence”. Balintfy chose a quadratic objective function with preference and satiation coefficients. “The history of the application of mathematical programming to menu planning” by Lily Lancaster is also a good read.

  4. You are minimizing calories based on a constraint of nutrients, but don’t you want to maximize nutrients, based on a given number of calories? Perhaps also with a minimum for each nutrient.

    • It wouldn’t be easy to turn “maximize nutrients” into a single linear expression. But also some nutrients are bad if you have too much, and maximizing nutrients would probably result in just taking a single food that has the densest of one particular thing (eg a spice with lots of manganese per gram or whatever) and using that to fill any needed calories between the calories provided by the mins for all the nutrients and the target calorie count.

  5. Hey, so it really blew me away to get linked to this post, because i remember doing the same thing as a university student with Mathematica back in 2012. My goal was to minimize costs, and I used a few month’s worth of saved receipts and nutrition labels to build up my data set. I’ll see if any of the original files remain – my backups hygeine has not been the best for the last five years – but i seem to remember finding that i could get down to around $2 a day for a diet that met my macronutrient goals and wasn’t absolutely awful.

    One thing that i did to avoid the “kilo of alfalfa” problem was to set individual maximum daily allowances for each food item. I scrolled through my items spreadsheet and noted how many servings of each item i’d be happy eating basically every day forever.

    Thanks for posting this code for a Python solution. I’m recently living and cooking on my own again and taking an updated crack at this was hanging in the back of my mind for a while.

  6. Hi Jeremy!

    Thank you so much by share your time and knowledge. The world need more this! I found your site and this post after a few hours looking for a solution like yours.

    I believe that by making small changes to this project, it is possible to extract very cool things. For example, chaning raw ingredients by recipes and creating a module to reuse reuse excess ingredients in packaging, in addition to obtaining a more robust diet plan, the savings could be even greater.

    I will to fork your project in github. If you want, we can talk a little bit about it. What do you think?

    Regards,

    Jeff

    • Feel free to experiment! I think the difficulty with recipe bundling is that it probably requires integer variables, but it would be interesting to hear how your experiments turn out!

  7. Jeremy,

    Let’s play a little: how about we replace the name of the variable foods with recipes and believe that the table of nutrients in these recipes is already calculated. The solution code is the same.

    Perhaps the only thing that is interesting to do at that moment, would be to understand which ingredients and in what quantities were left to be reused in other recipes in order to maintain the economy proposed by the initial problem. Do you agree?

    Perhaps if we come to a solution, we would be going a step further than Stingler.

Leave a Reply