Project

Genetic Algorithm Using Stalagmite Function In Matlab

Genetic Algorithm Using Stalagmite Function In Matlab

Aim:- To obtain global maximum value of stalagmite function using genetic algorithm

Theory:-

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.

Five phases are considered in a genetic algorithm.

  1. Initial population
  2. Fitness function
  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.

Fitness Function

The fitness function determines how 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.

Selection

The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.

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.

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.

Stalagmite Function:-

Code:-

clear all
close all
clc
% Assigning Search engine values
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% Number of iterations
steps=50;
% Creating 2D mesh
[xx ,yy]=meshgrid(x,y);

% Using 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);
    end
end

% study 1
tic
for i=1:steps
    [inputs,fopt(i)]=ga(@stalagmite,2);         % Assigning values of stalagmite to two variables
    xopt(i)=inputs(1);                          
    yopt(i)=inputs(2);
end

study_time_1=toc;
figure(1)                                        % First Plot
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading interp;
ylabel('y_label')
xlabel('x_label')
plot3(xopt,yopt,fopt);
title('study_1');
subplot(2,1,2);
plot(fopt);
xlabel('Number of Steps');
ylabel('Maximum');

% Study 2 
tic
for j=1:steps
    [inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);     %Assigning values stalagmite function
    xopt(j)=inputs(1);
    yopt(j)=inputs(2);
end

study_time_2=toc

figure (2)                                                         % Second Plot
subplot(2,1,1);
hold on 
surfc(xx,yy,f);
shading interp;
ylabel('y_label');
xlabel('x_label');
plot3(xopt,yopt,fopt);
title('study_2');
subplot(2,1,2);
plot(fopt);
xlabel('Number of Steps');
ylabel('Maximum');

% Study 3 
options=optimoptions('ga');
options=optimoptions(options,'populationsize',450);
tic
for k=1:steps
    [inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);       % Assigning values from stalagmite function
    xopt(k)=inputs(1);
    yopt(k)=inputs(2);
end

study_time_3=toc

figure (3)                                               % Third Plot
subplot(2,1,1);
hold on 
surfc(xx,yy,f);
shading interp;
ylabel('y_label');
xlabel('x_label');
plot3(xopt,yopt,fopt);
title('study_3');
subplot(2,1,2);
plot(fopt);
xlabel('Number of Steps');
ylabel('Maximum');

maximum=fopt
figure(4)                                  % Fourth Plot
surfc(xx,yy,f)
xlabel('xlabel')
ylabel('ylabel')
shading interp
[inputs,fval]=ga(@stalagmite,2)

Stalagmite Function Code:-

function[f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);

% Formula of Stalagmite Function
term1=(sin((5.1*pi*x)+0.5))^6;
term2=(sin((5.1*pi*y)+0.5))^6;
term3=exp(-4*log(2)*((x-0.0667)^2)/0.64);
term4=exp(-4*log(2)*((y-0.0667)^2)/0.64);

f=-(term1*term2*term3*term4);
end

Code Explaination :-

x and y variables are assigned values

number of iterations are set to 50

Two dimensional mesh is created witth x and y variable

for loop is used and values of length of 2D x,y are assigned to input vectors of stalagmite function

Three studies are made with different values and three different plots are obtained to get the global maxima of stalagmite function.

stalagmite function seperate file is made to store input vector values.

Output:-

Plot 1

Plot 2

Plot 3

Plot 4

Global Maxima Value

Error Occured:-

Error Rectified By using optimoption function instead of stalagmite.

Conclusion:-

It can be concluded that golbal maxima of stalagmite function depends on number of iterations.

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.