CVXPY:如何有效解决一系列类似问题 [英] CVXPY: how to efficiently solve a series of similar problems

查看:382
本文介绍了CVXPY:如何有效解决一系列类似问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 CVXPY建模语言.我想解决一系列此类问题-格式仍然相同,但参数(常数)不同.

I have a large problem defined in CVXPY modelling language. I want to solve series of this problems - still the same format but with different parameters (constants).

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

I found out that after calling problem.solve() internal problem generating takes 20 s and main optimization runtime takes 0.2 s. It's a lot of time when I want to solve dozens of similar problems.

是否有用于CVXPY的任何工具,例如 YALMIP优化器,或者有任何减少产生问题的可能性时间?

Is there any tool for CVXPY like YALMIP optimizer or any possibility to reduce problem generating time?

推荐答案

是的.甚至在官方文档中进行了解释. >

Yes there is. And it's even explained in the official docs.

参数

参数是常量的符号表示.参数的目的是在不重建整个问题的情况下更改问题中的常量值.

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.

直接从文档中获取示例(已修改):

Example straight from the docs (modified):

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方法(通过构造证明是凸性的).

So what does it do

As you noticed, the setup of your optimization-problem might take some time, because it's following the DCP-approach (which proves convexity by construction).

使用parameter,此DCP处理仅执行一次!每个新的解决方案只会改变问题中的一些小部分.重要的是要尽可能精确地描述您的参数,以便DCP可以正常工作.例如:Parameter(sign="positive").

Using parameter, this DCP-processing is only done one time! Every new solve willl only change some small parts within the problem. It's important to describe your parameter as precise as possible, so that DCP can work. Example: Parameter(sign="positive").

也许.如果您认为特殊的猜测(例如,上次迭代的解决方案矢量)是解决新问题的良好开端,则根据求解器的不同,您也可以使用热启动.

Maybe. Depending on the solver, you can also use warm-starting, if you think a special guess (e.g. solution-vector of your last iteration) is a good start for the new problem.

这将用prob.solve(warm_start=True)替换:prob.solve(),导致重新使用以前的解决方案(说明

This would replace: prob.solve() with prob.solve(warm_start=True), resulting in reusing the previous solution as a start (explained here). Manually defining this vector does not seem to be possible (from cvxpy).

可悲的是,据我所知,唯一支持此功能(在cvxpy中)的求解器是SCS(其他人将忽略它而不会崩溃)!

Sadly, the only solver supporting this (within cvxpy) is SCS as far as i know (and others will ignore it without crashing)!

这篇关于CVXPY:如何有效解决一系列类似问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