% Program no -1 Main Program
%Writing a program to perform optimization of a stalagmite function(F) to determine the global maximum of F and
%find its corresponding x and y values
close all
clear all
clc
%assigning the range or search space for the function to consider the following values
x = linspace(0,0.6,150)
y = linspace(0,0.6,150)
[xx, yy] = meshgrid(x,y) %creates a matrix representation out of the x and y values
%Determining the value of stalagnite function-F by calling it and
%also using for-loop,where the values of x and y are taken in the form of a
%row matrix in every iteration.
for i= 1:length(xx)
for j=1:length(yy)
inputs(1) = xx(i,j);
inputs(2) = yy(i,j);
F(i,j) = stalagmite_function(inputs);
end
end
%plotting a 3D surface graph which plots the stalagmite function value at various x and y values ,
%and Z axis representing the value of F
figure(1)
surfc(xx,yy,-F) %to obtain surface contour graph
shading interp %to obtain a smooth surface with coloured graphics
xlabel('x value')
ylabel('y values')
title('stalagmite function value F')
%Obtaining the optimised value using Genetic algorithm in MATLAB through studying three different cases
%case 1 - statistical behaviour without mentioning any bounded limits
%using for-loop
tic %tic toc is a inbuilt function which reports the time elapsed
n = 50 %no. of iterations
for k= 1:n
[inputs1,Fval_opt(k)]= ga(@stalagmite_function ,2);
xopt1(k) = inputs1(1);
yopt1(k) = inputs1(2);
end
case_1_time = toc
%to plot 3D surgace graph along with plotting marker points to represent the first case optimised set of values
%of x and y as well as optimised function- F value
figure(2)
subplot(2,1,1) %command to have two different graphs in one figure window
hold on
surfc(xx,yy,-F)
shading interp
xlabel('x values')
ylabel('y values')
plot3(xopt1,yopt1,-Fval_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('case 1 without any bounded inputs')
subplot(2,1,2)
plot(-Fval_opt) %to have 2d representation of F value against iterations
xlabel('no of iterations')
ylabel('function max')
% case 2 - statistical behaviour considering the bounded conditions with upper and lower bounds
% assigned.Since in this case more values are filtered and a narrowed set
% of optimised values are obtained when compared to case 1.
tic
for l = 1 : n
[inputs2,Fval_opt2(l)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.4,0.4]);
xopt2(l) = inputs2(1);
yopt2(l) = inputs2(2);
end
case_2_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-F)
shading interp
xlabel('x values')
ylabel('y values')
plot3(xopt2,yopt2,-Fval_opt2,'marker','o','markersize',5,'markerfacecolor','r')
title('case 2 with bounded inputs')
subplot(2,1,2)
plot(-Fval_opt2)
xlabel('no of iterations')
ylabel('function max')
%case 3 - Along with specifying bounds ,the population size is also
%increased.Generally by default due to random samples ,the GA takes 50 as
%the number of samples but it is increased to 200 which is nothing but the
%increased population size.With this the single set of optimised values are
%obtained
options=optimoptions('ga') %returns a set of default options for the ga solver.
options=optimoptions(options,'populationsize',200);%returns a copy of oldoptions with the named parameters
% altered with the specified values
tic
for m = 1: n
[inputs3,Fval_opt3(m)]= ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);
xopt3(m) = inputs3(1);
yopt3(m) = inputs3(2);
end
case_3_time=toc
figure(4)
subplot(2,1,1)
surfc(xx,yy,-F)
shading interp
title('case 3 with bounded limits and increased population size')
xlabel('x values')
ylabel('y values')
hold on
plot3(xopt3,yopt3,-Fval_opt3,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-Fval_opt3)
xlabel('no of iterations')
ylabel('function max')
%Program no 2 -Function code
%A seperate Program for the stalagmite function(F) to use it in finding global maxixma through Genetic Algorithm
%defining the function ,output being F
function [F] = stalagmite_function(inputs)
%defining the inputs , the inputs are two variables in a row vector
x = inputs(1);
y = inputs(2);
%defining the various terms of the mathematical expression of the function
f1X = (sin((5.1*pi*x)+0.5))^6 ;
f2X = (sin((5.1*pi*y)+0.5))^6;
f1Y = exp((-4*log(2))*(((x-0.0667)^2)/0.64));
f2Y = exp((-4*log(2))*(((y-0.0667)^2)/0.64));
F = -(f1X * f2X * f1Y * f2Y);
end