Python 中的线性编程:“模块”对象没有属性“LPX”

2024-01-06

对于 Django 网站,我使用了 Thomas Finley 的 glpk Python 库(http://tfinley.net/software/pyglpk/glpk.html#LPX http://tfinley.net/software/pyglpk/glpk.html#LPX) 求解整数线性规划。我按照他的教程进行操作(参见“简单示例”http://tfinley.net/software/pyglpk/discussion.html http://tfinley.net/software/pyglpk/discussion.html或在帖子的底部)构建我的实例,但是在更新我的系统(并且我假设是 python-glpk)之后,我现在收到此错误:

----> 1 lp = glpk.LPX()

AttributeError: 'module' object has no attribute 'LPX'

如果您想重现该错误,可以使用我粘贴在这里的示例(错误应该在第二行时发生):

import glpk            # Import the GLPK module
lp = glpk.LPX()        # Create empty problem instance
lp.name = 'sample'     # Assign symbolic name to problem
lp.obj.maximize = True # Set this as a maximization problem
lp.rows.add(3)         # Append three rows to this instance
for r in lp.rows:      # Iterate over all rows
r.name = chr(ord('p')+r.index) # Name them p, q, and r
lp.rows[0].bounds = None, 100.0  # Set bound -inf < p <= 100
lp.rows[1].bounds = None, 600.0  # Set bound -inf < q <= 600
lp.rows[2].bounds = None, 300.0  # Set bound -inf < r <= 300
lp.cols.add(3)         # Append three columns to this instance
for c in lp.cols:      # Iterate over all columns
    c.name = 'x%d' % c.index # Name them x0, x1, and x2
    c.bounds = 0.0, None     # Set bound 0 <= xi < inf
lp.obj[:] = [ 10.0, 6.0, 4.0 ]   # Set objective coefficients
lp.matrix = [ 1.0, 1.0, 1.0,     # Set nonzero entries of the
              10.0, 4.0, 5.0,     #   constraint matrix.  (In this
              2.0, 2.0, 6.0 ]    #   case, all are non-zero.)
lp.simplex()           # Solve this LP with the simplex method

在我尝试用另一个库重写我的代码之前(通过快速查找一个库,我没有找到很多令人信服的东西),是否有一个简单的解决方案? (例如,此处使用的函数是否已重命名为其他名称?) 在此先感谢您的帮助。


已弃用的“LPX api”(以“lpx”开头的函数和常量)在最新版本的 glpk 中已被删除。 python 绑定也需要更新。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 中的线性编程:“模块”对象没有属性“LPX” 的相关文章

随机推荐