为什么GLPK会产生错误`无法执行glpsol.exe`,而默认的PuLP求解器工作正常?

时间:2016-06-08 06:43:33

标签: python mathematical-optimization linear-programming pulp

在尝试使用PuLP解决二进制整数编程问题并使用GLPK作为求解器时,出现以下错误。我在Windows上安装了GLPK,也设置了路径。 pulp.pulpTestAll()的结果显示Solver <class 'pulp.solvers.GLPK_CMD'> passed

Traceback (most recent call last):
  File "C:\Python34\Cloud 5.py", line 464, in <module>
    resource(request, pmachine, l, q)
  File "C:\Python34\Cloud 5.py", line 238, in resource
    status = prob.solve(pulp.GLPK())
  File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 1643, in solve
    status = solver.actualSolve(self, **kwargs)
  File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\solvers.py", line 346, in actualSolve
    raise PulpSolverError("PuLP: cannot execute "+self.path)
pulp.solvers.PulpSolverError: PuLP: cannot execute glpsol.exe

然而,当我使用默认的PuLP解算器时,它运行得非常好。我正在使用status = prob.solve(pulp.GLPK())来调用解算器。完整的Python代码很长,因为我没有在这里发布它。从prob.writeLP('problem.txt')获得的PuLP输出如下:

\* Resource *\
Maximize
OBJ: 2 _y11 + 2 _y12 + 10 _y21 + 10 _y22
Subject To
_dummy: __dummy = 0
_C1: __dummy = 0
_C10: 100 _x121 + 200 _x221 <= 1024
_C11: _y11 + _y12 <= 1
_C12: _y21 + _y22 <= 1
_C13: _x111 - _y11 <= 0
_C14: _x112 - _y11 <= 0
_C15: _x121 - _y12 <= 0
_C16: _x211 - _y21 <= 0
_C17: _x212 - _y21 <= 0
_C18: _x221 - _y22 <= 0
_C19: _x111 + _x121 = 0
_dummy: __dummy = 0
_C2: __dummy = 0
_C20: _x211 + _x221 = 0
_C21: _x111 + _x121 = 0
_C22: _x211 + _x221 = 0
_C23: _x111 = 0
_C24: _x121 = 0
_C25: _x211 = 0
_C26: _x221 = 0
_C27: _x111 + _x112 + _x121 = 1
_C28: _x211 + _x212 + _x221 = 1
_dummy: __dummy = 0
_C3: __dummy = 0
_dummy: __dummy = 0
_C4: __dummy = 0
_C5: _x111 + 2 _x211 <= 4
_C6: _x112 + 2 _x212 <= 8
_C7: _x121 + 2 _x221 <= 4
_C8: 100 _x111 + 200 _x211 <= 1024
_C9: 100 _x112 + 200 _x212 <= 2000
Bounds
__dummy = 0
Binaries
_x111
_x112
_x121
_x211
_x212
_x221
_y11
_y12
_y21
_y22
End

为什么GLPK会产生错误?使用默认解算器,获得的输出为:

Optimal
Objective value: 12.0

The values of the variables : 

__dummy = None
_x111 = 0.0
_x112 = 1.0
_x121 = 0.0
_x211 = 0.0
_x212 = 1.0
_x221 = 0.0
_y11 = 1.0
_y12 = 0.0
_y21 = 1.0
_y22 = 0.0

1 个答案:

答案 0 :(得分:1)

实际上这不是答案。我打算将此添加为评论。但由于我现在没有50个声誉,所以我不能发表评论。所以我会在这里作为答案。 我觉得问题不在你的程序中。你的程序应该没问题。但可能是环境变量或权限中的一些问题。如果cannot execute glpsol.exe适合你,那么它应该至少没有这样的错误def executable(command): if os.path.isabs(command): if os.path.exists(command) and os.access(command, os.X_OK): return command for path in os.environ.get("PATH", []).split(os.pathsep): new_path = os.path.join(path, command) if os.path.exists(new_path) and os.access(new_path, os.X_OK): return os.path.join(path, command) return False executable = staticmethod(executable) 。但是您可能会无意中更改某些可能导致此问题的设置。

所以这里有一些有用的东西。查看PuLP中的这段代码,这就是您的程序失败的原因。

git fetch upstream b2
git checkout origin/b1
git merge upstream b2 (which shows a Fast-forward)
git push origin b1 (Which shows "Everything up-to-date")

不同的原因,例如文件路径中是否存在此类命令,是否具有访问权限,是否在环境路径中等等。您可能需要手动检查每个命令,然后您就会知道为什么节目失败。