Optimize daily food intake using nutrient requirements, food-specific limits, energy-share constraints (E%), and custom linear dietary constraints.
Clone the repository and activate the project environment:
julia --project=.Run interactively:
using Revise
using FoodOptimizer
main()Or run directly:
julia --project=. -e 'using FoodOptimizer; FoodOptimizer.main()'All file locations, optimisation settings, objectives and output options are configured through config.yml.
Typical configuration options include:
model:
objective: RI
energy_unit: kJ
energy_intake: 2500 kcal
food_item_limit: 10
threshold: 0.01The optimiser uses the following input files (in the data folder):
| File | Purpose |
|---|---|
matvaretabellen_2026.csv |
Main food database containing nutrient content per 100 g edible portion. |
matvaretabellen_extra.csv |
Additional user-defined foods not present in the main database. |
food_limits.csv |
Optional lower bounds, upper bounds and recommended amounts for individual foods. |
nutrient_limits.csv |
Lower bounds, upper bounds and recommended intake levels for nutrients. |
energy_limits.csv |
Lower bounds, upper bounds and recommended energy shares (E%). |
custom_constraints.csv |
Additional custom dietary constraints expressed as linear equations or inequalities. |
Each row in the food database represents a food identified by a unique Matvare ID.
Nutrient values are specified per 100 g edible portion.
Additional foods may be added through matvaretabellen_extra.csv.
Example:
Matvare ID,Matvare,Kalsium (Ca) (mg),Jod (I) (µg)
99.001,"Svensk salt, bordsalt, jodert, Jozo",10,5000Columns missing from matvaretabellen_extra.csv are automatically added during import.
Nutrient requirements are configured in nutrient_limits.csv:
Description,Lower,Upper,Recommended
Protein (g),52.8,135,66.4
Vitamin B12 (kobalamin) (µg),2,,4
Salt (NaCl) (g),0,5.75,Rules:
Lowerdefines a minimum intake.Upperdefines a maximum intake.Recommendedis used only by theRIobjective.- Empty cells disable the corresponding constraint.
Food-specific limits are configured in food_limits.csv:
Matvare ID,Matvare,Lower,Upper,Recommended
99.001,"Svensk salt, bordsalt, jodert, Jozo",,,0Rules:
Lowerdefines a minimum amount.Upperdefines a maximum amount.Recommendedis used by theRIobjective.- Amounts are expressed in model units of 100 g.
Example:
1.5 = 150 g/day
Energy-share constraints are defined in energy_limits.csv.
Example:
Description,Lower,Upper,Recommended
Fett (E%),25,40,32.5
Karbohydrat (E%),45,60,52.5
Protein (E%),10,20,15FoodOptimizer calculates energy shares from fat, carbohydrates, protein and alcohol using configurable energy factors:
| Nutrient | kcal/g | kJ/g |
|---|---|---|
| Fat | 9 | 37 |
| Carbohydrate | 4 | 17 |
| Protein | 4 | 17 |
| Alcohol | 7 | 29 |
The unit is selected through:
model:
energy_unit: kJor
model:
energy_unit: kcalAdditional constraints may be defined in custom_constraints.csv.
Example:
Description,Constraint
Andel umettede fettsyrer,[Enumettede fettsyrer (g)]+[Flerumettede fettsyrer (g)]>=2/3*[Fett (g)]
Tilsatt og fritt sukker,[Sukker, tilsatt (E%)]+[Sukker, fritt (E%)]<10Supported operators:
<=
>=
<
>
=
==
≤
≥
Supported syntax:
[Nutrient]
coefficient*[Nutrient]
constant
fractions such as 2/3
Energy-share variables may be referenced using:
[Fett (E%)]
[Protein (E%)]
The special variable
[total_energy]
is also supported.
Mixing E% terms and ordinary nutrient variables within the same custom constraint is not supported.
model:
objective: costMinimises total food cost while satisfying all constraints.
model:
objective: RIMinimises normalised absolute deviation from:
- nutrient recommendations in
nutrient_limits.csv - food recommendations in
food_limits.csv - energy share recommendations in
energy_limits.csv
while satisfying all lower and upper bounds.
The number of selected foods can be restricted:
model:
food_item_limit: 10
threshold: 0.01When food_item_limit > 0, binary decision variables are introduced and the optimiser can select at most the specified number of foods.
Foods with amounts below threshold are treated as not selected.
The optimiser can generate:
- optimal food amounts
- energy-share summary
- nutrient summary
- scaled nutrient contribution per selected food
- binding constraint report
- total daily cost
- reduced costs for selected foods (LP models only)
Results may optionally be exported to Excel:
io:
write_results_to_excel: trueSheets:
food_amountsenergy_summarynutrient_summaryscaled_foods
From the Matvaretabellen 2026 Excel workbook, export the sheet:
Matvarer (alle næringsstoffer)
Remove category header rows such as:
Grønnsaker
Drikke
Frukt
and export the remaining data to CSV.
If values are stored with decimal commas, convert them prior to export. One approach is to create a helper sheet using:
=NUMBERVALUE('Matvarer (alle næringsstoffer)'!C2, ",", " ")
and copy the formula over all numeric cells.
This software is intended for educational, research and informational purposes only.
It is not a substitute for professional dietary, nutritional or medical advice.
- Helsedirektoratet. Referanseverdier for energi og næringsstoffer. https://www.helsedirektoratet.no/rapporter/referanseverdier-for-energi-og-naeringsstoffer
- Matvaretabellen 2026. Mattilsynet. https://www.matvaretabellen.no