繁体   English   中英

纸浆中的线性整数优化

[英]linear integer optimization in pulp

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

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

任何人都可以帮我理解为什么会这样?

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

  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

祝好运。

当我在变量名称中使用非法字符时出现此错误。 从我在纸浆代码中收集的内容(确切地说是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

这在unbuntu中对我有用:

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

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

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

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

自制软件是最好的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM