Placement 2025 Scholarship: Your Future Starts Here | 6 Guaranteed Job Interviews | Limited to 100 seats. Apply Now

00D 00H 00M 00S

Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Placements

Student Reviews


For Business


More

Academic Training

Informative Articles

Find Jobs

We are Hiring!


All Courses

Choose a category

Mechanical

Electrical

Civil

Computer Science

Electronics

Offline Program

All Courses

All Courses

logo

CHOOSE A CATEGORY

Mechanical

Electrical

Civil

Computer Science

Electronics

Offline Program

Top Job Leading Courses

Automotive

CFD

FEA

Design

MBD

Med Tech

Courses by Software

Design

Solver

Automation

Vehicle Dynamics

CFD Solver

Preprocessor

Courses by Semester

First Year

Second Year

Third Year

Fourth Year

Courses by Domain

Automotive

CFD

Design

FEA

Tool-focused Courses

Design

Solver

Automation

Preprocessor

CFD Solver

Vehicle Dynamics

Machine learning

Machine Learning and AI

POPULAR COURSES

coursePost Graduate Program in Hybrid Electric Vehicle Design and Analysis
coursePost Graduate Program in Computational Fluid Dynamics
coursePost Graduate Program in CAD
coursePost Graduate Program in CAE
coursePost Graduate Program in Manufacturing Design
coursePost Graduate Program in Computational Design and Pre-processing
coursePost Graduate Program in Complete Passenger Car Design & Product Development
Executive Programs
Workshops
For Business

Success Stories

Placements

Student Reviews

More

Projects

Blogs

Academic Training

Find Jobs

Informative Articles

We're Hiring!

phone+91 9342691281Log in
  1. Home/
  2. Shaik Faraz/
  3. Week 4.1 - Genetic Algorithm

Week 4.1 - Genetic Algorithm

A stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings. Stalagmites are typically composed of calcium carbonate, but may consist of lava, mud, peat, pitch,…

    • Shaik Faraz

      updated on 25 Apr 2022

    A stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings. Stalagmites are typically composed of calcium carbonate, but may consist of lava, mud, peat, pitch, sand, sinter, and amberat (crystallized urine of pack rats).The corresponding formation hanging down from the ceiling of a cave is a stalactite.

    Introduction:

    In computer science and operations research, a genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection.

    The genetic algorithm is a technique for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution. The genetic algorithm constantly modifies the population size of individual solutions.

    Phases in Genetic Algoritham-

    1. Initialization of Population (Coding)

    The code generates its random population of defined size and from that population the code evaluates the fitness of the members of the population according to which it forms new population based on no. of iterations performed.

    1.Fitness Function

    It is that function which we have to optimize and its optimization is done with the help of number of generations which have been formed by the code accordance with the fitness value of each member generated, these fitness values are used for the reproduction of the children.

    1. Selection

    The idea of the selection phase is to select the fittest individuals and let them pass their genes to the next generation. The main goal of this phase is to find the region where the chances of getting the best solution are more.

    Inspiration for this is from the survival of the fittest.

    It should be a balance between exploration and exploitation of search space.

    GA tries to move the genotype to higher fitness in the search space.

    Too strong fitness selection bias can lead to sub-optimal solutions.

    Too little fitness bias selection results in unfocused search.

    1. Reproduction

    Generation of offspring happen in 2 way:

    Crossover

    Mutation

    1. A) Crossover

    Crossover is the most significant stage in the genetic algorithm. During crossover, a random point is selected while mating a pair of parents to generate offspring.

    There are 3 major types of crossover.

    Single Point Crossover:

    A point on both parents’ chromosomes is picked randomly, and designated a ‘crossover point’.

    Two-Point Crossover: Two crossover points are picked randomly from the parent chromosomes.

    Unifofm Crossover: In a uniform crossover, typically, each bit is chosen from either parent with equal probability.

    1. B) Mutation

    In a few new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This indicates that some of the bits in the bit chromosome can be flipped. Mutation happens to take care of diversity among the population and stop premature convergence.

    Syntax

    x = ga(fun,nvars)

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

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

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

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

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

    x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)

    x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

    x = ga(problem)

    [x,fval] = ga(___)

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

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

    Description

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

    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 C and 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,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],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(___), for any previous input arguments, also returns fval, the value of the fitness function at x.

    [x,fval,exitflag,output] = 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(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.

    BODY OF THE CONTANT:

    The above equations are used in the stalagmite function to plot the graph in the main functions.

    function [f] = stalagmite(input_vector)
    
    x = input_vector(1);
    
    y = input_vector(2);
    
    f1_x = (sin(5.1*pi*x + 0.5)).⁶;
    
    f1_y = (sin(5.1*pi*y + 0.5)).⁶;
    
    t1 = ((x-0.0667).²)/(0.64);
    
    f2_x = exp(-4.*log(2)*t1);
    
    t2 = ((y-0.0667).²)/(0.64);
    
    f2_y = exp(-4.*log(2)*t2);
    
    f = -(f1_x.*f2_x.*f1_y.*f2_y);
    
    end

    While using the function at the end as we are using the -ve sign so that the plot will be reversed as we have to find the maxima of the given function using ga.

    Main body

    • Firstly, creating the mesh from the two vectors which will be acting as a search space to find the maxima of the function.
    • Creating the mesh of these two vectors using the ‘mesh’ command and defining the no. of iterations as num_cases to iterate the values of the optimum values of the function.
    • Now calculating the values of fitness function at each point of the mesh grid formed.
    • Now creating its surface through ‘surfc’ command which will also provide the contour of the generated surface.
    • While using surfc applying the -ve sign to the function will generate the surface upside down which will be quite good to get the maxima of the function, as it will provide positive value for the maxima using the -ve sign.
    clear
    
    close all
    
    clc
    
    %% PROVIDING THE SEARCH SPACE FOR THE FUNCTION
    
    x=linspace(0,0.6,150);
    
    y=linspace(0,0.6,150);
    
    [X,Y]=meshgrid(x,y);
    
    num_cases=50; %Number of itreation for which the function will evaluate the fitness
    
    %% CREATING THE SURFACE PLOT OF STALAGEMITE FUNCTION
    
    for i=1:length(X)
    
    for j=1:length(Y)
    
    input_vector(1)=X(i,j);
    
    input_vector(2)=Y(i,j);
    
    f(i,j)=stalagmite(input_vector); %values of function in given search space
    
    end
    
    end
    
    figure(1)
    
    surfc(X,Y,-f); %generating the surface
    
    shading interp %To remove the lines produced by the mesh grid

    The plot generated is shown below:

    STATISTICAL STUDY WITHOUT BOUNDATION

    In this evaluating the optimum value of fitness function at each iteration and calculating it using the above-mentioned syntax in the intro part (which gives the inputs ie. Values of x and y at which the stalagmite function is minimum)

    Then plotting it into the surf command and also providing the optimum values of the function that how it is varying.

    Also using the tic and tok command to evaluate the time used for optimizing the function.

    %% STATISTICAL BEHAVIOUR (UNBOUNDED)

    tic %calculating the study time through tic and toc command
    
    for i=1:num_cases
    
    [inputs,fopt(i)]=ga(@stalagmite,2); %calculating the optimized value of the function
    
    xopt(i)=inputs(1); %at each iteration
    
    yopt(i)=inputs(2);
    
    end
    
    study1_time=toc %displaying the time taken to evaluate
    
    % PLOTING THE GRAPH
    
    figure(2)
    
    subplot(2,1,1)
    
    surfc(X,Y,-f);
    
    hold on
    
    shading interp
    
    plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
    
    xlabel(‘x’);
    
    ylabel(‘y’);
    
    grid on
    
    subplot(2,1,2)
    
    plot(-fopt);
    
    xlabel(‘iterations’)
    
    ylabel(‘function min value’)

    Result:

    From the figure as shown above it is clear that the results are not that much accurate and giving a lot of variation in optimizing the function.

    STATISTICAL STUDY WITH BOUNDATION

    The difference over here is in providing the upper and the lower limit in the ga so that the search space is reduced with the upper and lower boundary [0;0] and [1;1]

    And then plotting it as similar to the above study.

    %% STATISTICAL BEHAVIOUR (BOUNDED)

    tic %calculating the study time through tic and toc command
    
    for j=1:num_cases
    
    [inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); %calculating the optimized value of the function
    
    xopt(j)=inputs(1); %at each iteration with boundation
    
    yopt(j)=inputs(2);
    
    end
    
    study2_time=toc
    
    %PLOTTING
    
    figure(3)
    
    subplot(2,1,1)
    
    hold on
    
    surfc(X,Y,-f)
    
    shading interp
    
    plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
    
    xlabel(‘x’);
    
    ylabel(‘y’);
    
    grid on
    
    subplot(2,1,2)
    
    plot(-fopt);
    
    xlabel(‘iterations’)
    
    ylabel(‘function min value’)

    Result:

    It is clearly seen from the above that after providing the boundary limits the variation in the optimum value of the function has reduced.

    STATISTICAL STUDY WITH INCREASE IN SIZE OF POPULATION

    In this study using the ‘optimoptions’ command to increase the size of population.

    The benefit of that will be it will be working on the large set of numbers and which will be led to increase the possibilities of finding the better fitness of the function.

    The drawback is that the time for evaluation will increase.

    options=optimoptions(‘ga’,’populationsize’,300) %increaing the sixe of population by using
    
    %optimoptions function
    
    tic
    
    for k=1:num_cases
    
    [inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    
    xopt(k)=inputs(1);
    
    yopt(k)=inputs(2);
    
    end
    
    study3_time=toc
    
    %PLOTTING
    
    figure(3)
    
    subplot(2,1,1)
    
    hold on
    
    surfc(X,Y,-f)
    
    shading interp
    
    plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
    
    xlabel(‘x’);
    
    ylabel(‘y’);
    
    grid on
    
    subplot(2,1,2)
    
    plot(-fopt);
    
    xlabel(‘iterations’)
    
    ylabel(‘function min value’)

    Results:

    NOW THE WHOLE PROGRAMA AT ONCE:-

    clear
    
    close all
    
    clc
    
    %% PROVIDING THE SEARCH SPACE FOR THE FUNCTION
    
    x=linspace(0,0.6,150);
    
    y=linspace(0,0.6,150);
    
    [X,Y]=meshgrid(x,y);
    
    num_cases=50; %Number of itreation for which the function will evaluate the fitness
    
    %% CREATING THE SURFACE PLOT OF STALAGEMITE FUNCTION
    
    for i=1:length(X)
    
    for j=1:length(Y)
    
    input_vector(1)=X(i,j);
    
    input_vector(2)=Y(i,j);
    
    f(i,j)=stalagmite(input_vector); %values of function in given search space
    
    end
    
    end
    
    surfc(X,Y,-f); %generating the surface
    
    shading interp %To remove the lines produced by the mesh grid
    
    %% STATISTICAL BEHAVIOUR (UNBOUNDED)
    
    tic %calculating the study time through tic and toc command
    
    for i=1:num_cases
    
    [inputs,fopt(i)]=ga(@stalagmite,2); %calculating the optimized value of the function
    
    xopt(i)=inputs(1); %at each iteration
    
    yopt(i)=inputs(2);
    
    end
    
    study1_time=toc %displaying the time taken to evaluate
    
    % PLOTING THE GRAPH
    
    figure(1)
    
    subplot(2,1,1)
    
    surfc(X,Y,-f);
    
    hold on
    
    shading interp
    
    plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
    
    xlabel(‘x’);
    
    ylabel(‘y’);
    
    grid on
    
    subplot(2,1,2)
    
    plot(-fopt);
    
    xlabel(‘iterations’)
    
    ylabel(‘function min value’)
    
    %% STATISTICAL BEHAVIOUR (BOUNDED)
    
    tic %calculating the study time through tic and toc command
    
    for j=1:num_cases
    
    [inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); %calculating the optimized value of the function
    
    xopt(j)=inputs(1); %at each iteration with boundation
    
    yopt(j)=inputs(2);
    
    end
    
    study2_time=toc
    
    %PLOTTING
    
    figure(2)
    
    subplot(2,1,1)
    
    hold on
    
    surfc(X,Y,-f)
    
    shading interp
    
    plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
    
    xlabel(‘x’);
    
    ylabel(‘y’);
    
    grid on
    
    subplot(2,1,2)
    
    plot(-fopt);
    
    xlabel(‘iterations’)
    
    ylabel(‘function min value’)
    
    %% MODIFYING THE NUMBER OF POPULATION
    
    options=optimoptions(‘ga’,’populationsize’,300) %increaing the sixe of population by using
    
    %optimoptions function
    
    tic
    
    for k=1:num_cases
    
    [inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    
    xopt(k)=inputs(1);
    
    yopt(k)=inputs(2);
    
    end
    
    study3_time=toc
    
    %PLOTTING
    
    figure(3)
    
    subplot(2,1,1)
    
    hold on
    
    surfc(X,Y,-f)
    
    shading interp
    
    plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
    
    xlabel(‘x’);
    
    ylabel(‘y’);
    
    grid on
    
    subplot(2,1,2)
    
    plot(-fopt);
    
    xlabel(‘iterations’)
    
    ylabel(‘function min value’)

    Results:

     Screenshots of my code:

     optimization code stalagmite func codeoptimization code stalagmite code 1optimization code stalagmite code 2optimization code stalagmite code 3optimization code stalagmite code 4

    Command window display:

    optimization code stalagmite command 1optimization code stalagmite command 2optimization code stalagmite command 3optimization code stalagmite command 4

    Plots:

    optimization code stalagmite plot 1 and 2optimization code stalagmite plot 3 and 4

     

    Leave a comment

    Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.

    Please  login to add a comment

    Other comments...

    No comments yet!
    Be the first to add a comment

    Read more Projects by Shaik Faraz (27)

    Project 1 : CFD Meshing for Tesla Cyber Truck

    Objective:

    Aim: Performing topological cleanup and surface mesh on tesla cyber truck and on a wind tunnel based on selected target length values of its different components using element type as Tria, as well as appropriate selection of the volume that contains the vehicle and its surroundings with the wind tunnel for volumetric…

    calendar

    15 Nov 2022 04:17 AM IST

    • ANSA
    • CFD
    Read more

    Week 5 Challenge : Surface wrap on Automotive Assembly

    Objective:

    Aim: Perforform Topo cleanup and delete unwanted surfaces for Surface wrap. After Topo cleanup, Merge all 3 models and perform surface wrap. Target length for Wrap = 3 mm   1. Engine:    2. Gear box:   3. Transmission:   Procedure: 1. Topo Cleanup : (Engine, Transmission & Gear Box)…

    calendar

    10 Nov 2022 08:22 AM IST

      Read more

      Week 4 Challenge : CFD Meshing for BMW car

      Objective:

      Aim: To perform topological clean-up, and carry out the surface meshing of a BMW M6 car model and create a wind tunnel surrounding the same. Objectives: For the given model, check and solve all geometrical errors on half portion and Assign appropriate PIDs. Perform meshing with the given Target length and element Quality…

      calendar

      07 Nov 2022 11:33 AM IST

        Read more

        Week 3 Challenge : CFD meshing on Turbocharger

        Objective:

        Aim: Performing CFD meshing on Turbocharger using ANSA Objective: For the given model, check for the geometrical errors to make appropriate volumes. Create and assign PIDs as shown in the video. Perform surface mesh with the given target lengths as per PIDs. Blade stage-1 = 1 mm Blade stage-2 = 1 mm Impeller = 2 mm…

        calendar

        03 Nov 2022 08:06 AM IST

        • ANSA
        • CFD
        Read more

        Schedule a counselling session

        Please enter your name
        Please enter a valid email
        Please enter a valid number

        Related Courses

        coursecard

        Design loads considered on bridges

        Recently launched

        10 Hours of Content

        coursecard

        Design of Steel Superstructure in Bridges

        Recently launched

        16 Hours of Content

        coursecard

        Design for Manufacturability (DFM)

        Recently launched

        11 Hours of Content

        coursecard

        CATIA for Medical Product Design

        Recently launched

        5 Hours of Content

        coursecardcoursetype

        Accelerated Career Program in Embedded Systems (On-Campus) Courseware Partner: IT-ITes SSC nasscom

        Recently launched

        0 Hours of Content

        Schedule a counselling session

        Please enter your name
        Please enter a valid email
        Please enter a valid number

        logo

        Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.

        https://d27yxarlh48w6q.cloudfront.net/web/v1/images/facebook.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/insta.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/twitter.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/youtube.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/linkedin.svg

        Our Company

        News & EventsBlogCareersGrievance RedressalSkill-Lync ReviewsTermsPrivacy PolicyBecome an Affiliate
        map
        EpowerX Learning Technologies Pvt Ltd.
        4th Floor, BLOCK-B, Velachery - Tambaram Main Rd, Ram Nagar South, Madipakkam, Chennai, Tamil Nadu 600042.
        mail
        info@skill-lync.com
        mail
        ITgrievance@skill-lync.com

        Top Individual Courses

        Computational Combustion Using Python and CanteraIntroduction to Physical Modeling using SimscapeIntroduction to Structural Analysis using ANSYS WorkbenchIntroduction to Structural Analysis using ANSYS Workbench

        Top PG Programs

        Post Graduate Program in Hybrid Electric Vehicle Design and AnalysisPost Graduate Program in Computational Fluid DynamicsPost Graduate Program in CADPost Graduate Program in Electric Vehicle Design & Development

        Skill-Lync Plus

        Executive Program in Electric Vehicle Embedded SoftwareExecutive Program in Electric Vehicle DesignExecutive Program in Cybersecurity

        Trending Blogs

        Heat Transfer Principles in Energy-Efficient Refrigerators and Air Conditioners Advanced Modeling and Result Visualization in Simscape Exploring Simulink and Library Browser in Simscape Advanced Simulink Tools and Libraries in SimscapeExploring Simulink Basics in Simscape

        © 2025 Skill-Lync Inc. All Rights Reserved.

                    Do You Want To Showcase Your Technical Skills?
                    Sign-Up for our projects.