Real-Coded Genetic Algorithm in Matlab

Hello everyone. I have successfully developed a powerful real-coded genetic algorithm for unconstrained global optimization problems. This genetic algorithm is very powerful – it can guarantee to find the global optimal solution for your optimization problem with a short computing time.

In this video, I demonstrate the performance of my genetic algorithm, and then I will show you the code of the genetic algorithm in Matlab. It is very easy to customize this genetic algorithm to solve your problems. All you have to do are: 1) add your objective function, and 2) define your problem. The rest will be automatically handled by the programme. By the way, I want to tell you that, a more advanced version of this genetic algorithm for constrained optimization problems will be done very soon. Let’s begin.

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

population.m

function Y=population(p,nv,lb,ub)
% nv = number of variables
% p = population size
% lb = Lower bound
% ub= Upper bound
for i = 1:p
    for j = 1:nv
        Y(i,j)=(ub(j)-lb(j))*rand+lb(j);
    end
end
Y = Y;

crossover.m

function Y=crossover(P,n)
% P = population
% n = pair 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),:);
    A2=P(r1(2),:);
.
.
.

mutation.m

function Y=mutation(P,n)
% P = population
% n = pair of chromosomes to be mutated
[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),:);
    A2=P(r1(2),:);
.
.
.

local_search.m

function Y=local_search(X,s,lb,ub)
% X = current best solution
% step size
[x y]=size(X);
A=ones(2*y,1)*X;
j=1;
for i=1:y
        L1=X(1,j)+s*rand;
        if L1 > ub(i)
            L1 = ub(i);
.
.
.

ObjectiveFunction.m

function Y = ObjectiveFunction(X)
% To customize this GA code for your problems, you only need 2 things.
% First, add objective function on the blank space on m.file named "ObjectiveFunction.m". 
% Second, define your problem on the blank space on m.file named "GA.m"
% The rest will be automatically handled by the programme.
[x1 y1]=size(X);
B=zeros(1,y1);
for i = 1:y1
    B(1,i)=X(1,i)^2;
end
Y = sum(B);

evaluation.m

function Y=evaluation(P,ot)
[x1 y1]=size(P);
H=zeros(x1,1);
for i = 1:x1
   H(i,1)= ObjectiveFunction(P(i,:)); 
end
if ot == 1
    Y = H;
else
    Y=10^6-H;
end

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

clear all
clc
close all
tic
%--------------------------------------------------------------------------
% To customize this GA code for your problems, you only need 2 things.
% First, add objective function on the blank space on m.file named "ObjectiveFunction.m". 
% Second, define your problem on the blank space on m.file named "GA.m"
% The rest will be automatically handled by the programme.
nv = 2;                    % number of variables
lb = [-10 -10];            % lower bound
ub = [10 10];              % upper bound
ot = -1;                   % minimization ot = -1; maximization ot = 1
t =  3;                    % computing time (s)
%--------------------------------------------------------------------------
% Maximize performance of the GA (optional)
p=50; % population size
c=10;  % crossover rate
m=10; % mutation rate
s=5;  % adaptive restart search process
g=3;  % keep top chromosomes
r=3; % number of chromosomes in initial guess
ms = 0.01; % max step size for local search
%-------------------------------------------------------------------
% Stoping criteria
tg=10000000; % number of generattion - set be be large to use computing time
%--------------------------------------------------------------------------
P1=population(r, nv, lb, ub); % Initial guess
w=1;

for j = 1:tg
    P=population(p-r, nv, lb, ub);
    P(p-r+1:p,:)=P1;
    for i=1:tg   
        % Extended population
        P(p+1:p+2*c,:)=crossover(P,c);
        P(p+2*c+1:p+2*c+2*m,:)=mutation(P,m);
.
.
.
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: 008)

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 “Real-Coded Genetic Algorithm in Matlab”

  1. I want to download this whole matlab code. but i did not know how.
    After clicking the link, I wrote username, password, and email address.
    And then what do I have to do?

    1. Thanks for your interest. You need to have the membership … Just follow the instruction after clicking the link.

Leave a Reply

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