Re: [R] using solve.qp without a quadratic term

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Mon, 24 Dec 2007 15:02:17 +0100

>>>>> "ML" == Mark Leeds <markleeds_at_verizon.net>
>>>>> on Fri, 21 Dec 2007 19:38:19 -0600 (CST) writes:

    ML> I was playing around with a simple example using solve.qp ( function is in the quadprog package ) and the code is below. ( I'm not even sure there if there is a reasonable solution because I made the problem up  ). 
    ML> But, when I try to use solve.QP to solve it, I get the error that D in the quadratic function is not positive
    ML> definite. This is because Dmat is zero
    ML> because I don't have a quadratic term in my
    ML> objective function. So, I was wondering if
    ML> it was possible to use solve.QP when there isn't
    ML> a quadratic term in the objective function.  

    ML> I imagine that there are other functions in R that can be used but I would like to use solve.QP because, in my real problem,
    ML> I will have a lot of fairly complex constraints
    ML> and solve.QP provides a very nice way for implementing     ML> them. Maybe there is another linear solver that allows you to implement hundreds of constraints just solve.QP that I am unaware of ? Thanks for any suggestions.

    ML> # IN THE CODE BELOW, WE MINIMIZE     ML> # -3*b1 + 4*b2 + 6*b3

    ML> # SUBJECT TO

    ML> # b1 + b2 + b3 >=0
    ML> # -(b1  b2 + b3) >= 0 
    ML> # IE : b1 + b2 + b3 = 0.

So you want to solve a *linear* programming problem, not a quadratic. Linear is typically considerably easier.

The recommended (and hence always installed) package 'boot' has function simplex() to do this
and I see two other CRAN packages 'linprog' and 'lpSolve' also for the same problem; since ?simplex says that it may not be very efficient for large problems, you would e.g. lpSolve instead.

Regards,
Martin

    ML> Dmat <- matrix(0,3,3)          # QUADRATIC TERM
    ML> dvec <- c(-3,4,6)              # LINEAR TERM
    ML> Amat <- matrix(c(1,-1,0,1,-1,0,1,-1,0),3,3)

    ML> #print(Amat)

    ML> bvec = c(0,0,0)                  # THIRD ZERO IS SAME AS NO CONSTRAINT

    ML> result <- solve.QP(Dmat, dvec, Amat)

    ML> ______________________________________________
    ML> R-help_at_r-project.org mailing list
    ML> https://stat.ethz.ch/mailman/listinfo/r-help
    ML> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html     ML> and provide commented, minimal, self-contained, reproducible code.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 24 Dec 2007 - 14:07:46 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 24 Dec 2007 - 15:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.