Challenge

Week 4.1 - Genetic Algorithm

Genetic Algorithm

Aim:

To create a code for genetic algorithm using Matlab to find its global maxima.

Code:

clear all 
close all
clc

%input
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_t = 50;

%forming matrix
[X,Y] = meshgrid(x,y);

%for loop for 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- study of ga function
%for loop for ga studdy1
for i = 1: num_t
    
    [inputs, fval(i)] = ga(@stalagmite, 2);
    xval(i) = inputs(1);
    yval(i) = inputs(2);
    
end

%plotting
figure(1)

%creating subplot
%subplot1
subplot(2, 1, 1)
title('Study1- Study of ga function')
hold on
surf(X,Y,f)
shading interp
plot3(xval, yval, fval,'o','markersize',4,'markerfacecolor','r')

%subplot2
subplot(2, 1, 2)
plot(fval)

%labeling
xlabel('iteration')
ylabel('f_ max')

%study2- study of ga using upper and lower bounds
%for loop for ga study2
for i = 1: num_t
    
    [inputs, fval(i)] = ga(@stalagmite, 2, [], [], [], [], [0;0], [0.7;0.7]);
    xval(i) = inputs(1);
    yval(i) = inputs(2);
    
end

%plotting
figure(2)

%creating subplot
%subplot1
subplot(2, 1, 1)
title ('study2- study of ga using upper and lower bounds')
hold on
surf(X,Y,f)
shading interp
plot3(xval, yval, fval,'o','markersize',4,'markerfacecolor','r')

%subplot2
subplot(2, 1, 2)
plot(fval)
%labeling
xlabel('iteration')
ylabel('f_ max')

%study3- study of ga using optimisation
options = optimoptions('ga');
options = optimoptions(options, 'PopulationSize',250);

for i = 1: num_t
    
    [inputs, fval(i)] = ga(@stalagmite, 2, [], [], [], [], [0;0], [0.7;0.7],[],[], options);
    xval(i) = inputs(1);
    yval(i) = inputs(2);
    
end

display(fval)
%plotting
figure(3)

%creating subplot
%subplot1
subplot(2, 1, 1)
title('study3- study of ga using optimisation')
hold on
surf(X,Y,f)
shading interp
plot3(xval, yval, fval,'o','markersize',4,'markerfacecolor','r')

%subplot2
subplot(2, 1, 2)
plot(fval)

%labeling
xlabel('iteration')
ylabel('f_ max')

Explanation:

  • The Stalagmite function is used and is solved by the genetic algorithm.
  • The Stalagmite function is used to get the third axis value for the given x and y values.
  • The stalagmite function equation `f(x,y) = f_1,x f _2,xf_1,yf_2,y`

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

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

`f_2x = exp [-4ln(2)((x-0.0667)^2//0.64)]`

`f_2y = exp [-4ln(2) ((y-0.0667)^2//0.64)]`

  • The above equation solved using the matlab code,
%Stalagmite function

function [f] = stalagmite(inp)

%input
 x = inp(1);
 y = inp(2);

%formula used
fy1 = (sin((5.1*pi*x)+0.5))^6;

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

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

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

%output
f =  -(fx1*fx2*fy1*fy2); 

end
  • The input x and y values are given through the main coding.
  • The function flows into the Stalagmite Equations and answers are found using respective x and y values.
  • The product of the above equations is found and stored in a variable 'f'.
  • The main code is used to solve the stalagmite function by genetic algorithm.
  • The input x and y values for the stalagmite function are given using the linspace command.
  • The meshgrid command is used to form the matrix for the obtained x and y values and stored in X and Y.
  • The For loop is used inorder to create a series of values for the given x and y.
  • The for loop syntax, for index variable = starting value : ending value

Statement 1;

end

  • The nested for loop is used to solve the stalagmite function.
  • The index variables are "i" and "j" for each loop respectively.
  • The starting values for both the loop is 1.
  • The Ending value for loop one is the total length of the X.
  • The ending value for loop two is the total length of the Y.
  • The input vector 1 and 2 is calculated using the loop.
  • The stalagmite function is called here using its function name.
  • The for is terminated using 'end'.
  • The series of for loops is used in order to find the characteristic of ga on the stalagmite function.

Genetic Algorithm

  • The genetic algorithm is a process in which the global maxima and minimum of a particular function are found using a random selection of values inside the given function.
  • The Genetic Algorithm is mainly used with the optimization function.

