Challenge

Week 4.1 - Genetic Algorithm

code:-

%defining our search premises

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

%creating 2D dimensional mesh

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

%evaluation of stalegmite 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


%solving stalagmite function for 2 unknown variables and get local maximas
[inputs,fval] = ga(@stalagmite,2);
x_out = inputs(1);
y_out = inputs(2);
xlabel('x-axis')
ylabel('y-axis')

surfc(xx,yy,-f)
shading interp

%case 1
%Finding GA for limited no. of iterations without any specific boundaries
a_int = 40;

for i=1:a_int
    [inputs,f1_out(i)] = ga(@stalagmite,2);
    x1_out(i) = inputs(1);
    y1_out(i) = inputs(2);
end

figure(1)
subplot(2,1,1)
hold on
surf(xx,yy,f)
shading interp
plot3(x1_out,y1_out,f1_out,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(f1_out)
xlabel('iterations')
ylabel('function minimas')

%case2 (finding ga for limited no.of iterations with specific boundaries)
tic
for i = 1:a_int
    [inputs,f2_out(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
    x2_out(i) = inputs(1);
    y2_out(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,f)
shading interp
plot3(x2_out,y2_out,f2_out,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded inputs')
subplot(2,1,2)
plot(f2_out)
xlabel('iterations')
ylabel('function minimas')

%case 3 (finding ga for limited no. of iterations with specific boundaries
%and specific population)
%using stoping criteria to give us precised results

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',200);
tic
for i = 1:a_int
    [inputs,f3_out(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    x3_out(i) = inputs(1);
    y3_out(i) = inputs(2);
end
study3_time = toc

figure(3)
subplot(2,1,1)
hold on
surf(xx,yy,f)
shading interp
plot3(x3_out,y3_out,f3_out,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded inputs with controlled population size')
subplot(2,1,2)
plot(f3_out)
xlabel('iterations')
ylabel('function minimas')

Function:-

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

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

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

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

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

f = -f1_x*f2_x*f1_y*f2_y;

Q&A

What is genetic algorithm?
A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

Five phases are considered in a genetic algorithm.

-Initial population
-Fitness function
-Selection
-Crossover
-Mutation
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.