Challenge

Week 4.1 - Genetic Algorithm

clear all
close all
clc
x = linspace(0 ,0.6, 150) ;
y = linspace(0 ,0.6, 150) ;
num_cases = 50 ;

[a, b] = meshgrid( x , y) ;  % 2D array

for i = 1 : length(a)   ;
    for j = 1 : length(b)  ; 

c(1) = a(i,j) ;
c(2) = b(i,j) ; 

f(i,j) = stalagmite_function(c) ;  

    end
end
    

tic

for i = 1 :num_cases

    [inputs , fopt(i)] = ga(@stalagmite_function, 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) 
shading interp
plot3(xopt,yopt,-fopt,'marker','o' ) 
subplot(2 ,1 ,2)
plot(-fopt)
xlabel('iterations')
ylabel('minima')
title('unbounded inputs')
tic

for j = 1 :num_cases

    [inputs , fopt(j)] = ga(@stalagmite_function, 2, [], [], [], [], [0;0], [0.4;0.4]) ; 
    xopt(j) = inputs(1) ;
    yopt(j) = 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' ) 
subplot(2 ,1 ,2)
plot(-fopt)
xlabel('iterations')
ylabel('minima')
title('bounded inputs')

tic

options = optimoptions('ga') ;
options = optimoptions(options , 'populationsize' , 200) ; 


for k = 1 :num_cases

    [inputs, fopt(k)] = ga(@stalagmite_function , 2 , [] , [] , [] , [] , [0  0] , [0.6 0.6] , [] , [] , options) ; 
    xopt(k) = inputs(1) ;
    yopt(k) = 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' ) ;
subplot(2 ,1 ,2)
plot(-fopt)
xlabel('iterations')
ylabel('minima')
title('bounded inputs')



% Function code

function [output] = stalagmite_function(c)
x = c(1) ;
y = c(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) ) ;

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

end 

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.