Stalagmite function optimization using Genetic Algorithim
This program optimises the stalagmite function to find the global maximum of the function using genetic algorithm optimization.
Genetic algorithim (ga) is a inbuilt minimization funtion in MATLAB and Octave. This function works on the natural selection theory of evolution. This function starts with a random population size and then selects parents based on fitness values of each individual. Fitness values are evaluated using the fitness function which is taken as one of the inputs in ga. These parents are then used to produce off springs using cross over operator. This operator combines the solution of parents to generate a new solution (off spring) which is usually mutated before adding to the population. This process is repeated untill an optimum (minimum) solution is reached or the number of iteration specified by user are complete.
The syntax for ga used in MATLAB is as follows:
where
- x = local minimum
- fun = objective function
- nvars = number of variables in objective function
- A,b = linear inequalities such that A*x<=b
- Aeq,beq = liner equality such that Aeq*x = beq
- lb,ub = lower and upper bounds for the solution
- nonlcon = non linear constraints
- options = optimization options
clear all
close all
clc
% defining search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 40; % number of times ga runs
% creating a 2D mesh
[xx yy] = meshgrid(x,y);
% stalagmite function
function [output] = stalagmite(input_vector)
a = input_vector(1);
b = input_vector(2);
f1x = (sin(5.1*pi*a+0.5))^6;
f1y = (sin(5.1*pi*b+0.5))^6;
f2x = exp(-4*log(2)*(a-0.0667)^2/0.64);
f2y = exp(-4*log(2)*(b-0.0667)^2/0.64);
[output] = -(f1x*f2x*f1y*f2y);
end
% 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
% study 1 : statistical behaviour
tic
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\')
xlabel(\'X values\')
ylabel(\'Y values\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
% study 2 : statistical behaviour with upper and lower bounds
tic
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(\'Bounded inputs\')
xlabel(\'X values\')
ylabel(\'Y values\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
% study 3 : increasing GA population size
options = optimoptions(\'ga\');
options = optimoptions(options,\'PopulationSize\',450);
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(\'Bounded inputs with different population size\')
xlabel(\'X values\')
ylabel(\'Y values\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
References:
1. https://in.mathworks.com/help/gads/ga.html