Binary Chromosome Encoding in Genetic Algorithm in Matlab

Hello everyone!

In this post, I am going to talk about the binary chromosome encoding in Genetic Algorithm in Matlab. More specifically, I will show you how to select the number of bits of the binary chromosome to correctly represent the solutions.

Here are the details of the optimization problem to be solved in this video.

Let’s see how it works . . .

……………………..

Matlab Code

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,33)); 

evaluation.m

function Y=evaluation(P)
[x1 y1]=size(P);
H=zeros(1,x1);
for i = 1:x1
    A=bi2de(P(i,1:18));
    x1=-3+A*(12.1-(-3))/(2^(18)-1);
    B=bi2de(P(i,18+1:y1));
    x2=4.1+B*(5.8-4.1)/(2^(15)-1);
    H(1,i)= 21.5 + x1*sin(4*pi*x1) + x2*sin(20*pi*x2);

end
Y=H; 

GA.m

clear all
close all
clc
%--------------------------------------------------------------------------
p=100; % Population size
c=50; % number of pairs of chromosomes to be crossovered
m=30; % 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;
.
.
.
Sorry! This is only a half of the Matlab code. 

Notice: It would take you from 1 to 3 hours to re-type the Matlab code yourself; or with just €2.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 €5.99 but today it’s only €2.99 (save €3 today – available for a limited time only)

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

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.

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 and publications!

4 Replies to “Binary Chromosome Encoding in Genetic Algorithm in Matlab”

Leave a Reply

Your email address will not be published. Required fields are marked *