Week 4.1 - Genetic Algorithm
- Aim- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
Theory-.
Stalagmite- stalagmite function is a mathematical model that actually helps in generates local Maxima and minima.
Ga- genetic algorithm
Ga is a method of solving both constrained and unconstrained optimization based on a natural selection process that mimics biological evaluation.
Ga comes under the evolutionary algorithms works with working principle based on mechanics of natural
Program are as follows:-
To find global Maxima
clear all
close all
clc
% drfining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating a 2Dimensional mesh
[xx yy] = meshgrid(x,y);;
%Evaluting 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)
shading interp
[x,fval] = ga(@stalagmite, 2)
function for stalagmite-:
function [f] =stagalmite(input_vector)
x= input_vector(1);
y= input_vector(2);
f1 = (sin(5.1*pi*x+0.5)).^6;
f2=exp((-4*log(2)*((x-0.667).^2))/0.64);
f3 = (sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2))*((y-0.667).^2)/0.64);
f =-1*(f1*f2*f3*f4);
end
Program
To find global minima and plotting
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[x,y] = meshgrid(x,y);
num_cases = 50
f =get_stagalmite(x,y);
tic
%study 1- satistical behavior
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time=toc
%ploting1
figure(1)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt , yopt, fopt,'marker','o','markersize',5,'markerfacecolor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function minimum')
%study 2- satistical behavior
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[.6;.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time=toc
%ploting1
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 inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function minimum')
%study 3 incrasing ga iteraion
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170)
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
%ploting1
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 with modifified population size')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function minimum')
function to get_stalagmite:-
function f =get_stagalmite(x,y)
f1 = (sin(5.1*pi*x+0.5)).^6;
f2=exp((-4*log(2)*((x-0.667).^2))/0.64);
f3 = (sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2))*((y-0.667).^2)/0.64);
f =-(f1.*f2.*f3.*f4);
end




Error - getting error while creating a stalagmite function, error shown because file name and function name was different. Image is uploaded below