Linear programming in Python : 'module' object has no attribute 'LPX'

放肆的年华 提交于 2019-12-11 02:56:11

问题


For a Django website, I used Thomas Finley's glpk Python library (http://tfinley.net/software/pyglpk/glpk.html#LPX) to solve an integer linear program. I followed his tutorial (see "simple example" in http://tfinley.net/software/pyglpk/discussion.html or at the bottom of the post) to build my instance, but after an update of my system (and, I assume, of python-glpk) I now get this error:

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

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

If you want to reproduce the error, you can use his example which I paste here (the error should happen as soon as the second line):

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

Before I attempt rewriting my code with another library (and by looking quickly for one I have not found a lot of convincing stuff), is there a simple fix to this? (e.g, have the functions used here been renamed to something else?) Thanks in advance for your help.


回答1:


The deprecated "LPX api" (functions and constants starting with 'lpx') was removed in a recent version of glpk. The python bindings need to be updated too.



来源:https://stackoverflow.com/questions/21431403/linear-programming-in-python-module-object-has-no-attribute-lpx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!