close all
clear all
clc
Xinput
xalinspace(0,0.6,150);
yalinspace(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.yylemeshgrid(x,y);
for ist:length(x) for jat:length(y)
Input vector(1)=xx():
input vector(2) syy(L): 1() stalogimite(input_vector)
Since we need to plot a 3D surface plot we takes 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 stalogmite we create another fuction file for the stalagmite function, here we take the ouput as -f since go only minimises a
given function so it-t is considered it automatically gives us the maxima function A = stalagmite(input_vector)
xwinput vector(1):
y=input_vector(2); nu(sin(5.1 pix+0.5)).A6: 12-exp((-4°log(2)-((x-0.0607).^2))/0.64);
tam(sin(5.1 pi*y+0.5)).^6:
14nexp((-4 log(2))*((y-0.0667).^2)/0.64):
A=-(11-12-13-14);
end.
now let us make our first study
we take no of iterations num spoces and we use the genetic algorithm function to coluciote the maxima of the function, we store the outputs into resulta which contain maximas and topt which contain the value of 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_timel=toc
figure(1)
subplot(2,1,1)
surf(x,y,-1)
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 xy-f to get the 3D plot.
We use the plot3 command to make a plot of xoptyopt 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 stalgmite
surface.
So we failed to reach the global maxima in this case.
the study time 2.49975)
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.
surface.
for ist:num_spaces
[results, fopt()]=ga(@stalgmite,2.[][].[][].[0:0].[0.6:0.6])
xopt(i)=results (1) yopt()=results(2)
end
study time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-1)
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 xy-f to get the 3D plot.
Wo use the plot3 command to make a plot of xopt,yopt and-fopt to plot the optimum maximas,
Also lot 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
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(x)
for j=1:length(y)
input_vector(1)=xx(i, j);
input_vector (2)=yy(i, j);
f(i,j)=stalgamite(input_vector);
end
end
%study 1-unbounded
tic
for i=1:num_spaces
[results, fopt(i)]=ga (@stalgamite, 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, 'marken', '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(@stalgamite, 2,[1,1],[1,1],[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')
study3- increased_initial_population
tic
options= optimoptions('ga')
options= optimoptions(options, populationsize,250)
for i=1:num_spaces
[results, fopt(i)]= ga(@stalgamite, 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')