A brief explanation of GA is attached in a separate word documents
Stalagmite Function
function [f] = stalagmite(input_vector)
a = input_vector(1);
b = input_vector(2);
%f(x,y)=f1x.f1y.f2x.f2y
f1_x = (sin((5.1*pi*a)+0.5))^6;
f1_y = (sin((5.1*pi*b)+0.5))^6;
f2_x = exp(-4*log(2)*(((a-0.0667)^2)/(0.64)));
f2_y = exp(-4*log(2)*(((b-0.0667)^2)/(0.64)));
f = -(f1_x*f1_y*f2_x*f2_y);
% Now for maximizing the function a negative sign is used.
end
Main Program
clear all
close all
clc
%% Syntax for ga
% x=ga(fun,nvars,A,B,Aeq,Beq, lb, ub,nonicon,Intcon.options)
% X is the variable to store the output of the genetic algorithm.
% fun is the function handle to call the function(stalagmite)
% A and B are used to represent the linear inequalities of the function, empty in our case[]
% Aeq and Beq are used to represent the equality constraints similarly, empty in our case[]
% 'lb' and 'ub are used to specify the upper and the lower bounds of the variables of the function.
% ‘nonlcon’ represents the non-linear constraints.
% ‘Intcon’ represents the integer constraint, such that the value of the function is an integer.
% ‘options’ used to provide the value of the default parameters used named parameters, if required to replace the default parameters. This is done by using the ‘optimoptions’.
%% Generating X Y and Z(F)Coordinates for Stalagmite population
x = linspace(0,0.6,150); % generates 150 coordinates between 0 and 0.6
y = linspace(0,0.6,150);
[xx ,yy] = meshgrid(x,y); % Creating a 2d mesh
% Nested IF for creating the 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
% Plotting
figure(1)
surfc(xx,yy,-f);
shading interp
[inputs,fval] = ga(@stalagmite,2);
xlabel('x-axis')
ylabel('y-axis')
%% study1 - Statistical Behaviour
% we are introducing paramaeter num cases and GA to perform 50 times
% input ,fopt =ga(fun,nvars)
% nvars = no of variable
% Input = represents the value of parameter x and y
% fopt = is the least value of the function
tic
num_cases = 50; %for running the'ga'for 50 times
for i = 1:num_cases
[inputs1,fopt1(i)] = ga(@stalagmite,2);
xopt1(i) = inputs1(1);
yopt1(i) = inputs1(2);
end
study_time1 = toc;
mean1 = -(mean(fopt1))
% we have used subplot to plot the fopt value against the iterations
% performed
%plotting
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x-axis')
ylabel('y-axis')
zlabel('function')
shading interp
plot3(xopt1,yopt1,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded points')
subplot(2,1,2)
plot(fopt1)
xlabel('interations')
ylabel('function minimum')
% Conclusion is with no of iterations the plot looks more confusing and
% optimal points are not derived.
% tic and toc commands are used to study the time taken for this process.
%% Study2 - Statistical behavior with upper and lower bounds
% In study 2, we are introducing one more parameter upper bound and lower bound
%, when the Ub and Lb are given the crossover and mutation, takes place with
% in the bounds and helps GA to concentrate on the optimal solution
tic
for i = 1:num_cases
[inputs2,fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt2 = inputs2(1);
yopt2 = inputs2(2);
end
study_time2 = toc
%plotting
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x-axis')
ylabel('y-axis')
zlabel('function')
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded points')
subplot(2,1,2)
plot(fopt2)
xlabel('interations')
ylabel('function minimum')
% Conclusion is with no off iterations and LP/UP the plot looks to come to
% arrives at a nearer optimal solution
%% Study3 - Increasing GA interactions
% in study 3, we add the third parameter increased population and creates
% many new offsprings and help GA to find out the best solution
options = optimoptions('ga');
options = optimoptions(options,'populationsize',300);
tic
for i = 1:num_cases
[inputs3,fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt3 = inputs3(1);
yopt3 = inputs3(2);
end
study_time3 = toc
%plotting
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x-axis')
ylabel('y-axis')
zlabel('function')
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded points with increase population size')
subplot(2,1,2)
plot(fopt3)
xlabel('interations')
ylabel('function minimum')
% Conclusion - from the plot we see that the optimal solution arrives
% with very minimum variations and increasing the population from 170 to
% 300 gives better results and maintains the value.
%% overall conclusion is GA has to be used when we have more no complex variables.