Optimise Stalagmite function and find global maxima using GA
Objective
> Concept of genetic algorithm and syntax for ga in Matlab.
> Code to optimise the stalagmite function and find the global maxima of the function.
Genetic algorithm
Genetic Algorithms(GAs) are adaptive heuristic search algorithms that belong to the larger part of evolutionary algorithms. Genetic algorithms are based on the ideas of natural selection and genetics. These are intelligent exploitation of random search provided with historical data to direct the search into the region of better performance in solution space. They are commonly used to generate high-quality solutions for optimization problems and search problems.
Genetic algorithms simulate the process of natural selection which means those species who can adapt to changes in their environment are able to survive and reproduce and go to next generation. In simple words, they simulate “survival of the fittest” among individual of consecutive generation for solving a problem. Each generation consist of a population of individuals and each individual represents a point in search space and possible solution. Each individual is represented as a string of character/integer/float/bits. This string is analogous to the Chromosome.
Syntax of GA
>x = ga(fun,nvars) - finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
').Aeq*x').x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)function [f] = stalagmite(ip_vector)
x = ip_vector(1);
y = ip_vector(2);
f1 = (sin(5.1*pi*x+0.5)).^6;
f2 = exp(-4*log(2)*((x-0.0667).^2)/0.64);
f3 = (sin(5.1*pi*y+0.5)).^6;
f4 = exp(-4*log(2)*((y-0.0667).^2)/0.64);
f = -(f1*f2*f3*f4);
% Optimization_using_Genetic_algorithm
close all
clc
% Inputs
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Convertng into 2D array
[xx,yy] = meshgrid(x,y);
% Function call
for i =1:length(xx)
for j = 1:length(yy)
ip_vector(1) = xx(i,j);
ip_vector(2) = yy(i,j);
f(i,j) = stalagmite(ip_vector);
end
end
% Plotting stalagmite func
surfc(xx,yy,-f)
shading interp
xlabel('x values')
ylabel('y values')
title('Stalagmite Function')
% ------------------------------------------------------------------------------
% Study 1 - Statistical behaviour
tic
iter = 50;
for i = 1:iter
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
Studytime1 = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title('Unbounded Inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')
% ------------------------------------------------------------------------------
% Study 2 - With upper and lower bounds
tic
for i = 1:iter
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
Studytime2 = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title('UB & LB Inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')
% ------------------------------------------------------------------------------
% Study 3 - Increasing GA Iterations
tic
options = optimoptions('ga');
options = optimoptions(options,'populationsize',250)
for i = 1:iter
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
Studytime3 = toc
figure(4)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title('Optimized Solution')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')




