Week 4.1 - Genetic Algorithm
clear all
close all
clc
% Inputs
x = linspace(0,0.8,180);
y = linspace(0,0.8,180);
num_spaces = 60;
% Forming 2d array
[xx,yy] = meshgrid(x,y);
for i=1:length(x)
for j=length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stalagmite(input_vector);
end
end
% Study 1-unbounded
tic
for i=1:num_spaces
[results,fopt(i)]=ga(@stalagmite,2)
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time1 = toc;
figure(1)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','*')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
title('Unbounded')
% Study 2-bounded
tic
for i=1:num_spaces
[results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8])
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','*')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
title('Bounded')
% Study 3-inreased initial population
tic
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',240)
for i=1:num_spaces
[results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8],[],[],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','*')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
title('Increased Initial Population')
stalagmite code
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.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
Concep of Genetic Algorithm
It is a method to solve constrained and unconstrained optimization problems.it repeatedly modifies a population of solution.
In programming I used linspace for defineing range between 0 to 0.8 in 180 parts.
num_space code is used to make 60 iteration od given data set.
Meshgrid command is used to plot the graph.
stalagmite function is desiged according to the quadratic equation given.
We make 3 studies in program that are unbounded,bounded and increased initial population.
for that ga cammand is used , ga syntax eplain as , in ga first we call stalagmite function then no. variables in function after that square bracket means ther eis no any linar constrained is exist.Then it defines the set of lower and upper bound of design variables.
While plotting we give each graph specific title , xlabel and ylabel.