1

cvxpy用来解决凸优化问题,这是我的约束:

在此处输入图像描述

那么我该如何表达这个约束cvxpy呢?中的sum_entries函数cvxpy只能对整个矩阵/向量求和,但不能对向量的一部分求和。

4

1 回答 1

1

只需使用索引选择一个子集(在以下示例中:经典的基于 python 的切片;但更复杂的索引/numpy 样式是可能的):

例子:

from cvxpy import *

x = Variable(5)
constraints = []
constraints.append(x >= 0)   # all vars
constraints.append(x <= 10)  # all vars
constraints.append(sum_entries(x[:3]) <= 3)  # only part of vector; sum(first-three) <=3
objective = Maximize(sum_entries(x))
problem = Problem(objective, constraints)
problem.solve()
print(problem.status)
print(x.value.T)

输出:

optimal
[[  1.   1.   1.  10.  10.]]

我还怀疑您在这里误解了问题,但是该公式图像当然是不完整的,无法实现。

于 2017-09-23T19:53:05.680 回答