Week 4.1 - Genetic Algorithm
Objective:
To Write a code in MATLAB to optimise the stalagmite function and find the Global Maxima of the function.
Concept:
Genetic Algorithm is a serach based optimization technique based on the principles of Genetics and Natural Selection. Its is frequently used to find optimal or near optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.
Advantages of Genetic Algorithm:
1.Does not require any derivative information.
2.Is faster and more efficient as compared to the traditional methods.
3.Has very good parallel capabilities.
Limitations of Genetics Algorithm:
1.Genetics are not suited for all problems, especially problems which are simple and for whihc derivative informationn is available.
2.Fitness value is calculated repreateddly whihc might be computationally expensive for some problems.
Code:
clear all
close all
clc
%Input Variable
x = linspace(0, 0.6,150);
y = linspace(0, 0.6, 150);
%Number of iteration
num_cases = 50;
% combing the input parameters
[xx,yy] = meshgrid(x,y);
We are clearing other codes in command using clear all, close all, clc. Afterwards we create 2 variables x and y and send values between 0 to 0.6 and divide it into 150 parts. This is done using linspace command.
We create num_cases variable and use mesh grid to create a 2D Plot.
%Calculating Stalagmite function and equation to vector f
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
In this section we call the stalagmite function which would go to another function file and do the calculations.
function [f] = Stalagmite(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((-4*log(2) * (input_vector(1) - 0.0667)^2)/0.64);
f2y = exp((-4*log(2) * (input_vector(2) - 0.0667)^2)/0.64);
f = -(f1x * f1y * f2x * f2y);
end
%Study 1 - statistical behavior with unbounded inputs
for k = 1:num_cases
[input1,fopt1(k)] = ga(@Stalagmite,2);
xopt1(k) = input1(1);
yopt1(k) = input1(2);
end
time = toc;
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt1, yopt1, -fopt1, 'marker', 'o','markersize' , 5, 'markerfacecolor', 'r')
title('statistical behavior with unbounded inputs')
subplot(2,1,2)
plot(-fopt1)
xlabel('iteration')
ylabel('function maximum')
In this we use the calculated values and do the study 1 and plot the graph with labels and legends along with axis range.
Drive Link:
https://drive.google.com/drive/folders/1-bNgElk1ejuJbPHAcHGh87-UP6VhslPy?usp=sharing
Output:

Errors faced:
1. Had some problem with typos and multiplication calculations after some debuggings solved these issues.
Conclusions:
Thus wrote the concept the genetic algorithm and did calculations and made plots with it successfully.