Week 4.1 - Genetic Algorithm
1.The code is starts with clear all close all clc.
2.Then the x array and y array is defined using linspzce command to create values at equal intervals.
3.A 2d mesh of x and y is created using meshgrid command and the number of iterations or cases that the code should perform is defined.this os given by n=50.
4.To evealute the stalagmite function for loop is used
5.The input vector values of xxand yy are defined and the stalagmite function for these input vector is defined.T his will form a basic stalagmite function and we can plot it seperately if needed.
6.After preparing the inputs needed for the function ,next step is to obtain the results for the 3 cases
7.BY using GA syntax ,it is used to give values for inputs and optimum max value.
8.to know the time taken to solve the loop for 50 iterations TIC and TOC command is used.
9.Then the function created in the loop is plotted by using surfc command for xx,yyand function output.
1o.The plot3 command plote the 3d points at xopt yopt and optimum value of function with o shaped and red color.
11.The resulte will be different for each time we run the program.
12.The syntax for GA contains option term and the rest of the code is same as for study1 and 2.
clear all
close all
clc
% define our search space
x=linspace(0,0.6,150); %x array
y=linspace(0,0.6,150); %y array
n=50;
%creating 2d mesh
[xx yy]=meshgrid(x,y);
%evaluatng a 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=statistical behaviour
tic
for i=1:n
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('statistical behaviour with upload inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('no of iterations')
ylabel('function maximum')
%study2:stastical behaviour with the upper and lower bounds
tic
for i=1:n
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('statistical behaviour with upload inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No of iterations')
ylabel('Function maximum')
%study3 stastical behaviour with increasing iterationsof GA
options=optimoptions('ga');
options=optimoptions(options,'populationsize',300);
tic
for i=1:n
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
for i=1:n
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('statistical behaviour with upload inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('no of iterations')
ylabel('function maximum')