You are on page 1of 5

MATLAB ASSIGNMENT

SIMULATED ANNEALING

REVATHI S
410115004
ICE. DEPT.
SIMULATED ANNEALING

OBJECTIVE:

Simulate annealing to minimize the function: f(x, y) = (x + 2y -7)2 + (2x + y - 5)2


with x and y ranging from -10 to 10. Plot the following graph a) 3D graph
b) Convergence graph

ALGORITHM:

Step 1: Initialize the value the temperature ‘T’.

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 3: Compute the corresponding cost function value f (xc , yc).

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 5: Compute the corresponding cost function value f (xn, yn).

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 8: Reduce the temperature T=T*r , where r=1/log(100) is a scaling factor.

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

M code for function:


function[result]=minfn(x,y)
result=((x+2*y-7)^2+(2*x+y-5)^2)
RESULTS:

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

Figure 2: convergence plot

You might also like