📄 car1_1.m
字号:
% 计算一条流水线加工流程时间
clc
clear all
best1=[];
age_num=200; % 输入计算代数age_num
k=200; % 输入初始种群规模
pb=.1; % 变异概率
n1=1; %第一阶段设备数
% 按实际加工顺序,输入各工件加工时间
timec=[ 0 375 1 12 2 142 3 245 4 412
0 632 1 452 2 758 3 278 4 398
0 12 1 876 2 124 3 534 4 765
0 460 1 542 2 523 3 120 4 499
0 528 1 101 2 789 3 124 4 999
0 796 1 245 2 632 3 375 4 123
0 532 1 230 2 543 3 896 4 452
0 14 1 124 2 214 3 543 4 785
0 257 1 527 2 753 3 210 4 463
0 896 1 896 2 214 3 258 4 259
0 532 1 302 2 501 3 765 4 988
];
[m,n]=size(timec); % 工件数(timec的行数)m,工序数(timec的列数)n
n=n/2;
for i=n:-1:1
timec(:,2*i-1)=[];
end
age=1; % 求第一代最小加工流程时间mint及相应的加工顺序minp
for k1=1:k % 随机产生k个第一阶段加工顺序
rand1=rand(1,m);
for j=1:m
[useless,i]=max(rand1);
sequence(k1,j)=i;
rand1(i)=0;
end
end
for ki=1:k % 计算最小加工流程时间mint及相应的加工顺序minp
sequence1=sequence(ki,:);
[makespan,c]=netedtime_1(timec,sequence1,m,n);
cmax(ki)=makespan;
end
[best,i]=min(cmax);
besti1=sequence(i,:);
best1=[best1,best];
% 将加工顺序进行交叉,得到row(偶数)个新的加工顺序,种群总数仍为k个
for age=2:age_num
age
% 选择适应值小于平均值的染色体
ave=mean(cmax);
sele=[];
i=0;
for ki=1:k
if cmax(ki)<ave
i=i+1;
sele(i,:)=sequence(ki,:);
end
end
poss=size(sele,1);
pj=k/2/poss; % 交叉概率
cross=[];% 交叉
mt1=[];
for i=1:poss
r=rand(1);
if r<pj
cross=[cross;i]; %cross存放需要交叉的染色体的序号
end;
end;
row=length(cross);%row需要交叉的染色体的个数
if ceil(row/2)~=row/2
row=row-1;
end
% 交叉位置
r=ceil(rand(1)*(m-1));
for i=1:row/2
a1=sele(cross(2*i-1),:);
a2=sele(cross(2*i),:);
a12=a1;
a11=a1(1:r);
a22=a2;
a21=a2(1:r);
m5=m;m6=m;
for i1=1:r
for i2=m5:-1:1
if a12(i2)==a2(i1)
a12(i2)=[];
m5=m5-1;
end
end
for i3=m6:-1:1
if a22(i3)==a1(i1)
a22(i3)=[];
m6=m6-1;
end
end
end
a1=[a21,a12];
a2=[a11,a22];
sequence_cross((2*i-1),:)=a1;
sequence_cross((2*i),:)=a2;
end % for i=1:row/2
for ki=1:row % 计算最小加工流程时间mint及相应的加工顺序
sequence1=sequence_cross(ki,:);
[makespan,c]=netedtime_1(timec,sequence1,m,n);
mt1(ki)=makespan;
if mt1(ki)<cmax(cross(ki))
sequence(cross(ki),:)=sequence_cross(ki,:);
cmax(cross(ki))=mt1(ki);
end
end
[mint,j]=min(mt1);
% 每一代交叉运算结束后,确定最小停机时间best及相应的加工顺序besti
if mint<best;
best=mint;
besti1=sequence_cross(j,:);
end
variation=[];% 变异
j=0;
mt1=[];
for i=1:k
r=rand(1);
if r<pb
j=j+1;
variation=[variation;i]; % variation存放变异染色体序号
r=ceil(rand(1)*(m-1));
z11=sequence(i,1:r);
z12=sequence(i,r+1:m);
sequence_var(j,:)=[z12,z11];
end
end
row=length(variation);
for ki=1:row % 计算最小加工流程时间mint及相应的加工顺序
sequence1=sequence_var(ki,:);
[makespan,c]=netedtime_1(timec,sequence1,m,n);
mt1(ki)=makespan;
if mt1(ki)<cmax(variation(ki))
sequence(variation(ki),:)=sequence_var(ki,:);
cmax(variation(ki))=mt1(ki);
end
end
% 求最小停机时间mint及相应的加工顺序minp
[mint,j]=min(mt1);
% 每一代变异运算结束后,确定最小停机时间best及相应的加工顺序besti
if mint<best;
best=mint;
besti1=sequence_var(j,:);
end
best1=[best1,best];
end % for age=2:age_num
[makespan,c]=netedtime_1(timec,besti1,m,n);
sprintf('最佳加工顺序'),best,besti1
age=1:age_num;
plot(age,best1)
title('加工流程时间优化过程 ')
xlabel('计算代数')
ylabel('加工流程时间(小时)')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -