% h8 n. H6 v5 C3 m" O2 g
# g' h* k1 |3 J6 _" Q7 J %mm1 simulation in matlab m6 a* M+ W8 A2 L+ x4 A3 H% ?
clc;clear; ST_Idle=0;
6 m1 W/ K8 w. X1 D4 iST_Busy=1; EV_NULL=0;" ?# W7 U$ t8 i9 b! O, E
EV_Arrive=1;
' K: t, H5 \: ^7 q1 @; ?9 ^& AEV_Depart=2;
) U, z2 e G/ r6 q& cEV_LEN=3; % next_event_type=[];
1 N; t6 R/ U# G, \, U% num_custs_delayed=[];+ Q: k6 S# X3 _; u
% num_delays_required=[];/ {9 `8 x! T4 S+ E" N6 l
% num_events=[];
5 a5 O1 L" M' \7 X) o% num_in_q=[];
% A/ g( l, k8 j+ k2 l4 i! j% server_status=[];
9 e" H. P c) |! M' E% area_num_in_q=[];6 _4 D: d8 z. V5 r' `. Z
% area_server_status=[];
2 q' a# d1 @9 J& o# d% mean_interarrival=[]; {, ^8 j& V: y3 @* u* H: i2 y4 I
% mean_service=[]; e) I# r0 Y( w- m
% sim_time=[];0 s) J N5 c" c" s
% time_last_event=[]; |7 c! i, {' |& P T* E' z! v- W. M8 I
% total_of_delays=[];
1 h7 l4 x$ R* H; w4 F v%
* I3 ?- H2 n" s, s Y5 j( Z* Otime_arrival=[]; %到达时刻 time_next_event=zeros(1,EV_LEN);+ n/ H: D6 Y$ u' _
%仿真参数
2 |9 i, @9 Y% Lnum_events=EV_LEN-1;
( c+ c9 S+ ]. Umean_interarrival=1;
; U- w$ {) S4 f6 p- Qmean_service=.5;
; g" x* w# U- M. enum_delays_required=2000; % outfile=fopen('mm1.txt','w');
9 V0 u. r* @( z) v2 f qfprintf(outfile, 'Single-server queueing system\n\n');
- Q* n+ F: I6 o8 R" `fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
6 h* a& M) C+ `" |9 Z: G7 L: Pfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
- k% C5 L& s% s0 Gfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required); %%%%%%%%%%%%%part10 w! Y; Y+ K* q9 j. z. l; V3 I
sim_time=0.0;
% u2 j5 G) S- p2 E' Z) x% /* Initialize the state variables. */ server_status = 0;%idle0 a9 t# @4 S9 y' |% W) P* ?
num_in_q = 0;
3 R1 f6 Y, Y. Q2 T. Q3 S( O7 S time_last_event = 0.0;
5 _8 L. B5 a# v0 w9 X/ I / [$ \$ c! K) h- y
% /* Initialize the statistical counters. */ num_custs_delayed = 0;
2 \% u0 }+ I; ~- z total_of_delays = 0.0;3 _& @) F! b# h
area_num_in_q = 0.0;: \* l/ N+ v/ j4 p" @( ~, ^
area_server_status = 0.0;5 I! ~& [& U8 j/ D. p: m6 f
# F6 D/ ^" x( ^# v1 Y
% /* Initialize event list. Since no customers are present, the departure6 k2 H) A! Y3 a2 f3 }8 Q+ [! T& P
% (service completion) event is eliminated from consideration. */2 ]; s, ?9 f( r3 \' {! |2 Q
time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
/ U* f5 {- D1 I$ H) W9 N( f3 i time_next_event(EV_Depart) = 1.0e+230; ) ?8 V: K* G/ q- p7 `+ j l
( u W& l+ R* _, s6 O$ v$ L 2 v% |6 D* L& ~2 ~. s2 A1 c
/ Y. g: N+ l1 v7 b/ u# B
%%%%%%%%%%%%part2
2 @+ ]( P* ~" d4 l; Swhile (num_custs_delayed < num_delays_required)
3 b$ Z5 D" F/ X%Run the simulation while more delays are still needed.3 }' L' \7 ]4 {9 w$ O3 M2 y
%/* Determine the next event. */ min_time_next_event = 1.0e+290;
; \1 D- p! W1 E* H4 m5 e, E& h next_event_type = 0;' i$ ~, P; J9 w0 s O" K$ c, U
9 V* `; [+ P# W) ]! x+ S%/* Determine the event type of the next event to occur. */ for i = 1: num_events
' u/ W) J8 b: l, ~& V if (time_next_event(i) < min_time_next_event) ; o* M/ U6 Q- F# a: V
min_time_next_event = time_next_event(i);8 \ ~: R& l' n p
next_event_type = i;6 h$ r7 X V/ F9 W9 [# A1 d
end
. X( i/ s, C8 ]' S end- H( M4 ^+ X1 K$ l
) ~; O* F' L, m/ V" s
%/* Check to see whether the event list is empty. */ if (next_event_type == 0)
! W1 V2 {1 e& @7 n: |9 l 2 H: e, t1 k" x$ F
%/* The event list is empty, so stop the simulation. */ fprintf(outfile, '\nEvent list empty at time %f', sim_time);% W/ K+ w/ o8 p7 Y
exit(1);
) c& Q% u* V. ?( `4 D- B& s end3 U( q' E* ?4 ~) Q6 z9 |; }
%/* The event list is not empty, so advance the simulation clock. */ sim_time = min_time_next_event;3 I/ O: N! m6 e* E- t, O; e
1 F. ?# t5 |* X8 _& R! ]
4 M4 R& B) w! X( x6 f& [ B) A: E%/* 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;1 L. w1 V2 s- ]# l* L, v5 d/ O! c
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;
6 `4 A5 \! @- D9 e/ h+ r4 M - z6 `# ?% T' C8 A. a
%/* Invoke the appropriate event function. */ if(next_event_type==EV_Arrive)/ d2 H/ s3 J- l# L$ @# f2 z
double delay;6 P6 a/ a' d- v" ^% j+ x
, Y0 b9 Y2 e: X' j0 {9 T
%/* Schedule next arrival. */ time_next_event(1) = sim_time + randexp(mean_interarrival); %/* Check to see whether server is busy. */ if (server_status == ST_Busy)
' {9 `9 W& W+ A7 \0 N
$ c, l @7 E( _+ p8 w%/* Server is busy, so increment number of customers in queue. */ num_in_q=1+num_in_q;
t' c% Y1 J4 u9 _
0 W: P0 t8 x) i%/* Check to see whether an overflow condition exists. */ if (num_in_q > Q_LIMIT) 9 X0 G/ O, x% g4 T8 ~+ X& P4 ~& U
%/* The queue has overflowed, so stop the simulation. */ fprintf(outfile, '\nOverflow of the array time_arrival at');: H4 E7 @# z- b9 |( v& B
fprintf(outfile, ' time %f', sim_time);1 u3 T: O1 Q8 i* a) A6 z4 ]! Q
exit(2);
3 Z k! s9 B6 U end
8 Q& G" [8 k3 D) B4 Y%/* 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 * J$ ~9 r" ?" i8 G3 X' I3 A
%/* Server is idle, so arriving customer has a delay of zero. (The following two statements are for program clarity $ f6 B2 [- u2 E$ `5 e" c P% k
%and do not affect the results of the simulation.) */ delay = 0.0;
2 @+ n$ d4 U. ?2 |5 F+ a+ A5 B/ m/ J total_of_delays =total_of_delays + delay;# c8 @, c3 J2 B8 [. v, H% B/ k
$ x3 L. a% }5 |
%/* Increment the number of customers delayed, and make server busy. */ num_custs_delayed = 1 + num_custs_delayed;
: W- [$ @) C# s1 ~& h server_status = ST_Busy;
$ @ _3 ` n2 k) f / Z* n7 C5 N# j7 P/ [* \
%/* Schedule a departure (service completion). */ time_next_event(EV_Depart) = sim_time + randexp(mean_service);: f: g, D. ~% _, p1 c# n0 ?: D
end % if (server_status == ST_Busy) " Z3 m! Z# p" t7 E. t( P4 Q& W
%%%%%%%%depart2 y& r. t, U8 ?7 v, c% Y
else
7 y! s) ?# \$ M# f+ r 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;
" P k- z6 v) J s: U3 X* H" g time_next_event(EV_Depart) = 1.0e+230;+ z9 `5 r3 {2 j$ l6 y
B" u' w; ?/ l& G9 T/ W 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);5 E/ D2 |' V- z1 Z& L5 V( L
total_of_delays =total_of_delays + delay; %/* Increment the number of customers delayed, and schedule departure. */ num_custs_delayed = 1 + num_custs_delayed;
, N0 `0 h. m; N3 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));
2 }* l: c; v& L9 ` time_arrival=tempForPop;4 E6 z. S% y, L+ M$ c! y( j
end %if (num_in_q == 0)- c R" ?. F' ^( R+ w
8 \. I, E( ?* w g3 \4 B
end %if(next_event_type==EV_Arrive)- S" k: A$ H/ X- j
' [1 M2 ]& T9 S: b7 ^) x, i
end %while4 X& G. P0 k$ T B; m
6 } ~0 S3 P% g/ k%%%%%%%%%% part 32 G' ]& G& V- a
%/* 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);1 g5 P! ^1 e6 c
fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);! I7 W1 O1 y8 ~6 H( f# l3 k* l
fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
) C& V, ^8 P0 v/ `0 v fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
2 O7 ]- j' U! _+ M# q fclose(outfile); |