Week 4.1 - Genetic Algorithm
genitical algoritham is an matametical operation used to determine the maximum values of the functions
there are the inputs are x,y and num-case,
x = lispace(0,0.6,150)
y=linspace(0,0.6,150)
num_case = 50
the linspace commad used to determine the xand y values between 0 to 0.6 thwre are 150 equal interval values
for making 2d array, the meshgrid is (x,y)
i is the length of x and j is the length of y and the j is the sum of vector 1&2
vectors 1&2 is the , input_vector(1)=xx(i,j)
input_vector(2)=yy(i,j)
j=stalagmite (input_vector)
we can studing the first statistical behavior
the fopt of the input is genitic algoritham @stalagmite 2
(input,fopt(i))=ga(@stalagmite, 2)
xopt and yopt are the input of i of 1&2
xopt(i)=input(1)
yopt9i)=input(2)
for making the plot for the figure one
sub plot is (2,1,1)&(2,1,2) and the surf is (x,y,f)
plotin the figure 1 is ,
plot3(xopt,yopt,fopt,'marker','o',markersize',5,markerfacecolor','r')
we can be studing the second statistica behaviour
the plot of the input is genetic algorithm and the xopt and uopt are the inputs of i at 1&2
for giving the tic &toc commad to second study time
for making the plot 2
subplot and surf are same for the figure one
plot command is ,
plot3(xopt,yopt,fopt,'marker',markersize',5,'markerfacecolor','r')
xlabel is interation and ylabel is function is minimum
we can be studying the ga interation with increasng
options=optimoptions(ga)
options=optimoptions(options,'populationsize',250)
that is increasd the population to 250
and providing xopt,yopt and fopt
for making the plot3
all are same to making the plot of figure 1&2
plot39xopt,yopt,fopt,marker','o',markersize,5,markerfacecolor,'r')
providing the x7y labels
clear all
close all
clc
%input
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 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) = stalagmite(input_vector)
end
end
%study 1 satistical behaviour
tic
for i = 1:num_case
[input,fopt(i)] = ga(@stalagmite, 2)
xopt(i)=input(1);
yopt(i)=input(2);
end
study_timel = toc
figure(1)
subplot(2,1,1)
surf(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fopt)
xlabel('lterations')
ylabel('Function Minimum')
%study 2_statistical behaviour with upper and lower bomds
tic
for i =1:num_case
[inputs,fopt(i)] = ga(@stalagmite, 2);
xopt(i) = input(1);
yopt(i) = input(2);
end
study_time2=toc
figure(2)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'Marker','.','Markersize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fopt)
title('iterations')
xlabel('iterations')
ylabel('function mimimum')
%study 3 increasing ga iterations
tic
options = optimoptions('ga');
options = optimoptions(options,'populationsize',250);
for i = num_case
[inputs,fopt(i)]=ga(@stalagmite, 2)
xopt(i) = input(1);
yopt(i) = input(2);
end
study_time3=toc
figure(3)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bouned input wisth')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('Functon Minimum')
FUNCTION OF STALGMITE
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.06670).^2))/0.64);
f3 = (sin(5.1*pi*y+0.5)).^6;
f4 = exp((-4*log(2)*((y-0.06670).^2))/0.64);
A = (f1*f2*f3*f4)
end