Aim of the project:
Optimization of the stalagmite function using the genetic algorithm and to find out the global maxima
%% Description
%
% This is a program to maximize the Stalagmite function by using Genetic Algorithm and conducting its 3 studies.
%
%% Output
%
% Plot of Stalagmite function.
% Plot of all 3 studies.
% Maximized output of stalagmite function.
% Plot of maximum output vs no of iterations.
% Program
%% 1) Defining Range
x = linspace(0,0.6,150); % range of x
y = linspace(0,0.6,150); % range of y
[xx yy] = meshgrid(x,y); % creating 2D array of range
iterations = 40;
%% 2) Solving Stalagmite Function
% nested loop is run for the entire range to input each range value one by one.
for i = 1:length(xx)
for j= 1:length(yy)
input_vector(1) = xx(i,j); % picking values from xx matrix
input_vector(2) = yy(i,j); % picking values from yy matrix
sol(i,j) = stalagmite(input_vector); % calling the stalagmite function and proving inputs.
end
end
%% 2.1) Plotting Stalagmite Function
figure(1)
surfc(xx, yy, -sol); % plotting the stalagmite functn using the surfc command
shading interp % removing the shadings from the plot
xlabel('xx'); ylabel('yy');
title('Stalagmite Function')
%% 3) First Study (Unbound Inputs)
%
% use the for loop to run the itaerations and call the ga function every time
%
tic; % start the timer for first study
for i = 1:iterations; % start the loop
[sol1 fval1(i)] = ga(@stalagmite, 2); % calling ga function
% sol1 is the solution
% and fval1 is the function value
sol_x1(i) = sol1(1); % storing the value of x seperately
sol_y1(i) = sol1(2); % storing the value of y seperately
clc % clearing the command window after each iteration.
end
study1_time = toc; % stopping the timer
%% 3.1) Plotting First Study
figure(2)
subplot(2,1,1); % creating first half of the figure
plot3(-sol_x1, -sol_y1, -fval1,'- .','markersize',10,'color','r') % plotting the solution of first study
xlabel('x'); ylabel('y'); zlabel('function value'); % labelling the axes
title('Study 1'); % title of the plot
hold on % holding the next plots
surfc(-xx,-yy,-sol); % plotting the stalagmite function
shading interp; % removing the shading from the plot
legend('Solutions')
subplot(2,1,2); % creating second half of the figure
plot(fval1,'-o'); % plotting the function values
title('Iterations vs Function Value'); % title of the plot
xlabel('Iterations'); ylabel('Function Value'); % labelling the axes
legend('function values')
hold off
%% 4) Second Study (Bounded Inputs)
%
% use the for loop to run the iterations and also provide the lower and upper bounds
%
tic;
for i = 1:iterations % starting the timer
[sol2 fval2(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1]); % calling the ga function
sol_x2(i) = sol2(1);
sol_y2(i) = sol2(2); % storing x and y values seperately
clc % clearing command window on each iteration
end
study2_time = toc; % stooping the study timer
%% 4.1) Plotting Second Study
figure(3)
subplot(2,1,1); % generating the upper half of the figure
plot3(-sol_x2,-sol_y2,-fval2,'- .','markersize',10,'color','r'); % plotting the solution of the function
title('Study 2'); % title of plot
xlabel('x'); ylabel('y'); zlabel('z'); % labelling the axes
hold on
surfc(-xx, -yy, -sol); % plotting the stalgmite function
shading interp;
legend('Solutions') % inseting the required legends
subplot(2,1,2); % generating the lower half of the plot
plot(fval2,'-o'); % plotting the function values
xlabel('Iterations'); ylabel('Function Value'); % labelling the axes
title('Iterations vs Function Values'); % title of the plot
legend('function values') % inserting required legends
hold off
%% 5) Third Study (Bounded Inputs with Population Size')
tic; % starting the study timer
options = optimoptions('ga'); % initializing options
options = optimoptions('ga','PopulationSize',500); % proving options parameters using optimoptions function
% now ga will maximize the default optimization parameters replaced by values in options
for i = 1:iterations % starting the loop
[sol3 fval3(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1],[],options); % solving the stalagmite function
sol_x3(i) = sol3(1); % seperating x and y values
sol_y3(i) = sol3(2);
clc % clearing command window on each iteration
end
study3_time = toc; % stopping the timer
%% 5.1) Plotting the third Study
figure(4)
subplot(2,1,1); % generating the upper half of the plot
plot3(-sol_x3, -sol_y3, -fval3,'- .','markersize',15,'color','r'); % plotting the solution
xlabel('x'); ylabel('y'); zlabel('Function Value'); % labelling the axes
title('Study 3'); % title of the plot
hold on
surfc(-xx,-yy,-sol); % plotting th stalagmite function
shading interp;
legend('Solution') % inserting required legends
subplot(2,1,2); % generating lower half of the plot
plot(fval3,'-o'); % plotting the function values
xlabel('Iterations'); ylabel('Function Value'); % labelling the axes
title('Iterations vs Function Value'); % title of the plot
legend('function values') % inserting legendd
hold off
%% Displaying Soluton Outputs
disp('------------------------------Maximized Stalagmite Function------------------------------')
disp('Solution Values :') % displaying value of solution obtained after third study
disp(' ')
disp(sol_x3) % values of x (input_vector(1))
disp(' ')
disp(sol_y3) % values of y (input_vector(2))
disp('Function Values :')
disp(' ')
disp(fval3) % displaying function values
disp(' ')
disp('------------------------------Time taken for each study------------------------------')
fprintf('\nTime taken for first study = %f seconds\n\n',study1_time) % displaying time taken for all three studies
fprintf('Time taken for second study = %f seconds\n\n',study2_time)
fprintf('Time taken for third study = %f seconds\n\n',study3_time)
function [F] = stalagmite(input)
f1x = (sin(5.1*pi*input(1) + 0.5))^6;
f1y = (sin(5.1*pi*input(2) + 0.5))^6;
f2x = exp(-4*log(2)*(((input(1) - 0.0667^2)/(0.64))));
f2y = exp(-4*log(2)*(((input(2) - 0.0667^2)/(0.64))));
F = -(f1x * f1y * f2x * f2y);
end