我想转换以下SDP - 它只是验证了约束的可行性 - 从CVX(MATLAB)到CVXPY(Python):
Ah = [1.0058, -0.0058; 1, 0];
Bh = [-1; 0];
Ch = [1.0058, -0.0058; -0.9829, 0.0056];
Dh = [-1; 1];
M = [0, 1;1, 0];
ni = size(M,1)/2;
n = size(Ah,1);
rho = 0.5;
cvx_begin sdp quiet
variable P(n,n) semidefinite
variable lambda(ni) nonnegative
Mblk = M*kron(diag(lambda),eye(2));
lambda(ni) == 1 % break homogeneity (many ways to do this...)
[Ah Bh]'*P*[Ah Bh] - rho^2*blkdiag(P,0) + [Ch Dh]'*Mblk*[Ch Dh] <= 0
cvx_end
switch cvx_status
case 'Solved'
feas = 1;
otherwise
feas = 0;
end
下面是我的Python代码,
import cvxpy as cvx
import numpy as np
import scipy as sp
Ah = np.array([[1.0058, -0.0058], [1, 0]])
Bh = np.array([[-1], [0]])
Ch = np.array([[1.0058, -0.0058], [-0.9829, 0.0056]])
Dh = np.array([[-1], [1]])
M = np.array([[0, 1], [1, 0]])
ni, n = M.shape[0] / 2, Ah.shape[0]
rho = 0.5
P = cvx.Semidef(n)
lamda = cvx.Variable()
Mblk = np.dot(M, np.kron(cvx.diag(lamda), np.eye(2)))
ABh = np.concatenate((Ah, Bh), axis=1)
CDh = np.concatenate((Ch, Dh), axis=1)
constraints = [lamda[-1] == 1,
np.dot(ABh.T, np.dot(P, ABh)) - rho**2*np.linalg.block_diag(P, 0) +
np.dot(CDh.T, np.dot(Mblk, CDh)) << 0]
prob = cvx.Problem(cvx.Minimize(1), constraints)
feas = prob.status is cvx.OPTIMAL
运行程序时有几个错误。 1.当我打印Mblk时,显示
追踪(最近一次呼叫最后一次):
文件 “/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py” 第2820行,在run_code中
Out [1]:exec code_obj in self.user_global_ns,self.user_ns
文件“”,第1行,
mblk的
文件“/usr/lib/python2.7/dist-packages/IPython/core/displayhook.py”, 第247行,致电
format_dict,md_dict = self.compute_format_data(result)
文件“/usr/lib/python2.7/dist-packages/IPython/core/displayhook.py”, 第157行,在compute_format_data
中返回self.shell.display_formatter.format(result)
文件“/usr/lib/python2.7/dist-packages/IPython/core/formatters.py”, 第152行,格式为
data = formatter(obj)
文件“/usr/lib/python2.7/dist-packages/IPython/core/formatters.py”, 第481行,致电
printer.pretty(OBJ)
文件“/usr/lib/python2.7/dist-packages/IPython/lib/pretty.py”,行 362,漂亮
return _default_pprint(obj,self,cycle)
文件“/usr/lib/python2.7/dist-packages/IPython/lib/pretty.py”,行 482,在_default_pprint
p.text(再版(OBJ))
文件“/usr/lib/python2.7/dist-packages/numpy/core/numeric.py”,行 1553,在array_repr
中',',“array(”)
文件“/usr/lib/python2.7/dist-packages/numpy/core/arrayprint.py”,一行 454,在array2string
中separator,prefix,formatter = formatter)
文件“/usr/lib/python2.7/dist-packages/numpy/core/arrayprint.py”,一行 256,在_array2string
中'int':IntegerFormat(数据),
文件“/usr/lib/python2.7/dist-packages/numpy/core/arrayprint.py”,一行 641,在 init
中max_str_len = max(len(str(maximum.reduce(data))),
文件 “/usr/local/lib/python2.7/dist-packages/cvxpy/constraints/leq_constraint.py” 第67行,非零
提升异常(“无法评估约束的真值。”)
例外:无法评估约束的真值。
当我走到这一行时,
constraints = [lamda[-1] == 1,
np.dot(ABh.T, np.dot(P, ABh)) - rho**2*np.linalg.block_diag(P, 0) +
np.dot(CDh.T, np.dot(Mblk, CDh)) << 0]
显示
回溯(最近一次调用最后一次):文件
“... / sdp.py”,第22行,
np.dot(ABh.T, np.dot(P, ABh)) - rho**2*np.linalg.block_diag(P, 0) +
ValueError:使用序列设置数组元素。
如何解决这些问题?
答案 0 :(得分:3)
您的代码的一个大问题是您无法在CVXPY对象上使用NumPy函数。您需要使用等效的CVXPY函数。这是您的代码的工作版本:
import cvxpy as cvx
import numpy as np
import scipy as sp
Ah = np.array([[1.0058, -0.0058], [1, 0]])
Bh = np.array([[-1], [0]])
Ch = np.array([[1.0058, -0.0058], [-0.9829, 0.0056]])
Dh = np.array([[-1], [1]])
M = np.array([[0, 1], [1, 0]])
ni, n = M.shape[0] / 2, Ah.shape[0]
rho = 0.5
P = cvx.Semidef(n)
lamda = cvx.Variable()
Mblk = M*lamda*np.eye(2)
ABh = cvx.hstack(Ah, Bh)
CDh = cvx.hstack(Ch, Dh)
zeros = np.zeros((n,1))
constraints = [lamda[-1] == 1,
ABh.T*P*ABh - rho**2*cvx.bmat([[P,zeros],[zeros.T, 0]]) +
CDh.T*Mblk*CDh << 0]
prob = cvx.Problem(cvx.Minimize(1), constraints)
prob.solve()
feas = prob.status is cvx.OPTIMAL
我删除了kron功能,因为它在这里没有做任何事情,CVXPY目前不支持左侧有变量的Kronecker产品。如果你需要,我可以添加它。