To calculate the global maxima of a stalagmite function
AIM
To understand the concept of genetic algorithm and to optimise the stalagmite function and to find the global maxima of the stalagmite function.
THEORY
It is a search based optimisation technique based on the principle of natural selection. It searches for the optimal solution in the solution space. It starts with a random population of the solution and at each iteration reproduction of the new solution is done to optimise the solution.
GA FLOW CHART
STALAGMITE FUNCTION
PROGRAM TO FIND THE STALAGMITE FUNCTION
% program to calculate the global maxima of a stalagmite function
clear all
close all
clc
% defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50; %number of iterations to run ga i.e 50
%to create 2Dimensional mesh
[xx,yy] = meshgrid(x,y);
%FOR loop for evaluating or calling 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_function(input_vector);
end
end
tic
% study 1 - statistical behaviour
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite_function,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc;
%plotting
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5, 'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
tic
%study 2 - statistical behaviour - with upper and lower bounds
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc;
%plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5, 'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
%study 3 - increasing GA iterations
options = optimoptions ('ga');
options = optimoptions(options,'populationsize',280);
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time3 = toc;
%plotting
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5, 'markerfacecolor','r')
title('bounded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
stalagmite function code
%for calling a stalagmite funciton
function[f] = stalagmite_function(input_vector)
x1 = input_vector(1);
y1 = input_vector(2);
f1_x = (sin((5.1*pi*x1)+0.5))^6 ;
f1_y = (sin((5.1*pi*y1)+0.5))^6 ;
f2_x = exp((-4*(log(2))*(x1-0.0667).^2)/0.64);
f2_y = exp((-4*(log(2))*(y1-0.0667).^2)/0.64);
f = -(f1_x*f1_y*f2_x*f2_y); %-ve sign for finding global maximum
end
STEPS
1. firstly we use linspace command to create 150 values from 0 to 0.6.
2. We use meshgrid command to evaluate the functions of two variables and three dimensional plots.
3. We use the stalagmite function for different iterations.
4. We use the ga function to define the stalagmite function for various iterations.
5. We measure the time for each of 50 iterations and the optimum value is obtained.
OUTPUTS