2

我有一个凸优化问题,我试图用 cvxpy 解决。给定一个1 x n行向量y和一个m x n矩阵C,我想找到一个标量b和一个1 x m行向量a,使得 的平方和y - (aC + b(aC @ aC))尽可能小(@表示元素的乘法)。此外,所有整数都a必须是非负数并且总和为 1 和-100 <= b <= 100。下面是我尝试使用 cvxpy 解决这个问题。

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.解决a. 我在想我的函数不是真的凸的,或者我不明白如何构造正确的 cvxpy Problem。任何帮助将不胜感激。

4

0 回答 0