Obtaining maxima using Genetic Algorithm for a Stalagmite Function
Aim:-Obtaining maxima from the given stalagmite function by using Genetic algorithm
Objective:-
- Surface plot is to be made from the given objective function using the ga command.
- Bounding the inputs to values provided so that ; the population moves towards the maxima peaks.
- Increasing the population size thereby increasing the total no of iterations performed.
Introduction:-
- Genetic Algorithm assigns raw fitness value to the intial population that is created . Intial population is taken by random sampling .
- Fitness value is the value that is substituted in the objective function for the points selected in the random sampling.Since the genetic algorithm is based on finding minima ; it considers the points having min values as elite point.
- Elite points are those which are passed to next generation without any changes.
- In other parent points next generation is obtained by either crossover or mutation.
- Crossover the children takes identity of both parent while in mutation it takes identity of the single parent.
- This iterations continues until it reaches a best fitness value in which even after further iterations the fitness value does not improve.
Program:-
- The values of input x and y are linearly spaced between 0 to 0.6 and num_cases gives us the no of iterations that is being perfomed that is 50.
- 1D vector is converted into a 2D vector by using the meshgrid command and is called as xx,yy.
- Stalagmite function is having 4 terms forming the fitness function that is to be maximized.
4.For loop is used so as to enter the values of vector xx and yy in the stalagmite function in the manner that is forms an array of 150 X 150 .Fitness Function is multiplied by negative as genetic algorithm always finds the minima of the function while our objective is to find the maxima.
Since to maxima of f(x)=minima of -f(x) as both function corresponds to same value .
After the value of the fitness function is evaluted is multiplied by negative .
5. tic and toc are used to calculate the time required for the study to complete.
6.Syntax for study 1:- x = ga(fun,nvars) in these objective function is minimized by the ga function
7.Syntax for study 2:- x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub.
8.Syntax for study 3:- x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options.
9.Surf command is used plot the 3D contour and other subplot shows the value of function
1.Genetic Algorithm is implemented using random sampling
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
%Meshgrid
[xx,yy]=meshgrid(x,y);
%Stalagmite Function
for i=1:length(xx);
for j=1:length(yy);
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stalagmite(input_vector);
end
end
f=-f %To invert the surface plot which is downwards facing
tic
%Statistical Behaviour
for i=1:num_cases;
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc;
fopt=-fopt %Invert the function
figure(1)
subplot(2,1,1);
surfc(x,y,f);
xlabel(\'x value\');
ylabel(\'y value\');
shading interp;
hold on;
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\');
title(\'Unbounded output\');
subplot(2,1,2)
plot(fopt);
xlabel(\'Iterations\');
ylabel(\'Function minimum\');
2.Code in which the input values upper and lower limit is given
%Study between upper and lower bound
tic
for j=1:num_cases;
[inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(j)=inputs(1);
yopt(j)=inputs(2);
end
study2_time=toc;
fopt=-fopt;
figure(2)
subplot(2,1,1);
surfc(x,y,f);
xlabel(\'x value\');
ylabel(\'y value\');
shading interp;
hold on;
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\');
title(\'bounded inputs\');
subplot(2,1,2)
plot(fopt);
xlabel(\'Iterations\');
ylabel(\'Function minimum\');
3.Results are converged by increasing the population size
%Study by increasing the iterations
tic
options=gaoptimset(\'ga\');
options=gaoptimset(options,\'populationsize\',170);
for k=1:num_cases;
[inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc;
fopt=-fopt;
figure(3)
subplot(2,1,1);
surfc(x,y,f);
xlabel(\'x value\');
ylabel(\'y value\');
shading interp;
hold on;
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\');
title(\'bounded inputs\');
subplot(2,1,2)
plot(fopt);
xlabel(\'Iterations\');
ylabel(\'Function minimum\');
Results:-
Fig.Results of the variables are shown
Fig:-Genetic algorithm takes random value of the input x and y rather than taking form the set assigned
Fig:As the input values x and y are bounded the solution converges
Fig:After changing the population size the function value converges to narrow range
Error:-
Fig.Surface plot was missing
This was due to surf command written twice in the code .
Conclusion:-Genetic Algorithm works on random sampling to choose the intial solution and through iterations converges to the solution.
References:-
For Genetic Algorithm
https://in.mathworks.com/help/gads/ga.html?s_tid=srchtitle#mw_d9f21ac7-dfb6-4e81-bbdc-c1cb5533af18