Week 4 - Genetic Algorithm
AIM:
To write code using MATLAB and optimise stalagmite function and to find out globalmaxima of the function.
OBJECTIVE:
#To write the code and to define the stalagmite function
#This program start with 2Dimensional mesh where one dimensional array is created using 1D and 2D array
#The stalagmite takes input vector and calculates f
#the coding for stalagmite function is used to get the minimum values of stalagmite function with different case study for
generic algorithm to make a graph in 3D.
THEORY:
#Genetic algorithm is a search algorithum which is based on natural selection and natural genetics. it basically based on the concept of "survival of fittest (Darwinian theory)
#It generally used to find the true or appropriate solution to optimization and search problem.
#This technique is inspired by evolutionary biologies such as inheritance,mutation,crossover and selection
#In each generation, the fitness of each individual is evaluated and multiple individuals are selected from the current population and modified to form a new population
#This new population is used in the next iteration and this algorithm terminals when satisfied fitness is generated

In real world the applications of genetic algorithm includes computer gamimg, automotive design, robotics, traffic/shipment routing and more.
How does ga work?
#Genetic algorithm gives global maxima and minima irrespective to the start function or region of function. The primary global optimisation is an improvement.
#Basically there are 3 types of operators:
#Selection
#Cross-over
#Mutation
SELECTION:
Selection is the stage of genetic algorithm in which individual values are chosen frpm a population of natural breeeding
The generic selection procedure may be implemented as follows:
#The fitness function is evaluated for each individual, providing fitness values which are normalised . normalisation means dividing the fitness value
of each individual by the sum of all fitness values, so that the sum of all resulting fitness values equal 1
#Accumulated normal fitness values are computed: the accumulated fitness values of an individual is the sum of its owm fitness value plus and the fitness value of all the previous individuals, the accumilated fitness of the last individual must be 1
#A random number R between o and 1 is chosen
#The selected individual is the first one whose accumulated normalized values is greater than or equal to R
CROSSOVER:
#The crossover operator is analogous to reproduction and biological crossover. In this more than one parent is selected and one or more off-strings
are produced using the genetic material of the parents.
# crossover is used in GA with a high probability.
#n section we will discuss some of most popularity used calculating operations
#it is to be noted that this cross overs are very generic and the GA designer must choose and implement problem specific cross over as well.
ONEPOINT CROSS OVER:
In this one-point crossover, a random crossover point is selected and the tails of its two parents are swapped to get new off-springs.
MULTIPOINT CROSSOVER:
Multi point crossover is a generalization of the one-point crossover wherein alternating segments are swapped to get new off-springs.
MUTUATION:
In this the generic operator is used to maintain genetic diversity from one generation of population of genetic algorithm chromosomes to the next
mutation alteres one or more gene values in a chromosomes from its initial state. In mutation the situation may change entirely from the previous solution. Hence GA can come up with better solution by using mutation.
#Mutation occurs during evolution according to a user define mutation probability. this propability should be set low. if its high the search turn into primitive random search
FITNESS FUNCTION:
#Takes the solution as input and producers the suitability of the solution as ouptput. This evolutes how close the solution to the optimal solution as desired problem
SYNTAX FOR GA:
STOPPING CRITERIA:
#Stopping criteria is occured during convergence
#Lower population is done when the GA is continuously repeating where the stopping criteria meets.
SIGNIFICANCE OF RUNNING MULTIPLE ITERATION AND CAUSES:
#In the significance of running multiple iteration is caused due to the population, bounds and GA
# where GA is not a accurate value to get the precise value to run multiple hydrations
PROGRAM:
Stalagmite function code:
function M = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
fx1 = (sin(5.1*pi*x +0.5))^6;
fx2 = exp(-4*log(2)*((x-0.0667)^2)/0.64);
fy1 = (sin(5.1*pi*y +0.5))^6;
fy2 = exp(-4*log(2)*((y-0.0667)^2)/0.64);
M = -(fx1*fx2*fy1*fy2);
end
MAIN CODE:
clear all
close all
clc
%Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%creating a 2 dimensional 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
tic
%study1-statical behavior
for i= 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite, 2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time1 = toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function maximum')
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
study_time2 = toc
figure(2)
subplot(2,1,1)
surf(xx,yy,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
%study 3 increasing ga iterations
options = optimoptions('ga')
options = optimoptions(options,'populationsize',180);
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
study_time3 = toc
figure(3)
subplot(2,1,1)
surf(xx,yy,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
Stepwise Explanation for code:
x = linspace(0,0.6,150) - Linespace command is used for providing intervals with a given value, where the values of x and y are given and these the inputs of x and y arrray
#y = linspace(0,0.6,150); Linespace command is used for providing intervals with a given value, where the values of x and y are given and these the inputs of x and y arrray
#num_cases = 50; where 50 is to run it for 50 times and it is called the GA(Genetic algorithm)
#[xx,yy] = meshgrid(x,y);where x and y are the axis plottes to be meshed and the command is used to mesh both 2D and 3D plot
#i=1:length(xx) for loop is created for repeating the sets od values with respective x
#for j=1:length(yy) for loop is created for repeating the sets od values with respective y
#input_vector(1) = xx(i,j) for each value x is basically an array is crested and it is called input _vector
#input_vector(2) = yy(i,j) for each value y is basically an array is crested and it is called input _vector
#f(i,j)=stalagmite(input_vector) where stalagmite is a function and the stalagmite takes the input_vector and calculates the f
#tic is used to calaculate the operation time performed
# [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]) where this command is for statiscal behaviour is started with inputs and where the stalagmite function is thecode
which has entered seperately amd 2 is the number of variables as inputs [],[],[],[] and the variable arguments are [0;0],[0.6;0.6] which are xx and yy
# xopt(i) = inputs(1) the x value for the input is wriiten with the variable called xopt
# yopt(i) = inputs(2) the y value for the input is wriiten with the variable called yopt
# end- in forloop end must be used to end
#study_time2 the toc command is used to calculate the operartion time performed for study 2
#study_time3 the toc command is used to calculate the operartion time performed for study 3
#subplot(2,1,1) is the coomand used to craete multiple plot and the x and y are the respective to the first column
#surf(xx,yy,-f) is used to create the surface for a function
#shading interp this command is used to plot the 3D graph and it will give smooth shading
#plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r'), the marker and the markersize command is used to create the size and shape of maxima
#title('bounded inputs')- this commamd is use to display the graph name
#subplot(2,1,2) the subplot is used for creatig multiple plots with a plot
#plot(-fopt) is given to plot the graph in maxima
#xlabel('iterations') xlabel command is used for creating x-axis name
#ylabel('function maximum') ylabel command is used for creating y-axis name
OUTPUT:
Graph for study 1:

Study_time 1: 4.89secs is the time taken to execute the study1.
RESULT:
The value for x = 0.0604
The value for y = 0.0604
The value for z = 0.9378 this is the minimum obtained from the x and y.
GRAPH FOR STUDY 2:

Study_time 2: 5.78 sec is the time taken to execute the study2.
RESULT:
The value for x = 0.0668
The value for y = 0.2618
The value for z = 0.8471 this is the minimum obtained from the x and y.
Graph for study 3:

Study_time 3: 15.586 sec is the time taken to execute the study3.
RESULT:
The value for x = 0.0668
The value for y = 0.0668
The value for z = 1 this is the minimum obtained from the x and y
Conclusion: