All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Genitic Algorithm: Aim Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function. Clearly expalin 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…
Anantha Padmanabhan
updated on 14 Jan 2020
Genitic Algorithm:
Aim
Theory
A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
The process of natural selection starts with the selection of fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found.
This notion can be applied for a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.
Five phases are considered in a genetic algorithm.
The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.
An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).
In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of an alphabet. Usually, binary values are used (string of 1s and 0s). We say that we encode the genes in a chromosome.
The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.
Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.
Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.
For example, consider the crossover point to be 3 as shown below.
Crossover point
Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.
Exchanging genes among parents
The new offspring are added to the population.
New offspring
In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.
Mutation: Before and After
Mutation occurs to maintain diversity within the population and prevent premature convergence.
The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.
The population has a fixed size. As new generations are formed, individuals with least fitness die, providing space for new offspring.
The sequence of phases is repeated to produce individuals in each new generation which are better than the previous generation.
Program
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)
input(1)=xx(i,j);
input(2)=yy(i,j);
f(i,j)=stalagmite(input);
[Input,fval(i)]=ga(@stalagmite,2);
x1(i)=Input(1);
y1(i)= Input(2);
end
end
figure(1)
surfc(xx,yy,f)
hold on
plot3(x1,y1,fval)
hold off
figure(2)
plot(fval)
shading interp
[Input,fval]=ga(@stalagmite,2);
for i=1:length(xx)
for j= 1:length(yy)
input(1)=xx(i,j);
input(2)=yy(i,j);
f(i,j)=stalagmite(input);
[Input,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[0.9:0.9]); %%
x2(i)=Input(1);
y2(i)= Input(2);
end
end
figure(3)
surfc(xx,yy,f)
hold on
plot3(x1,y1,fval)
hold off
figure(4)
plot(fval)
shading interp
options=optimoptions(\'ga\')
options=optimoptions(options,\'PopulationSize\',400);
tic;
for i=1:length(xx)
for j= 1:length(yy)
input(1)=xx(i,j);
input(2)=yy(i,j);
f(i,j)=stalagmite(input);
[Input,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[0.2:0.2],[],[],options);
x3(i)=Input(1);
y3(i)= Input(2);
end
end
toc;
v=toc;
figure(5)
surfc(xx,yy,f)
hold on
plot3(x3,y3,fval)
shading interp
hold off
figure(6)
plot(fval)
Stalagmite function
function [F]= stalagmite(Input)
x=Input(1);
y=Input(2);
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));
F=(f1*f2*f3*f4*-1);
end
Result:
In this we have done three type of analysis
Here we can see that the GA value converges to the point nothing but our global maxima.
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.
Other comments...
Underbody Coating
Undercoating is the sprayed application of a wax or rubber-based composite onto the entire underside of the vehicle. It’s designed to create a rust-proof layer between the underside of the vehicle and the water, ice, and salt of the road. Here in Canada, the roads we drive on can be tough on the underside of our…
05 Oct 2020 07:21 AM IST
Benchmarking
Reqirements : 5 Seater Small car Good Milleage and comfort Price range 8 to 10 lacs Swift Dzire Hyundai Aura Kia Sonet Hyundai Venue Ford Ecosport Looks Power / Torque 1197bhp 1197 1000 1397 1000 Brakes Disc disc disc disc disc Mileage 23 20 20 23 20 Cost…
05 Oct 2020 07:05 AM IST
Machine learning Basics
1. The difference in the formula for the standard deciation and Variance because the variance is for entire population set and the sample is a subset of the Population and we are taking this sample to analyze the population. The sample size from the population can be 1%, 10% or 60% max not equal to ti 100%. If it…
29 Mar 2020 11:09 AM IST
Parsing NASA thermodynamic data
Aim: Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. You will be reading this file NASA thermodynamic data(Tip: use fopen and fgetl). The formulae are shown below. Here R is the universal gas constant, T…
14 Jan 2020 07:20 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.