斯蒂格勒飲食問題(The Stigler diet Problem),給定人類一天所必須攝入的營養(yǎng) 和 每種食物的營養(yǎng),求出在花費最小的情況下滿足人一天的營養(yǎng)需求洽瞬。
- 數(shù)據(jù)說明
①人體每天必須攝入的營養(yǎng)表
營養(yǎng)種類 | 最低攝入量 |
---|---|
卡路里 (千卡) | 3 |
蛋白質(zhì) (g) | 70 |
鈣 (g) | 0.8 |
鐵 (mg) | 12 |
維生素A (KIU) | 5 |
維生素B1 (mg) | 1.8 |
維生素B2 (mg) | 2.7 |
煙酸 (mg) | 18 |
維生素C (mg) | 75 |
②食物營養(yǎng)表
商品 | 卡路里 (千卡) | 蛋白質(zhì) (g) | 鈣 (g) | 鐵 (mg) | 維生素A (KIU) | 維生素B1 (mg) | 維生素B2 (mg) | 煙酸 (mg) | 維生素C (mg) |
---|---|---|---|---|---|---|---|---|---|
小麥粉(濃縮) | 44.7 | 1411 | 2 | 365 | 0 | 55.4 | 33.3 | 441 | 0 |
通心粉 | 11.6 | 418 | 0.7 | 54 | 0 | 3.2 | 1.9 | 68 | 0 |
小麥谷物(濃縮) | 11.8 | 377 | 14.4 | 175 | 0 | 14.4 | 8.8 | 114 | 0 |
玉米片 | 11.4 | 252 | 0.1 | 56 | 0 | 13.5 | 2.3 | 68 | 0 |
棒子面 | 36 | 897 | 1.7 | 99 | 30.9 | 17.4 | 7.9 | 106 | 0 |
玉米粥 | 28.6 | 680 | 0.8 | 80 | 0 | 10.6 | 1.6 | 110 | 0 |
米 | 21.2 | 460 | 0.6 | 41 | 0 | 2 | 4.8 | 60 | 0 |
燕麥片 | 25.3 | 907 | 5.1 | 341 | 0 | 37.1 | 8.9 | 64 | 0 |
白面包(濃縮) | 15 | 488 | 2.5 | 115 | 0 | 13.8 | 8.5 | 126 | 0 |
全麥面包 | 12.2 | 484 | 2.7 | 125 | 0 | 13.9 | 6.4 | 160 | 0 |
黑麥面包 | 12.4 | 439 | 1.1 | 82 | 0 | 9.9 | 3 | 66 | 0 |
磅蛋糕 | 8 | 130 | 0.4 | 31 | 18.9 | 2.8 | 3 | 17 | 0 |
蘇打餅干 | 12.5 | 288 | 0.5 | 50 | 0 | 0 | 0 | 0 | 0 |
牛奶 | 6.1 | 310 | 10.5 | 18 | 16.8 | 4 | 16 | 7 | 177 |
煉乳(罐裝) | 8.4 | 422 | 15.1 | 9 | 26 | 3 | 23.5 | 11 | 60 |
黃油 | 10.8 | 9 | 0.2 | 3 | 44.2 | 0 | 0.2 | 2 | 0 |
人造黃油 | 20.6 | 17 | 0.6 | 6 | 55.8 | 0.2 | 0 | 0 | 0 |
蛋 | 2.9 | 238 | 1 | 52 | 18.6 | 2.8 | 6.5 | 1 | 0 |
奶酪(切達干酪) | 7.4 | 448 | 16.4 | 19 | 28.1 | 0.8 | 10.3 | 4 | 0 |
奶油 | 3.5 | 49 | 1.7 | 3 | 16.9 | 0.6 | 2.5 | 0 | 17 |
花生醬 | 15.7 | 661 | 1 | 48 | 0 | 9.6 | 8.1 | 471 | 0 |
蛋黃醬 | 8.6 | 18 | 0.2 | 8 | 2.7 | 0.4 | 0.5 | 0 | 0 |
克里斯科 | 20.1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
豬油 | 41.7 | 0 | 0 | 0 | 0.2 | 0 | 0.5 | 5 | 0 |
沙朗牛排 | 2.9 | 166 | 0.1 | 34 | 0.2 | 2.1 | 2.9 | 69 | 0 |
圓牛排 | 2.2 | 214 | 0.1 | 32 | 0.4 | 2.5 | 2.4 | 87 | 0 |
烤肋排 | 3.4 | 213 | 0.1 | 33 | 0 | 0 | 2 | 0 | 0 |
查克烤 | 3.6 | 309 | 0.2 | 46 | 0.4 | 1 | 4 | 120 | 0 |
盤子 | 8.5 | 404 | 0.2 | 62 | 0 | 0.9 | 0 | 0 | 0 |
肝臟(牛肉) | 2.2 | 333 | 0.2 | 139 | 169.2 | 6.4 | 50.8 | 316 | 525 |
羊腿 | 3.1 | 245 | 0.1 | 20 | 0 | 2.8 | 3.9 | 86 | 0 |
羊排(肋) | 3.3 | 140 | 0.1 | 15 | 0 | 1.7 | 2.7 | 54 | 0 |
豬排 | 3.5 | 196 | 0.2 | 30 | 0 | 17.4 | 2.7 | 60 | 0 |
豬里脊烤 | 4.4 | 249 | 0.3 | 37 | 0 | 18.2 | 3.6 | 79 | 0 |
熏肉 | 10.4 | 152 | 0.2 | 23 | 0 | 1.8 | 1.8 | 71 | 0 |
火腿,煙熏 | 6.7 | 212 | 0.2 | 31 | 0 | 9.9 | 3.3 | 50 | 0 |
咸豬肉 | 18.8 | 164 | 0.1 | 26 | 0 | 1.4 | 1.8 | 0 | 0 |
烤雞 | 1.8 | 184 | 0.1 | 30 | 0.1 | 0.9 | 1.8 | 68 | 46 |
小牛肉片 | 1.7 | 156 | 0.1 | 24 | 0 | 1.4 | 2.4 | 57 | 0 |
三文魚,粉紅色(罐裝) | 5.8 | 705 | 6.8 | 45 | 3.5 | 1 | 4.9 | 209 | 0 |
蘋果 | 5.8 | 27 | 0.5 | 36 | 7.3 | 3.6 | 2.7 | 5 | 544 |
香蕉 | 4.9 | 60 | 0.4 | 30 | 17.4 | 2.5 | 3.5 | 28 | 498 |
檸檬 | 1 | 21 | 0.5 | 14 | 0 | 0.5 | 0 | 4 | 952 |
橘子 | 2.2 | 40 | 1.1 | 18 | 11.1 | 3.6 | 1.3 | 10 | 1998 |
綠豆 | 2.4 | 138 | 3.7 | 80 | 69 | 4.3 | 5.8 | 37 | 862 |
卷心菜 | 2.6 | 125 | 4 | 36 | 7.2 | 9 | 4.5 | 26 | 5369 |
蘿卜 | 2.7 | 73 | 2.8 | 43 | 188.5 | 6.1 | 4.3 | 89 | 608 |
芹菜 | 0.9 | 51 | 3 | 23 | 0.9 | 1.4 | 1.4 | 9 | 313 |
萵苣 | 0.4 | 27 | 1.1 | 22 | 112.4 | 1.8 | 3.4 | 11 | 449 |
洋蔥 | 5.8 | 166 | 3.8 | 59 | 16.6 | 4.7 | 5.9 | 21 | 1184 |
土豆 | 14.3 | 336 | 1.8 | 118 | 6.7 | 29.4 | 7.1 | 198 | 2522 |
菠菜 | 1.1 | 106 | 0 | 138 | 918.4 | 5.7 | 13.8 | 33 | 2755 |
紅薯 | 9.6 | 138 | 2.7 | 54 | 290.7 | 8.4 | 5.4 | 83 | 1912 |
桃子(罐頭) | 3.7 | 20 | 0.4 | 10 | 21.5 | 0.5 | 1 | 31 | 196 |
梨(罐頭) | 3 | 8 | 0.3 | 8 | 0.8 | 0.8 | 0.8 | 5 | 81 |
菠蘿(罐裝) | 2.4 | 16 | 0.4 | 8 | 2 | 2.8 | 0.8 | 7 | 399 |
蘆筍(罐頭) | 0.4 | 33 | 0.3 | 12 | 16.3 | 1.4 | 2.1 | 17 | 272 |
綠豆(罐裝) | 1 | 54 | 2 | 65 | 53.9 | 1.6 | 4.3 | 32 | 431 |
豬肉和豆類(罐頭) | 7.5 | 364 | 4 | 134 | 3.5 | 8.3 | 7.7 | 56 | 0 |
玉米(罐頭) | 5.2 | 136 | 0.2 | 16 | 12 | 1.6 | 2.7 | 42 | 218 |
豌豆(罐頭) | 2.3 | 136 | 0.6 | 45 | 34.9 | 4.9 | 2.5 | 37 | 370 |
西紅柿(罐頭) | 1.3 | 63 | 0.7 | 38 | 53.2 | 3.4 | 2.5 | 36 | 1253 |
番茄湯(罐裝) | 1.6 | 71 | 0.6 | 43 | 57.9 | 3.5 | 2.4 | 67 | 862 |
桃子枢冤,干的 | 8.5 | 87 | 1.7 | 173 | 86.8 | 1.2 | 4.3 | 55 | 57 |
李子,干的 | 12.8 | 99 | 2.5 | 154 | 85.7 | 3.9 | 4.3 | 65 | 257 |
葡萄干铜秆,干的 | 13.5 | 104 | 2.5 | 136 | 4.5 | 6.3 | 1.4 | 24 | 136 |
豌豆淹真,干的 | 20 | 1367 | 4.2 | 345 | 2.9 | 28.7 | 18.4 | 162 | 0 |
利馬豆,干的 | 17.4 | 1055 | 3.7 | 459 | 5.1 | 26.9 | 38.2 | 93 | 0 |
海軍豆连茧,干的 | 26.9 | 1691 | 11.4 | 792 | 0 | 38.4 | 24.6 | 217 | 0 |
咖啡 | 0 | 0 | 0 | 0 | 0 | 4 | 5.1 | 50 | 0 |
茶 | 0 | 0 | 0 | 0 | 0 | 0 | 2.3 | 42 | 0 |
可可 | 8.7 | 237 | 3 | 72 | 0 | 2 | 11.9 | 40 | 0 |
巧克力 | 8 | 77 | 1.3 | 39 | 0 | 0.9 | 3.4 | 14 | 0 |
糖 | 34.9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
玉米糖漿 | 14.7 | 0 | 0.5 | 74 | 0 | 0 | 0 | 5 | 0 |
糖蜜 | 9 | 0 | 10.3 | 244 | 0 | 1.9 | 7.5 | 146 | 0 |
草莓蜜餞 | 6.4 | 11 | 0.4 | 7 | 0.2 | 0.2 | 0.4 | 3 | 0 |
中單位已經(jīng)和 中一致
表示每花買到表中各食物的營養(yǎng)量
- 準(zhǔn)備數(shù)據(jù)
將上述數(shù)據(jù)變成數(shù)組趟咆,方便讀取
nutrients = [
['卡路里 (千卡)', 3],
['蛋白質(zhì) (g)', 70],
['鈣 (g)', 0.8],
['鐵 (mg)', 12],
['維生素A (KIU)', 5],
['維生素B1 (mg)', 1.8],
['維生素B2 (mg)', 2.7],
['煙酸 (mg)', 18],
['維生素C (mg)', 75],
]
data = [['小麥粉(濃縮)', 44.7, 1411, 2.0, 365, 0.0, 55.4, 33.3, 441, 0],
['通心粉', 11.6, 418, 0.7, 54, 0.0, 3.2, 1.9, 68, 0],
['小麥谷物(濃縮)', 11.8, 377, 14.4, 175, 0.0, 14.4, 8.8, 114, 0],
['玉米片', 11.4, 252, 0.1, 56, 0.0, 13.5, 2.3, 68, 0],
['棒子面', 36.0, 897, 1.7, 99, 30.9, 17.4, 7.9, 106, 0],
['玉米粥', 28.6, 680, 0.8, 80, 0.0, 10.6, 1.6, 110, 0],
['米', 21.2, 460, 0.6, 41, 0.0, 2.0, 4.8, 60, 0],
['燕麥片', 25.3, 907, 5.1, 341, 0.0, 37.1, 8.9, 64, 0],
['白面包(濃縮)', 15.0, 488, 2.5, 115, 0.0, 13.8, 8.5, 126, 0],
['全麥面包', 12.2, 484, 2.7, 125, 0.0, 13.9, 6.4, 160, 0],
['黑麥面包', 12.4, 439, 1.1, 82, 0.0, 9.9, 3.0, 66, 0],
['磅蛋糕', 8.0, 130, 0.4, 31, 18.9, 2.8, 3.0, 17, 0],
['蘇打餅干', 12.5, 288, 0.5, 50, 0.0, 0.0, 0.0, 0, 0],
['牛奶', 6.1, 310, 10.5, 18, 16.8, 4.0, 16.0, 7, 177],
['煉乳(罐裝)', 8.4, 422, 15.1, 9, 26.0, 3.0, 23.5, 11, 60],
['黃油', 10.8, 9, 0.2, 3, 44.2, 0.0, 0.2, 2, 0],
['人造黃油', 20.6, 17, 0.6, 6, 55.8, 0.2, 0.0, 0, 0],
['蛋', 2.9, 238, 1.0, 52, 18.6, 2.8, 6.5, 1, 0],
['奶酪(切達干酪)', 7.4, 448, 16.4, 19, 28.1, 0.8, 10.3, 4, 0],
['奶油', 3.5, 49, 1.7, 3, 16.9, 0.6, 2.5, 0, 17],
['花生醬', 15.7, 661, 1.0, 48, 0.0, 9.6, 8.1, 471, 0],
['蛋黃醬', 8.6, 18, 0.2, 8, 2.7, 0.4, 0.5, 0, 0],
['克里斯科', 20.1, 0, 0.0, 0, 0.0, 0.0, 0.0, 0, 0],
['豬油', 41.7, 0, 0.0, 0, 0.2, 0.0, 0.5, 5, 0],
['沙朗牛排', 2.9, 166, 0.1, 34, 0.2, 2.1, 2.9, 69, 0],
['圓牛排', 2.2, 214, 0.1, 32, 0.4, 2.5, 2.4, 87, 0],
['烤肋排', 3.4, 213, 0.1, 33, 0.0, 0.0, 2.0, 0, 0],
['查克烤', 3.6, 309, 0.2, 46, 0.4, 1.0, 4.0, 120, 0],
['盤子', 8.5, 404, 0.2, 62, 0.0, 0.9, 0.0, 0, 0],
['肝臟(牛肉)', 2.2, 333, 0.2, 139, 169.2, 6.4, 50.8, 316, 525],
['羊腿', 3.1, 245, 0.1, 20, 0.0, 2.8, 3.9, 86, 0],
['羊排(肋)', 3.3, 140, 0.1, 15, 0.0, 1.7, 2.7, 54, 0],
['豬排', 3.5, 196, 0.2, 30, 0.0, 17.4, 2.7, 60, 0],
['豬里脊烤', 4.4, 249, 0.3, 37, 0.0, 18.2, 3.6, 79, 0],
['熏肉', 10.4, 152, 0.2, 23, 0.0, 1.8, 1.8, 71, 0],
['火腿,煙熏', 6.7, 212, 0.2, 31, 0.0, 9.9, 3.3, 50, 0],
['咸豬肉', 18.8, 164, 0.1, 26, 0.0, 1.4, 1.8, 0, 0],
['烤雞', 1.8, 184, 0.1, 30, 0.1, 0.9, 1.8, 68, 46],
['小牛肉片', 1.7, 156, 0.1, 24, 0.0, 1.4, 2.4, 57, 0],
['三文魚梅屉,粉紅色(罐裝)', 5.8, 705, 6.8, 45, 3.5, 1.0, 4.9, 209, 0],
['蘋果', 5.8, 27, 0.5, 36, 7.3, 3.6, 2.7, 5, 544],
['香蕉', 4.9, 60, 0.4, 30, 17.4, 2.5, 3.5, 28, 498],
['檸檬', 1.0, 21, 0.5, 14, 0.0, 0.5, 0.0, 4, 952],
['橘子', 2.2, 40, 1.1, 18, 11.1, 3.6, 1.3, 10, 1998],
['綠豆', 2.4, 138, 3.7, 80, 69.0, 4.3, 5.8, 37, 862],
['卷心菜', 2.6, 125, 4.0, 36, 7.2, 9.0, 4.5, 26, 5369],
['蘿卜', 2.7, 73, 2.8, 43, 188.5, 6.1, 4.3, 89, 608],
['芹菜', 0.9, 51, 3.0, 23, 0.9, 1.4, 1.4, 9, 313],
['萵苣', 0.4, 27, 1.1, 22, 112.4, 1.8, 3.4, 11, 449],
['洋蔥', 5.8, 166, 3.8, 59, 16.6, 4.7, 5.9, 21, 1184],
['土豆', 14.3, 336, 1.8, 118, 6.7, 29.4, 7.1, 198, 2522],
['菠菜', 1.1, 106, 0.0, 138, 918.4, 5.7, 13.8, 33, 2755],
['紅薯', 9.6, 138, 2.7, 54, 290.7, 8.4, 5.4, 83, 1912],
['桃子(罐頭)', 3.7, 20, 0.4, 10, 21.5, 0.5, 1.0, 31, 196],
['梨(罐頭)', 3.0, 8, 0.3, 8, 0.8, 0.8, 0.8, 5, 81],
['菠蘿(罐裝)', 2.4, 16, 0.4, 8, 2.0, 2.8, 0.8, 7, 399],
['蘆筍(罐頭)', 0.4, 33, 0.3, 12, 16.3, 1.4, 2.1, 17, 272],
['綠豆(罐裝)', 1.0, 54, 2.0, 65, 53.9, 1.6, 4.3, 32, 431],
['豬肉和豆類(罐頭)', 7.5, 364, 4.0, 134, 3.5, 8.3, 7.7, 56, 0],
['玉米(罐頭)', 5.2, 136, 0.2, 16, 12.0, 1.6, 2.7, 42, 218],
['豌豆(罐頭)', 2.3, 136, 0.6, 45, 34.9, 4.9, 2.5, 37, 370],
['西紅柿(罐頭)', 1.3, 63, 0.7, 38, 53.2, 3.4, 2.5, 36, 1253],
['番茄湯(罐裝)', 1.6, 71, 0.6, 43, 57.9, 3.5, 2.4, 67, 862],
['桃子值纱,干的', 8.5, 87, 1.7, 173, 86.8, 1.2, 4.3, 55, 57],
['李子,干的', 12.8, 99, 2.5, 154, 85.7, 3.9, 4.3, 65, 257],
['葡萄干坯汤,干的', 13.5, 104, 2.5, 136, 4.5, 6.3, 1.4, 24, 136],
['豌豆虐唠,干的', 20.0, 1367, 4.2, 345, 2.9, 28.7, 18.4, 162, 0],
['利馬豆,干的', 17.4, 1055, 3.7, 459, 5.1, 26.9, 38.2, 93, 0],
['海軍豆惰聂,干的', 26.9, 1691, 11.4, 792, 0.0, 38.4, 24.6, 217, 0],
['咖啡', 0.0, 0, 0.0, 0, 0.0, 4.0, 5.1, 50, 0],
['茶', 0.0, 0, 0.0, 0, 0.0, 0.0, 2.3, 42, 0],
['可可', 8.7, 237, 3.0, 72, 0.0, 2.0, 11.9, 40, 0],
['巧克力', 8.0, 77, 1.3, 39, 0.0, 0.9, 3.4, 14, 0],
['糖', 34.9, 0, 0.0, 0, 0.0, 0.0, 0.0, 0, 0],
['玉米糖漿', 14.7, 0, 0.5, 74, 0.0, 0.0, 0.0, 5, 0],
['糖蜜', 9.0, 0, 10.3, 244, 0.0, 1.9, 7.5, 146, 0],
['草莓蜜餞', 6.4, 11, 0.4, 7, 0.2, 0.2, 0.4, 3, 0]]
3.聲明求解器
# 導(dǎo)入 OR-Tools線性求解器包裝器疆偿, Glop線性求解器的接口
from ortools.linear_solver import pywraplp
# 實例化Glop解算器并命名它,名稱可以隨便取
solver = pywraplp.Solver('斯蒂格勒飲食問題求解器', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
4.創(chuàng)建變量
注意foods變量就是最終需要線性求解器優(yōu)化的變量搓幌,這里只定義每種食物花費的上下限杆故,線性求解器(pywraplp)得到具體的值
# foods表示每種食物所花費的金額(美元),下限0.0溉愁,上限正無窮
foods = [solver.NumVar(0.0, solver.infinity(), item[0]) for item in data]
print('食物種類為 =', solver.NumVariables())
['out']
食物種類為 = 154
5.定義約束
這里解釋一下处铛,相當(dāng)于函數(shù) y = a1x1 + a2x2+...an*xn
下面代碼中其中solver.Constraint(content, solver.infinity()) 表示y(每種必須的營養(yǎng))的攝入范圍為content(最少攝入量)到正無窮
const.SetCoefficient(foods[j], item[i + 1]) 表示設(shè)置xi的系數(shù)為item[i + 1],也就是說 食物i*某種營養(yǎng)含量i
最終拐揭,每種營養(yǎng)在所有食物的總量都必須高于最小攝入量
# 所有食物營養(yǎng)的總和應(yīng)滿足每種營養(yǎng)的最低要求
# SetCoefficient 表示食物營養(yǎng)總和高于nutrient[1]
constraints = []
for i, (name, content) in enumerate(nutrients):
# 為每種營養(yǎng)設(shè)定上下限 下限為人體每天最少補充量撤蟆,上限為正無窮
const = solver.Constraint(content, solver.infinity())
# 這里為該營養(yǎng) 在所有食物設(shè)定系數(shù)(含量),即 每1美元含量1*食物金額(美元)
# 所有食物中某種營養(yǎng)的攝入量必須高于最小值
for j, item in enumerate(data):
const.SetCoefficient(foods[j], item[i + 1])
print('約束總量為 =', solver.NumConstraints())
6.創(chuàng)建目標(biāo)函數(shù)
objective = solver.Objective()
for food in foods:
# 也是添加系數(shù),1*食物i+1*食物2+...+1*食物n=總金額
objective.SetCoefficient(food, 1)
objective.SetMinimization()
7.輸出結(jié)果
# 檢查問題是否有最佳解決方案
if status != solver.OPTIMAL:
print('問題沒有最優(yōu)解堂污!')
if status == solver.FEASIBLE:
print('找到了一個潛在的次優(yōu)解決方案家肯。')
else:
print('解算器無法解決問題。')
exit(1)
# 顯示每種食物的購買金額(以美元為單位)
# 食品每年的花費
nutrients_result = [0] * len(nutrients)
for i, food in enumerate(foods):
# 如果該食物花費大于0
if food.solution_value() > 0.0:
print(f'{data[i][0]}: ${365. * food.solution_value()}')
for j, _ in enumerate(nutrients):
nutrients_result[j] += data[i][j + 1] * food.solution_value()
print(f'\n最優(yōu)年價格: ${365. * objective.Value()}')
print('\n每日營養(yǎng)攝入:')
for i, nutrient in enumerate(nutrients):
print(f'{nutrient[0]}: {nutrients_result[i]} (min {nutrient[1]})')
['out']
小麥粉(濃縮): $10.774457511918223
肝臟(牛肉): $0.6907834111074193
卷心菜: $4.093268864842877
菠菜: $1.8277960703546996
海軍豆盟猖,干的: $22.275425687243036
最優(yōu)年價格: $39.66173154546625
每日營養(yǎng)攝入:
卡路里 (千卡): 3.000000000000001 (min 3)
蛋白質(zhì) (g): 147.41353494220908 (min 70)
鈣 (g): 0.8000000000000002 (min 0.8)
鐵 (mg): 60.4669221017366 (min 12)
維生素A (KIU): 5.0 (min 5)
維生素B1 (mg): 4.1204388048386225 (min 1.8)
維生素B2 (mg): 2.7000000000000006 (min 2.7)
煙酸 (mg): 27.31598070028833 (min 18)
維生素C (mg): 75.0 (min 75)