Challenge

Week 4.1 - Genetic Algorithm

function [S] = stalagmite(input_vector)
%input vector will be the complete workspace need to be assigned before
%running or while running the function
x = input_vector(1);
y = input_vector(2);

%complete stalagmite function equation is divided into 4 parts.
f1_x = (sin(5.1*pi*x + 0.5))^6;
f1_y = (sin(5.1*pi*y + 0.5))^6;
t1 = ((x-0.0667)^2)/(0.64);
f2_x = exp(-4.*log(2)*t1);
t2 = ((y-0.0667)^2)/(0.64);
f2_y = exp(-4.*log(2)*t2);
S = -(f1_x.*f2_x.*f1_y.*f2_y);
end
clear
close all
clc
%search space or input 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[X,Y]=meshgrid(x,y); %meshgrid form the 2D grid coordinates base on linear coordinates
num_cases=50; 
%numcases = Number of itreation for which the function will evaluate the fitness
for i=1:length(X)
for j=1:length(Y)
input_vector(1)=X(i,j);
input_vector(2)=Y(i,j);
f(i,j)=stalagmite(input_vector);

%stalagmite function is called with input vector as serch space
end
end
%plotting 
surfc(X,Y,-f); %generating the surface
shading interp 
[inputs, fval]=ga(@stalagmite,2)

xlabel('x values')
ylabel('Y values')
zlabel('foptimal')

clear
close all
clc
%search space or input 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[X,Y]=meshgrid(x,y); %meshgrid form the 2D grid coordinates base on linear coordinates
num_cases=50; 
for i=1:length(X)
for j=1:length(Y)
input_vector(1)=X(i,j);
input_vector(2)=Y(i,j);
f(i,j)=stalagmite(input_vector);
end
end
%% 
tic %calculating the study time through tic and toc command
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2); %calculating the optimized value of the function
xopt(i)=inputs(1); %at each iteration
yopt(i)=inputs(2);
end
study1_time=toc %displaying the time taken to evaluate
% PLOTING THE GRAPH
figure(1)
subplot(2,1,1)
surfc(X,Y,-f);
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
xlabel('x');
ylabel('y');
zlabel('fopt');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('iterations')
ylabel('function min value')


%% 
tic %calculating the study time through tic and toc command
for j=1:num_cases
[inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); %calculating the optimized value of the function
xopt(j)=inputs(1); %at each iteration with boundation
yopt(j)=inputs(2);
end
study2_time=toc
%PLOTTING
figure(2)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'markerfacecolor','r');
xlabel('x');
ylabel('y');
zlabel('fopti');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('iterations')
ylabel('function min value')

%%
%optimoptions function
options=optimoptions('ga');
options= optimoptions(options, 'PopulationSize',170);

tic
for k=1:num_cases
[inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc
%PLOTTING
figure(3)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
xlabel('x');
ylabel('y');
zlabel('fopt');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('iterations')
ylabel('function min value')
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.