Week 5 - Genetic Algorithm
AIM:
To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
OBJECTIVE:
1)Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
2)Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
3)Make sure that your code gives the same output, even if it is made to run several times.
4)Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
STATISTICAL FUNCTION FILE(FORMULA):
function [f] = stalagmite(input_vector)
f1x = (sin((5.1*pi).*input_vector(1)+0.5)).^6;
f1y = (sin((5.1*pi).*input_vector(2)+0.5)).^6;
f2x = exp((((input_vector(1)-0.0667).^2)/0.64).*(-4*log(2)));
f2y = exp((((input_vector(2)-0.0667).^2)/0.64).*(-4*log(2)));
f1=f1x*f1y*f2x*f2y;
f=-f1;
end
CODE:
clear all
close all
clc
% Creating one dimensional
n=150;
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Creating two dimensional grid
[xx,yy] = meshgrid(x,y);
tic
% Nested for loop
% Case1(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
figure(1)
surfc(xx, yy, f)
shading interp
title('Stalagmite function')
xlabel('x value');
ylabel('y value');
study_time=toc;
% Case2(Unbounded inputs)
tic
for i = 1:n
[inputs,fopt(i)]= ga(@stalagmite, 2);
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')
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations');
ylabel('Function minimum');
study_time1=toc;
% Case3(Bounded inputs)
tic
for i = 1:n
[inputs,fopt(i)]= ga(@stalagmite, 2,[],[],[],[],[0;0],[0.6;0.6]);
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')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations');
ylabel('Function minimum');
study_time2=toc;
% Case4(Bounded inputs with modified population size)
tic
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',500);
for i = 1:n
[inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(4)
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 modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations');
ylabel('Function minimum');
study_time3=toc;