Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 同时引用3个元素的二维数组上的CVXPY约束_Python_Numpy_Matrix_Constraints_Cvxpy - Fatal编程技术网

Python 同时引用3个元素的二维数组上的CVXPY约束

Python 同时引用3个元素的二维数组上的CVXPY约束,python,numpy,matrix,constraints,cvxpy,Python,Numpy,Matrix,Constraints,Cvxpy,我有一个VxV大小的矩阵S。我想在它上面加上几条规定,形式是:。我曾尝试过这样做,但这段代码遇到了问题ValueError:Atoms最多只能是2D。 我总结了一个简单的问题示例: def ILP_example(scores): V = scores.shape[0] u, v, w = np.meshgrid(range(V), range(V), range(V)) arr = cp.Variable(scores.shape) objective =

我有一个VxV大小的矩阵
S
。我想在它上面加上几条规定,形式是:。我曾尝试过这样做,但这段代码遇到了问题
ValueError:Atoms最多只能是2D。

我总结了一个简单的问题示例:

def ILP_example(scores):
    V = scores.shape[0]
    u, v, w = np.meshgrid(range(V), range(V), range(V))

    arr = cp.Variable(scores.shape)

    objective = cp.Maximize(
        cp.sum(cp.multiply(scores, arr))
    )
    constraints = [
        arr[u, v]           + arr[v, w]          - arr[u, w]          <= 1,
      ]
    prob = cp.Problem(objective, constraints)
    prob.solve()
    return
def ILP_示例(分数):
V=分数。形状[0]
u、 v,w=np.网格(范围(v),范围(v),范围(v))
arr=cp.Variable(scores.shape)
目标=cp。最大化(
cp.sum(cp.multiply(分数,arr))
)
约束=[

arr[u,v]+arr[v,w]-arr[u,w]似乎cvxpy支持的维度不超过2个,这就是使用
u
v
w
索引
arr
时所做的

作为替代方案,您可以简单地重塑这些索引变量,使其为一维:

u, v, w = [x.reshape(-1) for x in np.meshgrid(range(V), range(V), range(V))]
那么这就行了:

constraints = [arr[u, v] + arr[v, w] + arr[u, w] <= 1]

cvxpy似乎只支持两个维度,这就是使用
u
v
w
索引
arr
时所做的

作为替代方案,您可以简单地重塑这些索引变量,使其为一维:

u, v, w = [x.reshape(-1) for x in np.meshgrid(range(V), range(V), range(V))]
那么这就行了:

constraints = [arr[u, v] + arr[v, w] + arr[u, w] <= 1]