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.

6
  • As per stackoverflow.com/questions/11403932/…, have you tried from glpk import glpk? Commented Jan 29, 2014 at 13:50
  • Yes I have, but glpk.glpk doesn't have a "LPX" method either.. Commented Jan 29, 2014 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. Commented Jan 29, 2014 at 14:41
  • This actually worked for me right out of the box: print glpk.LPX() gave me <glpk.LPX 0-by-0 at 0xb728802c>. Is it possible that you have your own program called glpk.py which is the one being imported? print glpk.__file__ to check. Commented Jan 29, 2014 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 Commented Jan 29, 2014 at 15:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
@user3078439 Where can I find this compatibility header? (please answer in stackoverflow.com/q/25266593/855050)
It's in the examples/oldapi directory of the source code. I also replied in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.