Challenge

Week 4.1 - Genetic Algorithm

1] Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

clear all
close all
clc

%Input
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces=50;

%forming 2d array
[xx,yy]=meshgrid(x,y);
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-unbounded
tic
for i=1:num_spaces
[results,fopt(i)]=ga(@stalagmite,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')

%Study 2- bounded
tic
for i=1:num_spaces
[results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[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')

%Study3- increased initial population
tic
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',250)
for i=1:num_spaces
[results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[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')

Function

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

1. As always we start with clearing all data, Here we start with initializing the values of x and y taking 150 values in between 0 to 0.6, No. of iterations are considered 50.

2. Since we need to take a 3D plot, Here 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.

3.By creating another file for the stalagmite function, we take the output as the '-f'. As GA only minimizes the given function. In that case when we are using '-f' it gives a maxima.

4.Here we take no. of iterations= num_space and we use the genetic algorithm function to calculate 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.

5.GA is initiated with random values.Next step is to store the results into xopt,yopt. Now, we need to plot the surface of x,y,-f to get the plot.

6.We use plot3 command to make a plot of xopt,yopt and -fopt where we plot optimum values. Also the value of fopt function is plotted so that we can visualize the optimum values at each iteration.

7.To get the time taken to complete cycle we use the tic toc command to record the time.

Study(1)

The two of the gievn pictures shows the output of first study where we can see that the optimum values of x and y went outside the maximum limit.

Study time is shown in the picture.

Study(2)

Here we don't fail as our values are in between the upper and lower (x,y) limit.

Study(3)

Here also the values of x and y are in between the lower nad upper limits.

ERRORS ENCOUNTERED

Here error is due to spelling mistake which is corrected.

Here, the error is due to stalagmite function is not defined

Here the path of the file folder was different.

Here the 'populationsize' is used in wrong way as it should be , 'PopulationSize'.

Concept of Genetic Algorithm and syntax is explained in the document attached.

References-

1.https://www.geeksforgeeks.org/genetic-algorithms/

2.https://en.wikipedia.org/wiki/Genetic_algorithm

3.Reference of the programs written for optimization

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.

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.