Week 5 - Genetic Algorithm
clc
clear all
close all
%%
% GA is the concept by which we solve the optimization problem copying the
% process of natural selection. GA attempts to generate the best solution
% considering operation such as mutation,cross-over and selection.
%%
% ga is used to find mimima or loacal minima. So in our example is used
% negative that will give us maxima.
% % Syntax
% x = ga(fun,nvars)
%
% x = ga(fun,nvars,A,b)
%
% x = ga(fun,nvars,A,b,Aeq,beq)
%
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
%
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
%
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
%
% x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
%
% x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
%
% x = ga(problem)
%
% [x,fval] = ga(___)
%
% [x,fval,exitflag,output] = ga(___)
%
% [x,fval,exitflag,output,population,scores] = ga(___)
%%
% We created a function with name stalagmite.
%%
% Defining our search space
% Linspace command is used to generate or return a rpw vector with equal
% spacing upto 150. Or we can say it generates a row vector of 1*150 with
% diff of 0.6.
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
num_samples= 50; % It runs the program for a number of time that is 50.
%%
%creating 2d program
% As we need a 3d surface plot we take 2d array of x and y using meshgrid
% command.
[xx,yy]=meshgrid(x, y);
%%
% Evaluating Stalagmite function
% for command is used to run iteration for a specific number of time.
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
%%
%we take no of iterations = num_samples and we use ga func to caluclate the
%maxima of the function. outputs are stored into results which containS
%maximas and fopt which contain the value at which the maxima is obtained.
%Here the ga takes random population.
%Command tic and toc is used to calculate the study time for 3 different
%cases.
% Study(1) statistical Behaviour
tic
for i=1:num_samples
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time= toc;
figure(1)
subplot(2,1,1);
% Subplot command is used to plot two diff grph in same figure.
hold on
surf(xx,yy,-f);
shading interp % to remove the lines produced by the mesh grid
xlabel('xdata');
xlabel('ydata');
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
%This command is used to create 3d line plots.
title('Unbounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
% From the graph obtained we can see we are not able to obtain the global
% maxima as values went far beyond the values of x and y.
% Time of the case study is 4.1923 sec.
%%
% Study2 bounding the values
% In this study we will use upper and lower bounds to obtain the value of
% global maxima.
% upper and lower bounds are used in this case study.
% such that lower bound is 0;0 where as upper bound is 1;1.
tic
for i=1:num_samples
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_2time= toc;
figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
% In this grpah we can observe that the graphn is much clear and clenaer
% than the previous one and values lie in the range of bounds we gave but
% still we could not find global maxima.
% Time for this study is 6.0070sec.
%%
% Study3_increasing the population
% In this we use optimoption command to increase the soze of population
% Benefit is that it will work on large set of number and will help us to
% find maxima.
options=optimoptions('ga');
options=optimoptions(options,'populationsize',350);
tic
for i=1:num_samples
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
% For calculating the optimized value of the function
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_3time= toc;
figure(3)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')
title('Bounded with increase Population')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
% IN this graph we can clearly see the maxima and time for tis study is
% 25.9949 sec. Because it is working on large set of numbers it takes more
% time than the previous studies.