[R] [Fwd: Re: optimization subject to constraints]

Ravi Varadhan rvaradhan at jhmi.edu
Tue Aug 10 17:00:59 CEST 2010


I think the problem is because the the Hessian of the augmented Lagrangian iis singular at c(0,0). 

Try this:

require(alabama)

heq <- function(x) {
x[1]^2+x[2]^2 - 1
}

>  constrOptim.nl(par=c(0,0), fn=f, heq=heq, control.outer=list(trace=FALSE))
$par
[1] -0.7071067 -0.7071067

$value
[1] -1.414213

$iterations
[1] 10

$lambda
[1] -0.7068717

$penalty
[1] -6.496021e-08

$counts
function gradient 
     100       30 


Ravi.

____________________________________________________________________

Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University

Ph. (410) 502-2619
email: rvaradhan at jhmi.edu


----- Original Message -----
From: Gildas Mazo <gildas.mazo at curie.fr>
Date: Tuesday, August 10, 2010 10:11 am
Subject: [R] [Fwd: Re:  optimization subject to constraints]
To: r-help at r-project.org


> 
> ----- Original Message -----

>  From Gildas Mazo <gildas.mazo at curie.fr>

>  Date Tue, 10 Aug 2010 15:49:19 +0200

>  To Matthias Gondan <matthias-gondan at gmx.de>
 Subject Re: [R] optimization subject to constraints
> Danke schön Matthias.
>  
>   I had naively started with x0 = c(0,0) and I got a "Redundant
>  constraints were found" error. What's the problem with (0,0) ?
>  
>  
>  
>  
>  
>  
>  
>  Matthias Gondan a écrit :
>  >  try this (package Rsolnp)
>  >
>  > library(Rsolnp)
>  >
>  > g<- function(x)
>  > {
>  >   return(x[1]^2+x[2]^2)
>  > } # constraint
>  >
>  > f<- function(x)
>  > {
>  >   return(x[1]+x[2])
>  > } # objective function
>  >
>  > x0 = c(1, 1)
>  >
>  > solnp(x0, fun=f, eqfun=g, eqB=c(1))
>  >
>  >
>  >
>  > Am 10.08.2010 14:59, schrieb Gildas Mazo:
>  >> Thanks, but I still cannot get to solve my problem: consider this 
> simple
>  >> example:
>  >>
>  >> ########
>  >>
>  >> f<- function(x){
>  >>    return(x[1]+x[2])
>  >> } # objective function
>  >>
>  >> g<- function(x){
>  >>    return(x[1]^2+x[2]^2)
>  >> } # constraint
>  >>
>  >> #########
>  >>
>  >> I wanna Maximize f(x) subject to g(x) = 1. By hand the solution is
>  >> (1/sqrt(2), 1/sqrt(2), sqrt(2)). This is to maximizing a linear function
>  >> subject to a nonlinear equality constraint.  I didn't find any suitable
>  >> function in the packages I went through.
>  >>
>  >> Thanks in advance,
>  >>
>  >> Gildas
>  >>
>  >>
>  >>
>  >>
>  >>
>  >> Spencer Graves a écrit :
>  >>>         To find every help page containing the term "constrained
>  >>> optimization", you can try the following:
>  >>>
>  >>>
>  >>> library(sos)
>  >>> co<- findFn('constrained optimization')
>  >>>
>  >>>
>  >>>        "Printing" this "co" object opens a table in a web browser 
> with
>  >>> all matches sorted first by the package with the most matches and 
> with
>  >>> hot links to the documentation page.
>  >>>
>  >>>
>  >>> writeFindFn2xls(co)
>  >>>
>  >>>
>  >>>        This writes an excel file, with the browser table as the second
>  >>> tab and the first being a summary of the packages.  This summary 
> table
>  >>> can be made more complete and useful using the "installPackages"
>  >>> function, as noted in the "sos" vignette.
>  >>>
>  >>>
>  >>>        A shameless plug from the lead author of the  "sos" package.
>  >>>        Spencer Graves
>  >>>
>  >>>
>  >>> On 8/9/2010 10:01 AM, Ravi Varadhan wrote:
>  >>>> constrOptim can only handle linear inequality constraints.  It cannot
>  >>>> handle
>  >>>> equality (linear or nonlinear) as well as nonlinear inequality
>  >>>> constraints.
>  >>>>
>  >>>> Ravi.
>  >>>>
>  >>>> -----Original Message-----
>  >>>> From: r-help-bounces at r-project.org
>  >>>> [ On
>  >>>> Behalf Of Dwayne Blind
>  >>>> Sent: Monday, August 09, 2010 12:56 PM
>  >>>> To: Gildas Mazo
>  >>>> Cc: r-help at r-project.org
>  >>>> Subject: Re: [R] optimization subject to constraints
>  >>>>
>  >>>> Hi !
>  >>>>
>  >>>> Why not constrOptim ?
>  >>>>
>  >>>> Dwayne
>  >>>>
>  >>>> 2010/8/9 Gildas Mazo<gildas.mazo at curie.fr>
>  >>>>
>  >>>>> Dear R users,
>  >>>>>
>  >>>>> I'm looking for tools to perform optimization subject to constraints,
>  >>>>> both linear and non-linear. I don't mind which algorithm may be
>  >>>>> used, my
>  >>>>> primary aim is to get something general and easy-to-use to study
>  >>>>> simples
>  >>>>> examples.
>  >>>>>
>  >>>>> Thanks for helping,
>  >>>>>
>  >>>>> Gildas
>  >>>>>
>  >>>>> ______________________________________________
>  >>>>> R-help at r-project.org mailing list
>  >>>>> 
>  >>>>> PLEASE do read the posting guide
>  >>>>>
>  >>>> 
>  >>>>
>  >>>>
>  >>>> -guide.html>
>  >>>>> and provide commented, minimal, self-contained, reproducible code.
>  >>>>>
>  >>>>      [[alternative HTML version deleted]]
>  >>>>
>  >>>> ______________________________________________
>  >>>> R-help at r-project.org mailing list
>  >>>> 
>  >>>> PLEASE do read the posting guide
>  >>>> 
>  >>>> and provide commented, minimal, self-contained, reproducible code.
>  >>>>
>  >>>> ______________________________________________
>  >>>> R-help at r-project.org mailing list
>  >>>> 
>  >>>> PLEASE do read the posting guide
>  >>>> 
>  >>>> and provide commented, minimal, self-contained, reproducible code.
>  >>
>  >
>  > ______________________________________________
>  > R-help at r-project.org mailing list
>  > 
>  > PLEASE do read the posting guide
>  > 
>  > and provide commented, minimal, self-contained, reproducible code.
>  >
>  >
>  
>   
> ______________________________________________
>  R-help at r-project.org mailing list
>  
>  PLEASE do read the posting guide 
>  and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list