CVXPY:如何高效解决一系列类似问题

CVXPY: how to efficiently solve a series of similar problems

我在 CVXPY modelling language 中定义了一个大问题。我想解决一系列这样的问题 - 仍然是相同的格式但具有不同的参数(常量)。

我发现调用 problem.solve() 后内部问题生成需要 20 秒,主要优化运行时间需要 0.2 秒。想解决几十个类似的问题,时间很多。

是否有类似 YALMIP optimizer 的 CVXPY 工具或任何可能减少问题生成时间的工具?

是的。甚至在官方docs.

中也有解释

Parameters

Parameters are symbolic representations of constants. The purpose of parameters is to change the value of a constant in a problem without reconstructing the entire problem.

直接来自文档的示例(已修改):

from cvxpy import *
import numpy

# Problem data.
n = 15
m = 10
numpy.random.seed(1)
A = numpy.random.randn(n, m)
b = numpy.random.randn(n, 1)
# gamma must be positive due to DCP rules.
gamma = Parameter(sign="positive")                       # !!!

# Construct the problem.
x = Variable(m)
error = sum_squares(A*x - b)
obj = Minimize(error + gamma*norm(x, 1))
prob = Problem(obj)                                      # !!!

# Construct a trade-off curve of ||Ax-b||^2 vs. ||x||_1
sq_penalty = []
l1_penalty = []
x_values = []
gamma_vals = numpy.logspace(-4, 6)
for val in gamma_vals:
    gamma.value = val                                    # !!!
    prob.solve()                                         # !!!
    # Use expr.value to get the numerical value of
    # an expression in the problem.
    sq_penalty.append(error.value)
    l1_penalty.append(norm(x, 1).value)
    x_values.append(x.value)

那么它有什么作用

如您所见,优化问题的设置可能需要一些时间,因为它遵循 DCP 方法(通过构造证明凸性)。

使用parameter,这个DCP处理只做一次!每个新的解决方案只会改变问题中的一些小部分。尽可能精确地描述您的参数很重要,这样 DCP 才能起作用。示例:Parameter(sign="positive").

还有什么可以做的吗

也许吧。根据求解器的不同,如果您认为特殊的猜测(例如上次迭代的解向量)是解决新问题的良好开端,您还可以使用 warm-starting

这将替换:prob.solve()prob.solve(warm_start=True),导致重新使用以前的解决方案作为开始(解释为 here)。手动定义这个向量似乎是不可能的(来自 cvxpy)。

遗憾的是,据我所知,唯一支持这个(在 cvxpy 中)的求解器是 SCS(其他人会忽略它而不会崩溃)!