我试图找到如何在具有等式和不等式约束的 R 中最大化二次函数:

最大化 x' * H * x受制于:Aeq * x = beqA * x >= bx >= 0
问题的简化版本可能是

最大化 x^2 + y^2x + y = 1 约束
x, y >= 0
由于这是一个最大化问题,我无法在 solve.QP 包中使用 quadprog 函数。

我也尝试使用 constrOptim。但请注意,存在等式约束,constrOptim 需要在可行域内部进行初始猜测。因此 constrOptim 不能用于等式约束。

我也尝试在 auglag 包中使用 alabama。但我似乎没有得到最大化问题的正确答案。

如果问题是最小化问题,那么简单问题的答案是 x = 0.5 和 y = 0.5。 auglagsolve.QP 都给了我这个答案。

但我正在寻找最大化问题的解决方案。几何的答案在于 (x = 1 and y = 0) OR (x = 0 and y = 1)。

最佳答案

这不是一个完整的答案,但可能会向您展示一些替代算法方法。

问题

这个问题似乎是非凸的,这使得它很难解决(并且限制了可用的好软件的数量)。

一般非线性优化

正如 Christoph 在评论中提到的,一般非线性优化是一种可能的方法。当然,我们失去了关于全局最优解的保证。在内部使用优秀的开源软件 ipopt 将是一个很好的第一次尝试。

替代方案:凸凹/凸差 - 编程

您可能会考虑凸凹编程(它解决了一些全局简单的问题),它有一个非常有效的启发式方法,称为 凸凹过程 ( Yuille, Alan L., and Anand Rangarajan. "The concave-convex procedure." Neural computation 15.4 (2003): 915-936. ),它应该比更一般的非线性方法工作得更好。

我不确定在 R 中是否有一种很好的方法来做到这一点(无需手动完成),但在 Python 中有非常现代的开源研究库 dccp (基于 cvxpy )。

代码

from cvxpy import *
import dccp
from dccp.problem import is_dccp

x = Variable(1)
y = Variable(1)
constraints = [x >= 0, y >= 0, x+y == 1]
objective = Maximize(square(x) + square(y))
problem = Problem(objective, constraints)

print("problem is DCP:", problem.is_dcp())
print("problem is DCCP:", is_dccp(problem))

problem.solve(method='dccp')

print('solution (x,y): ', x.value, y.value)

输出
('problem is DCP:', False)
('problem is DCCP:', True)
iteration= 1 cost value =  -2.22820497851 tau =  0.005
iteration= 2 cost value =  0.999999997451 tau =  0.006
iteration= 3 cost value =  0.999999997451 tau =  0.0072
('solution (x,y): ', 0.99999999872569856, 1.2743612156911721e-09)

编辑/更新

根据问题的大小(小),您还可以尝试 全局非线性求解器 couenne

代码

from pyomo.environ import *

model = ConcreteModel()

model.x = Var()
model.y = Var()

model.xpos = Constraint(expr = model.x >= 0)
model.ypos = Constraint(expr = model.y >= 0)
model.eq = Constraint(expr = model.x + model.y == 1)
model.obj = Objective(expr = model.x**2 + model.y**2, sense=maximize)

model.preprocess()

solver = 'couenne'
solver_io = 'nl'
stream_solver = True     # True prints solver output to screen
keepfiles =     True    # True prints intermediate file names (.nl,.sol,...)
opt = SolverFactory(solver,solver_io=solver_io)
results = opt.solve(model, keepfiles=keepfiles, tee=stream_solver)

print("Print values for all variables")
for v in model.component_data_objects(Var):
  print str(v), v.value

输出
Couenne 0.5.6 -- an Open-Source solver for Mixed Integer Nonlinear Optimization
Mailing list: [email protected]
Instructions: http://www.coin-or.org/Couenne

Couenne: new cutoff value -1.0000000000e+00 (0.004 seconds)
NLP0012I
              Num      Status      Obj             It       time                 Location
NLP0014I             1         OPT -0.5        6 0.004
Loaded instance "/tmp/tmpLwTNz1.pyomo.nl"
Constraints:            3
Variables:              2 (0 integer)
Auxiliaries:            3 (0 integer)

Coin0506I Presolve 11 (-1) rows, 4 (-1) columns and 23 (-2) elements
Clp0006I 0  Obj -0.9998 Primal inf 4.124795 (5) Dual inf 0.999999 (1)
Clp0006I 4  Obj -1
Clp0000I Optimal - objective value -1
Clp0032I Optimal objective -1 - 4 iterations time 0.002, Presolve 0.00
Clp0000I Optimal - objective value -1
NLP Heuristic: NLP0014I             2         OPT -1        5 0
no solution.
Clp0000I Optimal - objective value -1
Optimality Based BT: 0 improved bounds
Probing: 0 improved bounds
NLP Heuristic: no solution.
Cbc0013I At root node, 0 cuts changed objective from -1 to -1 in 1 passes
Cbc0014I Cut generator 0 (Couenne convexifier cuts) - 0 row cuts average 0.0 elements, 2 column cuts (2 active)
Cbc0004I Integer solution of -1 found after 0 iterations and 0 nodes (0.00 seconds)
Cbc0001I Search completed - best objective -1, took 0 iterations and 0 nodes (0.01 seconds)
Cbc0035I Maximum depth 0, 0 variables fixed on reduced cost

couenne: Optimal

    "Finished"

Linearization cuts added at root node:         12
Linearization cuts added in total:             12  (separation time: 0s)
Total solve time:                           0.008s (0.008s in branch-and-bound)
Lower bound:                                   -1
Upper bound:                                   -1  (gap: 0.00%)
Branch-and-bound nodes:                         0
Print values for all variables
x 0.0
y 1.0

关于r - 在 R 中最大化二次目标函数服从线性等式和不等式约束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39292544/

10-09 15:52