我正在尝试在 CVXPY 中实现此 LP:
但我正在努力寻找一种有效的方法来实现这里的第一个约束。我发现有效的唯一方法是将每个总和作为其自己的约束添加,但随着问题的规模变大,其规模会迅速扩大。有没有更简单/更有效的方法来指定此约束?
import cvxpy as cp
import numpy as np
n_j = 10
n_i = 100
a = cp.Variable(n_j, nonneg=True)
b = cp.Variable(n_i, nonneg=True)
g = np.random.randint(low=1, high=10, size=n_j)
v = np.random.normal(size=(n_i, n_j))
obj = cp.Minimize(cp.sum(cp.multiply(g, a)) + cp.sum(b))
constraints = [a[j] + b[i] >= values[i, j]
for j in range(n_j) for i in range(n_i)]
prob = cp.Problem(obj, constraints)
prob.solve()
最佳答案
我们可以将其转换为矩阵表示法:
其中e是所有的列向量。当然,e 向量应该具有适当的大小:每一项都应该是一个 (n_i x n_j) 矩阵。
在 CVXPY 中,这可以写成:
# changed into using explicit column vectors
a = cp.Variable((n_j,1), nonneg=True)
b = cp.Variable((n_i,1), nonneg=True)
g = np.random.randint(low=1, high=10, size=(n_j,1))
# column vectors of ones
e_i = np.ones((n_i,1))
e_j = np.ones((n_j,1))
# matrix style inequality
constraints = [e_i * a.T + b * e_j.T >= v]
关于python - CVXPY:有效编写成对和的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59672262/