Challenge

Week 4.1 - Genetic Algorithm

close all
clear all
clc
%input
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces=50;

First we initialise the values of x and y taking 150 values between the interval 0 and 0.6

We consider no of iterations num_spaces=50.

%forming 2d array
[xx,yy]=meshgrid(x,y);
for i=1:length(x)
for j=1:length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stalaglmite(input_vector)

Since we need to plot a 3D surface plot we take the 2d 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 another fuction file for the stalagmite function. here we take the ouput as -f since ga only minimises a given function.so if -f is considered it automatically gives us the maxima.

function A = stalagmite(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 =-(f1*f2*f3*f4);
end.

now let us make our first study-

we take no of iterations = num_spaces and we use the genetic algorithm function to caluclate the maxima of the function. we store the outputs into results which contain maximas and fopt which contain the value at which the maxima is obtained.

Here the ga is initiated with random population.

tic
for i=1:num_spaces
[results,fopt(i)]=ga(@stagalmite,2)
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time1=toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')

now we store the results into xopt, yopt.now we need to plot the surface of x,y,-f to get the 3D plot.

We use the plot3 command to make a plot of xopt,yopt and -fopt to plot the optimum maximas.

Also let us plot the fopt functio so that we can visualise the optimum values at each iteration.

To get the time taken to complete cycle we used the tic toc commands to record the time

If the study is executed the output is displayed as-

Here we can see the optimal values went outside the values of x and y resulting in falling away from stagalmite surface.

So we failed to reach the global maxima in this case.

the study time = 2.4997s

Now let us make our study having bounded values

let us specify the lower and upper bounds of x and y in this case,the ga is initiated with random population only.

for i=1:num_spaces
[results,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6])
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')

now we store the results into xopt, yopt.now we need to plot the surface of x,y,-f to get the 3D plot.

We use the plot3 command to make a plot of xopt,yopt and -fopt to plot the optimum maximas.

Also let us plot the fopt functio so that we can visualise the optimum values at each iteration.

To get the time taken to complete cycle we used the tic toc commands to record the time.

If the study is executed the output is displayed as-

Though the optimum values are within the limits it could not give us the exact maxima values.

The study time = 3.3602s which is more than above study.

NOW let us make our third study with initial population and bounded values-

in this case let us increase our initial population which is default as 50 to 250.

%study3- increased initial population
tic
options=optimoptions('ga')
options=optimoptions(options,'populationsize',250)
for i=1:num_spaces
[results,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time3=toc
figure(3)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')

Here in this case to set a initial population we need to use command called optimumoptions and get our value of 'populationsize' to 250.

now we store the results into xopt, yopt.now we need to plot the surface of x,y,-f to get the 3D plot.

We use the plot3 command to make a plot of xopt,yopt and -fopt to plot the optimum maximas.

Also let us plot the fopt functio so that we can visualise the optimum values at each iteration.

To get the time taken to complete cycle we used the tic toc commands to record the time.

If the study is executed the output is displayed as

-

though the time of execution is more i this case we are sucessful to find out the correct maxima for the stalagmite function.

So by increasing the initrial population size of the ga we can increase the accuracy of the algorithm.

The study time = 11.8696s which is more than above two studies.

OUTPUT-

All the explanations for the output graphs is given in the program explanation section individually.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.