使用NEH解决no-wait flowshop makespan问题 (源码)

8 篇文章 0 订阅
4 篇文章 0 订阅

1.NEH的原理

(1)将每个工件在所有机器上的加工时间求和;对求和后的值进行从大到小排序;
(2)首先选择第一个工件(加工时间最长的),用第二个工件插入到第一个工件的前后两个位置,计算makespan,小的被保存;
(3)将上一步保存的序列固定位置,使用下一个工件插入到之前的工件中,并比较得出最小的makespan并保存;
(4)重复上一步,得出最终结果。

2.源码

(1)主程序实现向上一步排好序的序列中插入工件

clc;
clear;
%主程序:
%S(ij) :i表示第i个job,j表示第j个machine
%S1(ij): S的转换
%TJob1:工件1的总的处理时间
%jobSize: 工件数量
%machineSize:机器数量
%TMSTab
S=[5 9 8 10 1;9 3 10 1 8;9 4 5 8 6;4 8 8 7 2];
global index;

[jobSize,machineSize] = size(S);
% S_row = sum(S,2);
% [seq sequence] = sort(S_row);
%1. 按照各行和的大小进行排序
S_row = sum(S,2);
[~,index] = sort(S_row);
index = fliplr(index')';
newS = S(index,:);
%2.首先比较job1与job2的顺序,然后将job3,job4...不断的插入并判断最小的makespan,插完为止
for i = 1:1:(jobSize-1)
    tempMakespan = 10000;
     if i==1
            tempS = newS(1,1:machineSize);   %截取矩阵
            sequence = tempS;
     end
    for j = 1:1:(i+1) 
       
        if j==1                         %插入到所有job的最前面。
            tempS = cat(1,newS((i+1),:),sequence);
        end
        if j>1 && j<(i+1)
            tempS = cat(1,sequence(1:(j-1),:),newS((i+1),:),sequence(j:i,:));
        end
        if j == (i+1)
            tempS = cat(1,sequence(1:i,:),newS((i+1),:));
        end
        [ start, complete ,TMS ] = Conbine005( tempS );
        
         if TMS < tempMakespan
            tempMakespan = TMS; 
            tempSequence = tempS;
%             tempS1 = tempS;
         end
    end
    sequence = tempSequence;
end
[ start, complete ,TMS ] = Conbine005( sequence );
 PlotGantt( start ,complete );

(2)使用此函数计算已知序列的各个工件在每个机器上的开始时间矩阵S(ij)及结束时间矩阵C(ij)

%第五-一版本:可变的machine数量,可变的job数量
%此版本改进关于矩阵行和列的问题:之后统一使用行表示job,使用列表示machine
%    M1   M2   M3   M4   M5      大小
% J1 5    9    8    10   1        4
% J2 9    3    10   1    8        2
% J3 9    4    5    8    6        3
% J4 4    8    8    7    2        1
function [ S, C, makespan ] = Conbine005( T )
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
%  Tij 指代第i个job第j个machine的处理时间

%参数:
%Matrix S(ij) : Starting time of O(ij);
%Matrix C(ij) : finish time of O(ij);
%Matrix T(ij) : processing time of O(ij);
%Matrix O(ij) : refer to operation in the ith job and jth machine; 
%makespan : 处理的总时间。

machineLength = length(T(1,:));
jobLength = length(T(:,1));
%计算第一个job在所有machine上的S,C
S(1,1) = 0;
C(1,1) = S(1,1) + T(1,1);
for i = 2:1:machineLength
    S(1,i) = C(1,i-1);
    C(1,i) = S(1,i) + T(1,i);
end
%计算第2,3,4....n-1个job在所有machine上的S,C
for  t = 2:1:jobLength
    if(T(t,1) >= T(t-1,2))
        S(t,1) = C(t-1,1);                                          %修改1        
    end
    if(T(t,1) < T(t-1,2))
        S(t,1) = C(t-1,1) + (T(t-1,2)-T(t,1));                        %修改2
    end
    C(t,1) = S(t,1) + T(t,1);
    
    for i = 2:1:machineLength-1
        S(t,i) = C(t,i-1);
        if(S(t,i) + T(t,i) >= C(t-1,i+1))
            C(t,i) = T(t,i) + S(t,i);
        end
        if(S(t,i) + T(t,i) < C(t-1,i+1))
            C(t,i) = C(t-1,i+1);
            dis = C(t-1,i+1) - (S(t,i) + T(t,i));
            S(t,i) = S(t,i) + dis;
            for j = 1:1:i-1
                S(t,j) = S(t,j) + dis;
                C(t,j) = C(t,j) + dis;
            end
        end
    end
    S(t,machineLength) = C(t,machineLength-1);
    C(t,machineLength) = C(t,machineLength-1) + T(t,machineLength);
end
%计算第n个job在所有machine上的S,C
S(jobLength,machineLength) = C(jobLength,machineLength-1);
C(jobLength,machineLength) = C(jobLength,machineLength-1) + T(jobLength,machineLength);

  PlotGantt( S ,C );
makespan = C(jobLength,machineLength);
end


%%
%测试   T=[1 3 2 4 3;2 1 1 3 2; 3 4 3 2 1; 1 1 2 1 3; 3 2 1 2 3]
%       T=[4 8 8 7 2;9 4 5 8 6;5 9 8 10 1;9 3 10 1 8]

(3)画甘特图的函数

function [] = PlotGantt( S ,C )
%UNTITLED4 Summary of this function goes here
%   Detailed explanation goes here
s = S';
c = C';
S1 = s(:).';
C1 = c(:).';
axis([0,80,0,6.5]);%x轴 y轴的范围
set(gca,'xtick',0:2:80) ;%x轴的增长幅度
set(gca,'ytick',0:1:6.5) ;%y轴的增长幅度
xlabel('加工时间'),ylabel('机器号');%x轴 y轴的名称
title('当前调度Gantt图');%图形的标题
% n_bay_nb=length(S(:,1));%total bays  //机器数目
n_task_nb = length(S1);%total tasks  //任务数目
%x轴 对应于画图位置的起始坐标x
n_start_time=S1;%start time of every task  //每个工序的开始时间
%length 对应于每个图形在x轴方向的长度
n_duration_time =C1 - S1;%duration time of every task  //每个工序的持续时间
%y轴 对应于画图位置的起始坐标y
n_bay_start=[ 0 1 2 3 4  0 1 2 3 4  0 1 2 3 4  0 1 2 3 4 0 1 2 3 4 ]; %bay id of every task  ==工序数目,即在哪一行画线
% for i = 1:1:
%工序号,可以根据工序号选择使用哪一种颜色
n_job_id=[0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4];%
rec=[0,0,0,0];%temp data space for every rectangle  
color=['r','g','b','c','m'];
for i =1:1:n_task_nb  
  rec(1) = n_start_time(i);%矩形的横坐标
  rec(2) = n_bay_start(i)+0.7;  %矩形的纵坐标
  rec(3) = n_duration_time(i);  %矩形的x轴方向的长度
  rec(4) = 0.6; 
%   txt=sprintf('p(%d,%d)=%d',n_bay_start(i)+1,n_job_id(i)+1,n_duration_time(i));%将机器号,工序号,加工时间连城字符串
    txt=sprintf('%d',n_duration_time(i));   
   rectangle('Position',rec,'LineWidth',0.5,'LineStyle','-','FaceColor',color(n_job_id(i)+1));%draw every rectangle  
   text(n_start_time(i)+0.2,(n_bay_start(i)+1),txt,'FontSize',18);%label the id of every task  ,字体的坐标和其它特性
%    text(n_start_time(i)+0.2,(n_bay_start(i)+1),txt,'FontWeight','Bold','FontSize',18);%label the id of every task  ,字体的坐标和其它特性
end  
grid on;
end




  • 12
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值