【问题标题】:CVXPY: Efficiently writing constraints for pairwise sumsCVXPY:有效地为成对和编写约束
【发布时间】:2020-01-11 00:29:09
【问题描述】:

我正在尝试在 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()

【问题讨论】:

    标签: python linear-programming cvxpy


    【解决方案1】:

    我们可以把它翻译成矩阵表示法:

    其中 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] 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多