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.
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.