为什么 GLPK 会产生错误“无法执行 glpsol.exe”,而默认的 PuLP 求解器可以完美运行?
Why does GLPK produce the error `cannot execute glpsol.exe` while the default PuLP solver works perfectly?
在尝试使用 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
其实这不是答案。我打算将其添加为评论。但我不能写评论,因为我现在没有 50 的声誉。所以我就把它作为答案放在这里。
我觉得问题不在你的程序中。你的程序应该没问题。但可能是环境变量或权限中的一些问题。如果 pulp.pulpTestAll()
对你有效,那么它应该至少没有这样的错误 cannot execute glpsol.exe
。但是您可能无意中更改了某些可能导致此问题的设置。
这里有一些有用的东西。查看 PuLP 中的这段代码,这就是您的程序失败的原因。
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 解决二进制整数规划问题并使用 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
其实这不是答案。我打算将其添加为评论。但我不能写评论,因为我现在没有 50 的声誉。所以我就把它作为答案放在这里。
我觉得问题不在你的程序中。你的程序应该没问题。但可能是环境变量或权限中的一些问题。如果 pulp.pulpTestAll()
对你有效,那么它应该至少没有这样的错误 cannot execute glpsol.exe
。但是您可能无意中更改了某些可能导致此问题的设置。
这里有一些有用的东西。查看 PuLP 中的这段代码,这就是您的程序失败的原因。
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)
各种原因,例如文件路径中是否存在此类命令,是否具有访问权限,是否在环境路径中等。您可能需要手动检查每一个,然后您就会确定为什么你的程序失败了。