7 Z* E; F0 G$ f- w" s3 ~! w8 N
3 n, l; f+ t( x, g0 Z %mm1 simulation in matlab" `: B$ \8 H) R& n: [$ [9 V9 O
clc;clear; ST_Idle=0;
' w8 r0 k( t- w, m1 uST_Busy=1; EV_NULL=0;
. F, S; \6 b2 \: o0 ?1 BEV_Arrive=1;* z* d- a2 j) B
EV_Depart=2;0 s: c; A" u6 m, G( p+ i" e
EV_LEN=3; % next_event_type=[];
2 h. ]6 K1 z( F. [% num_custs_delayed=[];4 z. P- u+ g( J- @9 e$ W" N
% num_delays_required=[];" [7 H8 B; P8 {+ O D5 X: `
% num_events=[];
& \0 R( `3 Z" g t# f% num_in_q=[];
; t9 A: [( s' N4 `8 a# m3 c% server_status=[];
1 F9 [, ?, m- e$ }$ o/ }8 t% area_num_in_q=[];& A% G0 J: f9 r5 ~& I. b+ ?
% area_server_status=[];
1 e$ z9 z: A9 L2 _% mean_interarrival=[];6 | A7 S' r1 z T' A% F
% mean_service=[];( A* [% m" h# T6 @2 o
% sim_time=[];4 j/ Q h; y2 b; G# V4 k
% time_last_event=[];
& F- N; f* L \. f. n* m% total_of_delays=[];' `. ^) |. |: ^
%
/ c, p2 u" Z- t6 r4 f( X6 Mtime_arrival=[]; %到达时刻 time_next_event=zeros(1,EV_LEN);
1 N# q/ A7 W/ h/ B7 k+ D%仿真参数
8 ~0 A0 \0 F3 ]! N8 \, Hnum_events=EV_LEN-1;6 V$ n3 t' j6 t) z, y
mean_interarrival=1; n: G/ E# j B
mean_service=.5;1 R7 \. `: ~' f8 q# q
num_delays_required=2000; % outfile=fopen('mm1.txt','w');
) y! d, V+ o0 e( R% j+ l6 Qfprintf(outfile, 'Single-server queueing system\n\n');( p# z# N( v6 l0 b+ ~
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
# v# P( v7 {5 f4 Z" Wfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);, c# a6 L6 s- v$ p
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required); %%%%%%%%%%%%%part1
* ^- g! T3 H) ssim_time=0.0;4 C9 {2 V1 W( w: q8 H
% /* Initialize the state variables. */ server_status = 0;%idle
+ {. \% F! @* Z* c/ G7 k6 K9 o num_in_q = 0;" I4 T# d8 f1 k7 ~% s
time_last_event = 0.0;( J. {; r* u6 }/ r4 ^' W# ]
2 f( t+ H7 n P2 ~( r! e4 w8 m% /* Initialize the statistical counters. */ num_custs_delayed = 0;
3 R6 w* A! V0 [6 x" j total_of_delays = 0.0;! M. K$ j, @1 E4 c
area_num_in_q = 0.0;
4 E! y, t% l7 ^6 U area_server_status = 0.0;6 e4 ?+ h0 g5 n& }
3 i9 ~* z* A- c6 F: s/ t+ m% /* Initialize event list. Since no customers are present, the departure
$ b0 L+ C. O' _/ c% (service completion) event is eliminated from consideration. */
{ N; L- x9 E" A time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
' ?& T7 G& E; G, f time_next_event(EV_Depart) = 1.0e+230; / U+ b5 n* R9 [+ E- ^1 J
L' C3 ?/ i {) F
# C' p* Z5 X9 q) G1 A
/ t+ n' ]0 b5 `# ]5 K$ M' y %%%%%%%%%%%%part26 t1 p7 ^# r; I8 x. O+ p
while (num_custs_delayed < num_delays_required) G$ I$ R" y' t
%Run the simulation while more delays are still needed.& W3 g3 t, T0 |. x+ V: L3 L
%/* Determine the next event. */ min_time_next_event = 1.0e+290;
/ k h8 u8 ?* v$ | next_event_type = 0;
9 r h7 U* B7 A' R
2 J; L2 K% |5 Y$ V%/* Determine the event type of the next event to occur. */ for i = 1: num_events; d6 A" {8 z3 ^+ u
if (time_next_event(i) < min_time_next_event)
" z. D. z! x9 A4 ]: O" ?: M min_time_next_event = time_next_event(i);
; Q% x1 r4 H0 n8 A% n- j# g" v7 M) K next_event_type = i;: v" R$ L, Y3 M1 b' u# M2 Z6 a4 Z
end6 g i) R6 r- c1 b- _" n6 j/ }
end1 a- P L+ M7 H/ g0 y+ M/ y) n
+ {9 B2 }& m" m3 o4 ]%/* Check to see whether the event list is empty. */ if (next_event_type == 0)
0 e ?! A2 |- o6 G* f; u3 e
; ^0 s& A u% d* x, L%/* The event list is empty, so stop the simulation. */ fprintf(outfile, '\nEvent list empty at time %f', sim_time);7 @$ q$ `% D6 @
exit(1);
) \7 J) `7 @4 O9 ]4 _ end
* E( S) f( T: \. ]- L' B" g%/* The event list is not empty, so advance the simulation clock. */ sim_time = min_time_next_event;
- l% X1 o( F# T% }. ?& [ . F* f1 n1 ^6 ]; M' r$ Y; N! L5 W
( l8 ~# e/ M- B. h7 x9 f; G8 i% `%/* Update time-average statistical accumulators. */ double time_since_last_event; %/* Compute time since last event, and update last-event-time marker. */ time_since_last_event = sim_time - time_last_event;4 N$ _$ `9 k% o5 h
time_last_event = sim_time; %/* Update area under number-in-queue function. */ area_num_in_q=area_num_in_q + num_in_q * time_since_last_event; %/* Update area under server-busy indicator function. */ area_server_status =area_server_status + server_status * time_since_last_event;
; h$ O& c4 C0 b/ ]' u* I # e/ Q [ p- D& A1 E( {
%/* Invoke the appropriate event function. */ if(next_event_type==EV_Arrive)
, p1 ~0 D% B( u" @ double delay;
3 }* G' \0 N! m7 {$ c5 \) K: {& G8 Y 1 l8 D6 r# W# |6 r$ M$ F% m
%/* Schedule next arrival. */ time_next_event(1) = sim_time + randexp(mean_interarrival); %/* Check to see whether server is busy. */ if (server_status == ST_Busy) & A, T6 C2 l+ C
7 q% P" i0 m1 ]3 [6 g* u%/* Server is busy, so increment number of customers in queue. */ num_in_q=1+num_in_q;8 Y$ M1 i+ S2 z. ? i U
. @7 X) E* a& D) b9 q% c5 x: m%/* Check to see whether an overflow condition exists. */ if (num_in_q > Q_LIMIT)
$ S& [' W5 t6 i* n. w+ G7 E( ~%/* The queue has overflowed, so stop the simulation. */ fprintf(outfile, '\nOverflow of the array time_arrival at');
9 A5 g( _3 P' M/ k fprintf(outfile, ' time %f', sim_time);
4 p7 O) n( x) E$ p* j7 { exit(2);
" y: V! q! c+ B; l end1 y7 y8 P% O2 `0 ^% @2 z F% J8 h
%/* There is still room in the queue, so store the time of arrival of the arriving customer at the (new) end of time_arrival. */ time_arrival(length(time_arrival)+1)=sim_time; else
9 z+ s- A9 \! @. h! g( Q%/* Server is idle, so arriving customer has a delay of zero. (The following two statements are for program clarity
; U0 ]8 P# X. G* t%and do not affect the results of the simulation.) */ delay = 0.0;
1 |* Z4 Y* i x" \0 R8 g total_of_delays =total_of_delays + delay;3 A5 g6 k: R; L" ^# a
# X2 r2 _: G3 E9 G& [ {7 r0 J" `; n
%/* Increment the number of customers delayed, and make server busy. */ num_custs_delayed = 1 + num_custs_delayed;
0 ^: q& j0 v1 G/ u7 f- m server_status = ST_Busy;
) v6 ?6 h: j" A ]1 A 4 z( T. k, ~" Q+ ^5 b. K; L' S
%/* Schedule a departure (service completion). */ time_next_event(EV_Depart) = sim_time + randexp(mean_service);0 h7 \+ w! x; ~; a$ k) @
end % if (server_status == ST_Busy) % k6 Y! B z0 i+ D( y' J
%%%%%%%%depart- W$ A: U w8 K4 V
else$ }1 ?8 c1 m8 A& \4 ]9 {% A
double delay; %/* Check to see whether the queue is empty. */ % /* The queue is empty so make the server idle and eliminate the departure (service completion) event from consideration. */ server_status = ST_Idle;
I, }5 B5 i6 r time_next_event(EV_Depart) = 1.0e+230;* @. Z5 `/ C7 |) d# h
& z2 Z x7 V/ ]: G1 `' p% o else %/* The queue is nonempty, so decrement the number of customers in queue. */ %/* Compute the delay of the customer who is beginning service and update the total delay accumulator. */ delay = sim_time - time_arrival(1);# ]7 j, a: }4 {0 b/ e9 z
total_of_delays =total_of_delays + delay; %/* Increment the number of customers delayed, and schedule departure. */ num_custs_delayed = 1 + num_custs_delayed;, [* s: @* c9 l8 k
time_next_event(EV_Depart) = sim_time + randexp(mean_service); %/* Move each customer in queue (if any) up one place. */ tempForPop=time_arrival(2:length(time_arrival));
# M& `* U. ?' n# n. T7 E- Z/ ~ time_arrival=tempForPop;
/ l3 ~! a9 y! ~* J; B end %if (num_in_q == 0)5 A% d; _4 x* Z
/ e; J1 d: ~& G$ B8 Y+ p end %if(next_event_type==EV_Arrive)
5 r* n; O9 e0 t4 h. _- S# K
$ z$ E# d2 Y0 G: o! |end %while1 ?5 I6 t( c+ }) H% q2 l/ Z
% Q4 M4 o" H7 K9 ^' s- i+ x%%%%%%%%%% part 3
+ {! F: p0 B/ o$ ~( H& m8 P%/* Invoke the report generator and end the simulation. */
fprintf(outfile, '\n\nAverage delay in queue%11.3f minutes\n\n',total_of_delays / num_custs_delayed);6 i& N( b- R; W5 D7 ~( ?
fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);6 \4 ^& O* ^* T( ~2 f( S
fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);* T; m3 B, J) B2 i+ j, R8 n) H
fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);0 A* A5 f9 h& N$ {
fclose(outfile); |