# G# a5 R. h. D8 a# p' @* }) P9 t9 x1 _2 `! ?2 U
%mm1 simulation in matlab& G7 X, d6 L P- C2 o% C3 t1 g
clc;clear; ST_Idle=0;/ Z9 C4 l: d, W3 M: A
ST_Busy=1; EV_NULL=0;
$ m4 H" O' ?# U) l. e) BEV_Arrive=1;1 p8 z7 h( T6 q! C& C$ I- G
EV_Depart=2;$ Q' c0 {" e0 z3 E. {: U5 z% _& ~: h
EV_LEN=3; % next_event_type=[];
8 s$ i: H- B: i' n; } h1 W% num_custs_delayed=[];+ @" D/ ]+ u# l/ _' K
% num_delays_required=[];% W" E% U% J) F* {! G
% num_events=[]; y Q% V: J1 s3 c2 E3 S
% num_in_q=[];
+ ]" g" U# ]" ~% server_status=[];& m, ?+ _1 x; G7 d+ R
% area_num_in_q=[];
& N2 X( k# s1 h# [1 b" N+ n L2 k% area_server_status=[];- q0 j' o4 F% ]# A1 ?% a% \; t) j
% mean_interarrival=[];
0 ?. e7 D% v* O: e9 i! ~% T+ [% mean_service=[];- W' q( B; f! E
% sim_time=[];7 h; x; X2 p A
% time_last_event=[];
' [. N$ e0 y/ p. b* H% total_of_delays=[];
1 F! i0 m8 _, A8 A3 n' w6 u* H7 |% 8 T0 `$ \9 u: b" `
time_arrival=[]; %到达时刻 time_next_event=zeros(1,EV_LEN);5 o) {' B. L' ^+ ^! @9 |
%仿真参数/ X" }- E) [" ]& j& f
num_events=EV_LEN-1;% `- Q3 x+ w0 ?/ } |
mean_interarrival=1;) i& a1 T6 c% N3 ]9 j
mean_service=.5;: o, H8 H' l& c7 I. Q
num_delays_required=2000; % outfile=fopen('mm1.txt','w');" N# n# a( \7 W6 Z
fprintf(outfile, 'Single-server queueing system\n\n');2 v. l8 _5 B; g1 H2 p9 `2 X: l
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
. [& y5 c9 h4 _, u7 ~( {fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);8 j. C7 Z$ `' C6 i# M
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required); %%%%%%%%%%%%%part1$ s9 g) p0 H& p: I" L
sim_time=0.0;
- U' L" F/ z t/ R2 r! W" x% /* Initialize the state variables. */ server_status = 0;%idle8 y! S+ [1 l) R' C: z, o: x {, n7 N
num_in_q = 0;2 e% o% Y) }1 B; p8 [1 d$ m
time_last_event = 0.0;
: |% J# P+ q. ^; [* t; y
# ^1 `: [+ u0 Z' y9 ^! R% /* Initialize the statistical counters. */ num_custs_delayed = 0;
" V s/ Q* g5 w# f' Z* T5 \ total_of_delays = 0.0;/ e7 u/ b* M' {: l+ }8 n4 J: c
area_num_in_q = 0.0;
6 M( L+ O$ [- Q2 U3 T area_server_status = 0.0;7 z5 o7 X* s% L1 j2 ?
" ?5 j( c+ X0 b' O& T; z0 l7 F/ L
% /* Initialize event list. Since no customers are present, the departure" i- A$ }4 j6 Y& z, N, L$ t
% (service completion) event is eliminated from consideration. */
- j9 ]- q% U/ F3 k time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);% F8 m7 N6 q2 X' r' x- o, |0 K, I
time_next_event(EV_Depart) = 1.0e+230; * d0 d6 E; Y$ H& I1 B
! M& f' F, J/ ?$ z
2 U# z- j7 a& u0 h) Y3 c* q2 {/ } t9 O& K5 Y! b1 S0 o
%%%%%%%%%%%%part23 z# x; d& ]* s9 S% g- c7 s% ~* p6 A
while (num_custs_delayed < num_delays_required)4 i! d9 `2 Y4 D+ G7 ~% I
%Run the simulation while more delays are still needed.! c1 ]$ {* @3 U$ S, d! N
%/* Determine the next event. */ min_time_next_event = 1.0e+290;! [5 R& z, [5 q" H& _. ~( Q0 L
next_event_type = 0;
) s# O& P, [. |7 y& m, }9 y ' Y# R" S1 s- z( C
%/* Determine the event type of the next event to occur. */ for i = 1: num_events
: p0 o8 `6 K) j) Y) | if (time_next_event(i) < min_time_next_event) 8 e- ]# H1 O# W9 s- }# \' ]
min_time_next_event = time_next_event(i);7 k% Y% q y' c3 @; Z6 p
next_event_type = i;2 y9 H a$ N) d, i
end8 q8 [; i" ~1 A C' X$ r- z1 O
end
0 n- W0 O' O) d L Z" i C7 K5 ?* t3 F8 ^) P
%/* Check to see whether the event list is empty. */ if (next_event_type == 0) + u8 }0 A' d9 m8 y
" m9 b- p7 h* i7 T%/* The event list is empty, so stop the simulation. */ fprintf(outfile, '\nEvent list empty at time %f', sim_time);
. F$ u2 d+ I+ X, ~7 {8 r+ | exit(1);7 h9 [ ~: q7 w$ B
end
) I; U- w. l+ q%/* The event list is not empty, so advance the simulation clock. */ sim_time = min_time_next_event;
, d% `1 M4 `: g+ m X0 J' B/ |; T% o
7 J! l, `* a' M3 b/ u + t( j! w2 b9 J9 H+ ?$ ?
%/* 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;% c. w, q5 ~- m1 d) D6 n
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;
9 I. L- b. Y( C. f7 Q" q& J
! X" h! e& d3 T4 C& f. Q8 j( F' q%/* Invoke the appropriate event function. */ if(next_event_type==EV_Arrive)
6 \2 G) y% C# w' N; c* k1 K double delay;
, ~, F# E# V9 s* l 7 `7 @5 ]2 v, W; ^9 F) I( W
%/* 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( [" y1 @+ P) P% g5 M* u
, l4 Q& Z1 `' q
%/* Server is busy, so increment number of customers in queue. */ num_in_q=1+num_in_q;/ |# v( o' N2 Z7 c. Q- m" C
+ X/ N! [; z8 |# K% Q" D8 C%/* Check to see whether an overflow condition exists. */ if (num_in_q > Q_LIMIT) / l" J2 [. }3 {8 e* ?: i
%/* The queue has overflowed, so stop the simulation. */ fprintf(outfile, '\nOverflow of the array time_arrival at');5 c* d" j. u+ m
fprintf(outfile, ' time %f', sim_time);
: k6 y* u" F) U1 g3 b( E exit(2);2 o' o3 w& F0 M! ~8 Q. b1 A
end
" p& v1 G# |8 w, ^8 D7 N/ G%/* 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
5 f1 Q) {3 x: g2 k/ X%/* Server is idle, so arriving customer has a delay of zero. (The following two statements are for program clarity / n* Z# J$ `( i9 i) o6 f7 H6 N
%and do not affect the results of the simulation.) */ delay = 0.0;
& Q3 f3 a) y L9 [5 f0 Q total_of_delays =total_of_delays + delay;
9 R4 N, Q; t! h- `8 ]' X } 7 c, x% B7 }: x* V( z4 M* V
%/* Increment the number of customers delayed, and make server busy. */ num_custs_delayed = 1 + num_custs_delayed;, s2 b3 d% ? |+ n
server_status = ST_Busy;
F* m& p7 w. D m; z( Z2 G 2 a- k( y" q! Q& D, J0 _
%/* Schedule a departure (service completion). */ time_next_event(EV_Depart) = sim_time + randexp(mean_service);( ^0 w- E, s' n
end % if (server_status == ST_Busy)
/ {8 Y& |! L1 l2 U2 p4 y%%%%%%%%depart" `) P H1 I1 h
else' M9 ?1 O9 M! }- [8 N6 B' A. L% }
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;1 Q( q; w' o# u4 s4 } ^
time_next_event(EV_Depart) = 1.0e+230;
# g/ l2 W1 ?3 f5 o
( [, V. x; k5 p 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);- L, m" {4 L5 ] N, K* k
total_of_delays =total_of_delays + delay; %/* Increment the number of customers delayed, and schedule departure. */ num_custs_delayed = 1 + num_custs_delayed;
& J: E$ A7 _3 J) O2 u2 Y# b 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));
9 h9 m. j' v$ C time_arrival=tempForPop;6 B z* n7 G6 R+ i3 [, a1 p% T
end %if (num_in_q == 0)
: F. V8 b: A) _) p+ x# w6 i h + j9 H3 H a6 P( S2 g; r, ]2 A) Z
end %if(next_event_type==EV_Arrive)# R t$ o! t, y7 D6 O; y
, {: b2 @7 {5 ?9 j2 }; R1 G, s
end %while
8 U' ]! J1 ~' Y _/ U. |
$ D# R. h q+ H6 @%%%%%%%%%% part 3- W4 w8 {9 d; _/ w; g9 B4 h
%/* 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);# P5 w0 t/ O! `" b' h* F% s
fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
" {" N/ V3 ?+ I" k: q( O! w fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);" i; B8 P; _" a, [& w8 ]
fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
0 f3 f+ L6 w0 F' |+ q6 N fclose(outfile); |