Challenge

Week 4.1 - Genetic Algorithm

AIM :

To Study about the Genetic algorthim and to calculate the local maxima using stalagmite function using Gentic algorthim using MATLAB

TO FIND:

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
  • Make sure that your code gives the same output, even if it is made to run several times.
  • Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary

Genetic Algorithm:

Gentic algorthim is method which is used to solve the both constrained and un constrained optimization problems based on natural process of mimics biological evolution.It was worked on three basic rules

selection rules , cross over rules and mutation rules.

syntax of GA :

Syntax of GA in MATLAB used to find the minimum of function of genetic algorithm, there are several ways to find ga depending on problem is done. Different syntax are given below,

Stalgamite function :

function [f] = stalagmite_func(input_vector)

f1x = (sin((5.1*pi).*input_vector(1)+0.5)).^6
f1y = (sin((5.1*pi).*input_vector(2)+0.5)).^6

f2x = exp((((input_vector(1)-0.0667).^2)/0.64).*(-4*log(2)));
f2y = exp(((input_vector(2)-0.0667).^2)/0.64).*(-4*log(2)) ;

f1 = f1x*f1y*f2x*f2y;

f = -f1; 
end

GA of staglamite function:

clear all 
close all
clc

% definning our space 

x = linspace(0,0.6,200);
y = linspace(0,0.6,200);
n = 50;
% creating 2D mesh

[xx yy] = meshgrid(x,y)



% calculate input vectors 

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_func(input_vector);
    end
end
 
% surface (xx ,yy,f)

surfc(xx,yy,f)

shading interp

[ inputs , fval] = ga(@stalagmite_func,2)
 

%study for global maxima

tic
% case 1  statistical behavouir

for k = 1:n

[inputs1 , fopt1(k)]= ga(@stalagmite_func,2);
xopt1(k) = inputs1(1);
yopt1(k) = inputs1(2);
end
study1_time = toc
figure(1) 
subplot(2,1,1)
hold on
surfc(xx, yy, -f)
shading interp

plot3(xopt1,yopt1,-fopt1,'marker','o','markerfacecolor','r')
xlabel('x values')
ylabel('Y values')
title('statistical behaviour')


figure(2)
plot(-fopt1)
xlabel('iterations')
ylabel('fopt')
title('statistical behaviour')




% case 2  statistical behavouir with upper and lower bound

for k = 1:n

[inputs2 , fopt2(k)]= ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
xopt2(k) = inputs2(1);
yopt2(k) = inputs2(2);
end

figure(3) 
subplot(2,1,1)
hold on
surfc(xx, yy, -f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markerfacecolor','r')
xlabel('x values')
ylabel('Y values')
title('statistical behaviour  with upper and lower bound')


figure(4)
plot(-fopt2)
xlabel('iterations')
ylabel('fopt')
title('statistical behaviour with upper and lower bound')


%tic
% case 3  statistical behavouir with increasing GA iterations

options = optimoptions('ga')
options = optimoptions(options,'populationsize',300)

for k = 1:n

[inputs3 , fopt3(k)]= ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt3(k) = inputs3(1);
yopt3(k) = inputs3(2);
end
figure(4) 
subplot(2,1,1)
hold on
surfc(xx, yy, -f)
shading interp
plot3(xopt1,yopt1,-fopt3,'marker','o','markerfacecolor','r')
xlabel('x values')
ylabel('Y values')
title('statistical behaviour with increasing GA iterations')


figure(6)
plot(-fopt3)
xlabel('iterations')
ylabel('fopt')
title('statistical behaviour  with increasing GA iterations')

Step wise explanation :

Each and every program starts with clear all , close all and clc command,

  • Now define the function , stalgamite function whic is used to find the minimum value of the function, so we here we use negative sign reverse case , that is maxima
  • % definning our space

    x = linspace(0,0.6,150)
    y = linspace(0,0.6,150)

  • n=50
  • Here we use linspace command define the search space
  • n = 50 here we restrict the iterations
  • and the used 2D [xx yy] = meshgrid(x,y)
  • evulating the function we used to define, input vector for loop.
  • And then we search 3 rules of genetic algorithm
  • statistical behavouir
  • statistical behavouir with bounded values
  • statistical behavouir with maximum iterations

PLOTS :

Statistical behaviour

Statistical Behavouir with upper and lower bound

Statistical behaviour with maximum iterations

Result :

Thus the local maxima using stalagmite function using Gentic algorthim using MATLAB is solved

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.