Challenge

Week 4.1 - Genetic Algorithm

1.Aim :

  • To Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • To explain the concept of genetic algorithm and the syntax for GAin MATLAB

2.Formulas used :

Where,

3.Objective of the project:

  • Plot graphs for all studies and for F maximum vs no. of iterations.
  • Using stalagmite function show the Global Maxima

4.Body of content:

Genetic Algorithm:

The genetic algorithm is a method for solving both Non-linear and non-differentiable problems that is based on natural selection. The process that drives biological evolution.

How Genetic Algorithm Works?

The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals from the current population to be the 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.

Syntax of GA:

  • x = ga(fun,nvars)

finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of function.

  • x = ga(fun,nvars,A,b)

finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').

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

finds a local minimum x to fun, subject to the linear equalities

Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').

  • 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 = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)

subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors Cand Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)

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

minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

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

ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options) requires that the variables listed in intcon take integer values..

  • x = ga(problem)

finds the minimum for problem, a structure described in problem.

  • [x,fval] = ga(___)

ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.

  • [x,fval,exitflag,output] = ga(___)

ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.

  • [x,fval,exitflag,output,population,scores] = ga(___)

ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.

Genetic algorithm Program

  • Stating the inputs for search the space x, y with the help of linspace command.
  • Using [xx, yy] = meshgrid(x, y), to return 2-D grid coordinates based on the coordinated contained in vector x and y. Xis a matrix where each row is a copy of x, and Y is a matrix where each column is a copy of y. The grid represented by the coordinates X And Y has length(y) rows and length(x) columns.

Evaluating the function

%write a program stalaqmite fuction
function [f] = stalagmite(input_vector)

%Stating the inputs
x = input_vector(1); 
y = input_vector(2);

%Equation of f1x, f1y, f2x, f2y
f1 = (sin(5.1*pi*x + 0.5))^6;
f2 = (sin(5.1*pi*y +0.5))^6;
f3 = exp(-4*log(2)*((x-0.0667)^2/0.64));
f4 = exp(-4*log(2)*((y-0.0667)^2/0.64));

%Fuction as per formula given
f = -(f1*f2*f3*f4);
end
  • Plotting the 3D Surface with respect to x ,y and f values with the command of surfc
  • Genetic algorithm has been executed with command of ga
  • The program is as follows
    clear all
    close all
    clc
    
    % Define Search Space
    x = linspace(0,0.6,150);
    y = linspace(0,0.6,150);
    
    % Creating two dimensional mesh
    [xx,yy] = meshgrid(x,y);       
    
    % EVALUATING THE STALGAMITE FUNCTION
    for i=1: length (xx)                        %For loop for x values
        for j=1: length(yy)                     %For loop for x values
            input (1) = xx(i,j);         %the xx valve of the same is defined as input vector 1
            input (2) = yy(i,j);         %the xx valve of the same is defined as input vector 2
            f(i,j) = stalagmite(input); % Staglagmite function is called (Staglagmite is stucture shows as peaks) 
        end
    end
    
    % CREATING 3D SURFACE OF THE STALAGMITE FUNCTION
    surfc(xx, yy, f)   
    xlabel('X-value')
    ylabel('Y- value')
    shading interp
    
    % RUNNING THE GA ALGORITHM
    [inputs, fval] = ga(@stalagmite, 2)​

    Study of Global Minima using GA

    Study 1: Evaluating GA without bounds

    • The function is iterated 50 times as the given.
    • Output are stored in row vector ‘input_vector’ and function valve is stored in array ‘fopt’
    • The time taken by this function is calculated by tic and toc comment.
    • Using Surfc() and subplot comment the graph is produced for valuation
    • With this global minima cannot be find out.

    Study 2: Evaluating GA with upper and lower bonds

    • As same as the previous study, we here introduce the Upper and lower bounds
    • x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) took this syntax and gave valve for lower bound and upper bound so that the search can be minimalized
    • Plot for the same is done

    Study 3: Evaluating GA by increasing the population

    • Population option is let you to describe the require population that the genetic algorithm uses.
    • Options = optimoptions (solvername, option), where solvername is GA and option to increase the PopulationSize.
    • ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options) with this syntax the optimization is done by GA

    Program is as follows

    clear all
    close all
    clc
    
    % Define Search Space
    x = linspace(0,0.6,150);
    y = linspace(0,0.6,150);
    numcase = 50;
    
    % Creating two dimensional mesh
    [xx,yy] = meshgrid(x,y);       
    
    % EVALUATING THE STALGAMITE FUNCTION
    for i=1:length(xx)                         %For loop for x values
        for j=1:length(yy)                     %For loop for y values
            input_vector (1) = xx(i,j);        %the xx valve of the same is defined as input vector 1
            input_vector (2) = yy(i,j);        %the yy valve of the same is defined as input vector 2
            f(i,j) = stalagmite(input_vector); % Staglagmite function is called (Staglagmite is stucture shows as peaks) 
        end
    end
    
    % Evaluating GA without bounds
    tic
    for k=1:numcase
        [inputs,fopt(k)] = ga(@stalagmite,2);
        xopt(k) = inputs (1);
        yopt(k) = inputs (2);
    end
    study1_time = toc
    
    % Evaluating GA with lower and upper Bounds
    tic
    for k=1:numcase
        [inputs1,fopt1(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
        xopt1(k) = inputs1 (1);
        yopt1(k) = inputs1 (2);
    end
    study2_time = toc
    
    % Evaluating GA by Increasing the population
    options = optimoptions('ga','PopulationSize',200);
    
    tic
    for k=1:numcase
        [inputs2,fopt2(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
        xopt2(k) = inputs2 (1);
        yopt2(k) = inputs2 (2);
    end
    study3_time = toc
    
    
    %Plotting for Studies without bounds
    figure(1)
    subplot(2,1,1)
    hold on
    surfc(xx,yy,f)
    shading interp
    title ('Studies without bounds')
    plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
    subplot (2,1,2)
    plot (fopt)
    xlabel ('iternation')
    ylabel ('function minima')
    
    %Plotting for Studies with lower and upper Bounds
    figure (2)
    subplot(2,1,1)
    hold on
    surfc(xx,yy,f)
    shading interp
    title ('Studies with lower and upper Bounds')
    plot3(xopt1,yopt1,fopt1,'marker','o','markersize',5,'markerfacecolor','r')
    subplot (2,1,2)
    plot (fopt1)
    xlabel ('iternation')
    ylabel ('function minima')
    
    %Plotting after Increasing the population
    figure (3)
    subplot(2,1,1)
    hold on
    surfc(xx,yy,f)
    shading interp
    title ('By Increasing the population')
    plot3(xopt2,yopt2,fopt2,'marker','o','markersize',5,'markerfacecolor','r')
    subplot (2,1,2)
    plot (fopt2)
    xlabel ('iternation')
    ylabel ('function minima')
    
    

    Results :

  • Above program Results shows various optimization to find global minima using GA
  • As the results shows the timing for each studies from the by increasing the population time taken will also increases
  • the time taken for each studies is as follows
  • The Graph shows the that best way to find the global minima will be by increasing the population
  • the graphs are attached bellow
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.