0

我是 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'

代码有什么问题以及纠正它的最佳方法是什么?先感谢您!

4

1 回答 1

0

有根据的猜测:您使用的是过时的 cvxpy 版本:

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

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

于 2018-02-10T22:41:26.490 回答