對於Django網站,我使用Thomas Finley的glpk Python庫(http://tfinley.net/software/pyglpk/glpk.html#LPX)來求解整數線性程序。我遵循他的教程(請參閱http://tfinley.net/software/pyglpk/discussion.html中的「簡單示例」或帖子底部)來構建我的實例,但在更新我的系統(以及我假設的python-glpk)之後,我現在得到以下錯誤:Python中的線性編程:'模塊'對象沒有屬性'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
:
----> 1 lp = glpk.LPX()
AttributeError: 'module' object has no attribute 'LPX'
如果要重現錯誤,你可以使用他的例子,我貼在這裏(誤差應儘快發生,因爲第二行)另一個庫(通過快速尋找一個我沒有找到很多令人信服的東西),是否有一個簡單的修復? (例如,這裏使用的函數是否已重新命名爲其他內容?) 在此先感謝您的幫助。
按照http://stackoverflow.com/questions/11403932/python-attributeerror-module-object-has-no-attribute-serial,你嘗試過'從glpk導入glpk'嗎? – Caramiriel
是的,我有,但是glpk.glpk沒有「LPX」方法。 – user3078439
您可以在對象上使用'help()'函數來獲取更多關於這些信息的信息。嘗試通過對其屬性使用「help(glpk)」和「help」來查找LPX。 – User