clc
clear
close all
%初始数据
J=[2,1,5,4,3;1,4,5,3,2;4,5,2,3,1;2,1,5,3,4;1,4,3,2,5;2,3,5,1,4;4,5,2,3,1;3,1,2,4,5;4,2,5,1,3;5,4,3,2,1];%工件*工序
T=[21,53,95,55,34;21,52,16,26,71;39,98,42,31,12;77,55,79,66,77;83,34,64,19,37;54,43,79,92,62;69,77,87,87,93;38,60,41,24,83;17,49,25,44,98;77,79,43,75,96];
M=size(J,1);%工件数
N=size(J,2);%工序数
MAX=200; % 最大迭代步数
min_f=inf; % 历史最小的适应度
pop_length=M*N; %编码长度
pop_num=100; %种群数量
Pcc=0.9; %交叉概率
Pmm=0.05; %变异概率
k=0;
popus=init(pop_length,pop_num,N,M); %初始化
for gen=1:MAX
sprintf('迭代次数%d',gen)
f=fit(popus,J,T,N,M);
if min(f)<=min_f
k=k+1;
ff_better{k}=f;
f_better(k)=min(f);
min_f=min(f);
[re,index]=min(f);
best_jieguo=popus(index(1),:);
best_popus=popus;
else
k=k+1;
ff_better{k}=ff_better{k-1};
f_better(k)= f_better(k-1);
popus=best_popus;
end
PPP{gen}=popus;
popus=choose(popus,f,Pcc,Pmm); %选择、交叉、变异
end
num_ff=length(ff_better);
aveff=zeros(1,num_ff);
for io=1:num_ff
ffo=ff_better{io};
aveff(io)=mean(ffo(:));
end
num_f=length(f_better);
t=1:num_f;
plot(t,f_better)
xlabel('迭代次数')
ylabel('函数适应度')
title('优化过程')
f_better(end)
[Te,T_end,T_begin,TTT,cixv,renwu]=fitt(best_jieguo,J,T,N,M);
T_begin(1,:)=[];TTT(1,:)=[];cixv(1,:)=[];renwu(1,:)=[];
%x轴 对应于画图位置的起始坐标x
start_time=[];
for i=1:size(T_begin,1)
start_time=[start_time,T_begin(i,:)];
end
%length 对应于每个图形在x轴方向的长度
duration_time=[];
for i=1:size(TTT,1)
duration_time=[duration_time,TTT(i,:)];
end
%y轴 对应于画图位置的起始坐标y
Cixv=[];
for i=1:size(cixv,1)
Cixv=[Cixv,cixv(i,:)];
end
Renwu=[];
for i=1:size(renwu,1)
Renwu=[Renwu,renwu(i,:)];
end
figure(2)
xlabel('加工时间'),ylabel('机器号');%x轴 y轴的名称
title('最佳调度');%图形的标题
n_task_nb = N*M;%total tasks //任务数目
%工序号,可以根据工序号选择使用哪一种颜色
jiqi=[];%
for i=1:M
jiqi=[jiqi,1:N];
end
set(gca,'ytick',0:1:5.5) ;%y轴的增长幅度
rec=[0,0,0,0];%temp data space for every rectangle
color=['r','g','b','c','m','r','g','b','c','m'];
for i =1:n_task_nb
rec(1) = start_time(i);%矩形的横坐标
rec(2) = jiqi(i); %矩形的纵坐标
rec(3) = duration_time(i); %矩形的x轴方向的长度
rec4 = color(Renwu(i));
temp=[[rec(1),rec(1)+rec(3),rec(1)+rec(3),rec(1)];[rec(2)-0.2,rec(2)-0.2,rec(2)+0.2,rec(2)+0.2]];
fill(temp(1,:),temp(2,:),rec4);
hold on
text(rec(1),rec(2)+0.3,[num2str(Renwu(i)),'-',num2str(Cixv(i))])
hold on
end