Backend for my blog post on finding the optimal diet and identifying superfoods. Solves the following LP for a range of tradeoff parameters and plots the results:
Minimize
subject to
where
git clone <url>
cd Nut-Opt
pip install .
python -m nutopt -f <food_list> -a <amino y/n> --n_sweep <n> --n_plot <n> --plotdir <dir>
-f
is a space-separated list of foods to include in the optimization. If not specified, all foods are included.
-a
is a flag to include amino acids in the optimization constraints. If not specified, amino acids are not included.
--n_sweep
is the number of tradeoff parameters
--n_plot
is the number of superfoods to radar plot.
The nutritional dataset in nutopt/data/
is from kaggle, originally sourced here.
These are editable in nutopt/definitions.py
. The default constraints are:
50 < total_fat (g) < 120
10 < saturated_fat (g) < 25
70 < protein (g) < 100
130 < carbohydrate (g) < 400
550 < choline (mg) < 2500
400 < folate (mcg) < 900
400 < folic_acid (mcg) < 900
16 < niacin (mg) < 30
5 < pantothenic_acid (mg) < 100
1.3 < riboflavin (mg) < 500
1.2 < thiamin (mg) < 200
3000 < vitamin_a (IU) < 8000
2.4 < vitamin_b12 (mcg) < 8
1.3 < vitamin_b6 (mg) < 4
90 < vitamin_c (mg) < 1000
600 < vitamin_d (IU) < 5000
15 < vitamin_e (mg) < 500
120 < vitamin_k (mcg) < 500
1500 < sodium (mg) < 3000
1300 < calcium (mg) < 2000
0.9 < copper (mg) < 7
8 < iron (mg) < 35
400 < magnesium (mg) < 500
2.3 < manganese (mg) < 7
700 < phosphorous (mg) < 3500
4700 < potassium (mg) < 7000
55 < selenium (mcg) < 300
11 < zinc (mg) < 35
0.287 < cystine (g) < inf
0.7 < histidine (g) < inf
1.4 < isoleucine (g) < inf
2.73 < leucine (g) < inf
2.1 < lysine (g) < inf
0.728 < methionine (g) < inf
0.875 < phenylalanine (g) < inf
1.05 < threonine (g) < inf
0.28 < tryptophan (g) < inf
0.875 < tyrosine (g) < inf
1.82 < valine (g) < inf
38 < fiber (g) < 55
0 < sugars (g) < 40
17 < polyunsaturated_fatty_acids (g) < 30
50 < cholesterol (mg) < 250
0 < alcohol (g) < 5
100 < water (g) < 2000
For all foods in the dataset and all default constraints active. The optimal tradeoff curve between calories and weight is shown below.
And here is the optimal diet for
Total calories: 871.1281429945614 kcal
Total weight: 1113.0965300000835 g
4.412183 Cauliflower, raw, green
1.453970 Watercress, raw
0.787405 Bamboo shoots, without salt, drained, boiled, cooked
0.657726 Chicken spread
0.588269 Cocoa, processed with alkali, unsweetened, dry powder
0.569966 Fish, raw, mixed species, roe
0.518734 Mushrooms, grilled, exposed to ultraviolet light, portabella
0.516959 Tofu, prepared with calcium sulfate, salted and fermented (fuyu)
0.506891 Cabbage, without salt, drained, boiled, cooked, chinese (pak-choi)
0.361921 Cereals ready-to-eat, FIBER ONE Bran Cereal, GENERAL MILLS
0.292224 Gelatin desserts, with aspartame, reduced calorie, dry mix
0.147996 Oil, soybean lecithin'
0.146714 Vitasoy USA Nasoya, Lite Silken Tofu
0.095675 Yeast extract spread
0.048087 Oil, wheat germ
0.023411 Mushrooms, raw, exposed to ultraviolet light, or crimini, italian, brown
0.002834 Desserts, unsweetened, tablets, rennin
Count of how many times foods appear in optimal diets.
Nutritional characteristics of the top four superfoods.