问题描述
我正在尝试解决问题
d = 0.5 * ||X - \Sigma||_{Frobenius Norm} + 0.01 * ||XX||_{1},
其中X是对称正定矩阵,所有诊断元素应为1.XX与X相同,除了对角矩阵为0.\ Sigma是已知的,我希望X的最小值为d.
where X is a symmetric positive definite matrix, and all the diagnoal element should be 1. XX is same with X except the diagonal matrix is 0. \Sigma is known, I want minimum d with X.
我的代码如下:
using Convex
m = 5;
A = randn(m, m);
x = Semidefinite(5);
xx=x;
xx[diagind(xx)].=0;
obj=vecnorm(A-x,2)+sumabs(xx)*0.01;
pro= minimize(obj, [x >= 0]);
pro.constraints+=[x[diagind(x)].=1];
solve!(pro)
我只是通过约束矩阵中的对角线元素来解决最优问题,但似乎diagind函数在这里无法正常工作,我该如何解决问题.
I just solve the optimal problem by constrain the diagonal elements in matrix, but it seems diagind function could not work here, How can I solve the problem.
推荐答案
我认为以下内容可以满足您的需求:
I think the following does what you want:
m = 5
Σ = randn(m, m)
X = Semidefinite(m)
XX = X - diagm(diag(X))
obj = 0.5 * vecnorm(X - Σ, 2) + 0.01 * sum(abs(XX))
constraints = [X >= 0, diag(X) == 1]
pro = minimize(obj, constraints)
solve!(pro)
对于操作类型:
-
diag
提取矩阵的对角线作为矢量 -
diagm
从向量中构造对角矩阵
diag
extracts the diagonal of a matrix, as a vectordiagm
constructs a diagonal matrix out of a vector
因此,要使XX
为对角线为零的X
,我们从中减去X
的对角线.为了约束具有对角线1
的X
,我们使用==
将其对角线与1
比较.
So, to have XX
be X
with zero diagonal, we subtract the diagonal of X
from it. And to constrain X
having diagonal 1
, we compare its diagonal with 1
, using ==
.
一个好主意是尽可能保持不可变的值,而不是尝试修改事物.我不知道Convex
是否支持.
It is a good idea to keep immutable values as far as possible, instead of trying to modify things. I don't know whether Convex
even supports that.
这篇关于朱莉娅错误使用带有diagind函数的凸包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!