Week 4 - Genetic Algorithm
Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
And Plot graphs for all 3 studies and for F maximum vs no. of iterations.
% Genetic Algorithm
% This example shows how to create and manage options for the genetic
% algorithm function |ga| using |optimoptions| in the Global Optimization
% To use the |ga| solver, we need to provide at least two input arguments,
% a fitness function and the number of variables in the problem. The first
% two output arguments returned by |ga| are |x|, the best point found, and
% |Fval|, the function value at the best point. A third output argument,
% |exitFlag| tells you the reason why |ga| stopped. |ga| can also return a
% fourth argument, |Output|, which contains information about the
% performance of the solver.
clear all
close all
clc
%creating the random variables range using linspace
[m,n] = meshgrid(linspace(0,0.6,150),linspace(0,0.6,150));
%iterating the each value individually to stalagmite function.
for i=1:length(m)
for j=1:length(n)
inputsv(1)= m(i,j);
inputsv(2)= n(i,j);
f(i,j)=stalagmite(inputsv);
end
end
% case 1: unbounded Bheaviour
iterations=15;
rng default % For reproducibility
thestate = rng;
% If you run this example without the |rng default| command, your result can differ. because ga is a stochastic algorithm.
% How the Genetic Algorithm Works?
% The Genetic Algorithm works on a population using a set of
% operators that are applied to the population. A population is a set
% of points in the design space. The initial population is generated
% randomly by default. The next generation of the population is computed
% using the fitness of the individuals in the current generation.
for i=1:iterations
[inputsv,fin(i)]=ga(@stalagmite,2);
xfin=inputsv(1);
yfin=inputsv(2);
plot3(xfin,yfin,fin,'marker','o','MarkerFaceColor','r','MarkerSize',4)
hold on
end
figure(1)
hold on
surfc(m,n,f)
shading interp
view(30,30)
title('unbounded')
figure(2)
plot(fin)
xlabel('Iterations')
ylabel('functions')
title('Unbounded')
hold off
figure(3)
rng(thestate);
% case 2: Bounded Behaviours
for i=1:iterations
[inputsv,fin(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.5,0.5]);
xfin=inputsv(1);
yfin=inputsv(2);
plot3(xfin,yfin,fin,'marker','o','MarkerFaceColor','r','MarkerSize',4)
hold on
end
hold on
surfc(m,n,f)
shading interp
view(30,30)
title('bounded')
figure(4)
plot(fin)
xlabel('Iterations')
ylabel('functions')
title('bounded')
hold off
% case 3: optimized behaviour
% Specifying Population Options
% The default initial population is created using a uniform random number
% generator. Default values for the population size and the range of the
% initial population are used to create the initial population.
% *Specify a population size*
% The default population size used by |ga| is 50 when the number of
% decision variables is less than 5 and 200 otherwise. This size can be a
% poor one for some problems; a smaller population size can be sufficient
% for smaller problems. Since we only have two variables, we specify a
% population size of 10. We directly set the value of the option
% |PopulationSize| to 10 in our previously created options, |opts|.
opts.PopulationSize = 10;
% Specify initial population range
% The default method for generating an initial population uses a uniform
% random number generator. This creates an initial population where all the
% points are in the range 0 to 1. For example, a population of size 3 in a
% problem with two variables could look like:
Population = 200;
opts.InitialPopulationRange = [-1 0; 1 2];
figure(5)
for i=1:iterations
rng(thestate);
[inputsv,fin(i),exitFlag,Output] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.5,0.5],[],[],opts);
xfin=inputsv(1);
yfin=inputsv(2);
plot3(xfin,yfin,fin,'marker','o','MarkerFaceColor','r','MarkerSize',4)
hold on
fmax=max(fin);
end
hold on
surfc(m,n,f)
view(30,30)
shading interp
title('population included')
figure(6)
plot(fin)
xlabel('Iterations')
ylabel('functions')
title('optimized')
fprintf('The number of generations was : %d\n', Output.generations);
fprintf('The number of function evaluations was : %d\n', Output.funccount);
fprintf('The best max function value found was : %g\n', fmax);
Result: