Maximizing the Stalagmite Mathematical model using Genetic Algorithm
Objective
1. To optimize a stalgamite mathematical model
2. To find the global maxima of the following model using Genetic Algorithm
Theory
Genetic Algorithm(GA):
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population \"evolves\" toward an optimal solution.
You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.
GAs follow the basic outline methods to achieve the optimal solution to a given specie which also points out towards the principle \"Survival of the fittest\" quoted by Charles Darwin which is based on the following foundation rules.
- successfull individuals competing over each other produces more offsprings than the one who havve performed poor.
- genes from good individuals propogate throughout the whole population which results in production of more high quality offsprings which can suit the environment.
- therefore,this increases the quality of the each coming generation hence making them more suited to the environment.
Code for Genetic Algorithm
- It starts with creating random initial populations.
- The algorithm then creates a set of new individuals by using individuals from the present population with every step basically by using Selection,Crossover & Mutation.
- In total a set of 3 studies are executed in the code.
- Study 1: Shows the statistical behviour of GA with 50 itterations.
- Study 2: Shows the statistical behaviour of GA when upper and lower bounds are defined with 50 itterations.
- Study 3: Shows the statistical behaviour of GA when population size of 300 is defined and number of iterations is increased to 100.
Due to the nature of GA taking random values the solution of a given function may not be the same each and every time and varies with itterations and populations.As in the figure it\'s easily visible that in the first 2 cases the maximum value of the function is varying with the itterations. However in the third case after increasing the population size and itterations the maximum value starts approaching to a single value.
In the end the Maximum value of Global Maxima is displayed using the disp command and is always obtained as the Global maxima after all the multiple successfull iterations.
Negative of the function has been taken to find the global maxima of the function since the GA aims and finds the Global Minima of the function by default.
clear all
close all
clc
%defining our work space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating a 2D mesh
[xx yy] = meshgrid(x,y);
%evaluating 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
surfc(f)
xlabel(\'X axis\');
ylabel(\'Y axis\');
shading interp
[inputs,f_val] = ga(@stalagmite,2)
num_cases = 50;
tic
%study 1 - Statistical behavior
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite, 2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
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 minimum\')
hold off
tic
%study 2 - Statistical behavior - with upper and lower bounds
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1,1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
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 minimum\')
hold off
%study 3 - Statistical behavior - increasing GA iterations
num_cases = 100;
options = gaoptimset(\'ga\');
options = gaoptimset(options,\'populationSize\', 300);
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
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 minimum\')
hold off
f = (-(fopt));
h = max(f);
disp(h);
function [fx_y] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1_x = (sin((5.1*pi.*x)+0.5)).^6;
f1_y = (sin((5.1*pi.*y)+0.5)).^6;
f2_x = exp(-4*log(2).*(((x-0.0667).^2)/0.64));
f2_y = exp(-4*log(2).*(((y-0.0667).^2)/0.64));
fx_y = -(f1_x.*f1_y.*f2_x.*f2_y);
end