Optimising Nutrition with Genetic Algorithms
I have been working on creating an Optimising Nutrition with Genetic Algorithms python script that will randomly select a number of foods from the Australian Food Composition Database will then generate random amounts for each food in grams for a number of specified generations with each generation trying to get closer and closer to matching 100% of the Daily Recommended Intake (RDI) for each essential vitamin and mineral, so the end result will be a list of ingredients and the amount in grams you should eat to get as close as possible to the RDI.
Kind of like a whole food version of Soylent
This would be helpful for some one wanting to do a daily eating plan and wants to hit their micronutrient RDI’s.
You can view the index of recipes generated by genetic algorithm here : https://alexlaverty.github.io/optimize-nutrition/
They are sorted by score, a lower score is better, a lower score means it’s closer to matching the RDI’s and the higher the score the further away from matching the RDI’s.
I have also specified different diet types to generate for All, meaning all foods in the database, Vegan to exclude any animal products, and WFPB meaning Whole Food Plant Based which excludes food if they do not match this criteria.
The script and repo is still a work in progress, so I am open to any feedback or suggestions.
This script is designed to optimize meal planning using a genetic algorithm, which is a type of artificial intelligence inspired by natural selection.
The main goal is to select and adjust quantities of various foods to ensure that the daily nutritional requirements such as essential vitamins and minerals are met.
By using a genetic algorithm, the script evolves a set of food combinations over multiple generations, gradually improving the meal plan to better meet these nutritional targets.
Key Features and Functionality:
Food Selection and Customization:
The script begins by reading a dataset of foods, each with nutritional information. It allows for dietary customization, such as selecting only vegan or whole-food plant-based foods, by applying exclusion rules. This ensures the optimization is tailored to different diet types.
Genetic Algorithm:
The core of the optimization is the genetic algorithm. The algorithm works by simulating the process of natural selection:
It starts with a randomly selected population of meal plans (combinations of foods and their quantities). Over several generations, the algorithm “evolves” better solutions by selecting the best meal plans (those that are closer to the nutritional targets), and creating new meal plans through processes like crossover (mixing different plans) and mutation (random changes). This evolutionary process continues until an optimal or satisfactory meal plan is found.
Nutrient Calculation:
Each food item in the selected meal plans is analyzed for its contribution to meeting the daily recommended intake (RDI) of various nutrients.
The script calculates how much of each nutrient each food provides, ensuring that the final combination meets or comes close to the daily nutritional goals for essential vitamins and minerals.
Output and Reports:
After running the optimization process, the script generates detailed reports in various formats. These include:
JSON files containing the optimized meal plans, which can be saved and reused. Markdown and HTML files summarizing the results in an easily readable format.
These reports show key details like the optimization score (how well the meal plan meets the nutritional goals), the number of foods used, and how the nutrients in the meal plan are distributed across different categories (e.g., nutrients at good levels, below target, or above target).
Scalability and Flexibility:
The script is designed to be flexible, allowing it to handle various numbers of foods (from 5 to 20) and a customizable number of generations (up to 300). This means the user can adjust how complex and refined the meal plan optimization process is.
How Genetic Algorithms Help in Meal Planning:
Genetic algorithms are particularly well-suited for problems like meal optimization, where the number of possible food combinations is large and complex. Instead of manually trying every possible combination, which would be time-consuming and inefficient, the genetic algorithm simulates the natural process of evolution to “search” for the best combination efficiently.
By creating a population of meal plans and evolving them through generations, the algorithm can gradually find the combination of foods that best meets the nutritional targets, all while considering factors like variety and balance in the meal.
In summary, this script automates the process of meal planning by intelligently selecting and adjusting food items to meet nutritional needs, making use of genetic algorithms to improve the meal plans over time. It provides a flexible, automated solution for anyone looking to optimize their diet for specific health goals or dietary preferences.
Analysis of the Results
After letting the script run every hour for a few days I fed the table into ChatGPT and asked it to analyse the results.
Here is ChatGPT’s analysis of the trends and optimal values based on the provided data:
Key Observations
- Best Score Range:
- The lowest (best) scores are in the range 5.07 to 6.17.
- The worst scores are above 20, with the worst being 31.85.
- Diet Type Trends:
- Best scores (≤6.17): Mostly “all” and “vegan” diets.
- Worst scores (>20): Mostly “wfpb” (Whole Food Plant-Based).
- Implication: The best-performing recipes tend to include all food groups or vegan options, while purely whole-food plant-based diets seem harder to optimize.
- Number of Ingredients (Foods):
- Best scores (≤6.17): Use between 10 to 20 ingredients.
- Worst scores (>20): Often have very few ingredients (e.g., 5–8), suggesting that having more variety contributes to better nutritional balance.
- Nutrient Balance (OK/Low/High):
- Best scores (≤6.17): Tend to have around 17-20 nutrients at OK levels, 9-12 nutrients below target, and 0-2 above target.
- Worst scores (>20): Have significantly more nutrients below target (e.g., 20-28 low), meaning they fail to meet nutritional needs.
- Generations & Computation Time:
- The best runs typically required 150-300 generations and computation times ranging 450s to 2000s.
- The worst runs often completed in very few generations (3-50), meaning they likely didn’t converge to a well-optimized solution.
Optimal Recipe Parameters for Genetic Algorithm
If you’re generating recipes, these values would likely yield the best results:
Parameter | Optimal Range |
---|---|
Diet Type | All or Vegan |
Number of Ingredients | 10 - 20 |
Nutrients (OK/Low/High) | ~18/10/2 |
Generations | 150 - 300 |
Computation Time | 500s - 2000s |
Using this analysis I’m going to continue to tweak the script and add in more features to get recipes that more closely match the recommended daily requirements for the essential vitamins and minerals and also adding more requirements to match, calories, other things that are not essential but still desirable.
I’ve also been working on a UI which updates in real time and shows you a chart of the score as it decreases with each generation :