引擎盖下的cvxpy最小二乘

时间:2019-02-21 17:50:13

标签: nonlinear-optimization cvxpy

我写了以下代码

def solve( p, a ):
    m,n,ids,inv,k = 0,len(p),{},{},0
    for i in range(n):
        for j in range(n):
            #if i != j:
            ids[(i,j)] = k
            inv[k] = (i,j)
            k = k+1
    # Problem data
    A = np.zeros((2*n,n*n))
    b = np.zeros(2*n)
    c = np.zeros(2*n)
    # for i in range(2*n):
    #    for j in range(n*(n-1)):
    #        A[i,j]= -1.00
    for j in range(n):
        for i in range(n):
            #if i != j:
            idx = ids[(i,j)]
            A[j,idx] = 1
        b[j] = 1
    for i in range(n):
        for j in range(n):
            #if i != j:
            idx = ids[(i,j)]
            A[i+n,idx] = p[j]
        b[i+n] = p[i]
    # Construct the problem
    x = cp.Variable(n*n)
    objective = cp.Minimize(cp.sum_squares(A*x-b))
    constraints = [0 <= x]
    prob = cp.Problem(objective,constraints)
    result = prob.solve()
    alpha = np.zeros((n,n))
    # vec = A*x.value-b
    for i in range(n):
        for j in range(n):
            #if i == j:
            #    alpha[i,j] = -1.00
            #else:
            alpha[i,j] = x.value[ids[(i,j)]]
    return (x,alpha)

基本上,我正在求解具有2n行和n * n列的线性方程组,即一个欠定的方程组。 CVXPY返回良好的答案。我需要知道的是,对于这种特殊的约束条件和优化标准,到底是采用哪种数学方法? CVXPY文档指向一本有关Convex Optimization的730页的书,我希望我有足够的时间和足够的背景知识来阅读它。缺少阅读CVXPY源代码,可以(至少是高级概述,关键字)可以说过上面的程序吗?

0 个答案:

没有答案