Week 5 - Genetic Algorithm
STALAGMITE Function (Original)
function [output] = stalagmite (input_vector)
x_val = input_vector(1);
y_val = input_vector(2);
f1 = [sin(5.1*pi()*x_val + 0.5)]^6;
f2 = [sin(5.1*pi()*y_val + 0.5)]^6;
f3 = exp((-4 * log(2))*(((x_val-0.0667)^2)/(0.64)));
f4 = exp((-4 * log(2))*(((y_val-0.0667)^2)/(0.64)));
output = f1 * f2 * f3 * f4;
end
------------------------------------------------------------------------------------------------------------------------
Modified Stalagmite function to find the maxima
function [output] = stalagmite_max (input_vector)
x_val = input_vector(1);
y_val = input_vector(2);
f1 = [sin(5.1*pi()*x_val + 0.5)]^6;
f2 = [sin(5.1*pi()*y_val + 0.5)]^6;
f3 = exp((-4 * log(2))*(((x_val-0.0667)^2)/(0.64)));
f4 = exp((-4 * log(2))*(((y_val-0.0667)^2)/(0.64)));
%Output changed for 'ga' function to find the maxima as 'ga' function
%is designed for minima so by mulitplying the output values to negative
%-1 no change in value is done only the maxima will be termed as the
%minima and we will have the values at which the maxima occurs
output = f1 * f2 * f3 * f4 * (-1);
end
-------------------------------------------------------------------------------------------------------------------------
COMPLETE Matlab Code
clear all
close all
clc
%Defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%Creating a 2Dimensional mesh
[xx yy] = meshgrid(x,y);
%Evaluating the stalamite 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
%Plot a 3Dimensional Surface plot to visualise the maxima and minima of
%the stalagmite equation
surfc(xx,yy,f)
%label the x and y axis
xlabel ('x value');
ylabel ('y value');
%use it to remove the lines out of the plot
shading interp
%using the Genetic Algorithm to find the minima of the function
[inputs_minima, fval_minima] = ga(@stalagmite, 2);
%the output of the GA-Gentic Algorithm is the values of the inputs at which
%the value of the function will be minimum and fval give the value of the
%function at that point
%define inequaltiy constraints for x and y values
%A represents the a1 and a2 for equation a1*x + a2*y <= b
A = [1 1];
%We take value of b as less than or equal to 0.2 because we have limited
%the boundary for the maxima to be in the range for 0<x<0.1 and 0<y<0.1
b = [0.2];
%Define the equality constraints for x and y values
%we do not know the values so we will leave it blank
Aeq = [ ];
beq = [ ];
%Defining the lower boundary for the variable
lb = [0,0];
%Defining the upper boundary for the variables
ub = [0.1,0.1];
%we have changed the function from 'stalagmite' to 'stalagmite_max' as the
%function 'ga' accepts only the function handle
%And also the 'ga' function is designed to find the minima of the function
%and we want to find the maxima so we required to change the output of the
%function to negative so the maxima will become minima and we will be to
%able fidn the x and y values for the Maxima of the function using 'ga'
[inputs_maxima, fval_maxima] = ga(@stalagmite_max, 2,A,b,Aeq,beq,lb,ub);
inputs_maxima
fval_maxima = fval_maxima * -1
%Hold the Surface Plot to show the Maxima on the Plot for clarity
hold on;
%Using scatter function to leave a Maxima marker on the surface
h = scatter3(inputs_maxima(1),inputs_maxima(2),fval_maxima,'filled');
%Using the marker properties to turn the color to black
h.MarkerFaceColor = 'black';
%Defining the legend of the Plot for better visual understanding
legend('Surface for Values', 'Value Contours', 'Maxima');
--------------------------------------------------------------------------------------------------
The concept of the GA algorithm is to find the optimal solution or near optimal solution to the problem by search based optimisation technique.
For this the GA can choose the values for the variable in infinite multiples, so we restricted the function search by definig the upper and lower boundaries.
-------------------------------------------------------------------------------------------------
Results:


