Here you will find daily news and tutorials about R, contributed by over 573 bloggers.
There are many ways to follow us - By e-mail:On Facebook: If you are an R blogger yourself you are invited to add your own R content feed to this site (Non-English R bloggers should add themselves- here)

I noticed that if I had a large variance in the random normal generator, the optimizer would jump all over the place but would not settle down in the best optimum. So I added in a second step that takes the best of the attempted values and uses them as a second set of start values. Then, a second smaller variance is used that does not allow for major jumps. The two step anneal function can be seen below.

twostep.anneal<-function(f,mu,n=1000,sig=c(1,.1),t=1,g=0.999){
xm = mu[1]
ym = mu[2]
fm = f(xm,ym)
x = rep(NA,n*2)
y = rep(NA,n*2)
fx = rep(NA,n*2)
for(k in 1:2){
s=sig[k]
if(k==2){
xm=best[1]
ym=best[2]
}
for (i in 1:n){
dxm = xm+rnorm(1,0,s)
dym = ym+rnorm(1,0,s)
fdm = f(dxm, dym)
t = t*g
if (runif(1) < (fdm/fm)^(1/t)){
xm = dxm
ym = dym
fm = fdm
}
x[(i+(k-1)*n)] = xm
y[(i+(k-1)*n)] = ym
fx[(i+(k-1)*n)] = fm
}
ii = which(fx==max(fx, na.rm=TRUE))[1]
best=c(x[ii], y[ii])
}
list(x = x, y = y, fx = fx, best = best , fbest = fx[ii], t=t)
}

To run the function and see the path of the anneal function use this code.