我用来解决LPP
的脚本如下:
Script:
# Import PuLP modeler functions
from pulp import *
# Create the 'prob' variable to contain the problem data
prob = LpProblem("The Whiskas Problem",LpMinimize)
LpVariable("example", None, 100)
# The 2 variables Beef and Chicken are created with a lower limit of zero
x1=LpVariable("ChickenPercent",0,None,LpInteger)
x2=LpVariable("BeefPercent",0)
# The objective function is added to 'prob' first
prob += 0.013*x1 + 0.008*x2, "Total Cost of Ingredients per can"
# The five constraints are entered
prob += x1 + x2 == 100, "PercentagesSum"
prob += 0.100*x1 + 0.200*x2 >= 8.0, "ProteinRequirement"
prob += 0.080*x1 + 0.100*x2 >= 6.0, "FatRequirement"
prob += 0.001*x1 + 0.005*x2 <= 2.0, "FibreRequirement"
prob += 0.002*x1 + 0.005*x2 <= 0.4, "SaltRequirement"
# The problem data is written to an .lp file
prob.writeLP("WhiskasModel.lp")
# The problem is solved using PuLP's choice of Solver
prob.solve()
# The status of the solution is printed to the screen
print( "\n", "Status:", LpStatus[prob.status],"\n")
# 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))
Question:
在输出中,我收到与pulp
相关的错误,此代码中应该进行哪些修改才能获得正确的输出?
output:
ImportError Traceback (most recent call last)
<ipython-input-17-35d0a00262fe> in <module>()
1 # Import PuLP modeler functions
----> 2 from pulp import *
3 # Create the 'prob' variable to contain the problem data
4 prob = LpProblem("The Whiskas Problem",LpMinimize)
5 LpVariable("example", None, 100)
ImportError: No module named 'pulp'
答案 0 :(得分:1)
您尚未安装pulp
确保您已经运行:
pip install pulp
或
pip3 install pulp
基于您在Jupyter或iPython笔记本中运行的任何Python内核版本。如果您已设置虚拟环境,请确保您已在虚拟环境中运行Jupyter安装。
此外,PuLP还有一些基础依赖项可能会从您的系统中丢失。要检查是否已正确设置所有内容,请运行:
>>> import pulp
>>> pulp.pulpTestAll()
您应该看到可能阻止模块安装/导入的缺失依赖项列表(如果有)。