Program to calculate the global maxima of a stalagmite function
Q1. Describe how Genetic Algorith works .
The genetic algorithm uses random sampling for optimization. It begins by creating a random initial population. THen it creates a sequence of new populations.
At each step the algorithm uses the individuals in the current generation to create the next population. To create the new population the algorithm scores each member of current population by calculating its fitness value, scales the raw fitness scores to convert them into a more useable range of values. It then selects members, called parents on the basis of their fitness. It produces childern from the parents, by making random changes to single parent, mutation, or by combining vector entities of a pair of parents, crossover.
It then replaces the current population with the children to form the next generation.
The algorithm stops when one of the stopping criteria is met.
Q2. Write code in Matlab to optimize the Stalagmite function.
You should find the maximum value of the function and not the minimum value. Make sure you watch the video carefully.
Also show that the code produces the same output, if run for several times. (say 100 iterations).
clear all
close all
clc
%calculating the values for stalagmite function
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
its = 50; %number of iterations GA is run
[xx,yy] = meshgrid(x,y);
for i = 1:length(xx)
for j = 1:length(yy)
inputv(1) = xx(i,j);
inputv(2) = yy(i,j);
funcn(i,j) = stalagmitec(inputv);
end
end
%plotting stalagmite function
figure(1)
hold on
surf(xx,yy,funcn)
shading interp
%increasing the populationg size for GA helps it converge the minimum values to a few %decimal places
options = gaoptimset(@ga);
options = gaoptimset(options,\'PopulationSize\',200);
tic %timer
for i = 1:its
[inputs, fval(i)] = ga(@stalagmitec,2,[],[],[],[],[0;0],[1;1],[],[],options);
xval(i) = inputs(1);
yval(i) = inputs(2);
end
timetaken = toc
plot3(xval,yval,fval,\'marker\',\'o\');
hold off
figure(2)
plot(fval)
xlabel(\'Iterations\');
ylabel(\'Minimum value\');
fprintf(\'Maximum value of stalagmite function is : %f\', -min(fval));
%stalagmite function
function [fval] = stalagmitec(array)
f1x = (sin((5.1*pi*array(1))+0.5))^6;
f1y = (sin((5.1*pi*array(2))+0.5))^6;
f2x = exp(-1*4*log(2)*((array(1)-0.0667)^2)/0.64);
f2y = exp(-1*4*log(2)*((array(2)-0.0667)^2)/0.64);
fval = -1*f1x*f1y*f2x*f2y; %minus sign so that GA finds the maximum value
end
I had to use gaoptimest instead of optimoptions on this code as I am using Matlab R2013, which does not support optimisation of GA by optimoptions. The gaoptimset command does help the minimum values found by GA to converge till 4 decimal places, but even after increasing the population size to 600 there exists an error of 4 decimal places. But still, the value converges to a variation of only 4th decimal places, which is quite close.
When GA is run for
1) 50 iterations without defining initial population size
2) 50 iterations with a population size of 200
3) 100 iterations with a population size of 200
4) 100 iterations with population size of 300
the following plots are obtained (see how the minimum value varies) :
50 Iterations , no initial population defined
Without defining inital population size and keeping the iterations only 50, there is quite some difference in the minimum values determined by GA i.e 0.8
50 Iterations , initial population size of 200
^ A zoomed in view of the points plot by GA.
It is seen that after increasing the initial population size, the results converge from 1 decimal place to 3rd decimal places i.e 0.003
100 iterations, initial population size of 200
^ A close up view of the points plot by GA
After increasing the number of iterations at population size of 200, it is seen that results converge to 4th decimal places i.e 0.0025
100 iterations, initial population size of 300
^ Zoomed in view of the points plot by GA
After increasing the initial population size to 300 at 100 iterations, it is seen that the results converge to a difference of 4th decimal place i.e 0.0014
Hence upon increasing the initial population size and number of iterations, the results of GA converge.