Challenge

Week 4.1 - Genetic Algorithm

  • function [f]=stalagmite(input_vector);
    % STALAGMITE FUNCTION 
    %formulae 
    %global maxima=f=(f1x*f1y*f2x*f2y)
    x=input_vector(1);
    y=input_vector(2);
    f1_x=[sin(5.1*pi*x+0.5)]^6;
    f1_y=[sin(5.1*pi*y+0.5)]^6;
    f2_x=exp(-4*log(2)*(((x-0.0667)^2)/(0.64)));
    f2_y=exp(-4*log(2)*(((y-0.0667)^2)/(0.64)));
    f=-(f1_x*f1_y*f2_x*f2_y);
    end
    
    
    
    
    % solving genetic algorithm
    
    % defining two input values
    x=linspace(0,0.6,150);
    y=linspace(0,0.6,150);
    
    % creating 2d mesh
    [xx yy]=meshgrid(x,y);
    % generating stalgamite function
    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
    figure(1)
    surfc(xx,yy,f)
    shading interp
    %creating genetic algorithm function with 2 input variables
    [inputs,fval]=ga(@stalagmite,2)
    %study 1 statistical behaviour with 2 inputs USING COMMAND tic toc to
    %calculate time required.
    num_case=70
    tic
    for i=1:num_case
        %optimizing function
        [inputs,fopt(i)]= ga(@stalagmite,2)
        xopt(i)=inputs(1);
        yopt(i)=inputs(2);
    end
    
    study_time_1=toc
    figure(2)
    subplot(2,1,1)
    hold on
    surfc(x,y,f)
    shading interp
    plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','g')
    title('unbounded inputs')
    subplot(2,1,2)
    plot(fopt)
    xlabel('number of iteration')
    ylabel('function maximum')
    
    %study2 bounded inputs x1,y1=0,0; x2,y2=0.6,0.6
    tic
    for i=1:num_case
        [inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6])
        xopt(i)=inputs(1);
        yopt(i)=inputs(2);
    end
    
    study_time_1=toc
    figure(3)
    subplot(2,1,1)
    hold on
    surfc(x,y,f)
    shading interp
    plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','g')
    title('bounded inputs')
    subplot(2,1,2)
    plot(fopt)
    xlabel('number of iteration')
    ylabel('function maximum')
    
    tic
    %study3 incrasing ga iteration 400 populationsize
    
    options=optimoptions('ga')
    options=optimoptions(options,'populationsize',400)
    
    for i=1:num_case
        [inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options)
        xopt(i)=inputs(1);
        yopt(i)=inputs(2);
    end
    
    study_time_1=toc
    figure(4)
    subplot(2,1,1)
    hold on
    surfc(x,y,f)
    shading interp
    plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','g')
    title('unbonded inputs')
    subplot(2,1,2)
    plot(fopt)
    xlabel('number of iteration')
    ylabel('function maximum')
    GENETIC ALGORITHM REPORT

  • ga = Find minimum of function using genetic algorithm

STALAGMITE FUNCTION:-
  • It is a mathematical model which helps to generate maxima and minima of a functions
  • Global maxima is maximum peak value of a entire function Values
  • Global minima is minimum peak value of a entire function Values
  • but local maxima and local minima are multiple number of values
GENETIC ALG0RITHM:-
  • Optimization techniques are the techniques used to discover the best solution out of all the possible solutions available under the constraints present.
  • The genetic algorithm is one such optimization algorithm built based on the natural evolutionary process of our nature.
3 case studies observed with 70 iterations
Plot graphs for all 3 studies and for Function maximum vs no. of iteration.
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.