Diet optimization

Status
Not open for further replies.
Sigh.

I have a problem: I'm fat.

I have a power: I'm a programmer.

So I thought I'd spend some calories typing on a keyboard. In the process, I wanted to create a PERFECT meal, with just the right amount of every nutrient. And also learn how to write optimizers.

That has, so far, failed to turn me into Casanova.

Here's the problem. I tried to write a program that would get food data from a government database called SR23, which is where sites like nutritiondata.com get their info. For each nutrient it tracks, I told the program three things: how much I need, how bad going over is (penalty over), and how bad it is to have too little (penalty under).

The program must generate a meal that has the lowest penalty. It is calculated as follows: for each nutrient, divide actual by desired amount. If answer < 1, answer = 1/answer. Multiply by penalty-over or penalty-under. Then, sum all the penalties for a result.

I'll describe below what I tried. But I wonder if all this is just plain wrong for the job? Is there something better?



My first attempt (as a test for other parts of the program) was to generate meals totally at random. That worked like you'd expect.

My second attempt used a genetic algorithm. But the scores it produced were nearly as bad as the random ones. All right, a little better.

My third attempt was to download a library called JNSGA2, which implements a multi-objective genetic algorithm. I had to create an objective for each nutrient. I'm pretty sure I'm using it wrong, because it works unbelievably slowly and returns the worst results so far. I guess I'll take another look at this later.

I wonder if I did the GA right. In the mutation step, I shift the amount of each food by a random value using the normal distribution.

For the crossover, I create two sets: foods shared by both meals and foods present in just one of them. For the former, I add all of them to a new meal, with an amount equal to the amount in one of the meals. For the latter, I shuffle it and add the first half to the new meal, with amount coming from whichever parent contained it.

Parents are selected using a tournament method (which I saw suggested on one site): I shuffle the parent generation, then go through it, comparing each adjacent pair and selecting the winner. If the winner is the second meal, I skip one position to avoid duplicates. From the selected winners, I randomly create enough pairs to fill the next generation.

I don't know if I'm making sense. I'd post code, but there's way too much of it, and I don't know what's needed.
 

ChrisB

Ars Tribunus Militum
2,422
Subscriptor
If you really want to optimize it to this level you probably want to take into account time as well - eating something with a high glycemic index right before you go to sleep is going to be worse (have a greater "penalty") than eating the same thing right before you exercise.

You probably want to factor exercise in there to as that's going to have a big impact on your daily caloric target (and when you perform the exercise will have a tie in to the penalty associated with certain foods).

You would probably want to incorporate some feedback loops in there also - body weight is an obvious one, but at a minimum you should also incorporate a body fat percentage in there as well (probably via caliper method as the electronic based resistance methods aren't that accurate or precise).

You could also incorporate some fitness based metrics in there as well also (resting heart-rate, blood pressure, exercise performance)

Finally you are going to want some historical data (I would guess a month would be good) to validate and/or build your algorithms (if you are using a learning/heuristic technique).

(Note that this is looking at it as a programming challenge - for the end result of loosing weight you should do that in concert with this - there is still probably a good weight loss lounge thread hanging around - if you put off doing anything until you have this program up and working - well, that's just another form of rationalizing :))
 

Mark Cianciosa

Wise, Aged Ars Veteran
161
This is a perfect problem for doing a least squares fitting.
http://en.wikipedia.org/wiki/Least_squares

png.latex


Where
png.latex
are your parameters, such as serving size for instance.

For instance if you were you were looking to target a certain amount of Calories using such foods as, Chicken, Rice, and Beans for instance.

png.latex


If you wanted to target a certain amount of calories and a certain amount of fiber using the same food.

png.latex


Here I have taken the values of sigma to be equal to one giving each target equal weighting. The Sigmas can control the amount of emphasis you give to each target.


These equations can than be minimized using any minimization method such as, Gardient Descent, Newtons Method, Downhill Simplex, etc...
http://en.wikipedia.org/wiki/Gradient_descent
http://en.wikipedia.org/wiki/Newton%27s ... timization
http://en.wikipedia.org/wiki/Nelder–Mead_method
 
Status
Not open for further replies.