Challenge

Week 4.1 - Genetic Algorithm

Aim : Write a code in MATLAB to optimize the stalagmite function and to find the Global Maxima of the function.

Introduction : Genatic Algoritham :- Genatic Algoritham is Based up on the evalution theory which was praposed by Charles Darwin. It is a search-based optimization technique based on the principles of Genetics and Natural Selection.
It will repetedly used to find the nearest optamal solutions of most difficult problems . if not it may take lifetime to solve that problem. It is Frequently used to solve problems in Research & in machine learning.

Theory :

Optimazation : Optimisation is a decision making process,It helps to find the best results.
Under this optimisation process it is porocedure to compair various solution for to get an optimum or a satisfactory solution is found.
These include Maximizing factors such as Productivity, Streangth, Reliability, Efficiency etc..
The optimization helpes to find the best value within the limits.

The following is the procedure followed by any optimisation process.

optimisation process.

Working of Genatic Algoritham : The follwing flow chat describes how the genetic algoritham works to find the optimal solutin.

Genatic Algoritham optimisation starts with populatin initialization,


Initialization: let us take an example of a simple mathematical function with two genes (x&y) as shown below

f(x,y) = 5x + 2y - 10

In this initialization ,first we need to deside the population size.
Higher the population size ,we get the more combinations,
Hence we get more accurate reasults.

Population is the number of sample data points
(chromsomes) taken to perform the genetic algoritham operation.

Selection: In this step, The parents are selected frm the sample data points,
The main Objective of the selection process is to select the high fit individuals.

crose-over :It is the process of taking two parents and producing the childs from them by interchanging the geans.

Mutation : Mutation is carried out t maintain the genetic diversity from one generation of a population of chromosomes
to the next.

Fitness Functin :
It is a function which measures how good your solution is.
Fitness function takes solutin as input and produces the sutability of the solution as the output.
This evaluate how close the solution is the optimal solution of the desired problem.

Objective:

  • To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • Explanation of the concept of genetic algorithm, & Explanation of the syntax for GA.
  • Same output even if it runs several times.
  • Ploting graphs for all 3 studies and for F maximum vs no. of iterations.

MATLAB PROGRAM :

Matlab code to optimize Stalagmite Function & find the Global Maxima of the function

FUNCTION CODE:

Explanation:

By using the above equation the function code was writen.

function [f] =  stalagmite(input_vector);

x = input_vector(1);
y = input_vector(2); 

term1 = 5.1*pi*x;
term2 = 5.1*pi*y;
term3 = (-4)*log(2);
term4 = ((x-0.0667)^2/0.64);
term5 = ((y-0.0667)^2/0.64);

f1_x = (sin(term1 + 0.5)).^6;
f2_x = exp(term3*term4);

f1_y = (sin(term2 + 0.5)).^6;
f2_y = exp(term3*term5);

f = -(f1_x*f2_x*f1_y*f2_y);
end

Main Code :

%Genetic Algorithm
clear all
close all
clc

%Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%To run the Genatic Algoritham 50 times
num_cases = 50 

%Creating 2Dimenctional 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);
        input_vector=[input_vector(1),input_vector(2)];
        f(i,j) = stalagmite(input_vector); % calling the function
    end
end

tic                        % tic tock cmmand for calculating the iteration time
%Study 1 - satistical behavior in unbonded condition
for i = 1:num_cases
        [input,f_opt(i)] = ga(@stalagmite,2);
        xopt(i) = input(1);
        yopt(i) = input(2); 
end
study1_time = toc


figure(1)                  %To Plotong the Graph-1
subplot(2,1,1)             %Using the subplot to make multipul plots
hold on                    %To hold the plots
surfc(x, y, -f)            %TO visualize output of study-1
shading interp             %To remove grid lines and make smoothe shade
plot3(xopt,yopt,-f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')  %To give the title for the plot
subplot(2,1,2)             %Subplot is used to make multipul plots
plot(-f_opt)               %(-)Negitive signe is used to get GA maxima 
xlabel('Iterations')       %To give the lable on x-axis
ylabel('Function minimum') %To give the lable on y-axis

%Study 2 - satistical behavior - with upper & lower bounds

tic
for i = 1:num_cases
        [input,f_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1:1]);
        xopt(i) = input(1);
        yopt(i) = input(2); 
end
study2_time = toc

