Matlab Code of Adaptive Restart Hybrid Genetic Algorithm

Hi everyone, this is an adaptive restart hybrid genetic algorithm. This genetic algorithm is designed to overcome the weakness of the traditional genetic algorithm. First, this genetic algorithm can explore very large search space of the optimization problem because it has a mechanism to automatically restart its search process if getting stuck in local optima. Second, this genetic algorithm can effectively exploit the promising regions in the search space of the problem because it has a local search algorithm integrated inside the loop. Thereby, this genetic algorithm is very robust to search for global optimal solution for large scale optimization problems.

In this video, first, I will demonstrate the capability of the genetic algorithm in solving an optimization problem with 60 variables, and then I will show you the Matlab code of the genetic algorithm. If you want, you can copy the Matlab code and customize it to solve your own problems. Let’s see.

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

population.m

function Y=population(p,d,LR,UR)
% d = dimensions of the problem
% p = population size
% LR = Lower bound
% UR = Up bound
Y=(UR-LR)*rand(p,d)+LR;

crossover.m

function Y=crossover(X,n)
% X = population
% n = no of pairs of chromosomes crossover
[x y]=size(X);
E=zeros(2*n,y);
for i= 1:n
    r=randi(x,1,2);
    while r(1)==r(2)
        r=randi(x,1,2);
    end
    % Note: r = selec two chromsomes for crossover
    A=X(r(1),:); % chromosome 1 for crossover
    B=X(r(2),:); % chromosome 2 for crossover
.
.
.

mutation.m

function Y=mutation(X,n)
% X = population
% n = no of pairs of chromosomes crossover
[x y]=size(X);
E=zeros(2*n,y);
for i= 1:n
    r=randi(x,1,2);
    while r(1)==r(2)
        r=randi(x,1,2);
    end
    % Note: r = selec two chromsomes for mutation
    A=X(r(1),:); % chromosome 1 for mutation
    B=X(r(2),:); % chromosome 2 for mutation
.
.
.  

local_search.m

function YY=local_search(X,n,s,LR,UR)
% X = current best solution
% n = no. of chromosomes seleected
%clc
%clear all
%close all
%X=population(1);
%=====================================================
[x y]=size(X);
A=ones(2*y,1)*X;
j=1;

for i=1:y
.
.
.

evaluation.m

function Y = evaluation(X)
% X = population
[x y]=size(X);
d=y; % number of dimensions
C=zeros(1,x);
for j=1:x
    P=X(j,:);
    B=zeros(2,d);
    for i=1:d
        x1=P(1,i);
        B(1,i)=x1^2;
        B(2,i)=cos(2*pi*x1);
    end
    Z=-20*exp(-0.2*sqrt(sum(B(1,:))/d))-exp(sum(B(2,:))/d)+20+exp(1);
    C(1,j)=1/Z; % obbjective function
end
Y=C;

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
for i =1:s
    [r1 c1]=find(B==max(B));
    Y1(i,:)=P1(max(c1),:);
    Fn(i)=1/B(max(c1));
.
.
.

GA.m

clear all
clc
close all
%--------------------------------------------------------------------------
dim = 60; % dimensions of the problem
LR=-15; % Lower bound
UR=30;  % Up bound
%----------------------------------------------------------------
p= 100; % population size
c= 2; % crossover rate
m= 10; % mutation rate
s= 150; % number of generations to restart if getting stuck in local minimum
g= 3; % elite chromosomes
r= 3;% number of initial chromosomes
n= 40; % no of chromosomes RAMDOMLY selected from local (in %)
tg=2000; % Total generations
redu=0.03;
%--------------------------------------------------------------------------
P1=population(r,dim,LR,UR); % Initial guess
w=1;
j=1;
K=0;
KK=0;
ww=1;
figure
title('Blue - Minimum         Red - Average');
xlabel('Generation')
ylabel('Objective function value')
hold on
while j <=tg
        P=population(p-r,dim,LR,UR);
        P(p-r+1:p,:)=P1;
        % Extended population
.
.
.
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 €3.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 €9.99 but today it’s only €3.99 (save €6 today – available for a limited time only)

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

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!

Leave a Reply

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