我一直在试图将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。
np.sum
。 - steven