Solving Optimization Problems

Matlab Code of Genetic Algorithm for Precedence-constrained Production Sequencing and Scheduling

Hello everyone! I’m going to show you a Genetic Algorithm for precedence-constrained production sequencing and scheduling problem.

This is a highly constrained optimization problem but Genetic Algorithm can handle this complexity.

In this video, first, I show you the problem description, and then I will show you some genetic operators such as chromosome encoding, crossover, mutation, evaluation, and selection. Next, I will run the Genetic Algorithm in Matlab to demonstrate its performance. Finally, I show you the details of the Matlab code so that you can copy, and customize it to solve your problems.

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

File 1: population.m

function [y0 y1]= population(n)
F1=zeros(1,40); % Generate zero matrix
F2 = F1;
F3 = F1;
F=F1;
O=F1;
%==========================================================================
% Labor requirement
L=[22 25 31 31 28 36 14 25 19 23 27 13 18 35 33 19 33 30 24 24 34 32 24 15 30 30 14 28 36 31 15 25 31 15 23 25 31 17 22 21];
% Material requirement
M =[6 86 55 88 63 11 6 6 77 47 73 72 19 42 22 73 56 65 59 22 42 32 56 19 29 42 34 84 18 31 83 35 89 47 65 16 31 48 68 59];
% Cost to produce the part
C=[15 21 22 8 10 13 11 3 24 23 3 6 3 10 3 14 3 2 2 1 18 16 5 19 1 4 8 12 6 6 22 1 6 16 15 23 11 17 16 11];
% Minimum number of parts required to produce
np = 12;
% Cost of running company per hour ($k)
cc = 0.17;
% Labor available
la = 650;
% Material available
ma = 1100;
% Working capital available
wa = 400;
% Product changeover
po = 2;
%==========================================================================
for k=1:n
    tt=1;
    while tt < np
% Randomly generate chromosome 
I=round(1+39*abs(rand(1,40)));
%I=sapsep(I);
%--------------------------------------------------------------------------
% Cut the chromosome based on labor constraint
A1=zeros(1,1);
A1(1) = L(I(1));
i1=2;
while sum(A1) <= la % labor in hours limited
    if I(i1) ==I(i1-1) % no changeover time
        A1(i1)=L(I(i1));
    else
        A1(i1)=L(I(i1))+po; % changeover time involved
    end
    i1=i1+1;
end 
% The feasible chromosome based on labor        
F1(k,1:i1-2)=I(1:i1-2); % the number of part selected to produce is i1-2
%--------------------------------------------------------------------------
% Cut the chromosome based on material constraint
A2=zeros(1,1);
A2(1) = M(I(1));
i2=2;
while sum(A2) <= ma % material available
        A2(i2)=M(I(i2));
        i2=i2+1;
end 
% The feasible chromosome based on material       
F2(k,1:i2-2)=I(1:i2-2); % the number of part selected to produce is i2-2
%--------------------------------------------------------------------------
% Cut the chromosome based on working capital constraint
A3=zeros(1,1);
A3(1) = C(I(1));
i3=2;

while sum(A3) <= wa % working capital constraint
    if I(i3) ==I(i3-1) % no changeover time
        g=0;
    else
        g=po; % changeover time involved
    end
    A3(i3)=C(I(i3))+ cc*(L(I(i3))+ g); % Including producing cost and running company cost
    i3=i3+1;
end 
% The feasible chromosome based on material       
F3(k,1:i3-2)=I(1:i3-2); % the number of part selected to produce is i3-2
%--------------------------------------------------------------------------
% Final chromosome that satisfies all of above constraints is determined as follows
f(1)=i1-2;
f(2)=i2-2;
f(3)=i3-3;
p=min(f); % the position to cut the initial chromosome
%-------------------------------------------------------------------
% The following commands ensure at least 12 different parts to be selected
% p - cutting point
B=zeros(40,40);
for i=1:40;
for j=1:p
    if I(j)==i
        B(i,i)=1;
    end
end
end
tt=sum(sum(B)); % total number of different parts
end
    F(k,1:p)=I(1:p); 
    O(k,:)=I;
end
%disp('It is noted that the chromosome before cutting operation is y0 and chromosome after cutting operation is y1');
y0=O;
y1=F;
end

File 2: crossover.m

function y= crossover(y0,y1,t)
F=zeros(2*t,40); % Generate zero matrix
Q=zeros(2,40);
Q1=zeros(1,40);
Q2=Q1;
k=1;
%==========================================================================
% Labor requirement
L=[22 25 31 31 28 36 14 25 19 23 27 13 18 35 33 19 33 30 24 24 34 32 24 15 30 30 14 28 36 31 15 25 31 15 23 25 31 17 22 21];
% Material requirement
M =[6 86 55 88 63 11 6 6 77 47 73 72 19 42 22 73 56 65 59 22 42 32 56 19 29 42 34 84 18 31 83 35 89 47 65 16 31 48 68 59];
% Cost to produce the part
C=[15 21 22 8 10 13 11 3 24 23 3 6 3 10 3 14 3 2 2 1 18 16 5 19 1 4 8 12 6 6 22 1 6 16 15 23 11 17 16 11];
% Minimum number of parts required to produce
np = 12;
% Cost of running company per hour ($k)
cc = 0.17;
% Labor available
la = 650;
% Material available
ma = 1100;
% Working capital available
wa = 400;
% Product changeover
po = 2;
[r c]=size(y0);
w=1;
%-------------------------------------------------------------------------
for i=1:t
    tt1=1;
    tt2=1;
    d=1;
        while (tt1 < np) | (tt2 < np)
