Week 5 - Genetic Algorithm
clear
close all
clc
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1_x = (sin(5.1*pi*x + 0.5)).^6;
f1_y = (sin(5.1*pi*y + 0.5)).^6;
t1 = ((x-0.0667).^2)/(0.64);
f2_x = exp(-4.*log(2)*t1);
t2 = ((y-0.0667).^2)/(0.64);
f2_y = exp(-4.*log(2)*t2);
f = -(f1_x*f2_x*f1_y*f2_y);
end
% creating the mesh
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx,yy]=meshgrid(x,y);
% for plotting the surface of stalagmite
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)
shading interp
% evalutaing the function
options=optimoptions('ga','PlotFcn',{@gaplotbestf,@gaplotstopping},'populationsize',170);
[x,fval]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],options)
xopt=x(1);
yopt=x(2);
figure(1)
hold on
plot3(xopt,yopt,-fval,'ro','markersize',5,'markerfacecolor','r')
OUTPUT:-



STUDY -01 STATISTICAL STUDY WITHOUT BOUNDATION
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% creating 2 - dimensional mesh
[xx,yy] = meshgrid(x,y);
% evaluating the stalagmite function
for i = 1 : length(xx)
for j = 1 : length(yy)
inputs_vector(1) = xx(i,j);
inputs_vector(2)= yy (i,j);
f(i,j) = stalagmite(inputs_vector);
end
end
num_cases = 50;
% STATISTICAL BEHAVIOUR (UNBOUNDED)
tic %calculating the study time through tic and toc command
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2); %calculating the optimized value of the function
xopt(i)=inputs(1); %at each iteration
yopt(i)=inputs(2);
end
study1_time=toc %displaying the time taken to evaluate
% PLOTING THE GRAPH
figure(1)
subplot(2,1,1)
surfc(x,y,-f);
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
xlabel('x');
ylabel('y');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('iterations')
ylabel('function min value')

STUDY -02 STATISTICAL STUDY WITH BOUNDATION.
% STATISTICAL BEHAVIOUR (BOUNDED)
tic %calculating the study time through tic and toc command
for j=1:num_cases
[inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); %calculating the optimized value of the function
xopt(j)=inputs(1); %at each iteration with boundation
yopt(j)=inputs(2);
end
study2_time=toc
%PLOTTING
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');
ylabel('y');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('iterations')
ylabel('function min value')

STUDY-03 STATISTICAL STUDY WITH INCREASE IN SIZE OF POPULATION.
%optimoptions function study 03
options=optimoptions('ga','populationsize',300) %increaing the sixe of population by using
tic
for k=1:num_cases
[inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc
%PLOTTING
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');
ylabel('y');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('iterations')
ylabel('function min value')

1. WHAT IS GA?
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions.
2. Step by step explain code to optimise the stalagmite function and find the global maxima of the function.
*In the intial stage of program we creat the code for stalgmite function.
*creating one dimension array for creating search space.
* By using mesh command search space to find global maxima of stalgmite function is created.
*Now calculating the values of fitness function at each point of the mesh grid formed.
*Now creating its surface through ‘surfc’ command which will also provide the contour of the generated surface.
*While using surfc applying the -ve sign to the function will generate the surface upside down which will be quite good to get the maxima of the function, as it will provide positive value for the maxima using the -ve sign.