Challenge

Week 4.1 - Genetic Algorithm

Report on genetic algorithm

AIM:

  • To write a code in MATLAB for optimizing the stalagmite function and to find the global maxima of the function.
  • To explain the concept of genetic algorithm and also explain the syntax for ga in MATLAB.

GOVERNING EQUATIONS:

Some of the syntax used in this code are

Formula for creating a Stalagmite function.

inp = -fx1*fx2*fy1*fy2

fx1= ((sin((5.1*pi*x)+0.5))^6);

fy1= ((sin((5.1*pi*y)+0.5))^6);

fx2= exp((-4*log(2))*(((x-0.0667)^2)/0.64));

fy2= exp((-4*log(2))*(((y-0.0667)^2)/0.64));

GA syntax

ga(fun,nvars,A,B,Aeq,Beq,lb,ub,nonlcon,intcon,options)

Syntax for creating a surface plot

surfc(x,y,f)

The syntax for removing the mesh/shading from the surface plot

shading interp

REPORT:

This program was done using MATLAB language.

Supplementary code: (Stalagmite function)

function [inp] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
fx1= ((sin((5.1*pi*x)+0.5))^6);
fy1= ((sin((5.1*pi*y)+0.5))^6);
fx2= exp((-4*log(2))*(((x-0.0667)^2)/0.64));
fy2= exp((-4*log(2))*(((y-0.0667)^2)/0.64));
inp = -fx1*fx2*fy1*fy2;
end

Main code: (GA)

clear all
close all
clc
% A program for Genetic Algorithm
% Giving the inputs for x and y
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
%Creating a mesh for x and y
[xx,yy] = meshgrid(x,y);
% Calling the function and defining the called function values to f
for m = 1:150
    for n = 1:150
        f(m,n) = stalagmite ([xx(m,n),yy(m,n)]);
    end
end
% Giving the number of times it has to run
num_cases = 50;
tic
%Study 1 - Statistical behavior [inputs,fopt(i)] = ga(fun,nvars).
for i= 1:num_cases
    [inputs,fopt(i)] = ga(@ stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    
end
study1_time = toc;
%plotting
figure(1)
subplot(2,1,1)
hold on
surfc (x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','Markersize',5)
title('Unbound inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('function minimum')

tic
% Study2- Statistical behavior( with upper and lower bounds)
% [inputs,fopt(i)] = ga(fun,nvars,A,B,Aeq,Beq,lb,ub)
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)
% plotting
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',3)
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('function minimum')

% Study 3 increases GA iteration
% Giving population
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);
tic
% [inputs,fopt(i)] = ga(fun,nvars,A,B,Aeq,Beq,lb,ub,nonlcon,intcon,options)
% nonlcon-Nonlinear constraints
% intcon-Integer constraints
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(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',3)
title('GA iterations')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('function minimum')

ANSWERS TO THE FOLLOWING QUESTIONS:

  • What is GA?

GA known as a Genetic algorithm is an algorithm that helps in optimizing problems.

GA takes a function and gives the global maxima and minima for that function.

CONCLUSION:

Thus a code in MATLAB for optimizing the stalagmite function and to find the global maxima of the function is done.

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.