Week 4.1 - Genetic Algorithm
- Stalagmite Coding as Genetic Algorithm:
clear all
close all
clc
% Defining our search space
x = linspace(0,0.6,150); % Value of x varies from 0 to 0.6 into 150 terms.
y = linspace(0,0.6,150); % Value of y varies from 0 to 0.6 into 150 terms.
f_output = zeros; % Here f_output is defined as zero matrix because in for loop f_output is a function of stalagmite.
% Creating a 2D mesh
[xx, yy] = meshgrid(x, y); % Defines value of xx & yy as meshgrid(x,y).
% Evaluating the stalagmite function in loop
for i = 1:length(xx) % Length command is used as xx matrix is of 150*150.
for j = 1:length(yy)
f(1)=xx(i,j);
f(2)=yy(i,j);
tic % This is used to start the evaluation of time for f_output.
f_output(i,j) = stalagmite_func(f); % The value of f_output is obtained as stalagmite functions value.
end
end
toc % This is used to end the evaluation of time for f_output.
figure(1) % This figure is to get a general mesh surface plot.
surfc(xx,yy,f_output,'MeshStyle','none') % This is 3D surface plot with solid edge & solid face colors.
xlabel('No. of Iterations') % Define x-axis label.
ylabel('Functions maximum value') % Define y-axis label.
zlabel('Global maximum function') % Define z-axis label.
title('Global maxima v/s Iterations') % Define title of a plot.
hold on % Stops & hold writing of commands till this point.
[inputs, fopt] = ga(@stalagmite_func,2); % Here genetic algorithm is defined outside "for" loop to put a hold on the looping of GA.
fxopt = inputs(1);
fyopt = inputs(2);
% Study1: Evaluation Stalagmite function as Genetic Algorithm without initial and final boundary conditions
% No. of Iterations
num_max = 50; % " Iterations are used to create a lot of samplings as GA is a set of random numbers. "
for i = 1:num_max % Here direct "num_max" is used as 1*1 matrix is available.
tic
[inputs, fopt(i)] = ga(@stalagmite_func,2); % This is a operating command for Genetic Algorithm as "[x,fval] = ga(fun,nvars)".
fxopt(i) = inputs(1); % Value of x is a function of GA as (i) in loop for 50 iterations.
fyopt(i) = inputs(2); % Value of y is a function of GA as (i) in loop for 50 iterations.
end
studytime1 = toc; % This is a time study for Study1.
figure(2) % This figure will give surface plot for Study 1.
subplot(2,1,1) % Divides the current figure into an m-by-n grid and creates axes in the position specified by p.
surfc(xx,yy,f_output,'Meshstyle','none')
shading interp % Varies the color in each line segment and face by interpolating the colormap index.
hold on
plot3(fxopt,fyopt,fopt,'Marker','o','Markersize',5,'MarkerFaceColor','r') % This create point markers in surface plot.
subplot(2,1,2)
plot(fopt) % Creates 2D plot for fopt.
title('Independent GA Plot')
xlabel('No. of Iterations')
ylabel('Minimum Function')
% Study2: Evaluation Stalagmite function as Genetic Algorithm with initial and final boundary conditions
% No. of Iterations
num_max = 50;
for i = 1:num_max
tic
[inputs, fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
% Stalagmite function is provided by Initial & Final boundary
% conditions as "[x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub)".
fxopt(i) = inputs(1);
fyopt(i) = inputs(2);
end
studytime2 = toc; % This is a time study for Study2.
figure(3) % This figure will give surface plot for Study 2.
subplot(2,1,1)
surfc(xx,yy,f_output,'Meshstyle','none')
shading interp
title('Global maxima v/s Iterations as Bounded')
hold on
plot3(fxopt,fyopt,fopt,'Marker','o','Markersize',5,'MarkerFaceColor','r')
axis([-0.2 0.6 -0.2 0.6 -1 0]) % Defines axis coordinates for figure 3.
subplot(2,1,2)
plot(fopt)
title('Dependent GA Plot')
xlabel('No. of Iterations')
ylabel('Minimum Function')
% Study3: Evaluation Stalagmite function as Genetic Algorithm with initial and final boundary conditions with Population size
% No. of Iterations
num_max = 50;
options = optimoptions('ga'); % Create optimization options and returns a set of default option for Solvername.
options = optimoptions(options, 'PopulationSize',300); % Create optimization options and returns options with the named parameters altered with the specified values.
for i = 1:num_max
tic
[inputs, fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
% Stalagmite function is provided by Initial & Final boundary
% conditions as "[x,fval] = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)".
fxopt(i) = inputs(1);
fyopt(i) = inputs(2);
end
studytime3 = toc; % This is a time study for Study2.
figure(4) % This figure will give surface plot for Study 3.
subplot(2,1,1)
surfc(xx,yy,f_output,'Meshstyle','none')
shading interp
title('Global maxima v/s Iterations as Bounded with Population size')
hold on
plot3(fxopt,fyopt,fopt,'Marker','o','Markersize',5,'MarkerFaceColor','r')
axis([-0.2 0.6 -0.2 0.6 -1 0])
subplot(2,1,2)
plot(fopt)
axis([0 50 -1 0])
title('Population Dependent GA Plot')
xlabel('No. of Iterations')
ylabel('Minimum Function')
Explanation:
Firstly the function is called in 'for loop' for defined values of x & y from the mesh grid. Output value for stalagmite function is 'f_output' and this value is in a loop so this matrix is giving random values in the loop and this gives an error which is sorted either by providing it zeros, any cell value, or other.
Similarly, we have to give the value for fxopt, fyopt, fopt....where opt is optimum. Now, this is plotted on the surface as figure 1.
Now creating 3 different studies for stalagmite function in form of a genetic algorithm. Study1 creates a basic GA in which we get random stalagmite coefficients. In Study1 no boundary condition is defined.
For Study2 we just define the boundary conditions as bounded regions and we get a limited range of value for GA. When we move further and define Study3, we provide population and optimization options for creating a proper output loop whenever the program is running.
Genetic Algorithm: For Matlab, GA is a set of random numbers with various parameters to follow. And each parameter gives a different optimized conclusion. Biologically these parameters work as mutation, crossover, and selection. In our coding, we used 3 GA functions:-
- [x,fval] = ga(fun,nvars)
- [x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
- [x,fval] = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
Here, fun = stalagmite function; nvars = no. of variables (2); A & b = linear inequalities (default : []) ; Aeq & beq = linear equalities (default = []) ; lb = lower bound (0,0 : initial boundary values); ub = upper bound (0.6,0.6 : final boundary values); nonlcon = non-linear constraints (default : [] ); IntCon = integer constraints (default : [] ); options = gives optimized output as provided before defining GA; x = inputs obtained during execution of a during iteration; fval = objective function value that give solutions.
- Stalagmite Function:
function[f_output] = stalagmite_func(f) % Creates a stalagmite function
x = f(1); % Provides the value of x from a for loop.
y = f(2); % Provides the value of y from a for loop.
fx1 = (sin(5.1*pi*x + 0.5))^6;
fx2 = exp(-4*log(2)*((x-0.0667)^2/(0.64)));
fy1 = (sin(5.1*pi*y + 0.5))^6;
fy2 = exp(-4*log(2)*((y-0.0667)^2/(0.64)));
f_output = -(fx1*fy1*fx2*fy2); % Formula for creating the stalagmite function.
end
This is a formula for stalagmite function :
- Command Window:
Elapsed time is 0.000714 seconds.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: maximum number of generations exceeded.
- Workspace:
Time evaluation for : Study1 = 0.0608; Study2 = 0.0611; Study3 = 0.2299. In Study1 we just provided basic formula for 'genetic algorithm', [x,fval] = ga(fun,nvars); now for Study2 we created lower and upper bound for the present GA formula, [x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub); but all above studies do not give perfect output for "stalagmite function", so to get perfect response from randomly terminated value of genetic algorithm we need to specify population and optimization options this is done in Study3, [x,fval] = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
- Plots For Various Studies:
" Stalagmite function with no genetic algorithm alternation ".
" This plot is driven by the help of "Genetic Algorithm", where GA is defined with no boundary & population condition ".
" In this plot, GA is defined with help of boundary condition which helps to manage the output values from GA to a small range of stalagmite sets ".
" This plot gives the perfect output for the genetic algorithm when population density is defined with optimization options ".
- Errors faced while Programming:
-
Here we used {} brackets instead of () brackets. The MatLab does not read the function {} as operand so it needs to be replaced by a suitable bracket structure.
Here the function is called "stalagmite_func" but for evaluating the GA function is approved by the wrong name "stalagmite" which is not the appropriate method. In all places for a single editor window, the calling of function needs to be the same.
REFERENCES:



