I have a database that I got from the Internet (USDA National Nutrient Database for Standard Reference), detailing the amount of each nutrient in each of a few thousand foodstuffs. I wanted to write a program that would be able to create a maximally nutritious meal based on this data.

Clara Dennis

Clara Dennis

Answered question

2022-11-12

I am not a mathematician, so go easy on me. I'm a programmer.
I have a database that I got from the Internet (USDA National Nutrient Database for Standard Reference), detailing the amount of each nutrient in each of a few thousand foodstuffs. I wanted to write a program that would be able to create a maximally nutritious meal based on this data.
For each nutrient, I have a target and two penalties - one for going over and one for going under the target (since, for example, it's a lot worse to get too much saturated fat than not enough). The goal is to minimize the sum of the penalties.
The meal can select from all the thousands of foodstuffs, but can only contain five or six.
I wrote the program in Java, implemented a genetic algorithm, specified my requirements, and let it run. It produced recommendations that were pure poison, and didn't seem to improve with time.
Maybe I just don't get genetic algorithms? Let's see what I did...
1) Create a population of randomly generated meals.
2) Normalize each one so it has 2000 calories, by multiplying the amount of each foodstuff proportionally.
3) Select the best 10% of meals to be parents.
4) Create a new generation - a few random to avoid local minima, the rest created by combining the numbers and amounts from the parents.
5) GOTO 2.
What other algorithm can I try? Someone advised me to use simplex algorithm, but I can't seem to explain to it (the implementation in Apache Commons Math) what my fitness function is. But he claimed it would be a natural fit, and I have even heard of someone who used simplex for exactly this.

Answer & Explanation

ontzeidena8a

ontzeidena8a

Beginner2022-11-13Added 17 answers

Step 1
Simplex is indeed a natural fit, so would be any other algorithm for solving linear programming problems. I will try to detail how you would formulate the problem.
You have foods f i and nutrients n j and a matrix a i j of how much of nutrient n j is in one serving of the f j . Let your variables be x i 0 (how much for each foodstuff) and requirements be r i (also for each foodstuff).
Step 2
Now you require A x = r if you want the exact solution and to minimize the number of ingredients perhaps minimize x i .
Another approach is to define y = A x and optimizing | y i r i | . To use the penalties you are suggesting is a little tricky (but possible), then optimizing i P i ( y i r i ) + i p i ( r i y i ) where P , p are penalties for overhitting/underhitting the target r i and the first sum is over those i which overhit, and the second over those that underhit.

Do you have a similar question?

Recalculate according to your conditions!

New Questions in College Statistics

Ask your question.
Get an expert answer.

Let our experts help you. Answer in as fast as 15 minutes.

Didn't find what you were looking for?