在 python 中解决 LPP 时出错

Getting error while solving LPP in python

我用来解决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'

您还没有安装pulp

确保您运行:

pip install pulp

pip3 install pulp 

基于您在 Jupyter 或 iPython 笔记本中 运行 的任何 Python 内核版本。如果您设置了虚拟环境,请确保您已经 运行 在虚拟环境中安装了 Jupyter。

此外,PuLP 有一些您的系统可能缺少的基础依赖项。要检查您是否已正确设置所有内容,运行:

>>> import pulp
>>> pulp.pulpTestAll()

您应该会看到一个缺失的依赖项列表(如果有的话),这些依赖项可能会停止 installing/importing 中的模块。