Project

Genetic Algorithm

GENETIC ALGORITHM:

The genetic algorithm is an optimizing algorithm derived from Charles Darwin's natural evolution principle. Its process based on natural selection where the most fit elements are chosen at random and are set up for reproduction to produce offspring for the next generation.

Genetic algorithms are used to solve both constrained and unconstrained mathematical models which are often not easy to solve using classical algorithms. Some application where GA is used, Combustion chamber design optimization, Airfoil design optimization, and many other applications which are based on or between the global/local maxima and minima of the mathematical model. GAs have also mathematically clarified how birds fly and hence the development of ornithopters.

The GAs use three primary rules to create off springs from the current population.

  • Selection rules select the parents
  • Crossover rules define the reproduction process
  • Mutation rules determine the traits of the children coming from their parents.

Syntax for GA:

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.)

PROBLEM:

Need to calculate the global maxima of the function f(x,y) using genetic algorithm.

CODE:

MAIN CODE:

% Defining our search space
pop = 150; % Default population size
x = linspace(0,0.6,pop); % x elements
y = linspace(0,0.6,pop); % y elements
Z = zeros(pop); A = Z; % Initializing the Z and A matrix for speed 

% Creating a 2D mesh
[X,Y] = meshgrid(x,y); % Meshing the x and y points to create a 2D array points

% Evaluating the stalagimite function
for i = 1:length(X)
    for j = 1:length(Y)
        input_vector(1) = X(i,j); 
        input_vector(2) = Y(i,j);
        Z(i,j) = stalagmite(input_vector); % Calculaing the Stalagmite funtion and putting in z
        A(i,j) = -Z(i,j); % Negating the Z vector for plotting purposes
    end
end

% Stalagmite function plot
figure()
hold on
surfc(X,Y,A)
shading interp
title('STALAGMITE FUNCTION');
xlabel('x axis')
ylabel('y axis')
zlabel('z axis')



%% Study 1 - Stastical behaviours 50 iterations

num_cases = 50; % Number of iterations 

minima_val = zeros(1,num_cases); maxima_val = minima_val; xopt = maxima_val; yopt = xopt; % Initializing the minima_val, maxima_val, xopt and yopt variables for speed.

tic % Timer start
for i = (1:num_cases)
    [inputs,minima_val(i)] = ga(@stalagmite, 2); % Genetic Algorithm is run and the three outputs are assigned in variables
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    maxima_val(i) = -minima_val(i); % Maxima of the function is negative to the minimum
end
study1_time = toc; % Timer ends and run-time is calculated

% Study 1 GA solution plot
figure()
subplot(2,1,1)
hold on
surfc(X,Y,A) % Stalgamite function plot
shading interp
plot3(xopt,yopt,maxima_val,'marker','o','markersize',5,'markerfacecolor','r') % GA Solution points plot
title('GA STUDY 1 - 50 Iterations')
xlabel('X Axis')
ylabel('Y Axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(maxima_val) % Maximum value plot
xlabel('Iterations')
ylabel('Maximum Value')



%% Study 2 - 100 Iterations

num_cases = 100; % Number of iterations in GA

minima_val = zeros(1,num_cases); maxima_val = zeros(1,num_cases); xopt = maxima_val; yopt = xopt; % Initializing the minima_val, maxima_val, xopt and yopt variables for speed.

tic % Timer start
for i = (1:num_cases)
    [inputs,minima_val(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]); % Genetic Algorithm is run and the three outputs are assigned in variables
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    maxima_val(i) = -minima_val(i); % Maxima of the function is negative to the minimum
end
study2_time = toc; % Timer ends and run-time is calculated

% Study 2 GA solution plot
figure()
subplot(2,1,1)
hold on
surfc(X,Y,A) % Stalgamite function plot
shading interp
plot3(xopt,yopt,maxima_val,'marker','o','markersize',5,'markerfacecolor','r') % GA Solution points plot
title('GA STUDY 2 - 100 Iterations')
xlabel('X Axis')
ylabel('Y Axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(maxima_val) % Maximum value plot
xlabel('Iterations')
ylabel('Maximum Value')


%% Study 3 - 150 iterations and 450 population

num_cases = 150; % Number of iterations in GA

minima_val = zeros(1,num_cases); maxima_val = zeros(1,num_cases); xopt = maxima_val; yopt = xopt; % Initializing the minima_val, maxima_val, xopt and yopt variables for speed.

omps_in = optimoptions('ga');
omps = optimoptions(omps_in, 'PopulationSize',450); % Defining the population size

tic % Timer starts
for i = (1:num_cases)
    [inputs,minima_val(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],omps); % Genetic Algorithm is run and the three outputs are assigned in variables
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    maxima_val(i) = -minima_val(i); 
end
study3_time = toc; % TImer ends and run-time is calculated

% Study 2 GA solution plot
figure()
subplot(2,1,1)
hold on
surfc(X,Y,A) % Stalgamite function plot
shading interp
plot3(xopt,yopt,maxima_val,'marker','o','markersize',5,'markerfacecolor','r') % GA Solution points plot
title('GA STUDY 3 - 150 Iterations with 450 population')
xlabel('X Axis')
ylabel('Y Axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(maxima_val) % Maximum value plot
xlabel('Iterations')
ylabel('Maximum Value')

STALAGMITE FUNCTION CODE:

function [Z] = stalagmite(input_vector)
    X = input_vector(1);
    Y = input_vector(2);
    f1x = (sin((5.1*pi*X)+0.5))^6; % f1(x)
    f1y = (sin((5.1*pi*Y)+0.5))^6; % f1(y)
    f2x = exp(-4*log(2)*((X-0.0667)^2)/0.64); %f2(x)
    f2y = exp(-4*log(2)*((Y-0.0667)^2)/0.64); %f2(y)
    Z = -(f1x*f1y*f2x*f2y); % f1(x)*f2(x)*f1(y)*f2(y)
end

RESULTS:

1. Stalagmite function plot:

2. Genetic Algorithm Study 2: 50 Iterations with no population size

3. Genetic Algorithm Study 2: 100 Iterations with default population size (150)

4. Genetic Algorithm Study 3: 150 Iterations and 450 population

CODE EXPLANATION:

  1. The default population size is defined as pop.
  2. The elements x and y are defined with a size of 150 each.
  3. The matrix z and A are initialized with all zeros for speed.
  4. Now the stalagmite function is calculated for every value in the X Y mesh and the value is assigned to z matrix
  5. Study one is started, with fifty number of iterations.
  6. Now initializing the minima_val, maxima_val, xopt and yopt variables for speed.
  7. Now the GA algorithm is run, by default the GA calculates the global minima, so to calculate the maxima value is opposite to minima value.
  8. The time to run the code is calculated using tic and toc.
  9. Study 2 is initiated with 100 number of iterations, again the steps from 7 and 8 are repeated.
  10. Study 3 is initiated with 150 number of iterations and 450 size population. So in order to define the population, the optimotions is used to optimize the arrays.
  11. The value of the global maxima is put in maximum value variable.
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.