Week 5 - Genetic Algorithm
PROGRAM:
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
num_cases = 50;
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 - Satistical behavior
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
hold on
surfc(x ,y, -f)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markerSize',5,'MarkerFaceColor','r')
title('Unbounded inputs')
subplot(2, 1, 2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function minimum')
tic
% Study 2 - Satistical behavior - with upper and lower bounds
for i = 1:num_cases
[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(x ,y, -f)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markerSize',5,'MarkerFaceColor','r')
title('Bounded inputs')
subplot(2, 1, 2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function minimum')
% Study 3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc;
figure(3)
subplot(2,1,1)
hold on
surfc(x ,y, -f)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markerSize',5,'MarkerFaceColor','r')
title('Bounded inputs')
subplot(2, 1, 2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function minimum')
function [f] = stalagmite(input_vector)
fx = (sin(5.1*pi*input_vector(1)+0.5))^6;
fy = (sin(5.1*pi*input_vector(2)+0.5))^6 ;
fxx = exp((-4*log(2)*(((input_vector(1)-0.0667)^2)/0.64)));
fyy = exp((-4*log(2)*(((input_vector(2)-0.0667)^2)/0.64)));
f = -(fx*fy*fxx*fyy);
endEXPLANATION:
STEP1
In step 1 i can enter the x and y values and also written the for loop program
(i.e)
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
num_cases = 50;
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
STEP 2
In this step 2 to written the Satistical behavior and their plots.
% Study 1 - Satistical behavior
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
hold on
surfc(x ,y, -f)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markerSize',5,'MarkerFaceColor','r')
title('Unbounded inputs')
subplot(2, 1, 2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function minimum')
tic
STEP 3
In this step 3 to written the Satistical behavior with upper and lower bounds and their plots.
% Study 2 - Satistical behavior - with upper and lower bounds
for i = 1:num_cases
[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(x ,y, -f)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markerSize',5,'MarkerFaceColor','r')
title('Bounded inputs')
subplot(2, 1, 2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function minimum')
STEP 4
In this step 4 to written the Satistical behavior and their plots
% Study 3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc;
figure(3)
subplot(2,1,1)
hold on
surfc(x ,y, -f)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markerSize',5,'MarkerFaceColor','r')
title('Bounded inputs')
subplot(2, 1, 2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function minimum')
STEP 5
We can the wrtitten the function coed of this program this function coad used to written the f values
(i.e)
function [f] = stalagmite(input_vector)
fx = (sin(5.1*pi*input_vector(1)+0.5))^6;
fy = (sin(5.1*pi*input_vector(2)+0.5))^6 ;
fxx = exp((-4*log(2)*(((input_vector(1)-0.0667)^2)/0.64)));
fyy = exp((-4*log(2)*(((input_vector(2)-0.0667)^2)/0.64)));
f = -(fx*fy*fxx*fyy);
end
ERRORS:
I had no errors faced in this program.
Genetic algorithm:
Generates a population of points at each iteration. The best point in the population approaches an optimal solution.Selects the next population by computation which uses random number generators.It is to find the maximum and minimum value.