Challenge

Week 4.1 - Genetic Algorithm

OPTIMIZING A STALAGMITE FUNCTION USING GENETIC ALGORITHM

Main body of the MATLAB code:

% Evaluating a stalagmite function using Genetic Algorithm

close all
clear all
clc

% Defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
counter = 50; % Variable for the for loop

% Creating a 2D mesh
[xx yy] = meshgrid(x,y);

% Evaluating the stalagmite function
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

% Plot of the stalagmite function
figure(1)
surfc(x,y,f) 
shading interp
title('Stalagmite function')
xlabel('X-Values')
ylabel('Y-Values')
zlabel('Function output')

% Evaluating the inverse stalagmite function
for i = 1:length(xx)
    for j = 1:length(yy)
        input_vector(1) = xx(i,j);
        input_vector(2) = yy(i,j);
        f_inv(i,j) = stalagmite_inv(input_vector);
    end
end

% Plot of the inverse stalagmite function
figure(2)
surfc(x,y,f_inv)
shading interp
title('Inverse Stalagmite function')
xlabel('X-Values')
ylabel('Y-Values')
zlabel('Function output')

%% Study 1 - Evaluating the Stalagmite function using Genetic Algorithm

tic
for i = 1:counter % For loop for obataining the optimum values for 50 different iterations
    [minima,fval(i)] = ga(@stalagmite_inv,2);
    xopt(i) = minima(1);
    yopt(i) = minima(2);
end
Study1_time = toc % Calculating the processing time 

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f_inv)
shading interp
plot3(xopt,yopt,fval,'Marker','O','MarkerSize',5,'MarkerFaceColor','r') % Creating a 3D plot of the calculated optimum values
title('Unbounded function')
xlabel('X-Values')
ylabel('Y-Values')
zlabel('Function output')
subplot(2,1,2)
plot(fval) % Plotting the fitness values of the function for different iterations
xlabel('Iterations')
ylabel('Fitness values')

%% Study 2 - Evaluating the stalagmite function using GA with lower and upper bounds
% Defining the bounds
lb = [0 0];
ub = [0.6 0.6];

tic
for i = 1:counter
    [minima,fval(i)] = ga(@stalagmite_inv,2,[],[],[],[],lb,ub);
    xopt(i) = minima(1);
    yopt(i) = minima(2);
end
Study2_time = toc

figure(4)
subplot(2,1,1)
hold on
surfc(x,y,f_inv)
shading interp
plot3(xopt,yopt,fval,'Marker','O','MarkerSize',5,'MarkerFaceColor','r')
title('Bounded function')
xlabel('X-Values')
ylabel('Y-Values')
zlabel('Function output')
subplot(2,1,2)
plot(fval)
xlabel('Iterations')
ylabel('Fitness values')

%% Study 3 - Evaluating the stalagmite function using ga with bounds and modified population size
% Setting the new population size
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',400); % By default population size is 50

% Upper and Lower bounds
lb = [0 0];
ub = [0.6 0.6];

tic
for i = 1:counter
    [minima,fval(i)] = ga(@stalagmite_inv,2,[],[],[],[],lb,ub,[],[],options);
    xopt(i) = minima(1);
    yopt(i) = minima(2);
end
Study3_time = toc

figure(5)
subplot(2,1,1)
hold on
surfc(x,y,f_inv)
shading interp
plot3(xopt,yopt,fval,'Marker','O','MarkerSize',5,'MarkerFaceColor','r')
title('Bounded function with modified population size')
xlabel('X-Values')
ylabel('Y-Values')
zlabel('Function output')
subplot(2,1,2)
plot(fval)
axis([0 50 -1 -0.9])
xlabel('Iterations')
ylabel('Fitness values')

Function 1 - stalagmite

% Stalagmite function

function [x] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);

f_1x = [sin(5.1*pi*x + 0.5)]^6;
f_1y = [sin(5.1*pi*y + 0.5)]^6;

f_2x = exp(-4*log(2)*(((x - 0.0667)^2)/0.64));
f_2y = exp(-4*log(2)*(((y - 0.0667)^2)/0.64));

[x] = (f_1x * f_2x * f_1y * f_2y);
end

Function 2 - stalagmite_inv

% Inverse Stalagmite function

function [x] = stalagmite_inv(input_vector)
x = input_vector(1);
y = input_vector(2);

f_1x = [sin(5.1*pi*x + 0.5)]^6;
f_1y = [sin(5.1*pi*y + 0.5)]^6;

f_2x = exp(-4*log(2)*(((x - 0.0667)^2)/0.64));
f_2y = exp(-4*log(2)*(((y - 0.0667)^2)/0.64));

[x] = -(f_1x * f_2x * f_1y * f_2y);
end

Report: https://drive.google.com/file/d/1XvgDksZLKLTVwZPFVQ7582-WuLYMF3wn/view?usp=sharing

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.