Week 4.1 - Genetic Algorithm
Genetic algorithm explanation(courtesy: Mhttps://in.mathworks.com/discovery/genetic-algorithm.html):
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 generation. Over successive generations, the population "evolves" toward an optimal solution.
Syntax: ga syntax used in MATLAB has two compulsory input arguments(func, number of variables) and several other optional input arguments depending on the number of constraints, type of constraints-equality or inequality constraints, bounded values of independent variable(just another type of inequality constraints) so on. Matlab then runs the algorithm to five the minimum value of the function subject to all the specified constraints taking the default population size as 50.
The basic syntax is [xval, fval]=ga(func, nvars)
Stalagmite function code:
function [output]=stalagmite(x)
f1x=(sin(5.1*pi.*x(1)+0.5)).^6;
f1y=(sin(5.1*pi.*x(2)+0.5)).^6;
f2x=exp(-4*log(2).*(x(1)-0.0667).^2/0.64);
f2y=exp(-4*log(2).*(x(2)-0.0667).^2/0.64);
output=-f1x.*f2x.*f1y.*f2y;
end
Main code:
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
numcases=50
[xx yy]=meshgrid(x,y);
for i=1:length(xx)
for j=1:length(yy)
input(1)=xx(i,j);
input(2)=yy(i,j);
zz(i,j)=stalagmite(input);
end
end
%Study 1=statistical behaviour
tic
for i=1:numcases
[input fopt(i)]=ga(@stalagmite,2);
xopt(i)=input(1);
yopt(i)=input(2);
end
study_time1=toc
figure(1)
subplot(2,1,1)
surfc(xx,yy,-zz)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
title('Unbounded inputs')
xlabel('iterations')
ylabel('function optimum')
%Study 2=statistical behaviour with bounded inputs
tic
for i=1:numcases
[input fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i)=input(1);
yopt(i)=input(2);
end
study_Time2=toc
figure(2)
subplot(2,1,1)
surfc(xx,yy,-zz)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
title('bounded inputs')
xlabel('iterations')
ylabel('function optimum')
%study 3 statistical behaviour with increased population size
tic
options=optimoptions('ga');
options=optimoptions(options,'populationsize',170);
for i=1:numcases
[input fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i)=input(1);
yopt(i)=input(2);
end
study_Time3=toc
figure(3)
subplot(3,1,1)
surfc(xx,yy,-zz)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(3,1,2)
plot(fopt)
title('Bounded with assumed population size')
xlabel('iterations')
ylabel('function optimum')
1) The x values and y values are assigned some 150 values using linspace command from 0-0.6. This is done just to plot stalagmite function. It DOES NOT define the domain in which genetic algorithm will be operated on
2) Then, using mesh grid command, a 2 dimensional grid- containing xx and yy values are obtained using which the values of stalagmite function were calculated and stored in zz
3)Thus, they are now ready to be plotted anytime when a surfc command is called.
Study-1 without any bounds and population size:
4)ga command was called but with no bounds or population size. The time was noted using tic and toc commands.
5)Each time the ga command was called, it gives different optimum values due to the fact that it initialises the algorithm by randomly picking points a. Thus, many points were taken outside the expected domain initially defined using linspace command. Thus, time taken would be least for this study
study-2 with bounds alone:
6)Now, we have let known matlab our desired domain [0,0.6] for both x and y values by using proper syntax for ga.That gave more specific values to look upon for algorithm and thus most of the time it was hovering around one or two specific values.
7) But, this slightly increased the running time
study-3 with bounds and population size:
8) Fixing of the population size at a higher value of 170 will allow the algorithm to reach even more closer to the actual value. So, in each iteration, one negligible difference between the values obtained and optimum value was observed. That a great improvement but comes with the cost of higher computing time.
Outputs are attached