Maximising of Stalagmite function using GA
Genetic algorithm is a optimisation algorithm that is derived from nature\'s evolution theory.
A population of input parameters is generated randomly. These are inputs correspond to the chromosomes that are explained by darwin. These parameters are asessed and the most fit ones are chosen for further computation by applying these to a fitness function. Next is the selection process.The ones that are more fit have higher probability of being picked. These picked ones then undergo crossover in order to obtain new ones. Sometimes the offspring gene is flipped and mutation happens. This helps keep variety in the pool. Each of these have a binary value. This completes one iteration of the genetic algorithm. This then repeats several number of times until the solution satisfies the minimum criteria.
A negative sign is added at the end of this code as the genetic algorithm function in MATLAB provides the minimum criteria value. Hence to obtain a maximum value the output has to be flipped/negated.
%genetic algorithm function code
clear all;
close all;
clc;
% Setting up grid co-ordinates
x = linspace(0, 0.6, 150);
y = linspace(0, 0.6, 150);
[xx,yy] = meshgrid(x,y);
S = zeros(size(xx));
% Stalagmite function vlaue for grid co-ordinates
for i = 1:length(xx)
for j = 1:length(yy)
input_vector(1) = xx(i, j);
input_vector(2) = yy(i, j);
S(i, j) = stalagmite(input_vector);
end
end
% Plotting Stalagmite function
figure(1);
hold on;
surfc(xx, yy, S);
shading interp;
% Finding optimized value
j = 100; % Number of iterations
optim_val = zeros(j, 1);
optim_para = zeros(j, 2);
for i = 1:j
[optim_para(i, :), optim_val(i)] = ga(@stalagmite, 2, [], [], [], [], [0; 0], [0.6; 0.6]);
end
figure(2);
i = [1:j];
figure(2);
plot(i, -(optim_val\'));
xlabel(\'Iteration\');
ylabel(\'Stalagmite Function value after each GA run\');
figure(1);
% plot3
plot3(optim_para(:,1), optim_para(:,2),optim_val(:), \'LineWidth\', 3, \'MarkerSize\', 10);
% Negative stalagmite value function
function [ fxy ] = stalagmite(vector)
x = vector(1);
y = vector(2);
f1x = (sin(5.1 * pi * x + 0.5))^6;
f1y = (sin(5.1 * pi * y + 0.5))^6;
f2x = exp((-4) *log(2) * ((x - 0.0667)^2/0.64));
f2y = exp((-4) *log(2) * ((y - 0.0667)^2/0.64));
fxy= -(f1x * f2x * f1y * f2y);
% This produces value which corresponds to negative value of stalagmite function
end