Challenge

Week 4.1 - Genetic Algorithm

PROGRAM
clear all
close all
clc
%input
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces=50;
%forming 2nd 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-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)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'Marker','0')
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,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)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','0')
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(@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','0')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
STALAGMITE FUNCTION 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
PROGRAM EXPLANATION
clear all
close all
clc
%input
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces=50;
here we start with taking the values of x&y between the interval 0 & 0.6 about 150 values & number of iteration is 50.
%forming 2nd 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);
we need to plot a 3d surface plot.we take the 2nd array of x&y using meshgrid command and store them in input vector array,so that it canbe used as an arguement to call the function stalagmite.
here we create another file for stalagmite function.
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
now we can make our first study,here ga is initiated with random population
%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)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'Marker','0')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
now we store the results in to xopt,yopt
for i=1:num_spaces
[results,fopt(i)]=ga(@stalagmite,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)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','0')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.