3

With regards to a quadratic programme, how would I set up an objective function like

min⁡ ∑a_i (x_i )^2

in the matrix form for packages “quadprog” or “limSolve” (for this package I'm not sure if it needs to be in matrix form)?

From the discussion I have seen so far, there has been no multiplication of the quadratic term.

josliber
  • 43,891
  • 12
  • 98
  • 133
NeilC
  • 33
  • 2

1 Answers1

2

Let's consider a simple linearly constrained quadratic program of the form you have mentioned:

min  0.5x^2 + 0.7y^2
s.t. x + y = 1
     x >= 0
     y >= 0

Solution with the quadprog package

The quadprog package accepts models of the following form:

min −d'b + 1/2b'Db
s.t. A'b >= b0

To get our problem into this form, we need to construct a matrix D with (2*0.5 2*0.7) as the main diagonal, as well as a matrix A with our three constraints and a right hand side b0:

dvec <- c(0, 0)
Dmat <- diag(c(1.0, 1.4))
Amat <- rbind(c(1, 1), c(1, 0), c(0, 1))
bvec <- c(1, 0, 0)
meq <- 1  # The first constraint is an equality constraint

Now we can feed this to solve.QP:

library(quadprog)
solve.QP(Dmat, dvec, t(Amat), bvec, meq=meq)$solution
# [1] 0.5833333 0.4166667

Solution with the limSolve package

The limSolve package's lsei function accepts models of the following form:

min  ||Ax-b||^2
s.t. Ex = f
     Gx >= h

To obtain our objective function we need to construct matrix A with (sqrt(0.5) sqrt(0.7)) as the main diagonal, set b to be the 0 vector, as well as matrices and vectors encoding the other information:

A <- diag(c(sqrt(0.5), sqrt(0.7)))
b <- c(0, 0)
E <- rbind(c(1, 1))
f <- 1
G <- diag(2)
h <- c(0, 0)

Now we can feed this information to lsei:

library(limSolve)
lsei(A, b, E, f, G, h)$X
# [1] 0.5833333 0.4166667
josliber
  • 43,891
  • 12
  • 98
  • 133
  • Thanks for this which is very helpful! Could I just clarify one area: If the objective function is min⁡ ∑a_i (x_i )^2 does this mean that I can multiply the two terms (using "limSolve") like (the a_i is not squared hence the change made to "A"): A <- diag(c((0.5), sqrt(0.7))) b <- c(0, 0) E <- rbind(c(1, 1)) f <- 1 G <- diag(2) h <- c(0, 0) lsei(A, b, E, f, G, h)$X My confusion is with "A <- diag(c((0.5), sqrt(0.7)))" in the sense that I'm not sure how to multiply these two terms? Thanks again. – NeilC Dec 29 '16 at 20:45
  • @NeilC I'm afraid I don't understand. Are you trying to solve a different optimization problem than the one I looked at in my answer? – josliber Dec 29 '16 at 20:49
  • thanks again. I wish to set up a quadratic programme whereby for the objective function I multiply a non quadratic term by a quadratic term like ∑a_i (x_i )^2 subject to linear constraints. Your example was helpful but instead of adding the terms together, I instead wish to multiply them. Do you know if that's possible within these two R packages? So instead of 0.5x^2 + 0.7y^2 is it possible to have 0.5x * 0.7y^2 – NeilC Dec 29 '16 at 22:31
  • @NeilC that is no longer a quadratic program (you have a cubic term). Neither quadprog nor limSolve will be helpful in that case. This is a very different question than the current one, so I would suggest asking a separate question with the "Ask Question" link at the top of the page. – josliber Dec 29 '16 at 22:37