我一直在尝试将一些代码从 Matlab 传递到 Python。我在 Matlab 上遇到了同样的凸优化问题,但我在将其传递给 CVXPY 或 CVXOPT 时遇到了问题。
n = 1000;
i = 20;
y = rand(n,1);
A = rand(n,i);
cvx_begin
variable x(n);
variable lambda(i);
minimize(sum_square(x-y));
subject to
x == A*lambda;
lambda >= zeros(i,1);
lambda'*ones(i,1) == 1;
cvx_end
这就是我用Python和CVXPY尝试过的。
import numpy as np
from cvxpy import *
# Problem data.
n = 100
i = 20
np.random.seed(1)
y = np.random.randn(n)
A = np.random.randn(n, i)
# Construct the problem.
x = Variable(n)
lmbd = Variable(i)
objective = Minimize(sum_squares(x - y))
constraints = [x == np.dot(A, lmbd),
lmbd <= np.zeros(itr),
np.sum(lmbd) == 1]
prob = Problem(objective, constraints)
print("status:", prob.status)
print("optimal value", prob.value)
尽管如此,它还是行不通。你们中有人知道如何使它工作吗?我很确定我的问题在于约束。并且它会很高兴与 CVXOPT 一起使用。