纸浆求解功能提供相同的输出

2024-05-13 02:09:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经为不同的日子编写了以下代码来生成膳食,但是我每天都得到相同的膳食。我想每隔一天有“肉”和“素”食团。在

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

我用果肉来减少糖分,同时限制热量的摄入。在

^{pr2}$

我绕过去问题解决()生成不同日期的菜单

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)))

我的问题是,输出总是重复的。我怎样才能隔天“吃肉”和“吃素”??在


Tags: nameinforissugardaysprintsweet
1条回答
网友
1楼 · 发布于 2024-05-13 02:09:06

@Khaned,最简单的方法就是设置两个问题实例。一种是肉类选择,另一种是素食选择。在不同的日子使用每一个。你可以在你想要运行计划的每一周交替开始的问题,以获得两周的膳食计划。在

可以这样设置解算器:

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])

如果你仍然想显示你不想在肉和素食之间选择的选项,你需要创建一个更复杂的模型,用约束指定这些项目的食物变量等于零。在

两个问题各解决一次。在

接下来,在一周中的每一天,将一个问题列在一个列表中,如下所示:

^{pr2}$

然后循环几天并像以前一样打印变量。在

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)))

相关问题 更多 >