步驟:
(1)確定問題是否屬於排隊論領域
(2)確定修理工個數s
(3)確定機器源數m
(4)找到時間終止點T
(5)帶入模型即可
function out=MMSmteam(s,m,mu1,mu2,T)
%M/M/S/m排隊模型
%s——修理工個數
%m——機器源數
%T——時間終止點
%mu1——機器離開-到達時間服從指數分佈
%mu2——修理時間服從指數分佈
%事件表:
% p_s——修理工空閒概率
% arrive_time——機器到達事件
% leave_time——機器離開事件
%mintime——事件表中的最近事件
%current_time——當前時間
%L——隊長
%tt——時間序列
%LL——隊長序列
%c——機器到達時間序列
%b——修理開始時間序列
%e——機器離開時間序列
%a_count——到達機器數
%b_count——修理機器數
%e_count——損失機器數
%初始化
arrive_time=exprnd(mu1,1,m);
arrive_time=sort(arrive_time);
leave_time=[];
current_time=0;
L=0;
LL=[L];
tt=[current_time];
c=[];
b=[];
e=[];
a_count=0;
%循環
while min([arrive_time,leave_time])<T
current_time=min([arrive_time,leave_time]);
tt=[tt,current_time]; %記錄時間序列
if current_time==min(arrive_time) %機器到達子過程
arrive_time(1)=[]; % 從事件表中抹去機器到達事件
a_count=a_count+1; %累加到達機器數
if L<s %有空閒修理工
L=L+1; %更新隊長
c=[c,current_time];%記錄機器到達時間序列
b=[b,current_time];%記錄修理開始時間序列
leave_time=[leave_time,current_time+exprnd(mu2)];%產生新的機器離開事件
leave_time=sort(leave_time);%離開事件表排序
else %無空閒修理工
L=L+1; %更新隊長
c=[c,current_time];%記錄機器到達時間序列
end
else %機器離開子過程
leave_time(1)=[];%從事件表中抹去機器離開事件
arrive_time=[arrive_time,current_time+exprnd(mu1)];
arrive_time=sort(arrive_time);%到達事件表排序
e=[e,current_time];%記錄機器離開時間序列
if L>s %有機器等待
L=L-1; %更新隊長
b=[b,current_time];%記錄修理開始時間序列
leave_time=[leave_time,current_time+exprnd(mu2)];%產生新的機器離開事件
leave_time=sort(leave_time);%離開事件表排序
else %無機器等待
L=L-1; %更新隊長
end
end
LL=[LL,L]; %記錄隊長序列
end
Ws=sum(e-c(1:length(e)))/length(e);
Wq=sum(b-c(1:length(b)))/length(b);
Wb=sum(e-b(1:length(e)))/length(e);
Ls=sum(diff([tt,T]).*LL)/T;
Lq=sum(diff([tt,T]).*max(LL-s,0))/T;
p_s=1.0/(factorial(m)/factorial(m).*(mu2/mu1)^0+factorial(m)/factorial(m-1).*(mu2/mu1)^1+factorial(m-2)/factorial(m-1).*(mu2/mu1)^2+factorial(m)/factorial(m-2).*(mu2/mu1)^2+factorial(m)/factorial(m-4).*(mu2/mu1)^4+factorial(m)/factorial(m-5).*(mu2/mu1)^5);
fprintf('修理工空閒概率:%d\n',p_s)%修理工空閒概率
fprintf('到達機器數:%d\n',a_count)%到達機器數
fprintf('平均逗留時間:%f\n',sum(e-c(1:length(e)))/length(e))%平均逗留時間
fprintf('平均等待時間:%f\n',sum(b-c(1:length(b)))/length(b))%平均等待時間
fprintf('平均修理時間:%f\n',sum(e-b(1:length(e)))/length(e))%平均修理時間
fprintf('平均隊長:%f\n',sum(diff([tt,T]).*LL)/T)%平均隊長
fprintf('平均等待隊長:%f\n',sum(diff([tt,T]).*max(LL-s,0))/T)%平均等待隊長
for i=0:m
p(i+1)=sum((LL==i).*diff([tt,T]))/T;%隊長爲i的概率
fprintf('隊長爲%d的概率:%f\n',i,p(i+1));
end
fprintf('機器不能馬上得到修理的概率:%f\n',1-sum(p(1:s)))%機器不能馬上得到修理的概率
out=[Ws,Wq,Wb,Ls,Lq,p];