Challenge

Week 4.1 - Genetic Algorithm

AIM:
 To Calculate the global maxima of a stalagmite function and to explain the syntax for Genetic Algorithm (GA) in MATLAB.
OBJECTIVE:
•	To write a code in MATLAB to optimise the stalagmite function and the find the global maxima of the function.
•	To explain the concept of genetic algorithm and the syntax for GA in MATLAB.
•	To plot graphs for all three studies and for F maximum vs Number of iterations.
THEORY:
GENETIC ALGORITHM
      Genetic Algorithm is basically search algorithm . it was developed by prof. John holland at the university of Michigan around 1975 . The process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
     The genetic Algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution . The genetic algorithm repeatedly modifies a population of individuals at random from the next generation.
    The genetic Algorithm  uses three main types of rules at each step to create the next generation from current population. They are follow as,
•	Selection rules select the individuals , called parents ,that contribute to the population at the next generation.
•	Cross over rules combine two parents to form children for the next generation.
•	Mutation rules apply random changes to individual parents to form children.
OPTIMISATION:
     Optimisation is basically an area of study that deals with minimise or maximise the particular function.
    An stalagmite function is an user defined function , the mathematical expression are given below.
GLOBAL MAXIMA
  The absolute maximum value of the function (the largest overall value of a set,ect.) Over its entire range.
GLOBAL MINIMA
  The absolute minimum value of the function (the smallest overall value of a set,ect.) Over its entire range.
SYNTAX OF GENETIC ALGORITHM (GA)
GA (Genetic Algorithm) syntax is used to find  minimum of function. The various Syntax are described below,

X = ga(fun,nvars)
     Fun – function name
    Nvars – number of Variables
X = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
Fun – function name
Nvars – number of Variables
A – Linear inequality constraints as real matrix
B – Linear inequality constraints as real vector
Aeq – Linear equality constraints as real matrix
Beq – Linear equality constraints as real vector
Lb – Lower Boundary
Ub – Upper boundary
X = ga(fun,nvars A,b,Aeq ,beq,lb,ub,nonicon,options)
Fun – function name
Nvars – number of Variables
A – Linear inequality constraints as real matrix
B – Linear inequality constraints as real vector
Aeq – Linear equality constraints as real matrix
Beq – Linear equality constraints as real vector
Lb – Lower Boundary
Ub – Upper boundary
NonIcon – Non linear constraints 
Options – creating optimisation options using ‘optimoptions’
EXPLANATION OF PROGRAM:
Main program
•	To assume the input values of X and Y by using the linspace command and the split variables are also initiate in that command.
•	With the input values x,y we created the 2D array using MESHGRID.
•	To get function of stalagmite which are created separately in the other tab by calling the stalagmite.
•	For loop is inserted for find the values of x and y .And values of 2D form of x and y to stalagmite.
•	The surface plot is plotted with x,yand z axis.
ANSWERS FOR QUESTIONS ASKED IN THE CHALLENGES
•	GA syntax are varied for three studies and they explained in GA syntax algorithm.
•	Num_cases is assigned as 40, that will run the input values as 50 times ,so that GA can get minima value correctly.
•	In the study 3 options = ‘optimoptions(ga)’
                                       Options = optimoptions(options,’populationsize’,170)
            Where populationsize are taken as 170 ( we taken the value of population is 170) whether the                  size are above from 50 the global maxima will obtain and its below from 50 local maxima 
•	To obtain proper view (I,e non inverted) we given negative value to ‘f’ at the time of plot.
•	Graphs are plotted for all 3 studies and subplots are also used for ‘F’ maximum vs no of iterations.
INPUT PROGRAM:
%programs for stalagmite_genetic _algorithm
clear all
close all
clc

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


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

% Evaluating the stalagmite Function
for i = 1:length(x)
    for j = 1:length(y)
        input_vector(1) =xx(i,j);
        input_vector(2) =yy(i,j);
         f(i,j) =  stalagmite(input_vector);
       
    end
end

% Plotting
surf(xx,yy,-f)
%shading interp
[inputs,fval] = ga(@stalagmite,2);



% Main_program_of_stalagmite_genetic_algorithm
clear all
close all
clc

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

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

% Evaluating the stalagmite Function
for i = 1:length(x)
    for j = 1:length(y)
        input_vector(1) =xx(i,j);
        input_vector(2) =yy(i,j);
         f(i,j) =  stalagmite(input_vector);
       
    end
end


tic
% Study 1 - statistical Behaviour 
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)
 
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'o','MarkerFaceColor','r');
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

tic
%study 2 - Statistical behaviour with upper and lower bounds

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study2_time = toc;

figure (2)
subplot(2,1,1)
hold on 
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'o','MarkerFaceColor','r');
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

% study 3 - Increasing GA iterations

options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);

tic
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);

     xopt(i) = inputs(1);
     yopt(i) = inputs(2);
end
study3_time = toc;

figure (3)
subplot(2,1,1)
hold on 
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'o','MarkerFaceColor','r');
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')



% Function_program_of_stalagmite_genetic_algorithm


function [f] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);
f1x = ((sin(5.1*pi*x+0.5)).^6);
f1y = ((sin(5.1*pi*y + 0.5)).^6);
a1 = (((x-0.0667).^2)/0.64);
a2 = (((y-0.0667).^2)/0.64);
f2x = exp(-4*log(2)*a1);
f2y = exp(-4*log(2)*a2);
f = -(f1x*f1y*f2x*f2y);

end

STUDY TIME ANALYSIS:
 TIC-TOC Command is used to measured how long to takes to complete study.Here the time taken for 3 studies are initiate below
•	Study_time 1 = 5.7849
•	Study_time 2 = 4.3462
•	Study_time 3 = 8.4222



CONCLUSION:
      Global maxima of a stalagmite function is calculated by using GA algorithm is done. The Graph for F maxima VS no. of iterations is ploted and noted . In the MATLAB GA will always give a minima value but we want to find the maxima value in this challenge so inverse the function ( f by negative value) and we will get the global maxima of a stalagmite function.

REFERENCES:
 https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-code-e396e98d8bf3?gi=fb232c859f46
 https://www.mathworks.com/help/gads/ga.html
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.