2

在开始一个更大的问题之前,我试图做以下简单的示例优化问题。编码:

from pulp import *
x = LpVariable("x", 0, 3)
y = LpVariable("y", 0, 1)
prob = LpProblem("myProblem", LpMinimize)

prob += x + y <= 2
#objective function
prob += -4*x + y

status = prob.solve(GLPK(msg = 0))
#results
value(x)

我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\mahabubalam\Desktop\Works\GUI\whiskas.py", line 85, in <module>
    status = prob.solve(GLPK(msg = 0))
  File "C:\Python34\lib\site-packages\pulp-1.5.6-py3.4.egg\pulp\pulp.py", line 1619, in solve
    status = solver.actualSolve(self, **kwargs)
  File "C:\Python34\lib\site-packages\pulp-1.5.6-py3.4.egg\pulp\solvers.py", line 335, in actualSolve
    raise PulpSolverError("PuLP: cannot execute "+self.path)
pulp.solvers.PulpSolverError: PuLP: cannot execute glpsol.exe

谁能帮我理解为什么会这样?

4

5 回答 5

4

执行以下两个步骤后,我已成功运行您的代码:

  1. 从下载 GLPK

    http://sourceforge.net/projects/winglpk/files/latest/download(如 oyvind 所述)

  2. 将其解压缩到(例如): C:\glpk_is_here\
  3. 在运行 python 之前将 GLPK 二进制文件添加到系统路径 C:\>set PATH=%PATH%;C:\glpk_is_here\glpk-4.55\w64

  4. 使用 (3) 中的相同 cmd 窗口,使用 python/ipython 运行您的代码:
    C:\>ipython your_code.py

  5. 查看结果 Out[4]: 2.0

祝你好运。

于 2015-03-27T19:43:20.053 回答
2

当我在变量名称中使用非法字符时出现此错误。从我在纸浆的代码(确切地说是 LpElement)中收集到的信息来看,这些字符-+[] ->/是不允许的,并且全部被下划线替换。

发现错误后,我使用以下函数对变量名进行预处理,从而解决了问题:

  def variableName(s):
    # illegalChars = "-+[] ->/"
    s = s.replace("-","(hyphen)")
    s = s.replace("+","(plus)")
    s = s.replace("[","(leftBracket)")
    s = s.replace("]","(rightBracket)")
    s = s.replace(" ","(space)")
    s = s.replace(">","(greaterThan)")
    s = s.replace("/","(slash)")
    return s
于 2015-05-30T20:29:52.083 回答
2

这在 unbuntu 中对我有用:

   sudo apt-get install python-glpk  
   sudo apt-get install glpk-utils

我认为在 Windows 中有类似的解决方案

于 2016-09-26T10:09:42.043 回答
1

安装 GLPK,例如从 sourceforge.net/projects/winglpk

于 2014-07-18T22:05:20.290 回答
1

对于 Mac -brew install glpk在终端上。

自制是最好的。

于 2016-03-09T20:21:29.277 回答