Challenge

Week 4.1 - Genetic Algorithm

Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function

% stalagmte function
function [f]=stalagmite_fun(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)));

%we are using the -ve sign so that the plot will be reversed as we have to find the maxima of the given function using ga  
f=-f1x*f1y*f2x*f2y;
end

Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report

clear all
close all
clc

% defining our search dpace
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

%num_cases is set which will be used as a counter for iteration to 50
num_cases=50;

% creating 2Dimensional mesh
[xx,yy]=meshgrid(x,y);

% evaluating 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_fun(input_vector);
    end
end

%study1-Unbounded statistical behaviour
% tic command is used to start the timer
tic
%‘for loop’ bounded by the number of iterations is formed
for i=1:num_cases;
    [inputs1,fopt1(i)]=ga(@stalagmite_fun,2);
    xopt(i)=inputs1(1);
    yopt(i)=inputs1(2);
end
%toc’ command is used to stop the timer and the output is stored in study1_time
study1_time=toc;

figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt1,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('unbounded statistical behaviour')
subplot(2,1,2)
plot(-fopt1)
xlabel('iteratiom')
ylabel('function maximum')

%study2-bounded statistical behaviour
% tic command is used to start the timer
tic
%‘for loop’ bounded by the number of iterations is formed
for i=1:num_cases;
    [inputs2,fopt2(i)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[1;1]);
    xopt(i)=inputs2(1);
    yopt(i)=inputs2(2);
end
%toc’ command is used to stop the timer and the output is stored in study1_time
study2_time=toc;

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt2,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded statistical behaviour')
subplot(2,1,2)
plot(-fopt2)
xlabel('iterations')
ylabel('function maximum')

%study3-increasing ga iteration i.e. population size
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',170);
% tic command is used to start the timer
tic
%‘for loop’ bounded by the number of iterations is formed
for i=1:num_cases;
    [inputs3,fopt3(i)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i)=inputs3(1);
    yopt(i)=inputs3(2);
end
%toc’ command is used to stop the timer and the output is stored in study1_time
study3_time=toc;

figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt3,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded statistical behaviour with modified population size')
subplot(2,1,2)
plot(-fopt3)
xlabel('iterations')
ylabel('function maximum')

Total_time=study1_time+study2_time+study3_time
fprintf('n the results are below :')

fprintf('nn study1 : unbounded statistical behaviour')
fprintf('nn the time taken for the study1 is : %f',study1_time)
fprintf('n coordinate of x & y for the global maxima of the function are : %f,%f',inputs1(1),inputs1(2))
fprintf('nn the global maxima of the function is : %f',-(mean(fopt1)))

fprintf('nn study2 : bounded statistical behaviour')
fprintf('nn the time taken for the study2 is : %f',study2_time)
fprintf('n coordinate of x & y for the global maxima of the function are : %f,%f',inputs2(1),inputs2(2))
fprintf('nn the global maxima of the function is : %f',-(mean(fopt2)))

fprintf('nn study3 : bounded statistical behaviour')
fprintf('nn the time taken for the study3 is : %f',study3_time)
fprintf('n coordinate of x & y for the global maxima of the function are : %f,%f',inputs3(1),inputs3(2))
fprintf('nn the global maxima of the function is : %f',-(mean(fopt3)))

Code Explaination

Step1-Start code with clear all, close all & clc

Step2- First, Linspace command is used to define x & y with 150 elements b/w 0 & 0.6 including the bounds.

Stpe3- Then, num_cases is set which will be used as a counter for iteration to 50

Step4- Then, 2D grid is made using meshgrid command is created from x & y to get xx & yy output

Step5- Then ‘for loop’ is used to solve the genetic algorithm for every element in the 2D meshgrid

Step6- Then length of the row & the column which are equal in length are used as counters

Step7- Then the input vector is declared as ga required input vector which is each value in meshgrid

Step8- Then output of stalagmite function is stored in matrix

Step9- Now tic command is used to start the timer

Step10- Then ‘for loop’ bounded by the number of iterations is formed

Step11- Then ga code is entered for unbounded statistical studies which have the following format

[inputs, fopt]=ga(fun, nvars)

‘Inputs’ represent the value of parameters x & y for which the function has the least value ‘fopt’

‘fun’ is the function to be optimized in this case stalagmite

‘nvars’ is the no. of design variables, in this case only x & y are the variables to be plot

Step12- The minimum value of the function for each iteration is output & its x & y coordinates which are stored in separate variables.

Step13- Then ‘toc’ command is used to stop the timer and the output is stored in study1_time

Step14- Now figure1 is plotted by following steps

Step15- The first subplot command is used to partition the part window in order to draw more than one plot in the same window, two rows & one column are specified & then the position of each plot to be selected

Step16- Then ‘hold on’ command is used to plot for being removed

Step17- The ‘surfc’ command is used to plot contour with xx & yy value from meshgrid as input and negative optimum value is used because we need to maximize the function

Step18- Then shading interp is used to smoothen the function plot 7 to remove the grid line

Step19- Then plot 3 command is used to plot a point in 3D, here it will plot-fopt because we need the maximum value of the function not the minimum

Step20- Now the grid title is selected

Step21- Then fopt value of the function is plotted on the second plot in the second row

Step22- Now its x & y axis are labelled

Step23-Then the same study is made but this time bounded with lower & upper limit which requires the same commands & step accepts the ga command which have the following format

[inputs, fopt]=ga(fun, nvars, A, b, Aeq, beq, lb, ub)

Inputs, fopt, fun & nvars are the same as study-1

Step24- The range of the genetic algorithm is bounded by introducing values for lower & upper bounds & hence the values of global maxima & the values of x & y will be within the defined search space in inputs

Step25- Then increasing population size study was done which is the same as before but is had different ga code i.e. [input, fopt]=ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, IntCon, options)

Step26- The range of the genetic algorithm is bounded as in study 2 but to improve the accuracy, the commands ‘optimoptions’ is introduced to increase the population size

Step27- Then total time is computed

Step28- Then using fpritf command the results are printed in the command windows

Step29- Then the study time is printed

Step30- then x, y coordinate of the maximum optimum values are printed

Step31- then the maximum optimum values of the function is printed which is calculated from the negative mean of the all iterations of the output value and the negative sign is used to get the maximum value

Step32- Then the study time of the whole 3 studies combined is printed.

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.