gpt4 book ai didi

python - pulp solve 函数给出相同的输出

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:37 28 4
gpt4 key购买 nike

我为不同的日子编写了以下代餐代码,但我每天都吃同样的餐。我想隔天吃“肉”和“素食”食物。

my dataframe is as follows:

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

我正在使用 PuLP 来减少糖分,同时限制卡路里摄入量。

# 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.solve() 以生成不同日期的菜单

prob.writeLP("SimpleDietProblem.lp")
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for i in days:
print(i)

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: {}\n\n".format(round(value(prob.objective),2)))

我的问题是输出一直重复。如何隔天“吃肉”和“吃素”??

最佳答案

@Khaned,做你想做的最简单的方法是设置两个 Problem 实例。一个有肉类选择,另一个有素食选择。在不同的日子使用每一个。您可以在每个星期交替开始问题,您希望运行该计划以获得为期两周的膳食计划。

您可以像这样设置求解器:

prob1 = LpProblem("Simple Diet Problem Meat Day",LpMinimize)
prob2 = LpProblem("Simple Diet Problem Vegetarian Day",LpMinimize)
#create data variables and dictionary
day1_df = df[df['Food_Groups'] != 'vegetarian']
day1_items = list(day1_df['name'])
day1_calories = dict(zip(day1_items,day1_df['energy']))
day1_sugars = dict(zip(day1_items,day1_df['sugar']))
day2_df = df[df['Food_Groups'] != 'Meat']
day2_items = list(day2_df['name'])
day2_calories = dict(zip(day2_items,day2_df['energy']))
day2_sugars = dict(zip(day2_items,day2_df['sugar']))
# variables
day1_vars =LpVariable.dicts("Food",day1_items,lowBound=0,cat='Integer')
day2_vars =LpVariable.dicts("Food",day2_items,lowBound=0,cat='Integer')

#Building the LP problem by adding the main objective function.
prob1 += lpSum([day1_sugars[i]*day1_vars[i] for i in day1_items])
prob2 += lpSum([day2_sugars[i]*day2_vars[i] for i in day2_items])

如果您仍想显示您不想整天在肉类和素食之间选择的选项,则需要创建一个更复杂的模型,并使用约束将这些项目的 food_vars 指定为零。

两个问题各做一次。

接下来,在一周中的每一天分配一个列表中的问题,例如:

days = [('Monday', prob1), ('Tuesday', prob2), ...]

然后像你已经做的那样循环这些天并打印变量。

for day, prob in days:
print(day)
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: {}\n\n".format(round(value(prob.objective),2)))

关于python - pulp solve 函数给出相同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55858168/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com