Challenge

Week 4.1 - Genetic Algorithm

clc;
close all;
clear all;

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

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

 
figure(1)
tic
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)=stalgmite(input_vector);
    end
end
study_1=toc
surfc(xx,yy,-f)
shading interp


figure(2)
iter=30;
 for i=1:iter
 [inputs,f_opt(i)]=ga(@stalgmite,2);
 x_opt(i)=inputs(1);
 y_opt(i)=inputs(2);
 end
 subplot(2,1,1)
 hold on
surfc(xx,yy,-f)
shading interp 
plot3(x_opt(i),y_opt(i),-f_opt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-f_opt)
title('unbounded inputs')
xlabel('iterations')
ylabel('Function maximum')


% study 2 statistical behaviour with upper and lower bounds

figure(3)
tic
for i=1:iter
 [inputs,f_opt(i)]=ga(@stalgmite,2,[],[],[],[],[0;0],[0.6;0.6]);
 x_opt(i)=inputs(1);
 y_opt(i)=inputs(2);
end
study_2=toc
 subplot(2,1,1)
 hold on
surfc(xx,yy,-f)
 shading interp 
plot3(x_opt(i),y_opt(i),-f_opt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-f_opt)
title('bounded inputs')
xlabel('iterations')
ylabel('Function maximum')

% study 3 statistical behaviour with increasing ga iteration



options=optimoptions('ga','populationsize',150);
tic

figure(4)
for i=1:iter
 [inputs,f_opt(i)]=ga(@stalgmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
 x_opt(i)=inputs(1);
 y_opt(i)=inputs(2);
end

study_3=toc;

 subplot(2,1,1)
 hold on
surfc(xx,yy,-f)
 shading interp 
plot3(x_opt(i),y_opt(i),-f_opt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-f_opt)
title('bounded inputs with modified population size')
xlabel('iterations')
ylabel('Function maximum')

function [f] =stalgmite(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


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.