Week 4 - Genetic Algorithm
Genetic Alghorithm:
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.
clear all
close all
clc
%To input values
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases=50;
% Creating a 2D Mesh
[xx yy] = meshgrid(x,y);
% Evaluating Stalagmite Function
for i = 1 : length(xx);
for j = 1 : length(yy);
input_value(1)= xx(i,j);
input_value(2) = yy(i,j);
f(i,j) = stalagmite(input_value);
end
end
%unbounded statistical behaviour
tic
for i=1:num_cases;
[inputs1,fopt1(i)]=ga(@stalagmite,2);
xopt(i)=inputs1(1);
yopt(i)=inputs1(2);
end
study1_time=toc;
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x-value')
ylabel('y-value')
zlabel('function value')
shading interp
plot3(xopt,yopt,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Statistical Behvaiour')
subplot(2,1,2)
plot(-fopt1)
xlabel('iterations')
ylabel('function maximum')
%statastical behaviour with upper and lower bound
tic;
for i=1:num_cases;
[inputs2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs2(1);
yopt(i)=inputs2(2);
end
study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x-value')
ylabel('y-value')
zlabel('function value')
shading interp
plot3(xopt,yopt,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Statistical Behvaiour')
subplot(2,1,2)
plot(-fopt2)
xlabel('iterations')
ylabel('function maximum')
%increasing Ga population size
options=optimoptions('ga');
options=optimoptions(options,'populationsize',170);
tic
for i=1:num_cases;
[inputs3,fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs3(1);
yopt(i)=inputs3(2);
end
study3_time=toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x-value')
ylabel('y-value')
zlabel('function value')
shading interp
plot3(xopt,yopt,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Statistical Behvaiour with Increased Population Size')
subplot(2,1,2)
plot(-fopt3)
xlabel('iterations')
ylabel('function maximum')
Total_Time = study1_time+study2_time+study3_time
fprintf ('n The Results Are Below :')
fprintf ('nn Study 1 : Unbounded Statistical Behvaiour')
fprintf ('nn The Time taken for the study 1 is : %f',study1_time)
fprintf ('n The Co-ordinates of x and y for the Global Maxima of the Function are : %f,%f',inputs1(1),inputs1(2))
fprintf ('nn The Global Maxima of the Function is : %f',-(mean(fopt1)))
fprintf ('nn Study 2 : Bounded Statistical Behvaiour')
fprintf ('nn The Time taken for the study 2 is : %f',study2_time)
fprintf ('n The Co-ordinates of x and y for the Global Maxima of the Function are : %f,%f',inputs2(1),inputs2(2))
fprintf ('nn The Global Maxima of the Function is : %f',-(mean(fopt2)))
fprintf ('nn Study 3 : Bounded Statistical Behvaiour with Increased Population Size')
fprintf ('nn The Time taken for the study 3 is : %f',study3_time)
fprintf ('n The Co-ordinates of x and y for the Global Maxima of the Function are : %f,%f',inputs3(1),inputs3(2))
fprintf ('nn The Global Maxima of the Function is : %f',-(mean(fopt3)))
fprintf ('nn The Total Time taken to Complete the total Study = %f',Total_Time)