我想以最少的糖摄入量制定一个最佳的饮食计划, 7天,但每日饮食计划应包括3种不同类别的食物。如何添加限制,使我可以从3种不同的食物中获取食物并制定7天的用餐计划?
我的数据框如下:
id name energy sugar Food_Groups
1 4-Grain Flakes 140 58.8 Breakfast
2 Beef Mince, Fried 1443 8.0 Meat
3 Pork 1000 3.0 Meat
4 cake 1200 150 Sweet
5 cheese 1100 140 Sweet
6 Juice 700 85 Drink
7 cabbage 60 13 vegetarian
8 cucumber 10 10 vegetarian
9 eggs 45 30 Breakfast
我创建了一个prob变量,制定了目标函数,创建了字典变量,添加了约束,并按以下方式解决了
# Create the 'prob' variable to contain the problem data
prob = LpProblem("Simple Diet Problem",LpMinimize)
#create data variables and dictionary
food_items = list(df['name'])
calories = dict(zip(food_items,df['energy']))
sugars = dict(zip(food_items,df['sugar']))
food_vars =LpVariable.dicts("Food",food_items,lowBound=0,cat='Integer')
#Building the LP problem by adding the main objective function.
prob += lpSum([sugars[i]*food_vars[i] for i in food_items])
#adding calorie constraint
prob += lpSum([calories[f] * food_vars[f] for f in food_items]) >=
1800.0, "CalorieMinimum"
prob += lpSum([calories[f] * food_vars[f] for f in food_items]) <=
2200.0, "CalorieMaximum"
prob.writeLP("SimpleDietProblem.lp")
prob.solve()
print("Status:", LpStatus[prob.status])
我想知道如何制作7天菜单?我尝试了以下代码,但是重复了相同的食物
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for i in days:
print(days)
prob.writeLP("SimpleDietProblem.lp")
prob.solve(PULP_CBC_CMD())
print("Status:", LpStatus[prob.status])
print("Therefore, the optimal balanced diet consists of\n"+"-")
for v in prob.variables():
if v.varValue:
print(v.name , "=", v.varValue)
print("The total sugar of this balanced diet is: {}".format(round(value(prob.objective),2)))