Calculation of global maxima for stalagmite function
Code-
clc
clear all
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=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);
f(i,j)=pro(input);
end
end
tic
for k=1:num_cases
thestate=rng;
[q,fopt(k)]=ga(@pro,2)
xopt(k)=q(1)
yopt(k)=q(2)
end
study_time_1=toc
figure(1)
subplot(2,1,1);
hold on
surfc(xx, yy, f);
shading interp;
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',3,\'markerfacecolor\',\'g\')
title(\'Unbounded Inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
tic
for k=1:num_cases
thestate=rng;
[q,fopt(k)]=ga(@pro,2,[],[],[],[],[0;0],[1;1])
xopt(k)=q(1)
yopt(k)=q(2)
end
study_time_2=toc
figure(2)
subplot(2,1,1);
hold on
surfc(xx, yy, f);
shading interp;
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',3,\'markerfacecolor\',\'g\')
title(\'Bounded Inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
options = optimoptions(\'ga\');
options = optimoptions(options,\'populationsize\',200);
tic
for k=1:num_cases
thestate=rng;
[q,fopt(k)]=ga(@pro,2,[],[],[],[],[0;0],[1;1],[],[],options)
xopt(k)=q(1)
yopt(k)=q(2)
end
study_time_3=toc
figure(3)
subplot(2,1,1);
hold on
surfc(xx, yy, f);
shading interp;
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',3,\'markerfacecolor\',\'g\')
title(\'Bounded Inputs- Incresed population\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
options = optimoptions(\'ga\');
options = optimoptions(options,\'populationsize\',400);
tic
for k=1:num_cases
thestate=rng;
[q,fopt(k)]=ga(@pro,2,[],[],[],[],[0;0],[1;1],[],[],options)
xopt(k)=q(1)
yopt(k)=q(2)
end
study_time_4=toc
figure(4)
subplot(2,1,1);
hold on
surfc(xx, yy, f);
shading interp;
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',3,\'markerfacecolor\',\'g\')
title(\'Bounded Inputs- Incresed population\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
maxvalue=(fopt)
Function- Stalagmite by the name pro
function[F]=pro(input)
f1=(sin(5.1*input(1)*pi + 5))^6;
f2=(sin(5.1*input(2)*pi + 5))^6;
f3=exp(-4*log(2)*((input(1)-0.0667)^2)/0.64);
f4=exp(-4*log(2)*((input(2)-0.0667)^2)/0.64);
J=(f1*f2*f3*f4);
F=1/(1+J);
end
Explanation-
Stalagmite function is defined according to the inputs and the function formula.
According to the x and y values the stalagmite function values are stored in f, the conditions are given for the maximisation statement.
Then ga solver is used to calculate the optimised x and y values and the optimised f values, which indicates the maximum values.
Here searchspace is not specified to the ga solver.
The stalagmite function is plotted and on the same figure the maxima values are plotted.
Same steps are followed for the next iteration,
In this iteration solution are constrained, initial and final boundries are specified.
Similarly after performing this in third iteration intial population range is specified and the stopping criteria is modified.
Genrtic Algorithm
Genetic algorithm is an optimisatin technique, it uses concepts from evolutionary biology. It starts with an initial generation of candidate solution which are tested against the objective function. Subsequent generations evolve from the previous one through selection, crossover, mutaion.
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.
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.
Syntax-
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,[],[],[],[],lowerboundaries,upperboundaries,[],[],options);
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 the reason why ga stopped. ga can also return a fourth argument, Output, which contains information about the performance of the solver.All solvers attempt to minimize an objective function. For maximization problem then we have to define g(x) = –f(x), and minimize g.
Error
-
Refrences-
1) https://in.mathworks.com/discovery/genetic-algorithm.html
2) https://www.youtube.com/watch?v=1i8muvzZkPw&t=216s
3) https://in.mathworks.com/discovery/genetic-algorithm.html
4) https://en.wikipedia.org/wiki/Stalagmite
5) https://in.mathworks.com/help/gads/examples/genetic-algorithm-options.html