Genetic Algorithm
clear all
close all
clc
% Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
% Creating a 2-Dimensional mesh
[xx, yy] = meshgrid(x, y);
% Evaluating 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
% Statistical Behaviour unbounded
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(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'MarkerSize',5,'MarkerFaceColor','r')
xlabel('x value')
ylabel('y value')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
% Statistical behaviour with upper & lower bounds
for i = 1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
xlabel('x value')
ylabel('y value')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
% GA Interations
options = optimoptions('ga')
options = optimoptions(options,'populationsize',170)
for i = 1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
xlabel('x value')
ylabel('y value')
title('bounded inputs with modified population size')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
% Stalagmite Function
function f = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
A = (sin(5.1*pi*x+0.5))^6;
B = exp(-4*log(2)*(x-0.0667)^2/.64);
C = (sin(5.1*pi*y+0.5))^6;
D = exp(-4*log(2)*(y-0.0667)^2/0.64);
f = -(A*B*C*D);
end



Genetic Algorithm (GA)
-> The genetic algorithm in matlab is derived from the idea of Charles Darwin's Evolution Theory.
-> It is a technique for solving both constrained & unconstrained optimization problems that are based on natural selection.
-> Genetic algorithm uses the local optimal points as an input & generates global optimal points for that particular function.
-> The genetic algorithm is used to find out the optimal solution of irregular or discontinuous functions for which finding the optimal solution is difficult.
-> A scaling factor is performed for all the local points available & based upon that factor they are selected as parents & transferred to the next generation.
-> Points with high scaling factors are directly transfered to the next generation as parents & remaining points undergo mutation and cross over based upon their scaling.
-> In the cross over, two point traits are mixed to generate a new child for the next generation.
-> So in mutation, the traits inside a single point are changed to create a child for the next generation.
-> Thus by repeating this process over and over again we will get a global optimization point.
-> For performing a genetic algorithm we use stalagmite function.
Explanation of Code is as follows
1) I started with linspace command to define x & y values with num_cases.
2) Then i used meshgrid command for 2D array.
3) for loop executed to evaluate the stalagmite function.
4) Then for statistical behaviour unbounded, respective coding and plotting was done & executed.
5) Then for statistical behaviour for lower & upper bounds coding & plotting was done and executed.
6) Then at last for statistical behaviour with increasing iterations, respective coding & plotting was done and executed.