Simulated Annealing (SA) with Matlab

function [x,fx] = sa(f,maxit,xL,xU)
% sa untuk minimasi fungsi dengan simulated annealing
% f adalah nama fungsi yang akan dicari minimumnya
% maxit – maksimum jumlah iterasi
% xL- batas bawah nilai x
% xU- batas atas nilai x
%
N=10;
n=length(xL);
x=zeros(N,n);
fx=zeros(N,1);
for i=1:N
x(i,:)=xL+(xU-xL).*rand(1,n);
fx(i)=feval(f,x(i,:));
end
iniT=mean(fx);%initial temperatur
parent=xL+(xU-xL).*rand(1,n);%nilai inisial x
fp=feval(f,parent);
k = 1; % boltzmann constant
c=0.7; %faktor reduksi
it=1;
deltaf=10;
while abs(deltaf)>1e-8
while it<maxit
newsol = (parent +(randperm(n)==n)*randn/100);
fnew=feval(f,newsol);
deltaf=fnew-fp;
if deltaf<0
parent=newsol;
fp=feval(f,parent);
else %gunakan kriteria metropolis
if rand < exp(-deltaf/(k*iniT))
%jika nilai rand < dari probabilitas
parent=newsol;
fp=feval(f,parent);
end
end
it=it+1;
end
iniT=c*iniT;
it=1;
if iniT<1e-8
break
end
end
x=parent;
fx=feval(f,x);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s