Project

Genetic algorithm study

The purpose of a genetic algorithm study is to find the global optimum value for a given population based on Darwin\'s theory of evolution. The hope is to achieve a population that allows the superior genes to pass on through to the next generation such that over successive generations, there is no increase in the superiority.

Genetic algorithm goes through many steps as shown below in the flow chart.

\"\"

An initial population is selected for which the algorithm is run. Various aspects are taken into consideration in order to achieve the best possible population at the end. During every generation, the average is taken and the population below the average is rejected. Only the population above average is furthered in the algorithm. This is run until there is no difference in the population over generations.

Code to run the genetic algorithm:

clear all
close all
clc

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

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

n=50;

%Study 1 - statistical model
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

tic
for i = 1:n
    
[inputs, fopt(i)] = ga(@stalagmite, 2);

xopt(i) = inputs(1);
yopt(i) = inputs(2);

end

figure(1)
study1_time = toc
subplot(2,1,1)
surfc(x,y,f);
shading interp
hold on
plot3(xopt, yopt, fopt, \'b\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Optimal value\')
ylabel(\'No. of iterations\')


%Study 2 - Upper and lower bounds
tic
for i = 1:n
    
[inputs2, fopt2(i)] = ga(@stalagmite, 2, [], [], [], [], [0;0], [1;1]);

xopt2(i) = inputs2(1);
yopt2(i) = inputs2(2);

end

study2_time = toc

figure(2)
subplot(2,1,1)
surfc(x,y,f);
shading interp
hold on
plot3(xopt2, yopt2, fopt2, \'b\')
subplot(2,1,2)
plot(fopt2)
xlabel(\'No. of iterations\')
ylabel(\'Optimum function\')


%Study 3 - Setting a population size

options = optimoptions(\'ga\', \'PopulationSize\', 2000);

tic
for i = 1:n
    
[inputs3, fopt3(i)] = ga(@stalagmite, 2, [], [], [], [], [0;0], [1;1], [], [], options);

xopt3(i) = inputs3(1);
yopt3(i) = inputs3(2);

end

study3_time = toc

figure(3)
subplot(2,1,1)
surfc(x,y,f);
shading interp
hold on
plot3(xopt3, yopt3, fopt3, \'b\')
subplot(2,1,2)
plot(fopt3)
xlabel(\'No. of iterations\')
ylabel(\'Optimum function\')

Code for stalagmite function:

function [out] = stalagmite(input_vector)

%Defining input conditions
x=input_vector(1);
y=input_vector(2);

%Solving the equations to obtain the values of the function
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)*(((y-0.0667)^2)/0.64));

F = f1x*f1y*f2x.*f2y;

%Output is set to negative in order to measure the global maxima instead of global minima %in the genetic algorithm
out = -F;

end

3 studies are conducted to study the conditions that provide the best possible results.

Study 1 is to study the basic statistical model.

\"\"

Study 1 provides us with results that are all over the place. The size of the population is small and no limits have been defined for the plot. From the plots we can see how the data is all over the place and the algorithm is defining the global maxima at many points. The simulation ran for about 4 seconds and the values changed each time the simulation was run for never converging. Hence, a second study was conducted.

The second study is run with upper and lower limits such that the results are found for a range of 0 to 1, with higher values suggesting that the population is superior.

\"\"

Study 2 shows how the algorithm shows the results in the upper quadrant of the stalagmite. This suggests that the population is going towards the superiority. However, it still is not at the best. For this reason, a third study is conducted. The simulation ran for about 6 seconds and the results were nearing convergence.

Study 3:

\"\"

Study 3 shows that the algorithm has converged to a value that is tending to 1. In this study a large pool of population is taken into consideration using the options function in the ga function. This algorithm has converged providing us with the best possible population barring any mutations. In study 3, the values of x and y converged to 0.0668 each and the simulation ran for 115s.

For the second and third studies, the funtion \'ga\' was studied. The syntax for \'ga\' is

ga(fun,nvars,Aineq,bineq,Aeq,beq,lb,ub,nonlcon,intcon,options).

fun refers to the function for which the algorithm is run for to find the fitness of the population.

nvars is the number of variables used, in this case, x and y, so 2 is input.

Aineq and bineq find local minimas subject to linear inequalities. We are not concerned with inequalities or equalities and give an empty array as input such that they are not taken into consideration.

lb and ub refer to the lower and upper limits for the input variables and the limits have been set as 0 to 1 for both the variables.

nonlcon and intcon are constraints for the input variables which we are not giving for this case. They have been input as null arrays as well so that we can give an input for options.

options is a function whereby we can define parameters for the algorithm. In this case, we define a population size of 2000.

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.