Skip to content

Constrained Optimization

5 messages · s t, Ravi Varadhan, apjaworski@mmm.com +1 more

#
Your initial value is indeed "infeasible", as the error message says.  Your
x[1] is 1.00004, which is not in the interval [0,1].

To incorporate both equalities and inequalities (linear or nonlinear), you
could try my function `constrOptim.nl'.  Contact me off the list if you are
interested.

Ravi.
----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
tml

 

----------------------------------------------------------------------------
--------


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of s t
Sent: Wednesday, November 04, 2009 5:48 PM
To: r-help at r-project.org
Subject: [R] Constrained Optimization

Hi All,
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.

I tried the "constrOptim" function in R and I'm running into some issues.
I first start off by setting my equalities to inequalities
x1+x2+x3 >= 1
x1+x2+x3 <= 1.001

However, I get a
"Error in constrOptim(c(1.00004, 0, 0), fr, grr, ui = t(A), ci = b) : 
  initial value not feasible
Execution halted" error.
The values 1.00004,0,0 are well within the constraints domain.

fr = function(x){
  x1 = x[1]
  x2 = x[2]
  x3 = x[3]
  x1*(1-x1) + x2*(1-x2) + x3*(1-x3)
}

grr = function(x){
  x1 = x[1]
  x2 = x[2]
  x3 = x[3]
  c(1-2*x1,1-2*x2,1-2*x3)
}

A =
matrix(c(1,1,1,-1,-1,-1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1),3,8,byrow=T)
b = c(1,-1.001,0,0,0,1,1,1)

y = constrOptim(c(1.00004,0,0),fr,grr,ui = t(A),ci = b)

Any help/pointers greatly appreciated.

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

            
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