Week 4.1 - Genetic Algorithm
a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
clear all
close all
clc
%defining our spaace
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% creating a 2D mesh
[xx yy] = meshgrid(x,y);
figure(12)
%Evaling the 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
surfc(xx,yy,-F)
xlabel('x value ')
ylabel('y value ')
shading interp
[inputs fval] =ga(@stalagmite,2);
hold on
%study1 ststical behaviour
num_cases=50
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1);
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',7,'MarkerFaceColor','r')
title('Unbounded inputs')
subplot(2,1,2 )
plot(fopt)
xlabel('iteration')
ylabel('function min.')
tic
% study 2 - statistical behaviour with upper and lower bounds
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i)= inputs (2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,fopt,'marker', 'o', 'markersize', 5, 'markerfacecolor', 'r')
title('bounded input')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
%study 3 increasing the ga iterations
tic
options = optimoptions('ga');
options = optimoptions (options,'populationSize',270);
for i=1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2, [],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1)
yopt(i) = inputs(2)
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt, yopt, fopt, 'marker', 'o', 'markersize', 5, 'markerfacecolor', 'r')
title('bounded inputs')
subplot (2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
the function of the pgm to execute the values
function[F]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
X1=[sin(5.1*pi*x+0.5)]^6;
Y1=[sin(5.1*pi*y+0.5)]^6;
X2=exp(-4*log(2)*(((x-0.0667)^2)/(0.64)));
Y2=exp(-4*log(2)*(((y-0.0667)^2)/(0.64)));
F=-(X1*Y1*X2*Y2);
end
We pgm outputs x and y the function call for it and it is formulated to find out F and returned to the pgm.

ga and stalagmite function used

the orange says max den green den blue hence the heights

the case 1 to find the statsical behaviour of ga with iteration 50 times

study time was done by tic toc opertor time take for this case was 3.1sec

case 2 statistical behaviour with upper and lower bounds

time taken was 3.7176sec

case 3 to find the optimum max nd min. with inc the iteration to 270 from 50

the study time taken was13.39 sec

error used in line 12 in the for loop ; instead of :
