'sum_entries / axis'相关错误

时间:2018-02-10 14:48:49

标签: python convex-optimization cvxpy

我是cvxpy的新手,非常感谢您对以下问题的帮助。我写了以下简单的优化代码:

import cvxpy as cvx
import numpy as np

m = 4
n = 3
c = np.array([[2, 3, 0], [4, 0, 5], [2, 3, 4], [5, 0, 3]])
s = np.array([[10, 25, 18, 15]])
d = np.array([[15, 20, 16]])

X = cvx.Variable(m, n)

objective = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(c, X)))

col_sums = cvx.sum_entries(X, axis=0)
row_sums = cvx.sum_entries(X, axis=1)

constraints = [X >= 0, col_sums <= d,
               row_sums.T == s, X[0][2]==0,
               X[1][1] == 0, X[3][1] == 0]
prob = cvx.Problem(objective, constraints)

print "Optimal value", prob.solve()
print "Optimal var"
print X.value # A numpy matrix.

不幸的是,当我运行代码时,会产生以下错误:

runfile('C:/Users/yavigol/Dropbox/Education/GT Optimization/W5/HW5code.py', wdir='C:/Users/yavigol/Dropbox/Education/GT Optimization/W5')
Traceback (most recent call last):

  File "<ipython-input-1-0cb639eae2e3>", line 1, in <module>
    runfile('C:/Users/yavigol/Dropbox/Education/GT Optimization/W5/HW5code.py', wdir='C:/Users/yavigol/Dropbox/Education/GT Optimization/W5')

  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/yavigol/Dropbox/Education/GT Optimization/W5/HW5code.py", line 41, in <module>
    col_sums = cvx.sum_entries(X, axis=0)

TypeError: __init__() got an unexpected keyword argument 'axis'

代码有什么问题,纠正它的最佳方法是什么? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

受过教育的猜测:您使用的是过时版本的cvxpy:

此功能是在0.3.3中引入的(link:git blame on this source-part)。

我在这里看不到任何其他解释(使用当前的1.0-dev分支会导致其他一些错误)。