Challenge

Week 4 - Genetic Algorithm

Aim: To find the global maxima of the stalagmite function and hence to optimize the results using matlab code.

Theory:

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.

The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:

  • Selection rules select the individuals, called parents, that contribute to the population at the next generation.

  • Crossover rules combine two parents to form children for the next generation.

  • Mutation rules apply random changes to individual parents to form children.

Governing Equation:

Syntax of genetic algorithm- `ga(fun,nvars,A,b,Aeq,beq,lb,ub)`

Equation of the stalagmite function- `f1_x1=(sin(5.1*pi*x+0.5))^6;`

`f2_x2=exp(-4*log(2)*(x-0.0667)^2/.64);`

`f3_y1=(sin(5.1*pi*y+0.5))^6;`

`f4_y2=exp(-4*log(2)*(y-0.0667)^2/.64);`

`f=-(f1_x1*f2_x2*f3_y1*f4_y2);`

Matlab code:

clear all
close all
clc
%the workspace is defined
x=linspace(0,0.6,150); %ange of x variable
y=linspace(0,0.6,150); %range of y variable
num_cases=50;           %number of iterations is specified

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

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-unbounded inputs

tic
for i=1:num_cases
    [inputs,fmax(i)]=ga(@stalagmite,2); %calling the stalagmite function
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
time1=toc  %time limits is specified

figure(1)
subplot(2,1,1) %a subplot of only the stalagmite function is specified
hold on
surfc(x,y,-f)
shading interp  %gives smoother looking graph
plot3(xopt,yopt,-fmax,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fmax) %a subplot of only the optimum function is also plotted
xlabel('iterations')
ylabel('function maximum')
colorbar()


%study 2-providing the upper and lower limits
tic
for i=1:num_cases
    [inputs,fmax(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
time2=toc  %time limits is specified
   
%plotting

figure(2)
subplot(2,1,1)  %first subplot of the  stalagmite function
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fmax,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fmax)   %second subplot of only the function values
xlabel('iterations')
ylabel('function maximum')
colorbar()

%study 3 increasing the ga iterations
options=optimoptions('ga');
options=optimoptions('ga','Populationsize',170) %increasing the ga iterations

tic
for i=1:num_cases
    [inputs,fmax(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options); %providing limits
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
time3=toc


figure(3)
subplot(2,1,1) %first subplot of only the stalagmite function
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fmax,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fmax)   %second subplot of the function values
xlabel('iterations')
ylabel('function maximum')
colorbar()

Function code:

function f=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);

f1_x1=(sin(5.1*pi*x+0.5))^6;
f2_x2=exp(-4*log(2)*(x-0.0667)^2/.64);
f3_y1=(sin(5.1*pi*y+0.5))^6;
f4_y2=exp(-4*log(2)*(y-0.0667)^2/.64);
f=-(f1_x1*f2_x2*f3_y1*f4_y2);
end

Code explanation

Main code:

  • Three studies are made in order completely understand the optimisation of the above code
  • Initially , the workspace is specified ,x and y are the variables used.The linspace command is used to create the 1D array
  • mesgrid command is used to form the 2D array.
  • For loop is used to create a loop inorder to input the values of x and y to the stalagmite function.
  • A problem is encountered where different optimal values of the functions are obtained when we run the function everytime.
  • To overcome this drawback another for loop inside the previous one is applied.And finally ga is used to call the genetic algorithm function.
  • Three studies are made :
  • The first study is all about plotting the stalagmite function and the optimum function values without providing the upper and lower limits for the function.
  • Two subplots are plotted in a single figure using subplot command.
  • plot3 (X,Y,Z,LineSpec) creates the plot using the specified line style, marker, and color. plot3 (X1,Y1,Z1,...,Xn,Yn,Zn) plots multiple sets of coordinates on the same set of axes.
  • The title for the graph is specified along with the x and y label.
  • The second study is all about providing the upper and lower bounds for the stalagmite function.
  • The following syntax is used to specify the upper and lower bounds.

[inputs,fmax(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);

  • And the same procedures as we did in the first cas is conducted in plotting the graphs.
  • The third study also contains the upper and lower bounds but here we are providing more population size.
  • Here, the reason for increasing the population size is because we get more number of optimal solutions.
  • The optimoptions is used to determine the population size.
  • And the same procedures are used to plot and subplot the grahs.
  • The tic and toc command are used to determine the time taken to perform and opertion , here, study.
  • It is found that as the complexity in the problem increases , the timee taken to execute the program also increases.

Function code explanation:

The function code basically is a mathematical expression,which is spliited into four different parts and are combined together in the final expression.

The function is saved under the name of stalagmite.

And the same name is specified in the main function wherever it is called.The matematical expression is specified above.

Output:

The graphs of all the studies are pasted below.

Graph of first study

Graph of second study

The graph of third study

It was found that the range of optimum sulutions obtained decreased from study 1 to study 3, but more optimum solution of the global maxima is obtained in study 3.

Errors faced :Basic typing errors which were resloved later.

Inference:

Genetic algorithm is a powerful tool which can be used to determine the optimum solution which helps us in various day to day applications.

Refernece links:

https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html

https://in.search.yahoo.com/search?fr=mcafee&type=E211IN714G0&p=plot3+in+matlab%27

https://in.search.yahoo.com/search;_ylt=AwrwJXiybe9eDjAAmU.7HAx.;_ylc=X1MDMjExNDcyMzAwMwRfcgMyBGZyA21jYWZlZQRncHJpZANSUXB1U2JYU1RPNjd2c0MybktwR3NBBG5fcnNsdAMwBG5fc3VnZwMxBG9yaWdpbgNpbi5zZWFyY2gueWFob28uY29tBHBvcwMwBHBxc3RyAwRwcXN0cmwDMARxc3RybAMxNgRxdWVyeQNvcHRpb25zaW4lMjBtYXRsYWIEdF9zdG1wAzE1OTI3NDk1MDI-?p=optionsin+matlab&fr2=sb-top&fr=mcafee&vm=r&type=E211IN714G0

https://en.wikipedia.org/wiki/Genetic_algorithm

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.