Skip to content

trougnouf/FoodStuffSolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FoodStuffSolver

A Python-based tool that generates cost-effective, nutritionally complete recipes using Linear Programming (LP) or CMA-ES evolutionary strategies. It balances macronutrients, micronutrients, and specific ratios (like Omega-3/6) while minimizing cost.

Dependencies

pip install pyyaml numpy scipy cma requests

Setup & Data

1. Importing Ingredients (USDA)

To fetch nutrient data from the USDA FoodData Central database:

  1. Get an API key from USDA API Sign Up.
  2. Save the key in a file named usda_api.key in the root directory.
  3. Run the tool with a Food ID:
    python tools/get_usda_ingredient.py <USDA_FOOD_ID>

2. Manual Editing (Required)

Ingredients imported from the USDA do not contain price or allergy information. You must manually edit the generated YAML files in the ingredients/ directory:

  • cost_per_unit: Enter the cost per unit (default is per gram). The solver treats ingredients with no cost as free/infinite, which may distort results.
  • allergen: Add a list of allergens if applicable (e.g., allergen: ["Peanuts"]).

Usage

Generating a Recipe

Run the solver to generate an optimal recipe based on your ingredients/ folder.

Basic Usage (Linear Programming - Fast/Optimal):

python solver.py

Common Options:

python solver.py \
  --max_calories 2150 \
  --min_proteins 112 \
  --allergies "Peanuts" "Shellfish"

Key Arguments

Argument Description
--solver lp (default, fast) or cma (slower).
--allergies List of allergens to exclude (checks the allergen field in YAMLs).
--max_calories Override maximum daily energy (kcal).
--min_proteins Override minimum daily protein (g).
--min_nutrient Set specific min for a nutrient (e.g., --min_nutrient "Iron, Fe" 20).
--ingredients_dpaths Custom path to ingredients directory (default: ingredients).
--ignore_max_qty Ignore the max_qty limit specified in ingredient YAML files.
--preload_recipe_fpath Path to an existing recipe YAML. Used to enforce minimum quantities and set the starting point.

Configuration

  • Nutrient Profiles: libnutrient.py contains default RDI/UL definitions. You can provide a custom profile via --nutrient_profile my_profile.yaml.
  • Ingredient Attributes: Aside from cost and allergens, you can also modify max_qty (limit usage of specific items) or satisfaction_multiplier in the ingredient YAML files.

File Structure

  • solver.py: Main entry point.
  • libingredient.py: Ingredient data models and I/O.
  • librecipe.py: Logic for recipe aggregation, cost calculation, and validation.
  • libnutrient.py: Nutrient definitions and unit conversions.
  • tools/: Scripts for data fetching.

About

Nutritionally / cost optimal foodstuff recipe solver

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages