最適化問題

最適化問題

# 勾配法
tol=0.0001;grad=100;
x=10;hist.x=x;lambda=0.1;
while(grad>tol) {
 grad=(2*x+2)
 x=x-lambda*grad
 hist.x=c(hist.x,x)
}
par(mfrow=c(1,2));
xs=seq(-10,10,0.1)
plot(xs,xs^2+2*xs+1)
plot(hist.x)

# naive stochastic optimization
sx=10;hist.sx=sx
for (i_loop in 1:100){
  sx.temp=sx+rnorm(1);
  if ((sx.temp^2+2*sx.temp+1)<(sx^2+2*sx+1)){
    sx=sx.temp
  }
  hist.sx=c(hist.sx,sx)
}

# simulated annealing
x=10;maxIt=1000;
c=1;s=1;temp=1;eta=0.99;
hist.x=matrix(0,nrow=maxIt);
for (i_loop in 1:maxIt) {
  x.new=x+rnorm(1,mean=0,sd=temp*s)
  E.old=x^2+2*x+1;
  E.new=x.new^2+2*x.new+1;
  Paccept=1/(1+exp((E.new-E.old)/(c*temp)))
  if (Paccept>runif(1)){x=x.new}
  temp=temp*eta;
  hist.x[i_loop]=x;
}

Leave a Reply