Challenge

Week 4.1 - Genetic Algorithm

Aim:

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

Objective:

  • 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.

explaination and code :

code for the stalagmite function :

as we have the mathematical function

To find global minima :

f = - (f1.f2.f3.f4)

To find global maxima :

f = (f1.f2.f3.f4)

where,

f1 =[sin(5.1*pi*x + 0.5)]^6

f2 =[sin(5.1*pi*y + 0.5)]^6

f3 = exp[-4log(2)*(x-0.0667)^2/0.64]

f4 =exp[-4log(2)*(y-0.0667)^2/0.64]

here x and y are two variable

hence the code for the function of stalagmite is as bellow

% function 
function [f] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);
f1 =(sin(5.1*pi.*x+0.5)).^6;
f2 =(sin(5.1*pi.*y+0.5)).^6;
f3 =exp(-4*log(2)*(x-0.0667)^2/0.64);
f4 = exp(-4*log(2)*(y-0.0667)^2/0.64);
f= (f1*f2*f3*f4);

end

here code for the function of global maxima is as bellow:

clear all
close all
clc

% defining the search space 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

% Creating 2D 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);
    end
end

surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')
shading interp
[inputs,fval] = ga(@stalagmite,2)

code for plotting global minima :

Here we have to study all three cases of global minima take two variable x and y tosolve the function.

num_cases = to run the ga number of time

call the stalgmite function

cases to study is

  • Unbounded condition
  • Bounded condition
  • bounded with modified population
clear all
close all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50
[x,y] = meshgrid(x,y)

% Evaluating the stalagmite function
for i = 1:length(x)
   for j = 1:length(y)
  input_vector(1)= x(i,j);
  input_vector(2)= y(i,j);
  f(i,j)=stalagmite(input_vector);
   end
end
study1_time = toc

%plotting1
figure(1)
subplot(2,1,1)
hold on

surfc(x,y,f)
xlabel('X axis')
ylabel('Y axis')
shading interp

tic
%study1 - unbounded condition 
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2)
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study1_time=toc;


figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('function minimum')
tic

%study2 - statical behaviour
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(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration');
ylabel('function minimum');

%study3 - Increasing GA Iterationa
options = optimoptions('ga')
options=optimoptions(options,'populationsize',300);

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
study_time=toc
figure(3)
subplot(2,1,1)
hold on 
surfc(x,y,-f)
 shading interp 
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration');
ylabel('function minimum');
     

code function for the stalagmite minimum:

here the code for the function of stalagmite which inbuilt function of matlab as per the formula put in the function, give the same name to the file as per function name

%function for stalagmit minimum
function[f] = get_stalagmite(x,y)


f1 =(sin(5.1*pi.*x+0.5)).^6;
f2 =(sin(5.1*pi.*y+0.5)).^6;
f3 =exp(-4*log(2)*(x-0.0667)^2/0.64);
f4 = exp(-4*log(2)*(y-0.0667)^2/0.64);

f=  -(f1.*f2.*f3.*f4);
end

Output :

Global maxima

For finding minimum function by ga :Study 1 output

Unbounded curve

Study 2 output

Bounded outpu

Error solving :

while functioning the code have to be with proper formula

Result :

the code for global maxima and stalagmite function is done .

The concept of genetic algorith and genetic syntax is done.

The graph 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.