Challenge

Week 4.1 - Genetic Algorithm

STALAGMITE FUNCTION :

close all 
clear 
clc

% Defining our search space 
x = linspace(0,0.6,350);
y = linspace(0,0.6,350);

% Creating a 2Dimensional 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

surfc(xx,yy,-f)

shading interp

[x, fval] = ga (@stalagmite,2)

STALAGMITE FUNCTION FORMULA :

function [f] = stalagmite(input_vector)
f1X = (sin((5.1*pi).*input_vector(1) + 0.5)).^6;
f1Y = (sin((5.1*pi).*input_vector(2) + 0.5)).^6;
f2X = exp(((input_vector(1)-0.0667).^2)/0.64 .*((-4*log(2))));
f2Y = exp(((input_vector(2)-0.0667).^2)/0.64 .*((-4*log(2))));
f =-(f1X*f1Y*f2X*f2Y);
end

linspace is said to be one dimentional array

meshgrid is also used to be two dimentional array

evaluating stalagmite function

where for loop is used with i and j as input vector of the function

input vector 1 and 2 is said to be an array of function which is said to be row vector

a statement is f (i,j) = stalagmite is used to determine a function.

that function is called stalagmite function where the function is used to run a set of formulas to get the values for the graphical represntation.

after the values is obtained and graph and plotting

we use the command surfc which is used to determine the graph without any linegrid in the graph

sharding interp is used to show the plot in a smoother and clean shape

at last GA is used to minimizes the function and in the function there are two inputs one is stalagmite function and another one is said to be variables are in the function to run.

GA SYNTAX :

ga = (@stalagmite, 2) is a syntax of the function. where the first one is said to be objective of the function and second one is said to be number of dimentions to the function.

STUDIES :

close all 
clear 
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
f = get_stalagmite(x,y);

tic
% Study 1 - sastistical behavior
for i = 1:num_cases
    [inputs, fopt(i)] = ga(@stalagmite,2);
    xopt(i)= inputs(1);
    yopt(i)= inputs(2);
end

study1_time = toc

figure(1)
subplot(2,1,1)
hold on
surfc(x, y, f)
grid on
shading interp
plot3 (xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

tic
% Study 2 - staticitical behavior - with upper and lower bounds
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)= inputs(1);
    yopt(i)= inputs(2);
end
study2_time = toc
figure (2)
subplot(2,1,1)
hold on
surfc (x,y,f)
shading interp
plot3 (xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

tic
% Study 3 - Increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);
tic
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3 (xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Bounded inputs with modified population size')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
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.