Quadratic optimization in R with quadprog: A working example

After having demonstrated linear programming in R I want to introduce quadprog, a package for modelling and solving quadratic problems.

A good theoretical introduction to quadratic programming (hence “quadratic optimization”) can be found here: https://optimization.mccormick.northwestern.edu/index.php/Quadratic_programming

On stackoverflow, a useful discussion in this context is here: https://stackoverflow.com/questions/31498344/quadratic-optimization-in-r-with-both-equality-and-inequality-constraints

Here, I demonstrate a working example for how to solve a quadratic problem with the solve.QP function available in quadprog for R.

Below follows a mathematical formulation of the problem to solve:

To model this problem in accordance with general quadratic programming logic we must apply vector and matrix calculation, and state the problem in the following form:

I do this in the code below:

# matrix definitions
C <- rbind(c(2,0),

A <- rbind(c(1,1),

# vector definitions
d <- c(-6,-20)

b <- c(10,0)

Now, I model the problem using the solve.QP function provided in the quadprog package:


# model and solve
solution <- solve.QP(Dmat = C,
                     dvec = d,
                     Amat = A,
                     bvec = b)

What is the optimal value of this quadratic problem?

solution$value + 5
## [1] 122.3333

What is the solution to this quadratic optimization problem?

## [1] 4.666667 2.666667

Feel free to check out my other posts on optimization in R, like e.g. with lpSolve and nloptr.

Leave a Reply

2 thoughts on “Quadratic optimization in R with quadprog: A working example

Leave a Reply

Your email address will not be published. Required fields are marked *