Week 5 - Genetic Algorithm
Aim:
To optimise the stalagmite functionand find the global maxima of the function using MATLAB code.
Objective:
To explain the concept of Genetic algorithmand its syntax and also to find the global maxima of the function.
Theory (GA):
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step,the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next genertion. Over successive generations ,the population "evolves" toward an optimal solution . You can apply the genetic algorithms, including problems in which the objective function is discontinuous,nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways.
Optimization:
- Optimization is the process of selection from the set of values for the imposed targets and the constraints.
- Process handling, where its integrated with each other and affects one another in all aspect.
Code:





function [f] = stalagmite(input_vector)
x = input_vector(1)
y = input_vector(2)
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
clear all
close all
clc
% Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Creating a 2Dimensional mesh
[xx,yy] = meshgrid(x, y);
num_cases=50; % number of iteration
% evaluating the stalagmite function
for i = 1:length(xx)
for j=1:length(yy)
z(1)=xx(i,j);
z(2)=yy(i,j);
f(i,j)=stalagmite(z);
end
end
% find the global maxima using the stalagmite function
options=optimoptions('ga','plotfcn',@gaplotbestf,'PopulationSize',200); %options 'Syntax'
[z,fval]=ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options); % ga 'syntax'
x_opt=z(1);
y_opt=z(2);
figure(1)
surfc(x,y,-f)
shading interp
hold on
plot3(x_opt,y_opt,-fval,'r*','MarkerSize',5)
title('optimise value using the stalagmitefunction')
% surfc(xx,yy,f);
% surfc(xx,yy,-f);
% shading interp
% statistical behaviour (unbounded)
tic %for calculating the study time (tic -toc command)
for i=1:num_cases
[inputs,fopt(i)]= ga (@stalagmite,2);
x_opt2(i)=inputs(1);
y_opt2(i)=inputs(2);
end
study1_time=toc;
%ploting the graph
figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(x_opt2,y_opt2,-fopt,'color','b','marker','o','markersize',5,'markerfacecolor','g');
title('unbound case')
xlabel('x')
ylabel('y')
legend('X values','Y values')
grid on
subplot(2,1,2)
plot(-fopt,'color','b','linewidth',1);
xlabel('No. of iterations')
ylabel('function maximum value')
% statistical behaviour (bounded)
tic
for j=1:num_cases
[z,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
x_opt(j)=z(1);
y_opt(j)=z(2);
end
study2_time=toc;
% plotting
figure(3)
subplot(2,1,1);
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_opt,y_opt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded case')
xlabel('x');
ylabel('y');
grid on
subplot(2,1,2);
plot(-fopt);
xlabel('No. of iteration');
ylabel('function maximum value')
% modifying the number of population
options=optimoptions('ga','populationsize',500);
tic
for k=1:num_cases
[z,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
x_opt(k)=z(1);
y_opt(k)=z(2);
end
study3_time=toc;
%plotting
figure(4)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_opt,y_opt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('Modifying the no.of population')
xlabel('x')
ylabel('y')
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('No.of iterations');
ylabel('function maximum value');