Testing My Adaptive Restart Genetic Algorithm (Matlab Code)

Hello everyone and welcome!

In this video, I’m going to test my adaptive genetic algorithm in Matlab.

This is an innovative version of traditional genetic algorithms because it has a mechanism to restart its search process whenever it’s getting stuck in local optima. In addition, this genetic algorithm has a local search. Therefore, this genetic algorithm is very powerful, and it can guarantee to find the global optimal solution for various optimization problems with a short computing time.

Here are details of the benchmark function:

Here is the innovative structure of my adaptive genetic algorithm:

Let’s see how my adaptive genetic algorithm works:

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

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.
y=(sum(sin(x) .^2, 2) - exp(-sum(x .^ 2, 2))) .* exp(-sum(sin(sqrt(abs(x))) .^2, 2));

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);
        end
.
.
.

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),:);
    Fn(i)=B(max(c1));
    P1(max(c1),:)=[];
    B(max(c1))=[];
    clear r1 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=20;  % adaptive restart search process
g=3;  % keep top chromosomes
r=3; % number of chromosomes in initial guess
ms = 0.001; % 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);
.
.
.
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: 015)

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 *