IliaN at gertner.health.gov.il
2013-Aug-19 11:33 UTC
[R] problems with constrained optimization
I tried ti run a simple example with function solnp() from Rsolnp: find max(x+y) on a unit circle x*x+y*y=1. The answer should be x=y=1/sqrt(2) with max=sqrt(2). Here are my code and results ################################################> # test Rsolnp> library(Rsolnp)> fn1 <- function(x)+ { + x[1] + x[2] + }> cond <- function(x)+ { + z<- x[1]*x[1] + x[2]*x[2] + }> x0 <- c(0,1)> cval <- c(1)> optim <-solnp(x0,fun=fn1,eqfun=cond,eqB=cval)Iter: 1 fn: 1.0000 Pars: -3.053e-11 1.000e+00 solnp--> Completed in 1 iterations> optim$pars [1] -3.053113e-11 1.000000e+00 $convergence [1] 0 $values [1] 1 1 $lagrange [,1] [1,] 0.5 $hessian [,1] [,2] [1,] 2e+10 0 [2,] 0e+00 1 $ineqx0 NULL $nfuneval [1] 13 $outer.iter [1] 1 $elapsed Time difference of 0.00500083 secs>##################################################### SOMETHING IS WRONG BUT I DO NOT SEE ANY MY MISTAKE. [[alternative HTML version deleted]]
Bad starting value. Try c(3/5,4/5) On Aug 19, 2013, at 1:33 PM, <IliaN at gertner.health.gov.il> wrote:> I tried ti run a simple example with function solnp() from Rsolnp: find > max(x+y) on a unit circle x*x+y*y=1. The answer should be x=y=1/sqrt(2) > with max=sqrt(2). > > Here are my code and results > > ################################################ > >> # test Rsolnp > >> library(Rsolnp) > >> fn1 <- function(x) > > + { > > + x[1] + x[2] > > + } > >> cond <- function(x) > > + { > > + z<- x[1]*x[1] + x[2]*x[2] > > + } > >> x0 <- c(0,1) > >> cval <- c(1) > >> optim <-solnp(x0,fun=fn1,eqfun=cond,eqB=cval) > > > > Iter: 1 fn: 1.0000 Pars: -3.053e-11 1.000e+00 > > solnp--> Completed in 1 iterations > >> optim > > $pars > > [1] -3.053113e-11 1.000000e+00 > > > > $convergence > > [1] 0 > > > > $values > > [1] 1 1 > > > > $lagrange > > [,1] > > [1,] 0.5 > > > > $hessian > > [,1] [,2] > > [1,] 2e+10 0 > > [2,] 0e+00 1 > > > > $ineqx0 > > NULL > > > > $nfuneval > > [1] 13 > > > > $outer.iter > > [1] 1 > > > > $elapsed > > Time difference of 0.00500083 secs > > > >> > > ##################################################### > > SOMETHING IS WRONG BUT I DO NOT SEE ANY MY MISTAKE. > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.