Challenge

Week 5 - Genetic Algorithm

Aim

1.Understand the concept of Optimization and Genetic Algorithm.

2.Stalagmite function.

Explanation of main terminology

1.Optimization :- process of selecting or finding out values from a given set of values inorder to get dezired output.

2.Population space :- total number of set of values provided in 2D array is called population space.

3.Population size :- Certain values choosen from population space .

Equations used

f1 = (sin(((5.1*pi).*input(1))+0.5)).^6.
f2= (sin(((5.1*pi).*input(2))+0.5)).^6.
f3= exp((-4*log(2).*(((input(1)-0.0667).^2)./0.64))).
f4= exp((-4*log(2).*(((input(2)-0.0667).^2)./0.64))).
Range of x = 0 to 0.6

Range of y = 0 to 0.6

Syntax for GA

X = ga(fun , nvariable , A , B , Aeq , Beq , lb , up , nonl con , options)

here

fun = Function used.

nvariable = number of variables.

A = linear inequality constraints (real matrix)

B = linear inequality constraints (real vector)

Aeq = linear equality constraints (real matrix)

Beq = linear equality constraints (real vector)

lb =lower limits

up =lower limits

nonl con =non linear constraints

options = optimization options

main function code

function[f]= stalagmite_fun(input)

f1 = (sin(((5.1*pi).*input(1))+0.5)).^6;

f2= (sin(((5.1*pi).*input(2))+0.5)).^6;

f3= exp((-4*log(2).*(((input(1)-0.0667).^2)./0.64)));

f4= exp((-4*log(2).*(((input(2)-0.0667).^2)./0.64)));

f = -(f1.*f2.*f3.*f4);
end

Case no. 1

Unbounded GA study - In this case lower and upper bounds are not defined or we can say that no any limits are provided within the axis.

%case no.1
%unbounded 
tic
for i = 1:num_cases;              % generating for loop
    [inputs,fopt(i)] = ga(@stalagmite_fun,2);  %computing optimam value of f ,x and y
    xopt(i) = inputs(1);          %define xopt
    yopt(i) = inputs(2);          %define xopt

end
time_study = toc
figure(2)
subplot(2,1,1)                    %breaking of figure into 2x1 size

surfc(x,y,-f)                     %creating 3d surface plot
hold on
xlabel('x')              %labelinf for x axis
ylabel('y')              %labelinf for y axis
zlabel('f')              %labelinf for z axis
%ploting of xopt,yopt,fopt in 3d by using plot3
plot3(xopt,yopt,-fopt,'Marker',"o",'MarkerSize',5,'MarkerFaceColor','r')

 title('UNBOUNDED GA')            %give title to plot
    
subplot(2,1,2)                   

plot(-fopt)                      %ploting -fopt
xlabel('iteration')              %labelinf for x axis
ylabel('function')               %labelinf for x axis

Result

Case no. 2

Bounded GA study - In this case upper and lower limits are provided .

limits are defined as blow

For x lower limit is 0 and upper limit is 0.4

For y lower is 0 and upper is 0.4

clear all
close all
clc

x = linspace(0,0.6,150);                %creating 1d array for x
y = linspace(0,0.6,150);                %creating 1d array for y


[xx,yy] = meshgrid(x,y);                %conversion of 1d array into 2d array
num_cases = 150;                        % define no. itration


%initialize nested for loop
for i = 1:length(xx)                     % initialize indexing for xx            

   for j= 1:length(yy)                   % initialize indexing for yy
input(1) = xx(i,j);                      % define inputes
input(2) = yy(i,j);                      % define inputes

f(i,j) = stalagmite_fun(input);      %computing 'f' by calling function 'stalgmite_fun'
   end
end
figure(1)
surf(xx,yy,f)
shading interp




%case no.2
%bounded 
tic
for i = 1:num_cases;      %creating separate figure%creating separate figure

     %computing optimam value of f ,x and y
    [inputs,fopt(i)] =  ga(@stalagmite_fun,2,[],[],[],[],[0,0],[0.4,0.4]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);

end
time_study = toc
figure(2)
subplot(2,1,1)

surfc(x,y,-f)

xlabel('x')              %labelinf for x axis
ylabel('y')              %labelinf for y axis
zlabel('f')              %labelinf for z axis
hold on

%ploting of xopt,yopt,fopt in 3d by using plot3
plot3(xopt,yopt,-fopt,'Marker',"o",'MarkerSize',5,'MarkerFaceColor','auto')

title('BOUNDED GA')

subplot(2,1,2)

plot(-fopt)
xlabel('iteration')
ylabel('function')

Result

Case no. 3

Bounded GA study with population size - In this case population size also provide with lower and upper limits.

here we take population size 250

%tic
for i = 1:num_cases;        %initialize for loop

 %computing optimam value of f ,x and y
    [inputs,fopt(i)] =  ga(@stalagmite_fun,2,[],[],[],[],[0,0],[0.4,0.4],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);

end
time_study = toc
 figure(3)
 %fopt = -fopt;
 f = -f;
 subplot(2,1,1)
 hold on
 surfc(x,y,f)
 shading interp
 plot3(xopt, yopt, -fopt, 'marker', 'o', 'markersize', 5, 'markerfacecolor', 'r')
 title('Bounded inputs with modified population size')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('Iterations')
 ylabel('functions')

%Stydy 3 increasing GA iteration
 options = optimoptions('ga');
 options = optimoptions (options, 'PopulationSize', 200);

 for i = 1:num_cases        %initialize for loop
 %computing optimam value of f ,x and y
    [inputs,fopt(i)] =  ga(@stalagmite_fun,2,[],[],[],[],[0,0],[0.4,0.4],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);

end

study3_time = toc;
figure(4)
%fopt = -fopt;
 f = -f;
 subplot(2,1,1)
 hold on
 surfc(x,y,f)
 shading interp
 plot3(xopt, yopt, -fopt, 'marker', 'o', 'markersize', 5, 'markerfacecolor', 'r')
 title('Bounded inputs with modified population size')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('Iterations')
 ylabel('functions')


Result

Conclusion

In caes 1 (unbounded GA) we does not have limits for x and y axis so we get values of function irrespective of given rang hance there is chances to get inaccurate solution.

but in case 2(bounded GA) we have provided the limits for x and y axis thats why get better and accurate solution .

since by provided limits for axis we still not get optimum solution so in case 3 we introduce population size also hance we get our optimum solution

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.