Week 4.1 - Genetic Algorithm
AIM - To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
What are Genetic Algorithms?
Nature has always been a great source of inspiration to all mankind. Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.
GAs were developed by John Holland and his students and colleagues at the University of Michigan, most notably David E. Goldberg and has since been tried on various optimization problems with a high degree of success.
In GAs, we have a pool or a population of possible solutions to the given problem. These solutions then undergo recombination and mutation (like in natural genetics), producing new children, and the process is repeated over various generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a higher chance to mate and yield more “fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”.
In this way we keep “evolving” better individuals or solutions over generations, till we reach a stopping criterion.
Genetic Algorithms are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solutions, keeping track of the best so far), as they exploit historical information as well.
Advantages of GAs
GAs have various advantages which have made them immensely popular. These include −
-
Does not require any derivative information (which may not be available for many real-world problems).
-
Is faster and more efficient as compared to the traditional methods.
-
Has very good parallel capabilities.
-
Optimizes both continuous and discrete functions and also multi-objective problems.
-
Provides a list of “good” solutions and not just a single solution.
-
Always gets an answer to the problem, which gets better over the time.
-
Useful when the search space is very large and there are a large number of parameters involved.
Limitations of GAs
Like any technique, GAs also suffer from a few limitations. These include −
-
GAs are not suited for all problems, especially problems which are simple and for which derivative information is available.
-
Fitness value is calculated repeatedly which might be computationally expensive for some problems.
-
Being stochastic, there are no guarantees on the optimality or the quality of the solution.
-
If not implemented properly, the GA may not converge to the optimal solution.
Stalagmite function in matlab
function [f]= stalagmite(input_vector
x= input_vector(1);
y= input_vector(2);
f1= (sin(5.1*pi*x+0.5))^6;
f2= (sin(5.1*pi*y+0.5))^6;
f3= exp(-4*log(2)*(x-0.0667)^2/0.64);
f4= exp(-4*log(2)*(y-0.0667)^2/0.64);
f= -(f1*f2*f3*f4); % negetive sign is added as GA algorithm always gives the Minimum value.
end
For first iteration :-
there is no boundary defined within GA
clear all
close all
clc
x=linspace(0,0.6,150);
y= linspace(0,0.6,150);
[xx,yy]= meshgrid(x,y);
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
num_cases=50; % GA runs for 50 iterations
tic % Study Time count start
for i=1:num_cases
[inputs,f_opt(i)]= ga(@stalagmite,2); % GA function is used to run the stalagmite data and give the optimal solutions.
x_opt(i)=inputs(1);
y_opt(i)=inputs(2);
opt_f(i)= -f_opt(i); %Ga gives us Minima value.Inverse f_opt is taken for Maxima value.
end
study1_time = toc % Study Time count end
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(x_opt,y_opt,f_opt,'marker','o','markersize',4,'markerfacecolor','r')%plotting the optimum values with a red dot marker along the stalagmite 3D curves
title('Unbounded inputs')
subplot(2,1,2)
plot(opt_f) %plot of opt_f for every iterations
xlabel('Number of iterations ---->')
ylabel('Function Max -->')
outputs :- 
2nd iteration:
Here the lower and upper bounds are defined
clear all
close all
clc
x=linspace(0,0.6,150);
y= linspace(0,0.6,150);
[xx,yy]= meshgrid(x,y);
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); %stalagmite function is called for every loop and output is stored in f
end
end
num_cases=100;
tic % Study Time start
for i=1:num_cases;
% lower boundary [0,0]
% upper boundary [0.6,0.6]
[inputs,f_opt(i)]= ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
x_opt(i)=inputs(1);
y_opt(i)=inputs(2);
opt_f(i)= -f_opt(i); %Ga gives us Minima value.Inverse f_opt is taken for Maxima value.
end
study2_time = toc % Study time end
subplot(2,1,1)
hold on
surfc(xx,yy,f) %Generating the stalagmite function.
shading interp % To bring visually smooth curve surface without mesh lines
plot3(x_opt,y_opt,f_opt,'marker','o','markersize',4,'markerfacecolor','r')
title(' With Bounded inputs')
subplot(2,1,2)
plot(opt_f) %Plotting opt_f value for every iteration
xlabel('Number of iterations ---->')
ylabel('Function Max -->')
Output

Iteration - 3
the population size is increased so as to converge to the Global maxima.
clear all
close all
clc
% x and y inputs
x=linspace(0,0.6,150);
y= linspace(0,0.6,150);
[xx,yy]= meshgrid(x,y);
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
num_cases=200; % GA runs for 200 iterations
options= optimoptions('ga');
options= optimoptions(options,'PopulationSize',500);
tic % Study Time start
for i=1:num_cases
[inputs,f_opt(i)]= ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],options);
x_opt(i)=inputs(1);
y_opt(i)=inputs(2);
opt_f(i)= -f_opt(i); %Ga gives us Minima value.Inverse f_opt is taken for Maxima value.
end
Study3_time = toc % Study Time end
subplot(2,1,1)
hold on
surfc(xx,yy,f) %Generating the stalagmite function.
shading interp % To bring visually smooth curve surface without mesh lines
plot3(x_opt,y_opt,f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with increased population size')
subplot(2,1,2)
plot(opt_f) %Plotting opt_f value for every iteration
xlabel('Number of iterations ---->')
ylabel('Function Max -->')
output :-

Conclusion
Thus it can be concluded that GA gives more accurate results in case of bounded inputs and by increasing the number of iterations and population size..