Quadratic Optimization in R with Equality and Inequality Constraints

I'm trying to find how to solve a quadratic problem in R with equality and inequality constraints and upper and lower bounds:

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

      

I checked the "quadprog" and "kernlab" packages, but ... I must be missing something as I have no idea how to specify both "A" and "Aeq" for solve.QP () or ipop ()

+3


source to share


1 answer


Here's a working example:



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

      

+1


source







All Articles