clear all
close all
clc
%input variables
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_funct(input_vector);
end
end
%study 1-unbounded
tic
for i=1:num_spaces
[inputs,fopt(i)] = ga(@stalagmite_funct,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time1=toc;
figure(1)
subplot (2,1,1)
surf(x,y,-f)
xlabel('xaxis')
ylabel('yaxis')
shading interp
hold on
plot3(xopt, yopt,-fopt, 'marker', 'o','markersize',5,'MarkerFaceColor','r')
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_funct, 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)
xlabel('xaxis')
ylabel('yaxis')
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, 'population size',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)
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')
%stalagmite function code
function A = stalagmite_funct(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
Program Explanation,
Step 1- Start with usual Clear and closing command to clear memory than defines the search space with 2 variable x &y. For that, creates I-dimensional array for xy & use linspace command to provide 150 value between 0 to 0.6
After that creates 2-dimensional array based on our 1-dimension array) Step 2-in next step, by using for loop, define the Than am creating an array called input vectort input_vector2 (row vectors) for each value of xy and save these value in input_vector
Step 3-Now, to calculate maxima, am typing a new function stalagmite by using the below formula
in
f(x,y) = f1xf2xf1yf2y
f1x=[sin(5.1pix+0.5)]^6
f1y=[sin(5.1piy+0.5)]^6
f2x=[4In(2)(x-0.0667)^2 divided by 0.64]
f2y = [4In(2)(y-0.0667)^2 divided by 0.64]
And stored its value in GA' Function
Than this function data for plot called in for loop & passing the number of variable as input. So, that for loop will generate the data for plot Genetic Algorithm is actually based on Random sample, means it will start with random sample of inputs &
based on inputs we evaluate the function, than comes on some natural selection & do mates & also cross-over & than creates a new generation which is better. "But every time we run ga' code we get different results, which is not useful.
To get beneficial results am doing some changes in inputs and check compare the results. I am doing three Studies to get desired output(maxima)
Step 4
Study-Statistical Behaviour
After creating xy variable(input variable). as it does not define the search space for 'ga, go' does not know its actual space and the value for xy go beyond or
below to 0.6 or 0
For that use num cases=50, means am going to run it 50 times. Sub-Step 1-1 use for loop and run the code from to 50 times.
After that call ga & use the new function which created @stalagmite, pass 2 variable as input and store the output
In GA opt
And than store the value of xy in xopt & yopt
Sub-Step 2-Now to visualize my output, use surface command(surfc), shading command(shading interp) is used to get smooth plot(without grid lines) in new figure window(figurel), also use tic toc commond to calculate the time taken for evaluation I use subplot command for multiple plotting. plot to show stalagmite function, and plotting the minimum value that gar trying to predict, over 50 studies 2nd plot for showing the number of iteration at x-axis and during each iteration value changes which represents as Function maxima on y axis.