Week 4.1 - Genetic Algorithm
Genetic Alogorithm:
It is a method which is used in matlab as a inbuilt command to find the optimal solutions of a function.
Generates a population of points at each iteration. The best point in the population approaches an optimal solution.
The genetic algorithm creates three types of children for the next generation:
-
Eliteare the individuals in the current generation with the best fitness values. These individuals automatically survive to the next generation.
-
Crossover are created by combining the vectors of a pair of parents.
-
Mutation children are created by introducing random changes, or mutations, to a single parent.
Syntax for ga:
[x,fval] = ga(@func,nvars)
[x,fval] = ga(fun,x0,A,b,Aeq,beq,lb,ub)
[x,fval] = ga(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
function[f]= stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x = (sin(5.1*pi*x+0.5)).^6;
f1_y = (sin(5.1*pi*y+0.5)).^6;
f2_x= exp(-4*log(2)*((x-0.0667).^2)/(0.64));
f2_y= exp(-4*log(2)*((y-0.0667).^2)/(0.64));
f = -(f1_x*f1_y*f2_x*f2_y);
end
% Code to optmise stalagmite function and find global maxima of a function
clear all
close all
clc
% Defining search space
x= linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx,yy]= meshgrid(x,y); % mesh grid to create x,y array
num_cases = 50; %no.of iterations that ga should perform
% Evaluating stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input(1)=xx(i,j);
input(2)=yy(i,j);
f(i,j) = stalagmite(input);
end
end
% study case 1- stastical behaviour
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2); % ga command is used to minimise a function by default
xopt(i)=inputs(1);
yopt(i)= inputs(2);
end
study1_time = toc % tic toc command is to measure time
figure(1)
subplot(2,1,1) % to create multiple plots in same figure window
hold on
surfc(xx,yy,-f) % -ve sign to find maximum of a function
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 case 2- using upper and lower bounds
tic
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(xx,yy,-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 case3- increasing ga iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',400);
tic
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(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markersize',5,'MarkerFaceColor','r')
title('bounded inputs with specified populatio size')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
Study 1

Study 2

study 3

Conclusion:
Using 'ga' the function is optmised into 3 study cases, each iteration produces the better values than its parent class, but the time required to study increases as the population size increases.
Thus the given stalagmite function is optmised and the global maxima is found by using genetic algorithm
