Challenge

Week 4.1 - Genetic Algorithm

close all
clear all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces=50;
[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)=stalagmite(input_vector);
   end
end   
% study 1 for 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)
surf(x,y,-f)
shading interp
hold on 
plot(xopt,yopt,-fopt,'marker','o')
plot(-fopt)
xlabel('iteration')
ylabel('maximum function')

%study2 for boumded
tic
for i=1:num_cases
[results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time2=toc
figure(2)
surf(x,y,-f)
shadind interp
hold on
plot(xopt,yopt,-fopt,'marker','o')
plot(-fopt)
xlabel('iteration')
ylabel('maximum function')
%study3 increased population
tic
options=optiomoptions('ga')
options=optiomoptions(options,'populationsize',250);
for i=1:num_spaces
  [results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
  xopt(i)=results(1)
  yopt(i)=results(2)
end
study_time3=toc
figure(3)
surf(x,y,-f)
shadind interp
hold on
plot(xopt,yopt,-fopt,'marker','o')
plot(-fopt)
xlabel('iteration')
ylabel('maximum function')

First we have given the values of x and y by taking the limits 0 to 0.6 with 150 values between them.Also considered the number of iterations is 50.Inorder to plot 3D surface,we have taken the 2D array of x and y by using mesh grid command.Then we create another function file for stalagmite function,the output of the function was taken as -f because the ga only minimises the function.

for the first study case of unbounded region,the results were stored in xopt,yopt.The plot3 command was used to plot xopt,yopt,-f.The tic toc commands were used to record the time taken to complete the cycle.

For the study2 of bounded region the lower and upper bounds of x and y were specified.Also the results were stored in xopt,yopt.The plotting was done by using plot3 command.

Also for the study3 we have done the same with increased population and specified bounds.
 






 











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.