简体   繁体   English

如何使用惩罚方法最小化约束违反的二次目标函数

[英]How to minimize an quadratic objective function with constraint violation using penalty method

I have compared many Quadratic Programming(QP) solvers like cvxopt , qpoases and osqp and found that osqp works faster and better for my application.我比较了许多二次规划 (QP) 求解器,如cvxoptqpoasesosqp ,发现 osqp 对我的应用程序来说工作得更快更好。

Now, I want to minimize an indefinite quadratic function with both equality and inequality constraints that may get violated depending on various factors.现在,我想最小化一个不确定的二次函数,同时具有相等和不等式约束,这些约束可能会因各种因素而被违反。 So I want to use l1 penalty method that penalizes the violating constraints.所以我想使用 l1 惩罚方法来惩罚违反约束的行为。

在此处输入图像描述 for example,例如,

I have modified an example , to violate the constraints.我修改了一个 示例,以违反约束。

import osqp
import scipy.sparse as sparse
import numpy as np

# Define problem data
P = sparse.csc_matrix([[4., 1.], [1., 2.]])
q = np.array([1., 1.])
A = sparse.csc_matrix([[1., 0.], [0., 1.], [1., 0.], [0., 1.]])
l = np.array([0., 0., 0.2, 1.1])
u = np.array([1., 1., 0.2, 1.1])

# Create an OSQP object
prob = osqp.OSQP()

# Setup workspace and change alpha parameter
prob.setup(P, q, A, l, u, alpha=1.0)

# Solve problem
res = prob.solve()
print res.x

Obviously, this is an infeasible problem, so we need to change the objective function to penalize the error.显然,这是一个不可行的问题,所以我们需要改变目标函数来惩罚错误。 So, I need help to formulate this problem that can be solved using osqp's python interface.所以,我需要帮助来制定这个可以使用 osqp 的 python 接口解决的问题。

Or, please let me know if there is any other python interface available to solve this kind of constraint violation problems.或者,请让我知道是否有任何其他 python 接口可用于解决此类违反约束的问题。

In general abs functions can be dangerous (they are non-differentiable). 通常, abs功能可能很危险(它们是不可微的)。 A standard way to deal with this is to add slacks. 解决此问题的标准方法是添加松弛。 Eg 例如

g(x) <= 0

becomes

g(x) <= s
s >= 0

Now add a term mu*s to the objective. 现在在目标中添加术语mu*s

For 对于

h(x) = 0

one could do 一个人可以做

h(x) = s1 - s2
s1, s2 >= 0

and add mu*(s1+s2) to the objective. 并将mu*(s1+s2)到目标。

As usual: this is just one approach (there are other formulations). 和往常一样:这只是一种方法(还有其他提法)。

I had the same problem and this question helped a lot.我遇到了同样的问题,这个问题很有帮助。 This is how I solved it in OSQP interface.这就是我在 OSQP 界面中解决它的方法。

I redefined example to be:我将示例重新定义为:

# Define problem data
P = sparse.csc_matrix([[4., 1.], [1., 2.]])
q = np.array([1., 1.])
A = sparse.csc_matrix([[1., 0.], [0., 1.], [1., 1.]])
l = np.array([0., 0., 3])
u = np.array([1., 1., 3])

Here first and second variable are constrained to be at most 1. But their sum should equal 3. This makes this problem unfeasible.这里第一个和第二个变量被限制为最多 1。但是它们的总和应该等于 3。这使得这个问题不可行。

Now let's transform inequality constraints as Erwin suggested by adding two slack variables.现在让我们按照 Erwin 的建议通过添加两个松弛变量来转换不等式约束。

# Redefine problem data with 2 slack variableы
# Added quadratic penalties to variables s1 and s2 with penalty coefficient == 1
P = sparse.csc_matrix([[4., 1., 0., 0.], [1., 2., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]]) 
# Zero linear penalties for s1 and s2.
q = np.array([1., 1., 0., 0.])
# First constraint is x1 <= s1, second is s1 >= 0. 
# Third constraint is x2 <= s2, fourth is s2 >= 0. 
A = sparse.csc_matrix([[1., 0., -1., 0.], [0., 0., 1., 0.], [0., 1., 0., -1.], [0., 0., 0., 1.], [1., 1., 0., 0.]])
l = np.array([-np.inf, 0., -np.inf, 0., 3])
u = np.array([0., np.inf, 0., np.inf, 3])

When I run solver, problem has a solution and is softly penalised for exceeding upper bounds.当我运行求解器时,问题有一个解决方案,并且会因超出上限而受到轻度惩罚。

iter   objective    pri res    dua res    rho        time
   1  -4.9403e-03   3.00e+00   5.99e+02   1.00e-01   8.31e-04s
  50   1.3500e+01   1.67e-07   7.91e-08   9.96e-01   8.71e-04s

status:               solved
number of iterations: 50
optimal objective:    13.5000
run time:             8.93e-04s
optimal rho estimate: 1.45e+00
[1.00 2.00 1.00 2.00]

Hope this helps somebody.希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM