genetic algorithm on solving hfsp

1 view (last 30 days)
Zhao Yuxuan
Zhao Yuxuan on 10 Jul 2020
Here's my code of using ga to find the optimum schedule of the hybrid flow shop problem.
The question here is that I can't get the correct convergence line showed by LC1&LC2 at the end.
I don't see where go wrong. Could anyone maybe have a look at the code? Many thx!
function [Zp,Y1p,Y2p,Y3p,Xp,bp,LC1,LC2]=hfsp(M,N,Er,Pc,Pm,T,P)]
%1: variable initialization
[m,n]=size(T)
Xp=zeros(m,n);
bq=zeros(1,m);
LC1=zeros(1,M);
LC1=zeros(1,M);
%2: random generation of initial population
farm=cell(1,N);
farm_p=cell(1,N);
for k=1:N
X=zeros(m,n);
priority=randperm(m);
for r=1:n
for h=1:m
X(h,r)=1+(P(r)-eps)*rand;
end
end
farm{k}=X;
farm_p{k}=priority;
end
fitness=zeros(1,N);
farm_sel=cell(1,N);
counter=0;
sum_fitness=0;
while counter<M
for i=1:N
X=farm{i};
priority=farm_p{i};
[Zp,Y1p,Y2p,Y3p]=MakeSpan1(X,T,priority);
Z=Zp;
fitness(i)=1./Z;
end
%3: elitism
Elite_no=round(N*Er);
[max_val,indx]=sort(fitness,'descend');
for s=1:Elite_no
farm_sel{s}=farm{indx(s)};
fitness(s)=fitness(indx(s));
end
for s=Elite_no+1:N
farm_sel{s}=farm{s};
fitness(s)=fitness(s);
end
farm_elite=farm_sel;
%4: selection operator
CumuProb=zeros(1,N);
for i=1:N
CumuProb(i)=sum(fitness(1:i));
end
MaxProb=CumuProb(N);
for j=1:N
pChosen=rand*MaxProb;
Ind=find(CumuProb>pChosen,1);
farm_sel{j}=farm{Ind};
end
newfarm=farm_sel;
%4: crossover operator
Ser=randperm(N);
for i=1:2:(N-1)
A=farm{Ser(i)};
B=farm{Ser(i+1)};
if rand<Pc
cp=unidrnd(m-1);
a=[A(1:cp,:);B((cp+1):m,:)];
b=[B(1:cp,:);A((cp+1):m,:)];
newfarm{i}=a;
newfarm{i+1}=b;
else
newfarm{i}=A;
newfarm{i+1}=B;
end
end
farm_2=newfarm;
%5: mutation operator
for i=1:N
if rand<Pm
X=farm_2{i};
I=unidrnd(m);
J=unidrnd(n);
X(I,J)=1+(P(J)-eps)*rand;
farm_2{i}=X;
farm_3{i}=farm_2{i};
else
farm_3{i}=farm_2{i};
end
end
farm_3=farm_2;
for i=1:N
X=farm_3{i};
priority=farm_p{i};
[Zp,Y1p,Y2p,Y3p]=MakeSpan1(X,T,priority);
fitness(i)=1./Zp;
end
Cmin=min(Zp);
LC3(counter+1)=Cmin;
max_fitness=max(fitness);
mean_fitness=mean(fitness);
LC1(counter+1)=max_fitness;
LC2(counter+1)=mean_fitness;
pos=find(fitness==max_fitness);
Xp=farm_3{pos(1)};
X=Xp;
bp=farm_p{N};
[Zp,Y1p,Y2p,Y3p]=MakeSpan1(X,T,priority);
counter=counter+1;
for g=1:Elite_no
farm_3{g}=farm_elite{g};
farm=farm_3;
end
end
figure(2)
plot(LC3);
figure(1);
plot(LC1);
hold on
plot(LC2,'r');
end

Answers (0)

Tags

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!