Challenge

Week 4.1 - Genetic Algorithm

% WRITING CODE FOR STALAGMITE FUCTION. SINCE THIS IS ALREADY IN MATLAB SO
% JUST CALLING IT BY FUNCTION 'STALAGMITE'
% STALAGMITE IS USED TO FIND LOCAL MINIMA FRON FICTION WITH DEFINED RANGE
% OF POPULATION SIZE

function func = stalagmite(input)
x = input(1);
y = input(2);
fx1 = (sin(((5.1*pi)*x)+0.5))^6;
fx2 = exp((((x-0.0667)^2)*(-4*log(2)))/0.64);
fy1 = (sin(((5.1*3.14)*y)+0.5))^6;
fy2 = exp((((y-0.0667)^2)*(-4*log(2)))/0.64);
func = -((fx1)*(fx2)*(fy1)*(fy2));
end

clear all
close all
clc
% DEFINING SIZE AND VALUE OF INPUT
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx, yy]  = meshgrid(x,y);
cases = 50
% RUNNING FOR LOOP FOR GETTING LOCAL MINIMA USING STALAGMITE FUCTION FOR
% DESIRED VALUES OF X AND Y CONSIDERING 150 SAMPLES IN EACH
for i=1:length(xx)
    for j=1:length(yy)
        input(1)=xx(i,j);
        input(2)=yy(i,j);
        f(i,j) = stalagmite(input);
    end
end

% CASE 1 
% TIC TRIGGER THE TIME RECOREDING AND 'TAC' END IT, THIS HELPS US IN
% UNDERSTANDING HOW MUCH TIME TAKE FOR RUNNING THE ALGORITHM
% BELOW CODE WILL OPTIMIZE THE 50 TIMES TIMES THE FUNCTION AND GET X OPT
% AND Y OPT VALUE. THIS IS BECAUSE INITALLY WE TOOK 150 SAMPLES FROM THE
% ONLY ONE LOCAL MINIMA FUCTION VALUE WILL BE DRAWN @X,Y. SO THIS FASHION
% IS REPEATED 50 TIME AND  F,X,Y OPTIMIZED VALUE IS FOUND
tic
for k = 1:cases
 [input, fopt(k)] = ga(@stalagmite, 2);
 x_opt1(k) = input(1);
 y_opt1(k) = input(2);
end
% STALAGMITE GIVES MINIMA WHEREAS WE NEED TO KNOW MAXIMA HENCE WE WILL TAKE
% OPPOSITE VALUE TO PLOT. THIS OPPOSITE VALUS IS NOTTHING BUT NEGATIVE
% VALUE OF FUNCTION AND INTURN THE LOCAL MAXIMA VALUE
f = -f
fopt = -fopt

% PLOTTING THE REULTS
time_study1 = toc;
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x_opt1,y_opt1,fopt,'markersize',5,'markerfacecolor', 'b')
title('Unbounded inputs 1')
subplot(2,1,2)
plot(fopt,'color','b', 'linewidth' , 1)
xlabel('Iterations')
ylabel('Function Maximum')

% CASE 2
tic
for k = 1:cases
 [input2, fopt2(k)] = ga(@stalagmite, 2);
 x_opt2(k) = input2(1);
 y_opt2(k) = input2(2);
end

fopt2 = -fopt2
time_study2 = toc;
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x_opt2,y_opt2,fopt2,'markersize',5,'markerfacecolor', 'b')
title('Unbounded inputs 2')
subplot(2,1,2)
plot(fopt2,'color','b', 'linewidth' , 1)
xlabel('Iterations')
ylabel('Function Maximum')


% CASE 3
tic
for k = 1:cases
 [input3,fopt3(k)] = ga(@stalagmite, 2);
 x_opt3(k) = input3(1);
 y_opt3(k) = input3(2);
end

fopt3 = -fopt3
time_study3 = toc;
figure(3)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(x_opt3,y_opt3,fopt3,'markersize',5,'markerfacecolor', 'b')
title('Unbounded inputs 3')
subplot(2,1,2)
plot(fopt3,'color','b', 'linewidth' , 1)
xlabel('Iterations')
ylabel('Function Maximum')


% CASE 4
%as we have taken 4 cases for running the optimization further more we are using optmioptions (in built function in the matlab ) and define global population size of 200 samples randomly. Hence matlab pick these samples and do the optimization.
%This eventually produces the Global Maxima

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

tic
for k=1:50
    [inputs4, fopt4(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1], [],[],options);
    x_opt4(k) = inputs4(1);
    y_opt4(k) = inputs4(2);
end
fopt4 = -fopt4
time_study = toc;
figure(4)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(x_opt4,y_opt4,fopt4,'markersize',5,'markerfacecolor', 'b')
title('Unbounded inputs 4')
subplot(2,1,2)
plot(fopt4,'color','b', 'linewidth' , 1)
xlabel('Iterations')
ylabel('Function Maximum')
rng('default')
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.