变量总和不匹配上的混合问题

问题描述 投票:0回答:1

I'm装盘重现这种混合example但使用较少的变量,这部分工作得很好:

import pulp
from pulp import *

# Creates a list of the Ingredients
Ingredients = ['CHICKEN', 'BEEF', 'MUTTON', 'RICE']

# A dictionary of the costs of each of the Ingredients is created
costs = {'CHICKEN': 15, 
         'BEEF': 12, 
         'MUTTON': 17, 
         'RICE': 12
        }

# A dictionary of the protein percent in each of the Ingredients is created
proteinPercent = {'CHICKEN': 17, 
                  'BEEF': 2, 
                  'MUTTON': 16, 
                  'RICE': 8
                 }

# A dictionary of the fat percent in each of the Ingredients is created
fatPercent = {'CHICKEN': 10, 
              'BEEF': 14, 
              'MUTTON': 13, 
              'RICE': 16, 
              }

# Create the 'prob' variable to contain the problem data
prob = LpProblem("The Whiskas Problem", LpMinimize)

# A dictionary called 'ingredient_vars' is created to contain the referenced Variables
ingredient_vars = LpVariable.dicts("Ingr",Ingredients,0)

# The objective function is added to 'prob' first
prob += lpSum([costs[i]*ingredient_vars[i] for i in Ingredients]), "Total Cost of Ingredients per can"

# The  constraints are added to 'prob'
prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 15.5, "ProteinRequirement"
prob += lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 12.3, "FatRequirement"


prob.writeLP("WhiskasModel.lp")
prob.solve()
# The status of the solution is printed to the screen
print ("Status:", LpStatus[prob.status])

# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
    print (v.name, "=", v.varValue)

# The optimised objective function value is printed to the screen
print ("Total Cost of Ingredients per can = ", value(prob.objective))

它可以计算各成分的所需的最佳量:

Status: Optimal
Ingr_BEEF = 0.0
Ingr_CHICKEN = 0.77916667
Ingr_MUTTON = 0.0
Ingr_RICE = 0.28177083
Total Cost of Ingredients per can =  15.068750009999999

但是,这并不等于100%,当我添加约束为实现这一目标的代码:

prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 100, "PercentagesSum"

我得到这样的结果:

Status: Optimal
Ingr_BEEF = 100.0
Ingr_CHICKEN = 0.0
Ingr_MUTTON = 0.0
Ingr_RICE = 0.0
Total Cost of Ingredients per can =  1200.0

这是不对的,因为它不符合其他含有。

编辑

看来我这interprete走错了路,我想这样的:如果我要生产3台,输入的总和应为3。

我认为是这样,那么:

# The constraints are added to 'prob'
prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 3, "PercentagesSum"
prob += lpSum(ingredient_vars["CHICKEN"]) <= 2, "CHICKEN"
prob += lpSum(ingredient_vars["BEEF"]) <= 1, "BEEF"
prob += lpSum(ingredient_vars["MUTTON"]) <= 1, "MUTTON"
prob += lpSum(ingredient_vars["RICE"]) <= 1, "RICE"

其中2,1,1,1是可用于每个原料的数量。

python optimization linear-programming pulp
1个回答
2
投票

因为它不符合其他含有这是不对的。[六]

其中约束被侵犯?如果你看一下你是如何定义的约束,你会发现,他们都满足。

prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 15.5, "ProteinRequirement"

其给出的100个单位牛肉解决方案意味着你有2 * 100 = 200个单位的蛋白质 - 远远超过需要15.5。

prob += lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 12.3, "FatRequirement"

其中给定的100个单位牛肉溶液意味着你有14 * 100 = 1400单位蛋白 - 远远超过需要12.3的。

真正的问题是,我想你混淆了单位位。当乘以百分比,你需要通过除以100。

© www.soinside.com 2019 - 2024. All rights reserved.