Week 4.1 - Genetic Algorithm


Genetic_algorithem-code
%inputs
x =linspace(0,0.6,150);
y =linspace(0,0.6,150);
%creating a 2Dimensional 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)= stagalmite(input_vector);
end
end
surfc(xx,yy,f)
shading interp
[input_vector,fval]=ga(@stagalmite,2)
Stagalmite-code
function A=stagalmite(input_vector)
x=input_vector(1);
y=input_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);
A =1/(1+(f1*f2*f3*f4));
EXPLANATION OF THE CODING:
*The program starts with clear all,close all and clc commands.
*First we initialise the values of xad y taking values between the interval 0 and 0.6
*We consider no of iteration num_spaces=50
*Since we need to plot a 3D surface plot we take the 2nd array of x and y using meshgrid command and store them in
input_vector array,so that it can be used as an argument to call the function stalagmite.
*We create a another function file for the stalagmite function.
*Here we take the outut values as -f since ga only minimises a iven function .
*So if -f is considered it automatically gives us the maxima.
a