Project

Implementing Genetic Algorithm for analysis of stalagmite function in MATLAB

AIM: Finding global maxima of a stalagmite function and optimizing it using MATLAB code.

OBJECTIVES:

  • Write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
  • Clearly explain the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
  • Make sure that your code gives the same output, even if it is made to run several times.


  • THEORY:

    Genetic Algorithm-

    The genetic algorithm is an algorithm used for solving both constrained and unconstrained optimization problems. A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. It selects random inputs called as population, check their fitness for selection and pairing purposes (the pair is called as parents) to generate new data (called as new/next generation). This next generation data is mutated from data acquired from parents which is essential for computing optimised output.

    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.

    Classical Algorithm

    Genetic Algorithm

    Generates a single point at each iteration. The sequence of points approaches an optimal solution.

    Generates a population of points at each iteration. The best point in the population approaches an optimal solution.

    Selects the next point in the sequence by a deterministic computation.

    Selects the next population by computation which uses random number generators.



  • Syntax of ga-

    x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)

    ð => defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq= [] and beq= [] if no linear equalities exist.)

    [x,fval] = ga(___),

    ð => for any previous input arguments, also returns fval, the value of the fitness function at x.

    GOVERNING EQUATION:


  • STEP BY STEP EXPLANATION:

    First, we write a code to define the stalagmite function. Here we break down the formula into 4 terms for convenience.

    Then we write main code were we define two inputs x and y both of which are one dimensional equally spaced array containing 100 elements. We utilise meshgrid command to create an array of x and y values.

    Now we use for loop to input values of 2D array in stalagmite function. Here we encounter an problem i.e. every time the code is executed different values for stalagmite function are obtained. This is overcome by introducing another loop into the code. We then input these values in genetic algorithm. For genetic algorithm ‘ga’ function is used.

    To improve our Genetic Algorithm, we undertake three cases to optimise the function so that we can accurately find the global maxima of the function. We need to do this because the GA randomly takes values and global maxima can change according to that.

    In case 1 we evaluate all possible global maxima for all the iterations using a loop. We two plot in one figure using subplot command. ‘surfc’ command is used for x vs y graph and for function max vs iteration we use simple ‘plot’ command. The values in figure (1) are unbounded values.

    In case 2 we put an upper bound and lower bound on ‘ga’ function and again plot two plots which we plot during case 1.

    In case 3 we increase GA iterations. We do this because in GA after successive iterations, the population "evolves" toward an optimal solution i.e more the number of iterations, more optimal the solution will be. Now we use same commands to plot the graph and curve.

  • CODE:

    Function code:

    function [F] =stalagmite(inputs)
     
    x=inputs(1);
    y=inputs(2);
     
     term1=(sin((5.1*pi*x)+0.5))^6;
     term2=exp((-4)*log(2)*((x-0.0667)^2)/0.64);
     term3=(sin((5.1*pi.*y)+0.5))^6;
     term4=exp((-4)*log(2)*((y-0.0667).^2)/0.64);
         
     F= -(term1*term2*term3*term4);
     
    end
  • Main Code:

  • clear all
    close all
    clc
     
    x=linspace(0,1,100);
    y=linspace(0,1,100);
     
    [xx,yy]=meshgrid(x,y);
     
    for i=1:length(xx)
        for j=1:length(yy)
            
            inputs(1)=xx(i,j);
            inputs(2)=yy(i,j);
            F(i,j)=stalagmite(inputs);
        end
    end
     
    num=100;
    tic
     
    % Case1 : Statistical Behaviour
     
    for i=1:num
       [inputs,Fout(i)]=ga(@stalagmite,2);
        xout(i)=inputs(1);
        yout(i)=inputs(2);
    end
     
     
     
    casel_time=toc; 
     
    figure(1)
    subplot(2,1,1) 
    surfc(xx,yy,-F) 
    shading interp 
    hold on
    plot3(xout,yout,-Fout,'marker','*','markersize',5,'markerfacecolor','r') 
    title('Unbounded Inputs')
    subplot(2,1,2) 
    plot(-Fout)
    xlabel('Iterations')
    ylabel('Function Maximum')
    tic
     
    %Case2 : Statistical Behaviour with upper bound & lower bound
     
    for i=1:num
        [inputs,Fout(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.3;0.3],[],[]);
        xout(i)=inputs(1);
        yout(i)=inputs(2);
    end
     
    case2_time=toc; 
     
    figure(2)
    subplot(2,1,1) 
    surfc(x,y,-F)
    shading interp 
    hold on
    plot3(xout,yout,-Fout,'marker','*','markersize',5,'markerfacecolor','g')
    title('Bounded Inputs')
    subplot(2,1,2) 
    plot(-Fout)
    xlabel('Iterations')
    ylabel('Function Maximum')
    tic
     
    %Case3 : Increasing Iterations
     
    options=optimoptions('ga'); 
    options=optimoptions(options,'populationsize',200);
     
    for i=1:num
    [inputs,Fout(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xout(i)=inputs(1);
    yout(i)=inputs(2);
    end
     
    case3_time=toc;
     
    figure(3)
    subplot(2,1,1)
    surfc(x,y,-F) 
    shading interp 
    hold on
    plot3(xout,yout,-Fout,'marker','*','markersize',5,'markerfacecolor','b')
    title('Bounded Inputs with Increased Population size')
    subplot(2,1,2) 
    plot(-Fout)
    xlabel('Iterations')
    ylabel('Function Maximum')          

    OUTPUT/RESULT/CONCLUSION:

  • Case 1 :



  • Case 2:



  • Case 3:



  • ERRORS:

    1. 1. Many typo-errors were encountered.

    2. 2. The plots obtained were not proper because ‘ga’ function is used to find minima but we have to find maxima so we have to introduce negative sign in function code in function definition line.

    Case 1:

  • Case 2:

  • Case 3:

  • 1. 3. Even after using negative sign in function code the graph was not proper.

    Case 1:


  • Case 2:

  • Case 3:


    So, to overcome this problem we introduced negative sign with each value of function output ‘F’ in main code to reverse the direction of plot.


    REFERENCES, IF ANY:

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

    https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-code-e396e98d8bf3

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

    https://in.mathworks.com/help/gads/ga.html#d120e39545

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.