Challenge

Week 4.1 - Genetic Algorithm

AIM : to find maxima of the function and get familiar with GENETIC ALGORITHM(ga)

clear all 
close all
clc

%defining our search space where here 0 to 0.6 is divided in 150
%parts/numbers.
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%this is provided so that iteration or loop is executed for 50 times
numcases = 50 ;
%calling our function
% f = get_stalagmite(x,y);
%meshing the two matrix x and y
[xx, yy ] = meshgrid(x , y);

%evaluating the stalagmite 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

% study1 in this no bounds are provided which means here  ga(@stalagmite,2)
% function only says that stalagmite function has 2 varaibles and the loop
% is executed for values between o to 0.6 50 times this leads different
% output every time.
tic 
for j = 1:numcases
    [inputs , fopt1(j)] = ga(@stalagmite,2);
    xopt1(j) = inputs(1);
    yopt1(j) = inputs(2);
end
study_time1 = toc
ok1 = fopt1
figure(1)
%subplot is used to divide our sapce in two parts so that two graphs are
%visible at same time, subplot(2,1,1) here 2 stands for two rows 1 stands
%for one coloumn ,this will divide it into two parts and the last 1 stands
%for position of our 1st plot .
subplot(2,1,1)
surfc(xx , yy ,f)
shading interp
hold on 
plot3(xopt1,yopt1,fopt1,'marker','o','markersize',5,'markerfacecolor','r')
%subplot(2,1,2) here 2 stands for two rows 1 stands
%for one coloumn ,this will divide it into two parts and the last 2 stands
%for position of our 2nd plot .
subplot(2,1,2)
plot(-fopt1)
title('UNBOUNDED INPUTS')
xlabel('iterations') %labelling
ylabel('maximum')

%study2 to overcome the 1st study and to narrow down the search space for
%function the upper and lower bounds are provided so that variety in the
%output can me reduced although this method too does not give same output every time 
%the bounds provided must be less then actual search space.
tic 
for k = 1:numcases
    [inputs2 , fopt2(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4]);
    xopt2(k) = inputs(1);
    yopt2(k) = inputs(2);
end
study_time2 = toc
ok2 =  fopt2
figure(2)
subplot(2,1,1) %dividing the plot as before
surfc(x , y ,f)
shading interp
hold on 
plot3(xopt2,yopt2,fopt2,'marker','o','markersize',7,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt2)
xlabel('iterations')
ylabel('func maxima')

%study3 here options are used to minimize the prameters and new values are
%added from options at first options = optimoptions('ga') declaration of
%options for ga and now we have to set new properties i.e population size
%for options so this is decided on no. of numcases we had into 100 i.e
%numcases*100 = 500. the empty 4 brackets acutally stand for   HybridFcn,
%InitialPopulationMatrix ,InitialPopulationRange,InitialScoresMatrix and
%two brackets after the bounds are for OutputFcn,PlotFcn.
%in this case the no. of cases has been increased to get steady output.                         
        
options = optimoptions('ga');
options = optimoptions(options,'populationSize',500);
tic 
for k = 1:150
    [inputs2 , fopt3(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);
    xopt3(k) = inputs(1);
    yopt3(k) = inputs(2);
end
study_time3 = toc
ok3 = fopt3
figure(3)
subplot(2,1,1)%dividing the plot as before
surfc(x , y ,f)
shading interp %makes the 3d plot look more asthetic
hold on 
%plot3 is used  to plot 3d plot
plot3(xopt3,yopt3,fopt3,'marker','o','markersize',7,'markerfacecolor','r')
title('bounded inputs inc GA')
subplot(2,1,2)
plot(-fopt3)
xlabel('iterations')
ylabel('func maxima')

%tic toc command is used to know the time taken by each study in seconds. 



==================================================================================


function [f] = stalagmite(x,y)
m =x;
n =y;

f1x = ((sin((5.1.*pi.*m) + 0.5)).^6);
f2x = exp(((-4).*log(2).*((m - 0.0667).^2))/0.64);
f1y = ((sin((5.1.*pi.*n) + 0.5)).^6);
f2y = exp(((-4).*log(2).*((n - 0.0667).^2))/0.64);

f = -(f1x.*f1y.*f2x.*f2y);


end


====================================================



function [f] = stalagmite(input_vector)
m = input_vector(1);
n = input_vector(2);

f1x = ((sin((5.1*pi*m) + 0.5))^6);
f2x = exp(((-4)*log(2)*((m - 0.0667)^2))/0.64);
f1y = ((sin((5.1*pi*n) + 0.5))^6);
f2y = exp(((-4)*log(2)*((n - 0.0667)^2))/0.64);

f = -(f1x*f1y*f2x*f2y);


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.