GENETIC ALGORITHM
GENETIC ALGORITHM
Introduction:
- Genetic algorithm is inspired by the process of natural selection that belongs to the larger class of evolutionary algorithm. Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on bio-inspired operators such as mutation, crossover and selection.
- This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
- There are three phases of genetic algorithm, They are
- Selection - The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.
- Cross-over - Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes
- Mutation - In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.
Genetic Algorithm in Matlab:
- The Genetic algorithm (GA) is the in-built function in matlab, We have to calculate the global maxima of a stalagmite function by optimizing the stalagmite function.
- The syntax of Genetic algorithm is given below
x= ga(fun,nvars,A,b,Aeq,beq,lb,ub,noncoln,options)
fun - Function handle
nvars - Number of variables
A&b - Linear inequality constraints
Aeq&beq - Linear equality constraints
lb&ub - Lower and Upper bounds
noncoln - non-linear constraints
options - optimization options
Stalagmite Function:
- By using the below formula we can calculate the optimize the stalagmite function and can find the global maxima of a stalagmite function.
- By adding the negative sign to the function we can actually calculate the global maxima of a stalagmite function.
- And we are actually doing three studies for the given function they are
Study-1: Statistical Behaviour(unbounded)
Study-2: Statistical Behaviour with upper & lower bounds
Study-3: Increasing GA Iterations
Program-1:
function [f] = get_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;
f2x = exp(-4*log(2)*((x-0.0667).^2/0.64));
f2y = exp(-4*log(2)*((y-0.0667).^2/0.64));
[f] = -(f1x.*f1y.*f2x.*f2y);
end
Code Explanation:
- In this program we are actually using the function command to calculate the given stalagmite function.
- They have asked to find to the global maxima for the function so we are adding the negative sign to the function.
Program-2
clear all
close all
clc
% Inputs X & Y
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Meshing X & Y for 2D array
[xx, yy] = meshgrid(x,y);
% Using for loop
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) = get_stalagmite(input_vector);
end
end
tic
% iterations
num_cases = 50;
% Study-1 Statistical Behaviour
for i = 1:num_cases
[inputs, fopt(i)] = ga(@get_stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
% Plotting-1
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title(\'Unbounded\')
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function Minimum\')
% Study-2 Statistical Behaviour with upper & lower bounds
tic
for i=1:num_cases
[inputs, fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
% plotting-2
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title(\'Bounded Inputs\')
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function Minimum\')
% Study-3 Increasing GA Iterations
options = optimoptions(\'ga\');
options = optimoptions(options,\'populationSize\',250);
tic
for i=1:num_cases
[inputs, fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
% Plotting-3
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title(\'Bounded Inputs with increase in population\')
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function Minimum\')
Code Explanation:
- Initially we start by giving the input values that is x&y by using the linspace command which creates the 1D array. And then we using the meshgrid command to create the 2D array.
- And then by using the for loop we are calculating the stalagmite function which is programmed at first by using the given formula.
- In the Study-1 we are using for loop for calculating the genetic algorithm for certain number of iterations and then we are also adding tic and toc command for calculating the time taken.
- [inputs, fopt(i)] = ga(@get_stalagmite,2) This is the syntax for genetic algorithm(study-1).
- Then we are plotting the two figures in one window so we are using the subplot command and then we are plotting the stalagmite surface using surf command and next we are plotting the optimum value of x,y&f using the plot3 command and adding markers to it.
- Finally we are plotting optimum value of F and the adding label to it and giving the title to the plot window.
- In the Study-2 we are calculating the genetic algorithm by adding the lower and upper bounds to it and then plotting it.
- [inputs, fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1]) This is the syntax for genetic algorithm(study-2).
- In Study-3 we are calculating the genetic algorithm by increasing the population size in the GA using options.
- [inputs, fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options) This is the syntax for genetic algorithm(study-3).
options = optimoptions(\'ga\');
options = optimoptions(options,\'populationSize\',250);
- By this we have found the global maxima for the given stalagmite function.
Output:
Study-1 Output:
Study-2 Output:
Study-3 Output:
Hence by the three study we can understand that by increasing the population size or iterations we can optimize the stalagmite function and the global maxima is calculated.
Result:
The Program to optimise the stalagmite function and to find the global maxima of the given function has been written.