Week 4.1 - Genetic Algorithm
Our objective is
1) Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
2) Plot graphs for all 3 studies and for F maximum vs no. of iterations.
Approach stragedy
The function is already given in video of stalagmite, so here we directly explain the coding and function of required and for that I will do 4 coding 1st is for global maxima 2nd is for function i.e stalagmite 3rd is for studies 4th is also a function but for minima.
WRITING CODE OF GLOBAL MAXIMA
FIRST OF ALL I STARTED WITH

clear all
close all
clc
x=linspace(0,0.6,151);
y=linspace(0,0.6,151);
[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-Axis')
ylabel('Y-Axis')
shading interp
[inputs,fval]=ga(@stalagmite,2);
STALAGMITE FUNCTION CODE
function question_given=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
fn1x=(sin((5.1*pi*x)+0.5))^6;
fn1y=(sin((5.1*pi*y)+0.5))^6;
fn2x=exp((-4*log(2)/0.64)*((x-0.0667)^2));
fn2y=exp((-4*log(2)/0.64)*((y-0.0667)^2));
question_given=fn1x*fn1y*fn2x*fn2y;
end
"I have run above code 3 times and the results came same as earlier plz find all 3 plots attached"
Plot graphs for all 3 studies and for F maximum vs no. of iterations
For global maxima we use "question_given=fn1x*fn1y*fn2x*fn2y;"
For global minima we use "question_given=-fn1x*fn1y*fn2x*fn2y;"
WRITING CODE OF GLOBAL MINIMA
\\ plz note this code I have got helped from sarang sir video named stalagmite HW \\
clear all
close all
clc
x=linspace(0,0.6,151);
y=linspace(0,0.6,151);
[x,y]=meshgrid(x,y);
num_case=50;
f=global_minima(x,y);
tic
%study1 statistical behaviour
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
running_time=toc
%ploting 1
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5)
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('function Minimum')
tic
%study2- Statistical behaviour
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.4,0.4],[],[]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
running_time=toc
%ploting 2
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5)
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('function Minimum')
%increasing ga iteration
options=optimoptions('ga')
options=optimoptions(options,'populationsize',170)
tic
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.4,0.4],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
running_time=toc
%ploting 3
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5)
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('function Minimum')
function question_given=global_minima(x,y)
fn1x=(sin((5.1*pi*x)+0.5)).^6;
fn1y=(sin((5.1*pi*y)+0.5)).^6;
fn2x=exp((-4*log(2)/0.64).*((x-0.0667).^2));
fn2y=exp((-4*log(2)/0.64)*((y-0.0667).^2));
question_given=-fn1x.*fn1y.*fn2x.*fn2y;
end
All above 3 cases have same coding the difference is only the minute changes, so I will explain only one case and if there is any minute changes came i will explain that also.
I have started with clear all, close all, clc this cmd is already discussed in 1st coding then I created x and y matrix by using linspace cmd of 151
then i created matrix of 151x151 by using meshgrid cmd and stored in [x,y], then i assigned 50 to num_case and wrote f=global_minima(x,y),after that i used tic this is for starting of time after that I used for loop with i=1:num_case then I wrote [inputs, fopt(i)]=ga(@stalagmite,2) here input is x and fopt(i) always changes the each loop , 2 is no. of variable after that I wrote xopt(i) & yopt(i) this always changes with each loop and stored in input(1) and input(2) respectively in matrix form after that I ended for loop and ended time by using toc cmd and stored in running_time.
Then I started with figure(1) which is used to naming plot windows further I wrote subplot(2,1,1) here 2 stands for no. of grid row,1 stands for no. of grid column & 1 stands for grid position of new axes.
hold on cmd is used to hold plot by keeping old lines and also keeping new lines then surf cmd is used to plot surface of counter figure then I used shading inturpt which make smooth color and disappear the grid lines afterward plot3 cmd is used to create 3d space lines by using xopt,yopt, fopt as variable then I put title then again subplot afterward plot(fopt) is used to plot fopt variable finally lebelling the x and y axis given above.
this whole things is used in all 3 studies there is but minute difference is in 3rd case that is