Matlab Assignment Simulated Annealing: Revathi S 410115004 Ice. Dept
Matlab Assignment Simulated Annealing: Revathi S 410115004 Ice. Dept
Matlab Assignment Simulated Annealing: Revathi S 410115004 Ice. Dept
SIMULATED ANNEALING
REVATHI S
410115004
ICE. DEPT.
SIMULATED ANNEALING
OBJECTIVE:
ALGORITHM:
Step 2: Randomly select the current values for the variables ‘x’ and ‘y’ from the
range as defined above. Let it be ‘xc’and ‘yc’ respectively.
Step 4: Randomly select the next set of values for the variables ‘x’ and ‘y’ from the
range as defined above. Let it be ‘xn’ and ‘yn’ respectively.
Step 6: If f (xn, yn)<= f(xc , yc), then the current values for the random variables
xc = xn, yc= yn.
Step 7: If f(xn, yn)>f(xc , yc), then the current values for the random variables
xc = xn, yc= yn are assigned only when
exp [(f (xc , yc)- f(xn, yn))/T] > rand
Note that when the temperature ‘T’ is less, the probability of selecting the
new values as the current values is less.
Step 9: Repeat STEP 3 to STEP8 for n times until ‘T’ reduces to the
particular percentage of initial value assigned to ‘T
M-code for Simulated Annealing:
clc
curvalx=randi([-10 10]);
curvaly=randi([-10 10]);
curcost=minfn(curvalx,curvaly);
T=1000;
for i=1:1:70
newvalx=randi([-10 10]);
newvaly=randi([-10 10]);
newcost=minfn(newvalx,newvaly);
if((newcost-curcost)<=0)
curvalx=newvalx;
curvaly=newvaly;
curcost=minfn(curvalx,curvaly);
else
if(exp((curcost-newcost)/T)>rand)
curvalx=newvalx;
curvaly=newvaly;
curcost=minfn(curvalx,curvaly);
end
end
T=T/log(70);
I(i)=i;
a(i)=curvalx;
b(i)=curvaly;
c(i)=minfn(curvalx,curvaly);
pause(0.2)
end
figure(1)
plot3(a,b,c)
xlabel('Current value of x');
ylabel('Current value of y');
zlabel('cost function');
title('3D plot');
hold on
figure(2)
plot(I,c);
xlabel('iterations');
ylabel('cost function');
title('Convergence plot');
hold on
With 70 iterations, the best values of x and y are 3, 1 respectively and the corresponding cost
function value is 8.
3D plot
60 X: -3
X: -4
Y: 4
Y: 6
Z: 53
Z: 50
50
X: -3
40 Y: 7
Z: 32
cost function
30 X: -2
Y: 6
Z: 18
20
X: 3
Y: 1
Z: 8
10
0
10
5 10
5
0
0
Current value of y -5
-5 Current value of x
-10 -10
Figure 1: 3D plot
Convergence Plot
55
50
45
40
35
cost function
30
25
20
15
10
5
0 10 20 30 40 50 60 70
iterations