Genetic Algorithm is a popular metaheuristic, stochastic optimization algorithm, based on the mechanisms of natural selection in Charles Darwin’s theory of natural evolution. Genetic Algorithm was first introduced by Holland in 1975 and now it is still very popular in various research community.

In this post, I’m going to show you a simple and easy-to-understand concept of genetic algorithm and its Matlab code to solve various optimization problems.

Did you know that Genetic algorithm (GA) is one of the most popular stochastic optimization algorithm often used to solve complex large scale optimization problems in various fields.

General concept of Genetic Algorithm (Gen & Cheng 1997):

+ Genetic algorithm starts with an initial set of random solutions called population.

+ Each individual in the population is called a chromosome representing a solution to the problem at hand.

+ The chromosomes evolve through successive iterations, called generations.

+ During each generation, the chromosomes are evaluated using some measures of fitness.

+ To create the next generation, new chromosome, called offspring, are formed by either (a) merging two chromosomes from current generation using a crossover operator or (b) modifying a chromosome using a mutation operator.

+ A new generation is formed by (a) selecting, according to the fitness values, some of the parents and offspring and (b) rejecting others so as to keep the population size constant.

+ Fitter chromosomes have higher probabilities of being selected.

+ After several generations, the algorithms converge to the best chromosome, which hopefully represents the optimum or suboptimal solution to the problem.

To test the performance of this Genetic Algorithm, I used the following test problem:

Let’s see how it works:

For more videos like this, check my YouTube channel here.

GA.m

clear all
close all
clc
%--------------------------------------------------------------------------
p=50; % Population size
c=40; % number of pairs of chromosomes to be crossovered
m=40; % number chromosomes to be mutated
tg=200; % Total number of generations 
%--------------------------------------------------------------------------
figure
title('Blue - Average         Red - Maximum');
xlabel('Generation')
ylabel('Objective Function Value')
hold on
P=population(p);
K=0;
[x1 y1]=size(P);
P1 = 0;
for i=1:tg   
    Cr=crossover(P,c);
    Mu=mutation(P,m);
    P(p+1:p+2*c,:)=Cr;
    P(p+2*c+1:p+2*c+m,:)=Mu;
.
.
.

population.m

function Y = population(n)
% n = population size

% It is noted that the number of bits to represent the variables 
% in binary numbers depends on the required accuracy (the number 
% of digits after comma)
.
.
.
Y=round(rand(n,40)); 

crossover.m

function Y=crossover(P,n)
% P = population
% n = number of pairs of chromosomes to be crossovered
[x1 y1]=size(P);
Z=zeros(2*n,y1); 
for i = 1:n
    r1=randi(x1,1,2);
    while r1(1)==r1(2)
        r1=randi(x1,1,2);
    end
    A1=P(r1(1),:); % parent 1
    A2=P(r1(2),:); % parent 2
    r2=1+randi(y1-1); % random cutting point
.
.
.

mutation.m

function Y=mutation(P,n)
% P = population
% n = chromosomes to be mutated
[x1 y1]=size(P);
Z=zeros(n,y1);
for i = 1:n
    r1=randi(x1);
    A1=P(r1,:); % random parent
    r2=randi(y1);
    if A1(1,r2)== 1
        A1(1,r2) = 0; % flick the bit
    else
        A1(1,r2) = 1;
    end
    Z(i,:)=A1;
end
Y=Z;

evaluation.m

function Y=evaluation(P)
[x1 y1]=size(P);
H=zeros(1,x1);
for i = 1:x1
    A=bi2de(P(i,1:y1/2));
    x=-3+A*(3-(-3))/(2^(y1/2)-1);
    B=bi2de(P(i,y1/2+1:y1));
    y=-3+B*(3-(-3))/(2^(y1/2)-1);
    H(1,i)= 3*(1-x)^2*exp(-x^2 - (y+1)^2)...
        - 10*(x/5 - x^3 - y^5)*exp(-x^2 - y^2) ...
        -1/3*exp(-(x+1)^2 - y^2);
end
Y=H; 

selection.m

function [YY1 YY2] = selection(P,F,p)
% P - population, F - fitness value, p - population size
.
.
.
Sorry! This is only a half of the code.

Notice: It’s possible to watch the video and re-type the Matlab code yourself – that would take you from 1 to 3 hours; or with just €1.99 (the cost of a cup of coffee), you can download/copy the whole Matlab code within 2 minutes. It’s your choice to make.

Original price is €4.99 but today it’s only €1.99 (save €3 today – available for a limited time only)

Download the whole Matlab code here (Membership Code ID: 016)

No need to build the Matlab code from scratch because it’s very time-consuming. My idol, Jim Rohn, once said: “Time is more value than money. You can get more money, but you cannot get more time”. If you think this code can be used in your research/teaching work, you should download it and then customize/modify/apply it to your work, without any obligation of citing the original source if you don’t want. However, redistribution (i.e., downloading the code/script here and then making it available on another site on the Internet) is strictly prohibited.

It is noted that this is the traditional genetic algorithm. For more advanced and robust versions of genetic algorithms with Matlab and Python codes, check these videos:

Adaptive Restart Binary Genetic Algorithm:

Python Code of Multi-Start Genetic Algorithm:

Adaptive Re-Start Hybrid Genetic Algorithm for Global Optimization:

If you have any question or problem, please contact Dr. Panda by email: learnwithpanda2018@gmail.com

Thank you very much and good luck with your research!

Dr.Panda

View Comments

Recent Posts

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

Hello everyone! In this post, I am going to show you my innovative version of…

7 months ago

Test Your Understanding About Genetic Algorithm (Test 2)

Hello everyone. Let’s take a test to check your understanding about genetic algorithm, with multiple…

7 months ago

Adaptive Restart Hybrid Genetic Algorithm

Hello everyone! In this post, I am going to show you my innovative version of…

7 months ago

Adaptive Re-Start Hybrid Genetic Algorithm (Test the Performance in Case Studies)

Hello everyone. In this post, I am going to show you my innovative version of…

1 year ago

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

Hello everyone! Let’s see how my innovative version of Genetic Algorithm, called Adaptive Re-start Hybrid…

1 year ago

Crypto Quiz (Test Your Knowledge About Cryptocurrency)

Hello everyone! Let’s take a short quiz, to test your knowledge about crypto-currency, or crypto.…

2 years ago