Week 4.1 - Genetic Algorithm
Aim : A code to optimise the stalagmite function and find the global maxima of the function.
Objective : To a Write a program for optimising the stalagmite function and find the global maxima of the function.
clear all
close all
clc
% inputs
x = linspace (0,0.6,150);
y = linspace(0,0.6,150);
[xx yy] = meshgrid(x,y);
for i = 1:length(xx)
for j = 1:length(yy)
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);
f(i,j) = stalagmite(input_vector);
end
end
% surfc(xx,yy,f)
% xlabel('x value')
% ylabel('y value')
% shading interp
% inputs for number of iterations
num_cases = 50;
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
% removing the crosshatch
shading interp
% plotting
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputstions')
hold off
subplot(2,1,2)
%plotting function output
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
% case 2
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputstions')
hold off
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
% case 3
options = optimoptions('ga')
options = optimoptions(options,'populationsize',200)
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.2;0.2],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded with inputstions')
hold off
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
Conclusion :
Explaination of Genetic Algoritham(ge )
it is method for solving both constrained and unconstrianed optimization problems with selection or without selection values .
The syntax are:
ga(function,number of variables )
For given above code,the aim was to optimise the valve
For case 1,since the inputs has only two varibles it is substituted as shown in above syntax ,but we didnt obtained the required ouput.
i,e. [inputs,fopt(i)] = ga(@stalagmite,2);
we obsevered the plot values are in zigzag manner,and we couldn't find the maximum value for that purpose we go for case 2
For case 2, we have similar condition
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0:0],[0.6:0.6]);
the open braceses indicates it has conditions like,
first two brecese indicates, it must satisfy
X + Y <1
y - x >1
the second two brecese indicates, it must satisfy
x + y = 2
y-x =3 for above
in plot (function maximum vs iterations ) the optimised values are started from -1 but that didn't satisfied the above condition and the peaks are not perfect .and the obtained plot are not perfect as for best fit the r must ranges between 0.95-0.99,for perfect fit r must ranges to 1
For case 3,we have similar condition
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.2;0.2],[],[],options);
the first four square brackets indicates as discussed in case 2 and we found the maxima valves are between in 0 and 0.2 so i have changed the valve from [0;0] [0.6;0.6] in case2 to [0;0][0.2;0.2] in case 3 and we obtained the required maximum value of function