PulpSolverError:PuLP:尝试在Python 2.7中执行glpsol时出错

时间:2015-09-21 06:03:34

标签: python python-2.7 pulp

我通过iPython笔记本和Python 2.7在OS X上运行PuLP。使用glpk安装brew install homebrew/science/glpk,并通过pip install pulp安装PuLP。

但是我在Python中遇到错误:

---------------------------------------------------------------------------
PulpSolverError                           Traceback (most recent call last)
<ipython-input-15-689fef0dd94f> in <module>()
      1 # Solve the problem
----> 2 status = prob.solve(GLPK(msg=0))
      3 

/Users/x/anaconda/envs/data/lib/python2.7/site-packages/pulp/pulp.pyc in solve(self, solver, **kwargs)
   1641         #time it
   1642         self.solutionTime = -clock()
-> 1643         status = solver.actualSolve(self, **kwargs)
   1644         self.solutionTime += clock()
   1645         self.restoreObjective(wasNone, dummyVar)

/Users/x/anaconda/envs/data/lib/python2.7/site-packages/pulp/solvers.pyc in actualSolve(self, lp)
    364                              stderr = pipe)
    365             if rc:
--> 366                 raise PulpSolverError("PuLP: Error while trying to execute "+self.path)
    367         else:
    368             if os.name != 'nt':

PulpSolverError: PuLP: Error while trying to execute glpsol

以下是触发此错误的代码:

from pulp import *

#Variables
x = LpVariable('x')
y = LpVariable('y')

# Problem
prob = LpProblem('problem', LpMinimize)

# Constraints
prob += x + y <= 1
prob += x <= 1
prob += -2 + y <= 4

# Objective function to minimize
prob += 

# Solve the problem
status = prob.solve(GLPK(msg=0))

导致错误的原因是什么,以及如何解决?

2 个答案:

答案 0 :(得分:2)

如果你跑

pulp.pulpTestAll()
你可能会看到这样的一行:

Solver pulp.solvers.GLPK_CMD unavailable

如果是这样,你所要做的就是在linux上安装glpk-utils软件包。 如果你成功了,你应该可以打电话

glpsol

来自命令行。

答案 1 :(得分:0)

我在Ubuntu中遇到了同样的错误,这解决了它。

  

make install之后需要发出以下命令:

     

sudo ldconfig

     

Ldconfig创建必要的链接并缓存到最新的   共享库。

https://lists.gnu.org/archive/html/help-glpk/2013-09/msg00018.html