简体   繁体   English

纸浆中的线性整数优化

[英]linear integer optimization in pulp

I was trying to do the following simple example optimization problem before starting a bigger problem. 在开始一个更大的问题之前,我试图做以下简单的示例优化问题。 The code: 编码:

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)

I am getting the following error: 我收到以下错误:

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

Can anyone please help me to understand why is that? 任何人都可以帮我理解为什么会这样?

I have successfully run your code after doing the following two steps: 执行以下两个步骤后,我已成功运行您的代码:

  1. Download GLPK from 从中下载GLPK

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

  2. Unzip it into (for example) : C:\\glpk_is_here\\ 将其解压缩到(例如): C:\\glpk_is_here\\
  3. Add GLPK binaries to your system path before running python C:\\> set PATH=%PATH%;C:\\glpk_is_here\\glpk-4.55\\w64 在运行python之前将GLPK二进制文件添加到系统路径C:\\> set PATH=%PATH%;C:\\glpk_is_here\\glpk-4.55\\w64

  4. Using the same cmd window from (3) , use python/ipython to run your code: 使用(3)中的相同cmd窗口 ,使用python / ipython运行代码:
    C:\\> ipython your_code.py

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

Good luck. 祝好运。

I got this error when I used illegal characters in the name of the variables. 当我在变量名称中使用非法字符时出现此错误。 From what I can gather in pulp's code (LpElement, to be exact), the characters -+[] ->/ are not allowed and are all replaced by an underscore. 从我在纸浆代码中收集的内容(确切地说是LpElement),字符-+[] ->/是不允许的,并且都被下划线替换。

Since discovering the error, I preprocess my variable names with the following function, fixing the issue: 自从发现错误后,我使用以下函数预处理我的变量名称,修复问题:

  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

This works for me in unbuntu: 这在unbuntu中对我有用:

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

I think in windows there is a similar solution 我认为在Windows中有一个类似的解决方案

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

For Mac -- brew install glpk on terminal. 对于Mac - brew install glpk在终端上brew install glpk

Homebrew is the best. 自制软件是最好的。

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

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