[R] Constrained Optimization

Berwin A Turlach berwin at maths.uwa.edu.au
Thu Nov 5 03:28:08 CET 2009


On Wed, 4 Nov 2009 14:48:08 -0800 (PST)
s t <thampy77 at yahoo.com> wrote:

> I'm trying to do the following constrained optimization example.
> Maximize x1*(1-x1) + x2*(1-x2) + x3*(1-x3)
> s.t. x1 + x2 + x3 = 1
> x1 >= 0 and x1 <= 1
> x2 >= 0 and x2 <= 1
> x3 >= 0 and x3 <= 1
> which are the constraints.
> I'm expecting the answer x1=x2=x3 = 1/3.

This is a quadratic programming problem (mininimising/maximising a
quadratic function under linear equality and inequality constraints).

There are several R packages that solve such problems, e.g.:

R> library(quadprog)
R> Dmat <- diag(3)
R> dvec <- rep(1,3)/3
R> Amat <- cbind(rep(1,3), diag(3), -diag(3))
R> bvec <- c(1, rep(0,3), rep(-1,3))
R> solve.QP(Dmat, dvec, Amat, bvec, meq=1)
$solution
[1] 0.3333333 0.3333333 0.3333333

$value
[1] -0.1666667

$unconstrainted.solution
[1] 0.3333333 0.3333333 0.3333333

$iterations
[1] 2 0

$iact
[1] 1

You may want to consult:
http://cran.r-project.org/web/views/Optimization.html

HTH.

Cheers,

	Berwin

========================== Full address ============================
Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)
School of Maths and Stats (M019)            +61 (8) 6488 3383 (self)
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway                   
Crawley WA 6009                e-mail: berwin at maths.uwa.edu.au
Australia                        http://www.maths.uwa.edu.au/~berwin




More information about the R-help mailing list