# [R] Schwefel Function Optimization

Hans W Borchers hwborchers at googlemail.com
Sat Feb 11 09:25:06 CET 2012

```Vartanian, Ara <aravart <at> indiana.edu> writes:

> All,
>
> I am looking for an optimization library that does well on something as
>  chaotic as the Schwefel function:
>
> schwefel <- function(x) sum(-x * sin(sqrt(abs(x))))
>
> With these guys, not much luck:
>
> > optim(c(1,1), schwefel)\$value
> [1] -7.890603
> > optim(c(1,1), schwefel, method="SANN", control=list(maxit=10000))\$value
> [1] -28.02825
> > optim(c(1,1), schwefel, lower=c(-500,-500), upper=c(500,500),
>                         method="L-BFGS-B")\$value
> [1] -7.890603
> > optim(c(1,1), schwefel, method="BFGS")\$value
> [1] -7.890603
> > optim(c(1,1), schwefel, method="CG")\$value
> [1] -7.890603

Why is it necessary over and over again to point to the Optimization Task
View? This is a question about a global optimization problem, and the task
view tells you to look at packages like 'NLoptim' with specialized routines,
or use one of the packages with evolutionary algorithms, such as 'DEoptim'
or'pso'.

library(DEoptim)
schwefel <- function(x) sum(-x * sin(sqrt(abs(x))))
de <- DEoptim(schwefel, lower = c(-500,-500), upper = c(500,500),
control = list(trace = FALSE))
de\$optim\$bestmem
#     par1     par2
# 420.9687 420.9687
de\$optim\$bestval
# [1] -837.9658

> All trapped in local minima. I get the right answer when I pick a starting
> point that's close:
>
> > optim(c(400,400), schwefel, lower=c(-500,-500), upper=c(500,500),
> >                             method="L-BFGS-B")\$value
> [1] -837.9658
>
> Of course I can always roll my own:
>
> r <- vector()
> for(i in 1:1000) {
>   x <- runif(2, -500,500)
>   m <- optim(x, schwefel, lower=c(-500,-500), upper=c(500,500),
>                 method="L-BFGS-B")
>   r <- rbind(r, c(m\$par, m\$value))
> }
>
> And this does fine. I'm just wondering if this is the right approach,
> or if there is some other package that wraps this kind of multi-start
> up so that the user doesn't have to think about it.
>
> Best,
>
> Ara

```