Challenge

Week 4.1 - Genetic Algorithm

% Writting the stalagmite function 

% stalagmite function 
function [Sw] = stalagmite(input)
x = input(1);
y = input(2);

p1 = (5.1*pi*x)+0.5;
p2 = (5.1*pi*y)+0.5;
q1 = ((x-0.0667).^2);
q2 = ((y-0.0667).^2);

term1 = (sin(p1)).^6;
term2 = exp((-4*log(2)*(q1))/0.64);
term3 = (sin(p2)).^6;
term4 = exp((-4*log(2)*(q2))/0.64);

Sw = -(term1*term2*term3*term4)
end
% Program for optimization of stalagmite function 


% use of genetic algorithm 

% in order to start the function we'll assume two variables 

x = linspace(0,1,150);
y = linspace(0,1,150);

% creating a 2D Array out of given input variables 

[xx yy] = meshgrid(x,y);

for i = 1:length(xx)
    for j = 1:length(yy)
    
    theta(1) = xx(i,j);
    theta(2) = yy(i,j);
    
    F(i,j) = stalagmite(theta);
    end
end

% consider that we want to run the program for 50 time so for that 

num_case = 50;

% study one 
% unbounded limits for x and y  

tic 
for i = 1:num_case;
    [coordinates,fvel(i)] = ga(@stalagmite,2)
    x_opt(i) = coordinates(1)
    y_opt(i) = coordinates(2)
end

study_time1 = toc
figure(1)
subplot(2,1,1)
surf(x,y,F)
shading interp
colormap 'jet'
colorbar
shading interp 
xlabel('x-axis')
ylabel('y-axis')
hold on 
plot3(x_opt,y_opt,fvel,'marker','o','MarkerSize',5,'MarkerEdgeColor','r')
title('Bounded_inputs')
subplot(2,1,2)
plot(fvel)
xlabel('iteration')
ylabel('maixmum')

% study two
% bounding the limits for x and y  
tic 
for i = 1:num_case;
    [coordinates,fvel(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
    x_opt(i) = coordinates(1)
    y_opt(i) = coordinates(2)
end

study_time2 = toc
figure(2)
subplot(2,1,1)
surf(x,y,F)
shading interp
colormap 'jet'
colorbar
shading interp 
xlabel('x-axis')
ylabel('y-axis')
hold on 
plot3(x_opt,y_opt,fvel,'marker','o','MarkerSize',5,'MarkerEdgeColor','r')
title('Bounded_inputs')
subplot(2,1,2)
plot(fvel)
xlabel('iteration')
ylabel('maixmum')

% study three
% Incressing the popullation size and bounding the limits for x and y  
options = optimoptions('ga')
options = optimoptions(options,'PopulationSize',200)
tic 
for i = 1:num_case;
    [coordinates,fvel(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)
    x_opt(i) = coordinates(1)
    y_opt(i) = coordinates(2)
end

study_time3 = toc
figure(3)
subplot(2,1,1)
surf(x,y,F)
shading interp
colormap 'jet'
colorbar
shading interp 
xlabel('x-axis')
ylabel('y-axis')
hold on 
plot3(x_opt,y_opt,fvel,'marker','o','MarkerSize',5,'MarkerEdgeColor','r')
title('Bounded_inputs')
subplot(2,1,2)
plot(fvel)
xlabel('iteration')
ylabel('maixmum')
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.