R中带有等式和不等式约束的二次优化

4

我正在尝试找到如何在R中解决二次问题,包括等式和不等式约束以及上下限:

min 0.5*x'*H*x + f'*x  
subject to:  A*x <= b   
Aeq*x = beq  
LB <= x <= UB 

我已经查看了“quadprog”和“kernlab”软件包,但是...我一定是漏了什么,因为我不知道如何同时为solve.QP()或ipop()指定'A'和'Aeq'。


1
也许这里的第二个例子会有所帮助。 - Bhas
1个回答

2
这是一个可用的示例:
library('quadprog')

# min  
#     -8 x1 -16 x2 + x1^2 + 4 x2^2 
#
# s.t.
#
# x1 + 2 x2 == 12 # equalities 
# x1 +   x2 <= 10 # inequalities (N.B. you need to turn it into "greater-equal" form )
# 1 <= x1 <= 3 # bounds 
# 1 <= x2 <= 6 # bounds 


H <- rbind(c(2, 0),
           c(0, 8))

f <- c(8,16)

# equalities
A.eq <- rbind(c(1,2))
b.eq <- c(12)

# inequalities
A.ge <- rbind(c(-1,-1))
b.ge <- c(-10)

# lower-bounds 
A.lbs <- rbind(c( 1, 0),
               c( 0, 1))
b.lbs <- c(1, 1)

# upper-bounds on variables
A.ubs <- rbind(c(-1, 0),
               c( 0,-1))
b.ubs <- c(-3, -6)

# solve
sol <- solve.QP(Dmat = H,
                dvec = f,
                Amat = t(rbind(A.eq, A.ge, A.lbs, A.ubs)),
                bvec = c(b.eq, b.ge, b.lbs, b.ubs),
                meq = 1) # this argument says the first "meq" rows of Amat are equalities

sol
> sol
$solution
[1] 3.0 4.5

$value
[1] -6

$unconstrained.solution
[1] 4 2

$iterations
[1] 3 0

$Lagrangian
[1] 10  0  0  0 12  0

$iact
[1] 1 5

2
A.leb.le应该分别改为A.geb.ge,不是吗? - Bhas
@digEmAll 谢谢!某种程度上我没有意识到'meq'参数的价值。顺便问一下,b.eq不应该初始化为11吗? - nevgeniev
是的,抱歉在注释块中模型代数有一个打字错误。 - digEmAll

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接