Study1 - Study of ga function of Stalagmite function

  • The study1 is done to see how ga works in a stalagmite function.
  • The For loop is used to get the value from the ga.
  • The new variable is assigned to limit the ga function and it is given as the end value for the loop(num_t =50).
  • The Function handle is used to call the stalagmite function.
  • The ga syntax for study1, x = ga(func, nvars)
  • The x is output, func is function and nvars is no variables required.
  • The solved values are stored in fval.
  • The input values 1 and 2 assigned to the variables xval and yval.
  • The plotting is done using the obtained value.
  • The figure1 option is used to create a separate plot for study1.
  • The subplot is used to show the two plots in the same figure, subplot(m,n,p)
  • The m is the number of rows, n is the number of columns, and p is a position to plot.
  • The subplot1 is used to create the surface and plotting the stalagmite function.
  • The surface is created for obtained stalagmite value and shade interp is used to smooth it.
  • The value obtained from the ga is also placed on the stalagmite function surface.
  • The marker command is used to denote the ga values on the surface separately.
  • The subplot2 is created in order to show the relation between iteration and fmaximum.
  • The label is done for the x-axis and y_axis as iteration and f_max.
  • Thus the study1 shows that ga is taking values randomly in the given function.
  • So, boundaries have to be given in order to get the accurate value.

Study2 - Study of ga function of stalagmite function using the upper and lower bounds

  • The study2 is done to see how ga works in a stalagmite function.
  • The For loop is used to get the value from the ga.
  • The new variable is assigned to limit the ga function and it is given as the end value for the loop(num_t =50).
  • The Function handle is used to call the stalagmite function.
  • The ga syntax for study2, x = ga(func, nvars, A, B, Aeq, Beq, lb, ub).
  • The x is output, func is function and nvars is a number of variables required.
  • The A and B are used when there is linear inequality.
  • The Aeq and Beq are used when there is linear equality.
  • The lb and ub are used to strict the ga function within certain values.
  • The solved values are stored in fval and inputs.
  • The input values 1 and 2 assigned to the variables xval and yval.
  • The plotting is done using the obtained value.
  • The figure2 option is used to create a separate plot for study2.
  • The subplot is used to show the two plots in the same figure, subplot(m,n,p).
  • The m is the number of rows, n is the number of columns, and p is a position to place respectively.
  • The subplot1 is used to create the surface for plotting the stalagmite function.
  • The surface is created for obtained stalagmite value and shade interp is used to smooth it.
  • The value obtained from the ga is also placed on the stalagmite function surface.
  • The marker command is used to denote the ga values on the surface separately.
  • The subplot2 is created in order to show the relation between iteration and fmaximum.
  • The label is done for the x-axis and y_axis as iteration and f_max.
  • Thus the study2 shows that ga is taking values randomly in function within the given boundary.
  • But still, now the values that we required are not clear.
  • So, optimization is used to solve the ga.

Optimization

  • Optimization is a process in which the condition is given to find the best value for the given function.

Study3 - Study of ga function of stalagmite function using optimization

  • The study3 is done to see how ga works in a stalagmite function.
  • The For loop is used to get the value from the ga.
  • The new variable is assigned to limit the ga function and it is given as the end value for the loop(num_t =50).
  • The Function handle is used to call the stalagmite function.
  • The ga syntax for study2, x = ga(func, nvars, A, B, Aeq, Beq, lb, ub,nonlcon,options).
  • The x is output, func is function and nvars is a number of variables required.
  • The A and B are used when there is linear inequality.
  • The Aeq and Beq are used when there is linear equality.
  • The lb and ub are used to strict the ga function within certain values.
  • The options is to limit the population that has taken by ga.
  • The optimoption('ga') the code says which function has to optimize.
  • Then the population has assigned as per requirement.
  • The solved values are stored in fval and inputs.
  • The input values 1 and 2 assigned to the variables xval and yval.
  • The plotting is done using the obtained value.
  • The figure3 option is used to create a separate plot for study3.
  • The subplot is used to show the two plots in the same figure, subplot(m,n,p).
  • The m is the number of rows, n is the number of columns, and p is a position to place respectively.
  • The subplot1 is used to create the surface for plotting the stalagmite function.
  • The surface is created for obtained stalagmite value and shade interp is used to smooth it.
  • The value obtained from the ga is also placed on the stalagmite function surface.
  • The marker command is used to denote the ga values on the surface separately.
  • The subplot2 is created in order to show the relation between iteration and fmaximum.
  • The label is done for the x-axis and y_axis as iteration and f_max.
  • At last, by using the optimization function the accurate value for global maxima using ga is found.

Conclusion

  • The Global value for the stalagmite function is calculated using Matlab.
  • The plots for the study are shown.
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.