Week 4.1 - Genetic Algorithm
% Evaluating stalagmite function using Genetic algorithm.
clear all
close all
clc
%defining value of x and y,taking 150 values between 0 to 0.6
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases = 50
%creating 2D mesh using mesh grid command.
[xx,yy] = meshgrid(x,y);
% Evaluating Stalagmite 150 iterations.
for i= 1:length(x)
for j= 1: length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
d(i,j)= stalagmite(input_vector);
end
end
%study 1 - Stastistical behaviour by using genetic algorithm by taking 50 iteration and considering
%only 2 variables
tic
for i= 1: num_cases
[inputs, fopt(i)]=ga(@stalagmite,2);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end
study1_time = toc;
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,d)
shading interp
plot3(xopt,yopt,fopt,'marker', 'o', 'markersize',5)
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Function minimum')
%study 2 - Statistical behavior by setting an upper and lower limits.
tic
for i= 1: num_cases
[inputs, fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end
study2_time = toc;
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,d)
shading interp
plot3(xopt,yopt,fopt,'marker', 'o', 'markersize',5)
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Function minimum')
%study 3 - By increasing GA iterations byh using optimoptions.
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);
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
study3_time = toc;
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,d)
shading interp
plot3(xopt,yopt,fopt,'marker', 'o', 'markersize',5)
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Function minimum')
% end
% surfc(xx,yy,f)
% xlabel('x value')
% ylabel('y value')
% shading interp
% [inputs, fval] = ga(@stalagmite,2);
%Stalagmite function code
%here we take the output as -f, As we need minimum values.
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);
end
Explanation
Theory about 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.
Five phases are considered in a genetic algorithm.
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation
Genetic Algorithms - Termination Condition
- When there has been no improvement in the population for X iterations.
- When we reach an absolute number of generations.
- When the objective function value has reached a certain pre-defined value.
Genetic algorithm syntax: x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
Significations of running multiple iterations:
To get accurate solutions
Result:
All the plots attached contain 2 graph
1) unbounded graph 3D plot. which statistically represents stalagmite functions.
2) Iterations v/s minimum function value.
Study 1: Shows the plot using the genetic algorithm by taking 50 iterations and considering only 2 variables. This value varies from -1 to 0. Which has a huge range of minimal values and needs more optimization.
Study 2: This study is improvised by applying lower and upper limit bound. This value varies from -0.4 to -1 . and the Values obtained are more. This study gives little more stabilized output but still requires optimization
Study 3: This study is improvised by increasing the number of iterations by using 'optimoptions' function. This gives a very much stabilized graph. and there are only 2 minimal values.