A Simple Crossover in Genetic Algorithm in Matlab

Hello everyone.

In this post, I’m going to talk about a basic concept of crossover in Genetic Algorithm and how to implement it in Matlab. More importantly, at the end of the video, I will show you how my innovative version of Genetic Algorithm with that kind of simple crossover works when solving this very difficult global optimization problem.

Here are the details of the benchmark problem to be solved in this video. As we can see, this problem is very difficult to find the global optimal solution, especially for search-based optimization algorithms, because the traditional optimization algorithms are more likely to get stuck in the big local optima.

Here is the working principle of a simple  one-cutting point crossover in Genetic Algorithm. Basically, this is just a process of cut and swap as illustrated in this Figure. Please keep in mind that these two parent chromosomes are randomly selected. Now, let’s see how we implement this crossover in Matlab.

Matlab Code

population.m

function Y = population(n)
% n = population size
% 40 = number of bits to express the number. Why? It depends on
% the required accuracy (the number of digits after comma) that we want
Y=round(rand(n,40));

crossover.m

function Y=crossover(P,n)
% P = population
% n = pair of chromosomes to be crossovered
[x1 y1]=size(P);
Z=zeros(2*n,40);
for i = 1:n
    r1=randi(x1,1,2);
    while r1(1)==r1(2)
        r1=randi(x1,1,2);
    end
    A1=P(r1(1),:);
    A2=P(r1(2),:);
    r2=1+randi(39);
    B1=A1(1,r2:40);
    A1(1,r2:40)=A2(1,r2:40);
    A2(1,r2:40)=B1;
    Z(2*i-1,:)=A1;
    Z(2*i,:)=A2;
end
Y=Z;

mutation.m

function Y=mutation(P,n)
% P = population
% n = chromosomes to be mutated
[x1 y1]=size(P);
Z=zeros(n,40);
for i = 1:n
    r1=randi(x1);
    A1=P(r1,:);
    r2=randi(40,1,2);
    while r2(1)==r2(2)
        r2=randi(40,1,2);
    end
    B1=A1(1,r2(1));
    A1(1,r2(1))=A1(1,r2(2));
    A1(1,r2(2))=B1;
    Z(i,:)=A1;
end
Y=Z;

evaluation.m

function Y=evaluation(P)
[x1 y1]=size(P);
H=zeros(x1,1);
for i = 1:x1
    A=bi2de(P(i,1:20));
    x=-40+A*(40-(-40))/(2^20-1);
    B=bi2de(P(i,21:40));
    y=-40+B*(40-(-40))/(2^20-1);
    H(i,1)= 0.4/(1+0.02*((x-(-20)).^2 +(y-(-20)).^2))...
    + 0.2/(1+0.5*((x-(-5)).^2 +(y-(-25)).^2))...
    + 0.7/(1+0.01*((x-(0)).^2 +(y-(30)).^2))...
    + 1/(1+2*((x-(30)).^2 +(y-(0)).^2))...
    + 0.05/(1+0.1*((x-(30)).^2 +(y-(-30)).^2));
end
Y=H;

selection.m

function [YY1 YY2] = selection(P1,B,p,s)
% P1 - population
% B - fitness value 
% p - population size
% s = select top s chromsomes
%-------------------------------------------------------------------------
% Top selection operation
B=B';
for i =1:s
    [r1 c1]=find(B==max(B));
    Y1(i,:)=P1(max(c1),:);
.
.
.

GA.m

% Main Code
clear all
clc
close all
tic
%--------------------------------------------------------------------------
% Population size
p =100;
% Number of pairs of chromosomes required for crossover
c=5;
% Number of chromosomes required for mutation
m=90;
% Adaptive stopping = no. of generations not improve quality
s=5;
% Number of chromosomes passing between generation
g=3;
% Number of chromosomes passing between runs
r=10;
%-------------------------------------------------------------------
% Computing time (s)
t = 30;
tg=1000000; % total number of generations (tg). We set tg very large to
% ensure that the GA will be stopped by computing time (t)
%--------------------------------------------------------------------------
figure
title('Blue - Average         Red - Maximum');
xlabel('Generation')
ylabel('Objective Function Value')
hold on
P1=population(r); % Initial guess
w=1;
ww=1;
KK = 0;
for j = 1:tg
    P=population(p-r);
    P(p-r+1:p,:)=P1;
    for i=1:tg   
        % Extended population
        P(p+1:p+2*c,:)=crossover(P,c);
.
.
.
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 €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 €4.99 but today it’s only €2.99 (save €2 today – available for a limited time only)

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

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!

2 Replies to “A Simple Crossover in Genetic Algorithm in Matlab”

  1. Greetings Dr,
    please, i want to know how to solve chaotic system with GA following your method and using your codes.
    cordially, Stève.

Leave a Reply

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