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.
pip install pyyaml numpy scipy cma requestsTo fetch nutrient data from the USDA FoodData Central database:
- Get an API key from USDA API Sign Up.
- Save the key in a file named
usda_api.keyin the root directory. - Run the tool with a Food ID:
python tools/get_usda_ingredient.py <USDA_FOOD_ID>
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"]).
Run the solver to generate an optimal recipe based on your ingredients/ folder.
Basic Usage (Linear Programming - Fast/Optimal):
python solver.pyCommon Options:
python solver.py \
--max_calories 2150 \
--min_proteins 112 \
--allergies "Peanuts" "Shellfish"| 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. |
- Nutrient Profiles:
libnutrient.pycontains 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) orsatisfaction_multiplierin the ingredient YAML files.
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.