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

时间:2019-04-25 21:56:22

标签: python optimization mathematical-optimization pulp

我为不同的日期编写了以下代码来生成膳食,但是我每天都会得到相同的膳食。我想隔天有“肉类”和“素食”类食物。

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

我的问题是输出重复了整天。如何在隔日获得“肉”和“素食”?

1 个答案:

答案 0 :(得分:3)

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