Week 4.1 - Genetic Algorithm
Aim- To write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
Theory -
Optimization is the process of making something better. Optimization refers to finding the values of inputs in such a way that we get the “best” output values. The definition of “best” varies from problem to problem, but in mathematical terms, it refers to maximizing or minimizing one or more objective functions, by varying the input parameters. The set of all possible solutions or values which the inputs can take makes up the search space. In this search space, lies a point or a set of points that gives the optimal solution. The aim of optimization is to find that point or set of points in the search space.
Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve.
Figure (Basic Structure of GA):-

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.
ga (genetic algorithm)
Find minimum of function using genetic algorithm
Syntax
Code for function (stalagmite) used in the main code:-
function f = stalagmite(inputs)
x = inputs(1);
y = inputs(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);
Main code-
% Program to optimize the stalagmite function and find the global maxima of the function.
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 2Dimensional mesh
[xx, yy] = meshgrid(x,y);
% Evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
inputs(1)=xx(i,j);
inputs(2)=yy(i,j);
f(i,j) = stalagmite(inputs);
end
end
% Study 1 - Statistical behaviour
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
fopt = -fopt
f = -f %for obtaining global maxima
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Unbound 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],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
fopt = -fopt
f = -f %for obtaining global maxima
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Bound inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')
% Study 3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',150);
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
fopt = -fopt
f = -f %for obtaining global maxima
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Bound inputs with modified population size')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')
- For Study 1 Statistical behavior

-
Study 2 - Statistical behaviour - with upper and lower bounds

- Study 3 increasing GA iterations




