Re: [R] using optimize() correctly ...
Berend Hasselman wrote: If you do resopt - optim(-5,f, method=SANN,control=list(fnscale=-1)) you will get the global maximum. SANN: simulated annealing. But starting in -4 takes you to the local maximum. So if I understand correctly, this method would also yield the same sort of result (ie non-guaranteed global max or min over the range -10 to 10). But the help for optim recommends optimize for one-dimensional maximization. Yes, I recall reading this. As far as I know there is no general foolproof method for finding a global optimum except trying different initial points. No method can really replace one's own knowledge about a function. Agreed, however, I am testing a genetic algorithm's ability to find the global maximum/minimum and was looking for another independent tool to verify the results I am getting for f(x) and f(x, y), ie my various test functions which will be more complex than the one I posted (and many will be multi-modal since those present a challenge for any algorithm). Since R has proven to be quite capable in the past I thought I would check and see if it could easily do this. I was hoping to avoid having to plot and re-run various functions repeatedly over various ranges or starting points. Thanks again for the help/information, much appreciated. Esmail __ R-help@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.
Re: [R] using optimize() correctly ...
Hi Ravi! Ravi Varadhan wrote: Yes. Most classical optimization methods (e.g. gradient-type, Newton-type) are local, i.e. they do not attempt to locate the global optimum. Ah .. I see. The primary difficulty with global optimization is that there are no mathematical conditions that characterize global optimum in multi-modal problems. .. A simplistic strategy to find global optimum is to use local methods with multiple starting values. :-) .. well, that is somewhat similar to the approach the genetic algorithm uses, well, at least with respect of having many starting points. Again the problem is that you don't have any guarantee that you have found the global optimum. Well, then it looks like I am out of luck if I wanted to plug in a function and provide end ranges and get the global max/min. I was hoping this would provide a way for me to verify the workings of a genetic algorithm I am testing. I appreciate you taking the time to explain this so clearly, thanks again, Esmail __ R-help@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.
Re: [R] using optimize() correctly ...
Hello Berend, Berend Hasselman wrote: Your function is not unimodal. The help for optimize states: If f is not unimodal, then optimize() may approximate a local, but perhaps non-global, minimum to the same accuracy. Ah ok, I didn't read the manual page carefully enough. Do you know if R has a function to find the global maximum/minimum of a function of x over a given interval? nlminb(), optim(), in particular the option `method = L-BFGS-B' or the function spg() in BB package were recommended to use if I wanted to optimize a function over x and y given their respective intervals. Will they also potentially only give me the local maxima/minima? I am not a regular R user, so my knowledge is clearly not where is could/should be. Thanks, Esmail __ R-help@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.
Re: [R] using optimize() correctly ...
On 24-05-2009, at 14:24, Esmail wrote: Hello Berend, Berend Hasselman wrote: Your function is not unimodal. The help for optimize states: If f is not unimodal, then optimize() may approximate a local, but perhaps non-global, minimum to the same accuracy. Ah ok, I didn't read the manual page carefully enough. Do you know if R has a function to find the global maximum/minimum of a function of x over a given interval? If you do resopt - optim(-5,f, method=SANN,control=list(fnscale=-1)) you will get the global maximum. SANN: simulated annealing. But starting in -4 takes you to the local maximum. But the help for optim recommends optimize for one-dimensional maximization. As far as I know there is no general foolproof method for finding a global optimum except trying different initial points. No method can really replace one's own knowledge about a function. Berend btw. i am away during this week so i won't be replying. __ R-help@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.
Re: [R] using optimize() correctly ...
Yes. Most classical optimization methods (e.g. gradient-type, Newton-type) are local, i.e. they do not attempt to locate the global optimum. The primary difficulty with global optimization is that there are no mathematical conditions that characterize global optimum in multi-modal problems. For local optimum, you have the first- and second-order Kuhn-Tucker conditions. A simplistic strategy to find global optimum is to use local methods with multiple starting values. Again the problem is that you don't haev any guarantee that you have found the global optimum. The larger the number of starting values, the greater your chances of finding the global optimum. There are more principled strategies than the random multi-start approach, but even they are not guaranteed to work. Ravi. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: Esmail esmail...@gmail.com Date: Sunday, May 24, 2009 8:27 am Subject: Re: [R] using optimize() correctly ... To: Berend Hasselman b...@xs4all.nl Cc: r-help@r-project.org Hello Berend, Berend Hasselman wrote: Your function is not unimodal. The help for optimize states: If f is not unimodal, then optimize() may approximate a local, but perhaps non-global, minimum to the same accuracy. Ah ok, I didn't read the manual page carefully enough. Do you know if R has a function to find the global maximum/minimum of a function of x over a given interval? nlminb(), optim(), in particular the option `method = L-BFGS-B' or the function spg() in BB package were recommended to use if I wanted to optimize a function over x and y given their respective intervals. Will they also potentially only give me the local maxima/minima? I am not a regular R user, so my knowledge is clearly not where is could/should be. Thanks, Esmail __ R-help@r-project.org mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. __ R-help@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.
[R] using optimize() correctly ...
Hi, I am trying to use the optimize function to optimize a function. The results I am getting don't agree with what I compute on my own and when I look at the graph of f(x) = 100 + ((x-10)**2 + (x-10)) * cos(x-10), where -10 = x = 10 in gnuplot. I suspect I am making a mistake in the usage of the R optimize function, perhaps someone could point out where? f-function(x) 100 + ((x-10)**2 + (x-10)) * cos(x-10) to MAXIMIZE: result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=TRUE) result $maximum [1] -2.728743 $objective [1] 247.3284 to MINIMIZE result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=FALSE) result $minimum [1] 6.290112 $objective [1] 91.52681 However, I believe the correct values should be minimize: x = -5.838 val= -133.020 maximize: x = -8.957 val= 438.448 Thanks, Esmail This is w/ R version 2.8.1 (2008-12-22) under Ubuntu 9.04 __ R-help@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.
Re: [R] using optimize() correctly ...
Esmail Bonakdarian-4 wrote: Hi, I am trying to use the optimize function to optimize a function. The results I am getting don't agree with what I compute on my own and when I look at the graph of f(x) = 100 + ((x-10)**2 + (x-10)) * cos(x-10), where -10 = x = 10 in gnuplot. I suspect I am making a mistake in the usage of the R optimize function, perhaps someone could point out where? f-function(x) 100 + ((x-10)**2 + (x-10)) * cos(x-10) to MAXIMIZE: result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=TRUE) result $maximum [1] -2.728743 $objective [1] 247.3284 to MINIMIZE result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=FALSE) result $minimum [1] 6.290112 $objective [1] 91.52681 However, I believe the correct values should be minimize: x = -5.838 val= -133.020 maximize: x = -8.957 val= 438.448 Your function is not unimodal. The help for optimize states: If f is not unimodal, then optimize() may approximate a local, but perhaps non-global, minimum to the same accuracy. In you case, optimize is locating a local maximum/minimum. You should restrict the search to the interval [-10 , -7 ] so result=optimize(f, c(-10,10), lower = -10, upper=-7, maximum=TRUE) will locate your global maximum. Berend -- View this message in context: http://www.nabble.com/using-optimize%28%29-correctly-...-tp23689607p23690832.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.