Challenge

Week 4.1 - Genetic Algorithm

Aim:

To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

Program;

 clear all
close all
clc

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

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

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

% Statistical Behaviour

tic

for k = 1:n
    [inputs,fopt(k)] = ga(@stalagmite,2);
    xopt(k) = inputs(1);
    yopt(k) = inputs(2);
end
study1_time = toc

figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('Statistical Behaviour with Unbound inputs')
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Iterations')
ylabel('Function maximum')

% Statistical Behaviour with upper and lower bounds

tic
for k= 1:n
    [inputs,fopt(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(k) = inputs(1);
    yopt(k) = inputs(2);
end
study2_time = toc
 
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('Stastistical behaviour with upper and lower bounds')
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Iterations')
ylabel('Function maximum')

% Statistical Behaviour with increasing iterations of GA

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);

tic
 for k=1:n
     [inputs,fopt(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
     xopt(k) = inputs(1);
     yopt(k) = inputs(2);
 end
 study3_time = toc

 figure(3)
 subplot(2,1,1)
 hold on
 surfc(xx,yy,-f)
 shading interp
 xlabel('X-axis')
 ylabel('Y-axis')
 title('Statistical Behaviour with increasing iterations')
 plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('No.of iterations')
 ylabel('Function maximum')

Results:

% Statistical Behaviour

% Statistical behaviour with upper and lower bounds

% Statistical behaviour with increasing iterations of GA

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.