%To Plotong the Graph-2
figure(2)
subplot(2,1,1)
hold on
surfc(x, y, -f)
shading interp
plot3(xopt,yopt,-f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-f_opt)
xlabel('Iterations')
ylabel('Function minimum')


%Study 3 -increasing gi iterations
options = optimoptions('ga'); % calculating the optimum solution for 'Ga' & saved in veriable called opction
options = optimoptions(options,'populationsize',250); % to increase the size of population

tic
for i = 1:num_cases
   [input,f_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1:1],[],[],options);
        xopt(i) = input(1);
        yopt(i) = input(2); 
end
study3_time = toc

%To Plotong the Graph 3
figure(3)
subplot(2,1,1)
hold on 
surfc(x, y, -f) 
shading interp
plot3(xopt,yopt,-f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2) 
plot(-f_opt)
xlabel('Iterations') 
ylabel('Function minimum')

step wise explanation of the code


step 1) Stating with clear all to clear the work space ,close all to close the graphs, 'clc' to errise the command window

step 2) To define search space with 2 variables x & y for to create 1 dimenctional array for x, y using linspace command and to provide 15 values in bitween 0 to 0.6.
After that to create 2 dimenctional array with respect to 1 dimenctional array

step 3) To calculate GA maxima write the supperate function by using the formula and named the function as stalagmite
by a variable called f

step 4) Evaluating the stalagmite function by using forloop hence genatic algoritham is actually based on random sample,it will starts with random samples of inputs we evaluate the function

To Get a better reasult and comparing the reasult with 3 studies doing small changes to get better reasult

step 5) Study-1 statistical behaviour with unbound conditin after creating x,y variable as it does not defines the surch space for GA. GA does'nt know its work space but the values for x,y goes below or above of values 0.6 to 0
for that use num _case=50 to run GA in 50 times.

By using forloop and run the code from 1- 50 times after that i call ga & use the new functin which by creating stalagmite, pass 2 variable as input and store the output in ga_opt.
And then store the value f x & y in 'xopt & yopt' Also using tic & toc cmmand to know how much time takes to complite study

step 6) To visualize output of study -1 use surface command (surfc), to remove grid lines and make smoothe shade use command (shade interp)
And to start new figure window (figure(1)) using subplot command to make multipul plots

1st plot to show stalagmite function and ploting the minimum value that GA trying to predict over 50 studies
2nd plot for showing the number of iteration at x_axis and during each iteration value changes which represents as function maxima on y-axis.

step 7) Study-2 statistical behaviour with bounded condition as the veriable x,y as it going above & below the 0 & 0.6.
by using Sintex GA give x&y the upper limit and lower limit to 0 to 0.6 ,by applying the bounddary condition so make it more clear and optimum.

step 8) Study-3 statistical behaviour with bounded condition & increasing GA iterations initial population size
If populatin size is too small we con't find the global maxima but we cen find local maxima which is shown in study-2
Hence in study-3 is simular to the process study-2 but population is increased to 250 and applying the constraint 'optimoptions' and saved its values in variable 'option',
To allow the GA to predict the optimum solution from 240 population.

The following Error that i faced wile programing:

ERROR-1. The following error that i faced wile runing the entire program

The errr that i face becouse i wrote clear all, close all, clc. above the function on the function program called stalagmite

Later i removed clear all close all & clc in the function code so that i got the reasult

Reasult: Hence optimisation of stalagmite function and the global maxima of the function have been shown,

Expalination of the concept of genetic algorithm, Ploting graphs for all 3 studies with different iterations was shown

figure 1 :

  • The value that 'GA' predicts, which is all over the plave on 3d graph it happens due to random sample during each iteration the vaue changes.
  • And the time taken to study -1 is (study1_time =3.4583)

figure 2 :

  • Here from the graph it is observed that the function predict minimum points is between 3peaks it is much clear than styudy-1.
  • The g raph y-axis minimum value lies between 0.4 &0.6 it is much accurate then study 1 but still not clear.
  • As we can observe the time taken for iteration is (study_2_time=3.3824.

figure 3 :

  • Here from subplot-1,it shows maxima only lies around 1 peak
  • Maxima lies at around the value 0.85 that GA predicts
  • in this study it gives the optimum value on increasing the number of iteration and applying constraints
  • the time taken to complitestyudy-3 is (study_3_time = 9.9820) which is higher then the study-1

Refarence:

https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_quick_guide.htm
https://skill-lync.com/knowledgebase/genetic-algorithm-explained
https://en.wikipedia.org/wiki/Genetic_algorithm

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.