3

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.

user3078439
  • 305
  • 1
  • 10
  • As per http://stackoverflow.com/questions/11403932/python-attributeerror-module-object-has-no-attribute-serial, have you tried `from glpk import glpk`? – Caramiriel Jan 29 '14 at 13:50
  • Yes I have, but glpk.glpk doesn't have a "LPX" method either.. – user3078439 Jan 29 '14 at 13:57
  • You can use the `help()` function on objects to get more information about these. Try looking for LPX by using `help(glpk)` and `help` on its attributes. – User Jan 29 '14 at 14:41
  • This actually worked for me right out of the box: `print glpk.LPX()` gave me ``. Is it possible that you have your own program called `glpk.py` which is the one being imported? `print glpk.__file__` to check. – DSM Jan 29 '14 at 14:42
  • Which version of glpk do you have? Because as I said, it used to work, but it doesn't after an update of python-glpk. To answer your question I get /usr/local/lib/python2.7/dist-packages/glpk/__init__.pyc – user3078439 Jan 29 '14 at 15:34
  • http://stackoverflow.com/q/25266593/855050 – a06e Aug 12 '14 at 14:16

1 Answers1

2

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.

Thomas
  • 2,093
  • 3
  • 23
  • 28
  • I wish retro-compatibility were more of a concern to developers. This was a real pain, I had to block a previous version of glpk. – user3078439 May 06 '14 at 18:33
  • Yes, but on the other hand, the two APIS coexisted for a couple of years. This transition started in 2006, I think. Additionally there is a compatibility header that brings it back. – Thomas May 10 '14 at 18:37
  • @user3078439 Where can I find this compatibility header? (please answer in http://stackoverflow.com/q/25266593/855050) – a06e Aug 12 '14 at 14:17
  • It's in the examples/oldapi directory of the source code. I also replied in your question. – Thomas Aug 12 '14 at 20:05