clear all close all
%MM1排队系统计算机仿真
SimCusNo=input('请设定MM1仿真系统的被服务游客数SimCusNo=[1000]'); if isempty(SimCusNo) SimCusNo=1000; end; Ba=5; Bs=4;
EventQueue=[]; IfOutOfNum = 0; NextTime = 0; CustomerNum = 0; EndTime = 0;
HasServedNum = 0; CurStatus =0; i=1;
EventInit=[0,0,0,0,0,0,0,0,0,0,0,0,0,0]; EventQueue(:,i)=EventInit; i=i+1;
while (IfOutOfNum==0)
if (HasServedNum >=SimCusNo) IfOutOfNum = 1; end;
t_CurTime = NextTime;
t_ReachTime = t_CurTime+(-Ba*log(rand())) ; if EndTime t_StartTime=EndTime; end ; if EndTime-NextTime>0 t_WaitTime=EndTime-NextTime else t_WaitTime=0; end; t_ServeTime = -Bs*log(rand()); if CustomerNum>0 CurStatus=1; else CurStatus=0 end Come_Event=[t_CurTime,... 1,... CustomerNum+HasServedNum+1,... t_CurTime,... t_ReachTime,... CurStatus,... CustomerNum,... CustomerNum+1,...) t_StartTime,... t_WaitTime,... t_ServeTime,... t_StartTime+t_ServeTime,... t_WaitTime+t_ServeTime,... HasServedNum... ]; EventQueue(:,i)=Come_Event; trace=find(EventQueue(2,1:i-1)==1) if length(trace)>0 setpos=trace(length(trace)); %min_w=find(min(EventQueue(1,setpos+1:i-1))); for mm=setpos+1:i-1 min_s=mm; for(nn=mm+1:i) if(EventQueue(1,nn) if(min_s==mm) temp=EventQueue(:,min_s); EventQueue(:,min_s)=EventQueue(:,mm); EventQueue(:,mm)=temp; end; end; end; end; i=i+1; Leave_Event=[Come_Event(12),... 2,... Come_Event(3),... Inf,... Inf,... Inf,... Inf,... Inf,... Inf,... Inf,... Inf,... Come_Event(12),... Inf,... Inf... ]; EventQueue(:,i)=Leave_Event; i=i+1; NextTime = t_ReachTime; CustomerNum=CustomerNum+1; EndTime = t_StartTime+t_ServeTime; %size(EventQueue); AssistArray=Reserve(EventQueue,t_CurTime,t_ReachTime); for jj=length(AssistArray):-1:1 CustomerNum=CustomerNum-1; HasServedNum=HasServedNum+1; EventQueue(8,AssistArray(jj))=CustomerNum; EventQueue(14,AssistArray(jj))=HasServedNum; if CustomerNum>0 CurStatus=1; else CurStatus=0; end ; %tmp=0; EventQueue(6,AssistArray(jj))=CurStatus; tmp=CustomerNum; if tmp-1>0 EventQueue(7,AssistArray(jj))=tmp-1; else EventQueue(7,AssistArray(jj))=0; end; end; end; disp('平均等待时间如下') AveWaitTime=CountWaitingTime(EventQueue,SimCusNo) disp('平均等待队长如下') AveQuLen=CountQueueLen(EventQueue,SimCusNo) disp('仿真结束'); ;