% Randomly select two parent chromosomes from initial populations (y0) for
% crossover operation
h=round(1+(r-1)*rand(1,2));
.
.
.

File 3: mutation.m

function y= mutation(y0,y1,t)
F=zeros(1,40); % Generate zero matrix
I=zeros(2,40);
Q=zeros(2,40);
k=1;
%==========================================================================
% Labor requirement
L=[22 25 31 31 28 36 14 25 19 23 27 13 18 35 33 19 33 30 24 24 34 32 24 15 30 30 14 28 36 31 15 25 31 15 23 25 31 17 22 21];
% Material requirement
M =[6 86 55 88 63 11 6 6 77 47 73 72 19 42 22 73 56 65 59 22 42 32 56 19 29 42 34 84 18 31 83 35 89 47 65 16 31 48 68 59];
% Cost to produce the part
C=[15 21 22 8 10 13 11 3 24 23 3 6 3 10 3 14 3 2 2 1 18 16 5 19 1 4 8 12 6 6 22 1 6 16 15 23 11 17 16 11];
% Minimum number of parts required to produce
np = 12;
% Cost of running company per hour ($k)
cc = 0.17;
% Labor available
la = 650;
% Material available
ma = 1100;
% Working capital available
wa = 400;
% Product changeover
po = 2;
v=1;

for i=1:t
%--------------------------------------------------------------------------
% Randomly select two parent chromosomes from initial populations (y0) for
% mutation operation
[r c]=size(y0);
h=round(1+(r-1)*abs(rand(1,2)));
 if h(1)==h(2)
     h=round(1+(r-1)*abs(rand(1,2))); % to reduce probability of getting two same numbers
 end
I(1,:)=y0(h(1),:); % Selected parent 1
I(2,:)=y0(h(2),:);  % Selected parent 2
tt1=1;
tt2=1;
d=1;
while (tt1 <np)| (tt2 < np)
.
.
.

File 4: evaluation.m

function y=evaluation(W)
[u v]=size(W);
R=zeros(1);
%--------------------------------------------------------------------------
for e=1:u
    x=W(e,:);
%--------------------------------------------------------------------------
A=zeros(1,1);
B=zeros(1,1);
T=A;
D=A;
S1=zeros(1,v);
%--------------------------------------------------------------------------
% Labor requirement
L=[22 25 31 31 28 36 14 25 19 23 27 13 18 35 33 19 33 30 24 24 34 32 24 15 30 30 14 28 36 31 15 25 31 15 23 25 31 17 22 21];
% Cost to produce the part
C=[15 21 22 8 10 13 11 3 24 23 3 6 3 10 3 14 3 2 2 1 18 16 5 19 1 4 8 12 6 6 22 1 6 16 15 23 11 17 16 11];
% Cost of running company per hour ($k)
cc = 0.17;
% Price to sell the part
P=[30 44 50 20 30 20 23 9 55 45 10 14 8 22 9 22 8 7 9 8 33 40 22 45 2 9 16 23 9 10 30 9 12 20 30 31 22 25 22 27];
% Deadline of the parts
DL=[30 25 10 20 25 15 30 10 25 10 25 20 5 10 25 15 5 25 10 15 25 30 25 30 15 25 20 25 30 10 15 30 5 20 10 15 20 5 30 25];
% Product changeover
po = 2;
% Labor available
la = 650;
%--------------------------------------------------------------------------
% Determine total income (I)
for i = 1:v
    if x(i) >0
    A(1,i) = P(x(i));
    end
end
I = sum(A);
%--------------------------------------------------------------------------
% Determine total cost of producing parts (CP)
for j = 1:v
    if x(j) >0
    B(1,j) = C(x(j));
    end
end
CP = sum(B);
.
.
.

File 5: selection.m

function y = selection(A,B,p)
[m n]=size(A);
F=zeros(p,n);
% Determine total fitness for the population
C=sum(B);
% Determine selection probability
D=B/C;
% Determine cumulative probability 
E= cumsum(D);
% Generate a vector constaining normalised random numbers
N=sort(rand(1,p));
d1=1;
d2=1;
% Select the chromosomes for new population using roulette wheel approach 
[r1 c1]=find(B==max(B));
F(1,:)=A(max(c1),:); % select the best one first
while d2 <= p-1
.
.
.

File 6: GA.m

clear all
clc
close all
%--------------------------------------------------------------------------
% Number of generation required
g= 1000;
% Population size
p =100;
% Number of pairs of chromosomes required for crossover
t=30;
% Number of pairs of chromosomes required for mutation
t1=20;
%--------------------------------------------------------------------------
% Generate the zeros matrix
E=zeros(p+2*t+2*t1,40);
% Randomly generate the initial population
[y0 A0]=population(p);
for i=1:g
    % Crossover operation
    A1=crossover(y0,A0,t);
    % Mutation operation
    A2=mutation(y0,A0,t1);
    % Extended population
    E(1:p,:)=A0;
    E(p+1:p+2*t,:)=A1;
    E(p+2*t+1:p+2*t+2*t1,:)=A2;
.
.
.
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 €1.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 €1.99 (save €3 today – available for a limited time only)

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

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

View Comments

Recent Posts

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

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

8 months ago

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