我要使用cvxpy解决凸优化问题。给定一个1 x n
行向量y
和一个m x n
矩阵C
,我想找到一个标量b
和一个1 x m
行向量{{ 1}},使得a
的平方和尽可能小(y - (aC + b(aC @ aC))
表示元素明智的相乘)。另外,@
中的所有整数都必须为非负数,且总和为1和a
。以下是我尝试使用cvxpy解决此问题的方法。
-100 <= b <= 100
当我尝试解决import numpy as np
import cvxpy as cvx
def find_a(y, C, b_min=-100, b_max=100):
b = cvx.Variable()
a = cvx.Variable( (1,C.shape[0]) )
aC = a * C # this should be matrix multiplication
x = (aC + cvx.multiply(b, cvx.square(aC)))
objective = cvx.Minimize ( cvx.sum_squares(y - x) )
constraints = [0. <= a,
a <= 1.,
b_min <= b,
b <= b_max,
cvx.sum(a) == 1.]
prob = cvx.Problem(objective, constraints)
result = prob.solve()
print a.value
print result
y = np.asarray([[0.10394265, 0.25867508, 0.31258457, 0.36452763, 0.36608997]])
C = np.asarray([
[0., 0.00169811, 0.01679245, 0.04075472, 0.03773585],
[0., 0.00892802, 0.03154158, 0.06091544, 0.07315024],
[0., 0.00962264, 0.03245283, 0.06245283, 0.07283019],
[0.04396226, 0.05245283, 0.12245283, 0.18358491, 0.23886792]])
find_a(y, C)
时,我不断收到DCPError: Problem does not follow DCP rules.
错误。我在想我的功能不是真正的凸面,或者我不明白如何构造适当的cvxpy a
。任何帮助将不胜感激。