function main()
clear;
clc;
Num_Work=6;
Num_Pop1=100;
Num_Pop2=100;
Num_Exc=5;
Pro_C1=0.8;
Pro_C2=0.2;
Pro_M1=0.2;
Pro_M2=0.05;
Num_Gene=250;
[Time,Matrix]=SHL();
Pop1=Ini_Pop(Matrix,Num_Pop1);
Pop2=Ini_Pop(Matrix,Num_Pop2);
Pop11=Pop1;
Pop21=Pop2;
for i=1:1:Num_Gene
Dec_Pop1=Decoding(Pop11,Time,Num_Work);
Fit_Pop1=Fitness(Dec_Pop1,Num_Work,Time,Pop11);
Sel_Pop1=Selection(Fit_Pop1,Num_Pop1,Pop11);
Cro_Pop1=Crossover(Sel_Pop1,Pro_C1);
Mut_Pop1=Mutation(Cro_Pop1,Pro_M1,Matrix);
Pop12=Mut_Pop1;
Dec_Pop12=Decoding(Pop12,Time,Num_Work);
Fit_Pop12=Fitness(Dec_Pop12,Num_Work,Time,Pop12);
[a1 b1]=max(Fit_Pop1);
[a2 b2]=min(Fit_Pop12);
Pop12(b2,:)=Pop11(b1,:);
Dec_Pop2=Decoding(Pop21,Time,Num_Work);
Fit_Pop2=Fitness(Dec_Pop2,Num_Work,Time,Pop21);
Sel_Pop2=Selection(Fit_Pop2,Num_Pop2,Pop21);
Cro_Pop2=Crossover(Sel_Pop2,Pro_C2);
Mut_Pop2=Mutation(Cro_Pop2,Pro_M2,Matrix);
Pop22=Mut_Pop2;
Dec_Pop22=Decoding(Pop22,Time,Num_Work);
Fit_Pop22=Fitness(Dec_Pop22,Num_Work,Time,Pop22);
[a3 b3]=max(Fit_Pop2);
[a4 b4]=min(Fit_Pop22);
Pop22(b4,:)=Pop21(b3,:);
% 种群1与种群2交叉
[Pop11,Pop21]=Exchange(Pop12,Pop22,Num_Exc,Time,Num_Work);
%求出每次迭代的最优结果,并画图显示
Dec_Pop01=Decoding(Pop11,Time,Num_Work);
Fit_Pop01=Fitness(Dec_Pop01,Num_Work,Time,Pop11);
[a01 b01]=max(Fit_Pop01);
Dec_Pop02=Decoding(Pop21,Time,Num_Work);
Fit_Pop02=Fitness(Dec_Pop02,Num_Work,Time,Pop21);
[a02 b02]=max(Fit_Pop02);
if a01>a02
Pop00=Pop11(b01,:);
Dec_Pop00=Decoding(Pop11(b01,:),Time,Num_Work);
else
Pop00=Pop21(b02,:);
Dec_Pop00=Decoding(Pop21(b02,:),Time,Num_Work);
end
Draw(Pop00,Dec_Pop00,Time); % 绘图函数
pause(0.0010); % 延时函数
if (max(Dec_Pop00)==52) % 判断迭代是否完成
msgbox({'迭代完成,迭代次数为',num2str(i)})
break;
end
end
% 迭代完成后计算最终结果
Dec_Pop1=Decoding(Pop11,Time,Num_Work);
Fit_Pop1=Fitness(Dec_Pop1,Num_Work,Time,Pop11);
[a1 b1]=max(Fit_Pop1);
Dec_Pop2=Decoding(Pop21,Time,Num_Work);
Fit_Pop2=Fitness(Dec_Pop2,Num_Work,Time,Pop21);
[a2 b2]=max(Fit_Pop2);
if a1>a2
Pop=Pop11(b1,:)
Dec_Pop=Decoding(Pop11(b1,:),Time,Num_Work)
else
Pop=Pop21(b2,:)
Dec_Pop=Decoding(Pop21(b2,:),Time,Num_Work)
end
Draw(Pop,Dec_Pop,Time);
save data
Time=[6 2 5 7 1 2 3 6 5 5 4 ];
SHL=[0 1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0]
function[RESULT]=Ini_Pop(M,N)
[a b]=size(M);
RESULT=zeros(N,1); % 初始化
for k=1:1:N
M1=M;
j=1;
while (j<=a)
t=0;
Flag=0;
for i=1:1:a % 寻找全0的列
if (M1(:,i)==0)
t=t+1;
Flag(t)=i;
end
end
rand1=randperm(t); % 在全0列中选则下一个工序
if (Flag(rand1(1,1))==RESULT(k,:)) % 判断是否是已经选择的数字
RESULT(k,j)=Flag(rand1(1,1));
M1(Flag(rand1(1,1)),:)=0; % 选中列所在行置0
j=j+1;
end
end
end
function[dec_result]=Decoding(Population,Time,Num_Work)
RESULT=Population;
RESULT_Time=Time(RESULT); % 将加工顺序转换为加工时间
[a b]=size(RESULT_Time);
SUM=sum(Time); % 总加工时间
AVE=SUM/Num_Work; % 平均加工时间
AVE1=max(AVE,max(Time)); % 最小工位节拍(理论)
for k=1:1:a
AVE1=max(AVE,max(Time));
RESULT_Time1=[RESULT_Time(k,:) 0]; % 为了便于迭代,在末尾补0
c2=0;
c1=SUM;
% 利用潜在增量方法迭代
while c2<c1 % 如果存在潜在增量则进行迭代
j=1;
WORKSPACE=zeros(1,b);
for i=1:1:b % 将加工单元分配到工作站
if WORKSPACE(j)+RESULT_Time1(i)>AVE1
c3(j)=i;
j=j+1;
end
WORKSPACE(j)=WORKSPACE(j)+RESULT_Time1(i);
end
WORKSPACE(Num_Work)=SUM-sum(WORKSPACE(1:Num_Work-1));
% 计算潜在增量
WORKSPACE1=WORKSPACE;
i=1:1:Num_Work-1;
WORKSPACE1(i)=WORKSPACE1(i)+RESULT_Time1(c3(i)); % 计算潜在增量
c1=max(WORKSPACE(1:Num_Work));
c2=min(WORKSPACE1(1:Num_Work-1)); % 改进后结果
AVE1=min(c1,c2);
end
dec_result(k,:)=WORKSPACE(1:Num_Work); % 译码结果
end
function[Fit]=Fitness(Dec_Pop,Num_Work,Time,Pop)
% 按时间最小的适应度
SUM=sum(Dec_Pop(1,:));
C=max(Dec_Pop');
Fit1=SUM./Num_Work./C;
% 按工位分配相对均匀的适应度
[a b]=size(Dec_Pop);
Fit2=ones(a)/(var(Dec_Pop')+0.01); % var 求方差分母加0.01为了防止分母为0
% 按是否在指定工位的适应度
RESULT_Time=Time(Pop);
[a b]=size(Pop);
for i=1:1:a
flag(i)=0; % 置0
[a1 b1]=find(Pop(i,:)==34); % 寻找出第34号工序所在位置
AA1(i)=sum(RESULT_Time(i,1:b1)); % 计算34号之前工序所用总时间
BB1_1(i)=sum(Dec_Pop(i,1:3)); % 计算前3号工位的总时间
BB1_2(i)=sum(Dec_Pop(i,1:4)); % 计算前4号工位的总时间
% 判断34号工序是否在第3第4之间(即第4号工位)
% (如果总时间大于前三个工位小于前四个工位,说明在4号工位加工)
if ((AA1(i)>BB1_1(i))&&(AA1(i)<BB1_2(i)))
flag(i)=flag(i)+3;
end
[a2 b2]=find(Pop(i,:)==26); % 寻找出第26号工序所在位置
AA2(i)=sum(RESULT_Time(i,1:b2)); % 计算26号之前所用的总时间
BB2(i)=sum(Dec_Pop(i,1:2)); % 计算前2号工位的总时间
% 判断26号工序是否在前2个工序内加工
if (AA2(i)<BB2(i))
flag(i)=flag(i)+3;
end
end
Fit=2*Fit1+Fit2'+flag;
function[Pop_1]=Selection(Fit_Pop,Num_Pop,Pop)
Pro_Pop=Fit_Pop/sum(Fit_Pop); % 种群中每个基因的概率
for j=1:1:Num_Pop
% 转轮选择法
RAND=rand();
for i=1:1:Num_Pop
if RAND<Pro_Pop(i)
N(j)=i; % 记录选择基因的序号
break;
end
RAND=RAND-Pro_Pop(i);
end
end
Pop_1=Pop(N,:);
function[Cross]=Crossover(Sel_Pop1,Pro_C)
[a1 b1]=size(Sel_Pop1);
d=randperm(a1);
n=fix(Pro_C*a1/2)*2; % 计算需要交叉的基因数目
% 采用两点交叉法,交换中间部分
for j=1:2:n
c=randperm(b1); % 计算需要交叉的基因
Cross1=Sel_Pop1(d(j),:); % 产生两个交叉点
Cross2=Sel_Pop1(d(j+1),:);
Head1=Cross1(1:min(c(1),c(2))); % 交换点头部
Body1=Cross1(min(c(1),c(2))+1:max(c(1),c(2))); % 交换点中部
Head2=Cross2(1:min(c(1),c(2)));
Body2=Cross2(min(c(1),c(2))+1:max(c(1),c(2)));
Cross1_1=Cross1;
Cross1_2=Cross1;
Cross1_3=Cross1;
Cross2_1=Cross2;
Cross2_2=Cross2;
Cross2_3=Cross2;
Cross1_1(min(c(1),c(2))+1:max(c(1),c(2)))=[]; % 删除要交换的部分
Cross2_1(min(c(1),c(2))+1:max(c(1),c(2)))=[]; % 删除要交换的部分
[a b]=size(Cross1_1);
for i=1:1:b
Cross2_2(find(Cross2_2==Cross1_1(i)))=[]; % 获得要交换的部分
Cross1_2(find(Cross1_2==Cross2_1(i)))=[]; % 获得要交换的部分
end
Cross1_3(min(c(1),c(2))+1:max(c(1),c(2)))=Cross2_2;
Cross2_3(min(c(1),c(2))+1:max(c(1),c(2)))=Cross1_2;
Cross(j,:)=Cross1_3;
Cross(j+1,:)=Cross2_3;
End
j=n+1:1:a1;
Cross(j,:)=Sel_Pop1(d(j),:);
function[Mutation]=Mutation(Cro_Pop1,Pro_M,Matrix)
[a b]=size(Cro_Pop1);
n=round(Pro_M*a); % 计算需要变异的基因数目
d=randperm(a); % 将基因打乱,前n个变异
Mutation=Cro_Pop1;
for j=1:1:n
c=randperm(b); % 产生随机变异点位置
Matation1=Cro_Pop1(d(j),:); % 变异前基因
M1=Matrix;
Matation2=Matation1(1:c(1)); % 变异点前半部分
Matation3=Matation1(c(1)+1:b); % 变异点后半部分
Matation2=sort(Matation2,'descend');
i=1:1:c(1);
M1(Matation2(i),:)=[]; % 在优先度矩阵中删除前半部分相应行列
M1(:,Matation2(i))=[];
[a1 b1]=size(M1);
Mat_Pop=Ini_Pop(M1,1); % 重新生成后半部分
if (max(a1,b1)>0)
i=1:1:b1;
Matation4=[Matation1(1:c(1)),Matation3(Mat_Pop(i))];
else
Matation4=Matation1;
end
Mutation(d(j),:)=Matation4;
end
function[Pop11,Pop21]=Exchange(Pop12,Pop22,Num_Exc,Time,Num_Work)
[Num_Pop1 b]=size(Pop12);
[Num_Pop2 b]=size(Pop22);
% 计算交换前的种群的适应度,种群中适应度最好的交换
Dec_Pop12=Decoding(Pop12,Time,Num_Work);
Fit_Pop12=Fitness(Dec_Pop12,Num_Work,Time,Pop12);
[a5 b5]=max(Fit_Pop12);
Dec_Pop22=Decoding(Pop22,Time,Num_Work);
Fit_Pop22=Fitness(Dec_Pop22,Num_Work,Time,Pop22);
[a6 b6]=max(Fit_Pop22);
Pop11(1,:)=Pop22(b6,:); % 交换适应度最好的一对基因
Pop21(1,:)=Pop12(b5,:);
% 随机交换其余的基因
d1=randperm(Num_Pop1);
d1(find(d1==b5))=[];
d2=randperm(Num_Pop2);
d2(find(d2==b6))=[];
i=1:1:Num_Exc; % 交换指定基因
Pop11(i+1,:)=Pop22(d2(i),:);
Pop21(i+1,:)=Pop12(d1(i),:);
i=Num_Exc+1:1:Num_Pop1-1; % 未交换的基因不变
Pop11(i+1,:)=Pop12(d1(i),:);
i=Num_Exc+1:1:Num_Pop2-1;
Pop21(i+1,:)=Pop22(d2(i),:);
function[]=Draw(Pop,Dec_Pop,Time)
[a b]=size(Time);
RESULT=Pop;
RESULT_Time=
- 1
- 2
- 3
- 4
前往页