Project

Genetic algorithm

Aim:

To analyse the local maximum of the genetic algorithm with different case studies using MATLAB.

Function:

The basic mathematical function which is used to find the loacl maximum is shown below,

Genetic algorithm:

Genetic algorithm is used to create a high quality solution for optimizing the various parameters such as mutation, crossover and selection. This genetic algorithm was introduced by john holland based on the charle darwin's theory of evolution. The theory of natural selection explains the survival and reproduction of particular organism based on the phenotype. In this project, we are going to select two different organisms and made a crossover of it and also find the local maximum of it. The "ga" is an inbuild function in the MATLAB. By initializing the ga in different format we can able to optimise it.

x = ga(fun,nvars) - This format is used to find the unconstrained minimum "x", in the function "fun".

[x,fval] = ga(___) - This is used to find the value of the fitness of the function.

[xopt,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],lb,ub, ConstraintFunction) - This is used for the optimization of different bounds.

Procedure:

  • Initially, two values for x and y is assigned.
  • The number of iteration is initialised and by using the meshgrid command the 2D array is formed.
  • By using the mathematical function, the x, y and f values are obtained till the end of the iteration.
  • The 3D plot is drawn by using the x, y and f values.
  • The local maximum values are obtained by "[x_output , fval] = ga(@stalagmite,2)".

Study 1 - To analyse the Statistical Behaviour

  • Using the function calling, the "fstd" is obtained for the assigned input.
  • Similarly, the maximum values in each axis are obtained.
  • The "tic toc" command is used to find the time taken by the study for the given number of iteration.
  • The 3D plot of x,y and f values are demonstrated and the trace path of f values is also plotted on the same figure using "subplot" command.
  • In this study, the unbounded inputs are provided.

Study 2 - To analyse the statistical Behaviour With upper and Lower Bounds

  • [inputs , fstd(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]) command is used to solve the statistical behaviour with various bounds values.
  • Various values such as fstd(i), xstd2, ystd3 were found.
  • The various plots are drawn on the same figure by using the subplot command

Study 3 - To analyse function based on increasing ga iterations

  • "Optimoptions" command is used to return the named parameters into specified values.
  • It was done mainly for analysing the function based on increasing ga iteration.
  • The option values thus obtained is subtituted in ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options) command for the optimization.
  • Then the various values are plotted.
clear all 
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
no_of_iteration = 50

[xx yy] = meshgrid(x,y);

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
% The f values are in negative sign
% - sign is included to show the perfect view of the surface plot
surfc(x,y,-f)
shading interp
xlabel('x values')
ylabel('y values')
zlabel('f values')

%maximum values
[x_output , fval] = ga(@stalagmite,2)


%Study 1 To analyse the Statistical Behaviour
tic
for i = 1:no_of_iteration
    [inputs , fstd(i)] = ga(@stalagmite,2);
    xstd1(i) = inputs(1);
    ystd1(i) = inputs(2);
end
%Time taken for the iterations
Study1_Timing = toc
figure
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xstd1,ystd1,-fstd,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded input')
subplot(2,1,2)
plot(-fstd)
xlabel('iterations')
ylabel('function maximum')


%Study 2 - To analyse the Statistical Behaviour With upper and Lower Bounds
tic
for i = 1:no_of_iteration
    [inputs , fstd(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xstd2(i) = inputs(1);
    ystd2(i) = inputs(2);
end
Study2_Timing = toc
figure
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xstd2,ystd2,-fstd,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded input')
subplot(2,1,2)
plot(-fstd)
xlabel('iterations')
ylabel('function maximum')


%Study 3 - To analyse function based on increasing ga iterations
options = optimoptions('ga')
options = optimoptions(options , 'populationsize', 200)
tic
for i = 1:no_of_iteration
    [inputs , fstd(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xstd3(i) = inputs(1);
    ystd3(i) = inputs(2);
end
Study3_Timing = toc
figure
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xstd3,ystd3,-fstd,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded input')
subplot(2,1,2)
plot(-fstd)
xlabel('iterations')
ylabel('function maximum')

Calling function:

In the calling function, the mathematical equations or functions to find the local maximum value is solved.

function [f] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);

f1x = (sin(5.1*pi*x + 0.5))^6;
f1y = (sin(5.1*pi*y + 0.5))^6;
f2x = exp(-4*log(2)*(x - 0.0667)^2/0.64);
f2y = exp(-4*log(2)*(x - 0.0667)^2/0.64);

f = -(f1x*f1y*f2x*f2y);
end

Output:

The result plot of the study 1 is shown below,

The result plot of study 2 is shown below,

The result plot of the study 3 is shown below,

From the various plots obtained and the time taken by the study explains the various results. The third plot shows the exact trace of the funtion value to represent the local maximum values.

Conclusion:

Thus the genetic algorithm was created in MATLAB.

Reference:

  1. https://in.mathworks.com/support/search.html/answers/257419-using-genetic-algorithm-with-bounds-and-non-linear-constraints.html?q=&fq=asset_type_name:answer%20category:gads/genetic-algorithm&page=1
  2. https://en.wikipedia.org/wiki/Genetic_algorithm
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.