Week 4.1 - Genetic Algorithm
Program for Genetic Algorithm
% Program for stalagmite function
function [f] = stalagmite(input_vector);
f1x = (sin(5.1*pi*input_vector(1)+0.5))^6;
f1y = (sin(5.1*pi*input_vector(2)+0.5))^6;
f2x = exp((-4*log(2)*(input_vector(1)-0.0667)^2)/0.64);
f2y = exp((-4*log(2)*(input_vector(2)-0.0667)^2)/0.64);
[f] = -(f1x*f1y*f2x*f2y);
end
% Code for finding global maxima
clear all
close all
clc
% Inputs
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% No of iteration
num_cases = 50;
% Combining the inputs
[xx,yy] = meshgrid(x,y);
% Calculating stalagmite function
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
tic
% Study1 : stastical behaviour with unbounded inputs
for k = 1:num_cases
[input1,fopt1(k)] = ga(@stalagmite,2)
xopt1(k) = input1(1);
yopt1(k) = input1(2);
end
time = toc
subplot(2,1,1);
figure(1);
hold on
surfc(x,y,-f)
shading interp
plot3(xopt1,yopt1,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
title('stastical behaviour with unbounded inputs');
subplot(2,1,2)
plot(-fopt1)
xlabel('iteration')
ylabel('function maximum')
% Study2 : stastical behaviour with bounded inputs
tic
for l = 1:num_cases
[input2,fopt2(l)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt2(l) = input2(1);
yopt2(l) = input2(2);
end
time2 = toc
figure(2);
subplot(2,1,1);
hold on
surfc(x,y,-f);
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('stastical behaviour with bounded inputs');
subplot(2,1,2)
plot(-fopt2)
xlabel('iteration')
ylabel('function maximum')
% Study3 : stastical behaviour with incresing ga iteration
options = optimoptions('ga')
options = optimoptions(options,'populationsize',170)
tic
for m = 1:num_cases
[input3,fopt3(m)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt3(m) = input3(1);
yopt3(m) = input3(2);
end
time3 = toc
figure(3);
subplot(2,1,1);
hold on
surfc(x,y,-f);
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('stastical behaviour with incresing ga iteration');
subplot(2,1,2)
plot(-fopt3)
xlabel('iteration')
ylabel('function maximum')
Outputs
Study1 : stastical behaviour with unbounded inputs

Study2 : stastical behaviour with bounded inputs

Study3 : stastical behaviour with incresing ga iteration

Aim : Finding optimized solution with the help of genetic algorithm & ploting the graph to find the global maxima .
Governing equation :

Objectives :
1) Study about optimization process & find global maxima.
2) Study about genetic algorithm & using "ga" synatx for matlab.
3) Plot graphs for all 3 studies and for F maximum vs no. of iterations.
Body of the program :
1) Using clear all, close all, clc command for making sure only program codes to run & older to erased.
2) x & y values given from 0 to 0.6 with 150 number .
3) The no. of iterations for ga is defind as num_cases = 50.
4) Mesh grid is used for 2d ploting.
5) For loop used to give inputs for stalagmite function which run for 50 iterations.
6) The tic & toc function records the current time.
7) Study1 : stastical behaviour with unbounded inputs. The optimum values for the inputs taken for each iterations with the help of for loop.
8) ga defines a set of lower and upper bounds for the variables.
9) Subplot divides the current figure into rectangular panes that are numbered rowwise. Each pane contains an axes object for output.
10) figure command is used to figure graphics objects.
11) Use hold on to add a second line plot without deleting the existing line plot.
12) surfc is used to creat 3d surface plot.
13) shading interp varies the color in each line segment and face.
14) Plot3 is used for 3d plotting.
15) title command is used for displaying the title.
16) xlabel & ylabel is used for displaying iteration in x-axis & function maximum y-axis.
17) Study2 : Same process follwed as per study1 to find stastical behaviour with bounded inputs. The only difference is upper & lower bound is given in ga syntax.
18) Study3 : Same process follwed as per study1 to find stastical behaviour with incresing ga iteration. The only difference is ga syntax changed by using optimoptions & incresing ga iteration / populationsize to 170.
Conclusion : In study1 , we plot unbound condition. In study2 , we have given bounded condition which can limit the search space within the defined limit. In study3, incresed population size with bounded condition applied & the result is only one global maixima.
Reference : https://in.mathworks.com/help/gads/ga.html#d122e39387