Project

Global Maxima of Stalagmite function using Genetic Algorithm

Genetic Algorithm- A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

Phases in Genetic Algorithm-

  1. Initial Population
  2. Fitness Population
  3. Selection
  4. Crossover
  5. Mutation

Initial population -The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve. An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).

here initial population is taken as - 150, ranging from 0 to 0.5 iteration.

Fitness function -The fitness function determines how much fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.

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);

Selection-The idea of the selection phase is to select the fittest individuals and let them pass their genes to the next generation. Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.

Crossover-Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes. For example, consider the crossover point to be 3 as shown below.

Mutation-In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.

Result:- Global Maximum point - x= 0.06711, y = 0.06376;

Conclusion -

The initial study - The initial study shows the basic plot of x and y as per the Fitness Function concern. this study does not show the global maximum possible value of the fitness function but it shows the Local maximum value thus the statistical study of this Function is required.

Statistical Study--

Study 1:- The study parameters are- 50 iterations and 150 population size. the result of the maximum value of study 1 still not convincing that it is a global maximum because there is more than one highest peak in Function Vs Iteration curve.

Study 2:- The study parameters are - 50 iterations and 200 population size. the result of the maximum value of the study 2 shows that the algorithm identifies the group of maximum possible peaks but it is not considering the global max possible peak of the study.

Study 3:- The study parameters are - 50 iterations and 200 population size. The result of the maximum value of the study 3 shows that the algorithm identifies the global maximum peaks from the study.

%Fitness_function%
for 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).*(((y-0.0667).^2)/0.64));

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


%Initial_Study%
clear all
close all
clc
% defining search space
x = linspace(0,0.5,150);
y = linspace(0,0.5,150);
num_cases = 50;
% creating a 2dimensional mesh
[xx,yy] = meshgrid(x,y);
% evaluating the stalagmite function
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);  % stalagmite function; 
    end
end
surfc(f)
xlabel('x value')
ylabel('y value')
shading interp
[inputs, fval] = ga(@stalagmite,2);   % genetic algorithm function


%Statistical_study%
%Study 1: study the statistical behavior of GA with 50 iterations and population size - 150%

tic
%study 1 - Statistical behavior
for i = 1:num_cases
    [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study1_time = toc;
figure(1)
subplot(2,1,1)   %To make subplot inside the main plot;
hold on
surfc(xx,yy,f)
shading interp
% plot3 command plot x-value,y-value,foptimum-value that genetic function predict over 100 interation. 
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r') 
title('Unbounded inputs')
subplot(2,1,2) %this plots the optimum value over the iteration numbers
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
hold off

%Study 2: Shows the statistical behavior of GA when upper and lower bounds are defined with 50 iterations with population size - 200%

%study 2 - Statistical behavior
num_cases = 50;
options = gaoptimset('ga');
options = gaoptimset(options,'populationSize',200);
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('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
hold off

%Study 3: Shows the statistical behavior of GA when upper and lower bounds are defined with 50 iterations with population size - 250%

%study 3 - Statistical behavior -  increasing GA iterations
num_cases = 50;
options = gaoptimset('ga');
options = gaoptimset(options,'populationSize', 250);

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('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
hold off

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.