Python 解算函数提供相同的输出
我已经为不同的日子编写了以下生成膳食的代码,但是我每天都吃相同的膳食。我想隔天吃“肉”和“素食”食物Python 解算函数提供相同的输出,python,optimization,mathematical-optimization,pulp,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 Por
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
我用果肉来减少糖的摄入,同时限制卡路里的摄入
# 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"
我的问题是输出在所有的日子里都是重复的。我如何在两天之间获得“肉”和“素食者”?@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])
如果您仍然希望显示所有日子都不在肉类和素食之间选择的选项,则需要创建一个更复杂的模型,其中约束指定这些项目的食物变量等于零
两个问题各解决一次
接下来,在一周中的每一天分配一个问题,如:
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)))
如何在您的想法中添加目标函数?我尝试了代码,但它不起作用,我还为prob1和Prob2添加了卡路里约束结果如何?你在for循环之前解决了prob1和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)))