Você está na página 1de 2

Coordinate Descent and Golden Selection Search

YIK LUN, KEI


golden = function(f, lower.bound, upper.bound, tolerance = 1e-5)
{
golden.ratio = 2/(sqrt(5) + 1)
x1 = lower.bound + golden.ratio*(upper.bound - lower.bound)
x2 = upper.bound - golden.ratio*(upper.bound - lower.bound)
f1 = f(x1)
f2 = f(x2)
while (abs(upper.bound - lower.bound) > tolerance)
{
if (f2 > f1){
lower.bound = x2
x2 = x1
f2 = f1
x1 = lower.bound + golden.ratio*(upper.bound - lower.bound)
f1 = f(x1)
}
else{
upper.bound = x1
x1 = x2
f1 = f2
x2 = upper.bound - golden.ratio*(upper.bound - lower.bound)
f2 = f(x2)
}
}
(lower.bound + upper.bound)/2
}
g<-function(x,y){ 5*x^2 - 6*x*y + 5*y^2}
x<-seq(-1.5,1.5,len=100)
y<-seq(-1.5,1.5,len=100)
z<-outer(x,y,g)
CD<-function(startx,starty,maxx,maxy,iter,tolerance = 1e-5){
new<- matrix(NA,ncol=2,nrow=(iter+1))
new[1,1]<-startx; new[1,2]<-starty
for(i in 2:(iter+1)){
a<-function(x){5*x^2 - 6*x*new[i-1,2] + 5*new[i-1,2]^2}
new[i,1] <- golden(a,new[i-1,1],maxx)
b<-function(y){5*new[i,1]^2 - 6*new[i,1]*y + 5*y^2}
new[i,2]<- golden(b,new[i-1,2],maxy)
if(abs(new[i,1]-new[i-1,1]) < tolerance && abs(new[i,2]-new[i-1,2]) < tolerance){break}
}
return(new)
}

cd<-CD(startx=-1.5,starty=-1.5,maxx=1.5,maxy=1.5,iter=15)

1.5

contour(x,y,z,levels = seq(0.5,5,by=.9))
newx<-sort(cbind(cd[,1],cd[,1]),decreasing=F)[-1]
newy<-sort(cbind(cd[,2],cd[,2]),decreasing=F)[-length(newx)]
cd<-cbind(newx,newy)
points(cd[,1],cd[,2],type="l",col="red")

1.0

4.1

0.5

2.3

0.5

0.5

1.4
3.2

1.5

1.5

1.0

0.5

0.0

0.5

1.0

1.5

Você também pode gostar