Solving Optimization Problems

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

function YY=population(p,nv,lb,ub)
% p = population size
% nv = number of variables
% 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
YY = Y;

function Y=crossover(P,n)
% P = population
% n = the number of pairs of chromosomes to be ...
% crossed (equivalent to the trational crossover rate)
[x1 y1]=size(P);
Z=zeros(2*n,y1);
for i = 1:n
    r1=randi(x1,1,2); % select 2 random parent chromosomes
    while r1(1)==r1(2)% make sure 2 selected chromosomes are not the same
        r1=randi(x1,1,2);
    end
    A1=P(r1(1),:); % parent 1
    A2=P(r1(2),:); % parent 2
    r2=1+randi(y1-1); % random cut point
    B1=A1(1,r2:y1); % swap the 2 parts
    A1(1,r2:y1)=A2(1,r2:y1); % swap the 2 parts
    A2(1,r2:y1)=B1; % swap the 2 parts
    Z(2*i-1,:)=A1;
    Z(2*i,:)=A2;
end
Y=Z;

function Y=mutation(P,n)
% P = population
% n = the number of pairs of chromosomes to ...
% be mutated (equivalent to the trational mutation rate)
[x1 y1]=size(P);
Z=zeros(2*n,y1);
for i = 1:n
    r1=randi(x1,1,2);
    while r1(1)==r1(2)% make sure 2 selected chromosomes are not the same
        r1=randi(x1,1,2);
    end
    A1=P(r1(1),:); % parent 1
    A2=P(r1(2),:); % parent 2
    r2=randi(y1); % random gene
    A0 = A1(r2); % swap the selected gene
    A1(r2)=A2(r2); % swap the selected gene
    A2(r2)=A0; % swap the selected gene
    Z(2*i-1,:)=A1;
    Z(2*i,:)=A2;
end
Y=Z;

function Y=local_search(X,s,lb,ub)
% X = current best chromosome
% s = step size
% lb = Lower bound
% ub = Upper bound

[x y]=size(X);
A=ones(2*y,1)*X;
j=1;
for i=1:y
.
.
.
end
Y = A;
function Y = objective_function(X)
d = length(X); % dimensions of the problem

% Parameters of the problem
a = 20;
b = 0.2;
c = 2*pi;

A1 = 0;
A2 = 0;
for i = 1:d
 xi = X(i); % variable xi
 A1 = A1 + xi^2;
 A2 = A2 + cos(c*xi);
end
Y = -a*exp(-b*sqrt(A1/d)) - exp(A2/d) + a + exp(1);
end

function YY=evaluation(P,ot,co)
% P = population
% ot = optimization type, max or min
% co = coefficient for converting min to max problem (to make sure ...
% that the objective function is always positive
[x1 y1]=size(P);
H=zeros(1,x1);
for i = 1:x1
   H(i)= objective_function(P(i,:)); 
end
% depending on type of optimization
if ot == 1 % for maximization problem
    Y = H + co; % add co to make sure all elements in Y are positive
else       % for minimization problem
    K=zeros(1,x1);
    for i = 1:x1
        K(i) = 1/(co + H(i)); % convert from min to max
    end
    Y = K;
end
YY = Y;

function [YY1 YY2] = selection(P,B,p,s)
% P - population
% B - fitness value 
% p - population size
% s = Keep top s chromsomes
%------------------------------
% Top selection operation
[x1 y1]=size(P);
Y1 = zeros(p,y1);
Fn = zeros(1,p);
for i =1:s
    [r1 c1]=find(B==max(B));
    Y1(i,:)=P(max(c1),:);
    Fn(i)=B(max(c1));
    P(max(c1),:)=[]; % remove
    B(:,max(c1))=[]; % remove
end
%------------------------------
% Determine total fitness for the population
C=sum(B);
% Determine selection probability
D=B/C;
% Determine cumulative probability 
E= cumsum(D);
N=rand(1);
d1=1;
d2=s;
while d2 <= p-1
    if N <= E(d1)
.
.
.
end
YY1=Y1;
YY2=Fn;
clear all
clc
close all
tic
%--------------------------------------------------------------------------
% The problem parameters:
nv = 10;                              % number of variables
lb = -32.768*ones(1,nv);              % lower bound = -32.768
ub =  32.768*ones(1,nv);              % upper bound = 32.768

ot =-1;                 % minimization ot = -1; maximization ot = 1
%--------------------------------------------------------------------------
% Parameters of the GA:
p=100;   % population size
c=30;    % the number of pairs of chromosomes to be crossed (equivalent to the trational crossover rate)
m=20;    % the number of pairs of chromosomes to be mutated (equivalent to the trational mutation rate)
rs=10;   % adaptive restart search process (generations)
g=5;     % keep top chromosomes
r=5;     % number of chromosomes in initial guess
msl=1;   % max step size for local search
co=20;   % coefficient for converting min to max problem (to make sure that the objective function is always positive
%--------------------------------------------------------------------------
% Termination criteria:
t =180;         % computing time (s)
tg = 1000;      % the number of generations of the GA
%--------------------------------------------------------------------------
figure
xlabel('Generation')
ylabel('Objective function value')
if ot ==1
    title('Blue dots = Maximum value         Red dots = Average value');
else
    title('Blue dots = Minimum value         Red dots = Average value');
end
hold on

P1=population(r, nv, lb, ub); % Initial guess
w=1;
ww=1;
i = 1;
j = 1;
while j <= tg
    P=population(p-r, nv, lb, ub);
    P(p-r+1:p,:)=P1;
    ms = msl;
    while i <= 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);
        P(p+2*c+2*m+1:p+2*c+2*m+2*nv,:)=local_search(P(1,:),ms,lb,ub);
.
.
.
Sorry! This is only a half of the Matlab code!

Do you want to get the whole Matlab code of my innovative Genetic Algorithm to use in your research, thesis, report, and publications?

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 €4.99 (the cost of a cup of coffee), you can download/copy the whole Matlab code within 3 minutes. It’s your choice to make.

Original price is €9.99 but today it’s only €4.99 (save €5 today – available for a limited time only)

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

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!

Dr.Panda

Recent Posts

Test Your Understanding About Genetic Algorithm (Test 2)

Hello everyone. Let’s take a test to check your understanding about genetic algorithm, with multiple…

8 months ago

Adaptive Restart Hybrid Genetic Algorithm

Hello everyone! In this post, I am going to show you my innovative version of…

8 months ago

Adaptive Re-Start Hybrid Genetic Algorithm (Test the Performance in Case Studies)

Hello everyone. In this post, I am going to show you my innovative version of…

1 year ago

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

Hello everyone! Let’s see how my innovative version of Genetic Algorithm, called Adaptive Re-start Hybrid…

1 year ago

Crypto Quiz (Test Your Knowledge About Cryptocurrency)

Hello everyone! Let’s take a short quiz, to test your knowledge about crypto-currency, or crypto.…

2 years ago

Test Your Crypto Knowledge (Test 2)

Hello everyone! Let’s take a test to check your knowledge in crypto-currency, or crypto. From…

2 years ago