Project

HOW TO SOLVE GENETIC ALGORITHM USING MATLAB

AIM : To calculate the global maxima of a stalagamite function

THEORY :

In computer science and operations research, a genetic algorithm is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimisation and search problems by relying on biologically inspired operators such as mutation, crossover and selection.

STALAGAMITE FUNCTION USED :

`f(x,y)=f_1x*f_2x*f_1y*f_2y`

`f_1x=(sin(5.1pix+0.5))^6`

`f_1y=(sin(5.1piy+0.5))^6`

`f_2x=exp(-4ln2(x-0.0667)^2/0.64)`

`f_2y=exp(-4ln2(y-0.0667)^2/0.64)`

three types of study was performed int his project :

  1. study of genetic algorithm with unbounded inputs
  2. study of genetic algorithm with lower and upper bounds
  3. study of genetic algorithm with lower and upper bounds and increased population size.

FUNCTION CODE :

function [y]=stag(input_vector)

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

 
   
  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);
  
  %Output
  
  y= -(f1x*f2x*f1y*f2y);%the -ve sign is used to calculate global maxima 
                        %from local maxima
 
end

MAIN CODE :

the code is explained within the code with '%' symbol

clear all
close all
clc

%defining 1D arrays (search space)
%the linspace set the range of data between two numbers

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

%creating 2D array (meshgrid)
%based on the 1D array created above
%mesh grid is used to create an array of m*n matrix

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


%for each value of x and y we create an array called input_vector

%the 'for' loop command is used for loop operation and generated the data.the input_vector stores 
%the value of matrix xx and yy.the output is stored in f matrix after the function stag is evalauated


for i=1:length(xx)
    
    for j=1:length(yy)
        
        input_vector(1)=xx(i,j);%row vector(has 2 columns)
        
        input_vector(2)=yy(i,j);
        
        %stalagmite takes in the input vector and calculates f
        %f is height or z axis
        
        f(i,j) = stag(input_vector);
        
    end
end


figure(1)
%creates a 3D surface plot with a counter plot underneath
%-f is the height (z-axis) and the -ve sign is to reverse the figure
%the colour of the surface varies with the height specified by (f)

surfc(xx,yy,-f)

%label used to name the axis in the figure

xlabel('x-value')
ylabel('y-value')
title('stalagmite function')

%shading to get smoother graph

shading interp

%genetic algorithm for random sampling
%ga automatically finds only min value
%stag is the function hadel
%2 is the number of variables ( x and y)


%study 1 with unbounded inputs
% tic toc command is used to calculate the time of the running command

tic

for i=1:limit
    
    [inputs,fval(i)] = ga(@stag,2);

    xopt1(i)= inputs(1);%values are gettign stored in xopt1
    yopt1(i)= inputs(2);%values are gettign stored in yopt2

end

study1_time=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt1,yopt1,-fval,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function min')

tic

%study 2 statistical behaviour with upper and lower bounds
%x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
%A and b are liner inequality constrains
%Aeq and beq are the liner equality constrain for x and y
%[] is nonexistent constrain

for i=1:limit
    [inputs,fval(i)]=ga(@stag,2,[],[],[],[],[0;0],[.4;.4]);
    
xopt2(i)= inputs(1);
yopt2(i)= inputs(2);
end
study2_time=toc
figure (3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp

%it plots the figure in 3D where values of x y and f are shown with red
%color circle with size 5

plot3(xopt2,yopt2,-fval,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function min')

%in 3rd study in addition to specifying the bounds, we also change and 
%increase the initial population size
%genetic algorithm starts with random samples,so by default ga takes 50 as
%the number of samples
%we are going to increase the population size to 200

options=optimoptions('ga');
options=optimoptions(options,'populationsize',200);

%x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)

tic
for i=1:limit
    [inputs,fval(i)]=ga(@stag,2,[],[],[],[],[0;0],[.6;.6],[],[],options);
    xopt3(i)= inputs(1);
    yopt3(i)= inputs(2);
end
study3_time=toc
figure (4)
subplot(2,1,1)

surfc(xx,yy,-f)
shading interp

title('bounded inputs with increased population size')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt3,yopt3,-fval,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function min')

OUTPUT :

FIGURE (1)

3D surface generated for random sampling

FIGURE (2)

3D surface generated for unbounded inputs

FIGURE (3)

3D surface plot generated by GA with lower and upper bounds

FIGURE (4)

3D surface plot generated by GA with lower and upper bounds AND increased population size

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.