R中的二次优化,具有相等和不等式约束

时间:2015-07-19 05:39:56

标签: r optimization constraints quadratic

我试图找到如何解决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' / p>

1 个答案:

答案 0 :(得分:1)

这是一个有效的例子:

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