Challenge

Week 4.1 - Genetic Algorithm

CODE :

Function code :

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

f1_x = (sin((5.1*pi*x)+0.5)).^6;

f1_y = (sin((5.1*pi*y)+0.5)).^6;

f2_x = exp(((-4*log(2))*(x-0.0667).^2)/0.64);

f2_y = exp(((-4*log(2))*(y-0.0667).^2)/0.64);

[f] = -(f1_x.*f1_y.*f2_x.*f2_y);

end


Main code :

clear all

close all

clc

%Defining our search space

x = linspace(0,0.6,150);

y = linspace(0,0.6,150);

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

num_cases = 80 ;

%calling 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

%Study 1 - Statistical behavior

tic

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(xx,yy,f)

shading interp

title('Unbound inputs')

xlabel('X values')

ylabel('Y values')

plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')

subplot(2,1,2)

plot(fopt)

xlabel('Iterations')

ylabel('Function maximum')

%Study 2 - Statistical behavior with upper and lower bounds

tic

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(xx,yy,f)

shading interp

title('Bounded inputs')

xlabel('X values')

ylabel('Y values')

plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')

subplot(2,1,2)

plot(fopt)

xlabel('Iterations')

ylabel('Function maximum')

%Study 3 - Increasing GA iterations

options = optimoptions('ga')

options = optimoptions(options,'populationSize',200)

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(xx,yy,f)

shading interp

xlabel('X values')

ylabel('Y values')

plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')

subplot(2,1,2)

plot(fopt)

xlabel('Iterations')

ylabel('Function maximum')

STEPS :

The code starts with defining x and y as inputs using linspace

A function known as Stalagmite_func is defined as hown in the 2nd stage of codes.

2 for loops are incorporated in the main code so that all the combinations of x and y values is taken in the stalagmite_func

function

In the 1st study GA function runs for 80 iterations. Plots of the stalagmites and maximum values generated in each iteration

is shown.

In the 2nd study GA function is incorporated with lower and upper bounds and runs for 80 iterations. Plots of the stalagmites

and maximum values generated in each iteration is shown.

In the 3rd study GA function is incorporated with lower and upper bounds along with the population size of 200. Plots of the

stalagmites and maximum values generated in each iteration is shown.

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.