Matmul不是DCP

问题描述

以下是我的代码

import numpy as np
import cvxpy as cp
from itertools import product

threshold = lambda A,n: np.clip(A,n)

def edge_entropy(A,r,c_lst):
    A_r = A
    res = 0
    for i,j in product(list(range(len(c_lst))),repeat=2):
        temp = cp.matmul(c_lst[i].T,A_r) # maybe need clipping
        temp_1 = cp.matmul(temp,c_lst[j])
        temp_2 = cp.sum(temp)
        P_ij = temp_1/temp_2
        res += -1 * P_ij * cp.log(P_ij)
    return res

N = 10 # Number of nodes
c1 = np.reshape(np.array([1,1,1]),(-1,1))
c2 = np.reshape(np.array([0,0]),1))
c3 = np.reshape(np.array([0,1))
H_star = np.array([0.5,0.5,0.5])
c_lst = [c1,c2,c3]

A_hat = cp.Variable((N,N),boolean=True)
objective = cp.Minimize(cp.square(edge_entropy(A_hat,c_lst) - H_star[0]))
prob = cp.Problem(objective)
prob.solve()
A_hat_val = A_hat.value
print(edge_entropy(A_hat_val,c_lst))

这是错误消息:

[[1. 0. 1. 0. 0. 0. 0. 0. 1. 1.]] @ var0 @ [[1.]
 [0.]
 [1.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [1.]
 [1.]] / Sum([[1. 0. 1. 0. 0. 0. 0. 0. 1. 1.]] @ var0,None,False)

对此我有两个问题:

  1. 我正在遇到此错误消息,我应该怎么做才能使其成为DCP?这些维度对我来说似乎都是正确的,并且运算都是基本矩阵乘法。

  2. 当我们有一个约束为布尔值的矩阵时,矩阵乘法如何工作?我会假设这就像用1的矩阵做矩阵乘法,然后将结果投影回1的矩阵?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)