( P/ G/ g0 k5 j0 n* h1 ]6 E' L2 H: d: j. l
%mm1 simulation in matlab* S8 R! j7 Q+ q: U5 `' e
clc;clear; ST_Idle=0;
# q- R H( T% {* WST_Busy=1; EV_NULL=0;2 ^2 }0 ]. D) U- z
EV_Arrive=1;
, u m- S4 F: s' F" S7 CEV_Depart=2;
0 c/ y; y* B( |" ?* M/ B6 cEV_LEN=3; % next_event_type=[];
6 m) T/ x2 f# M% P) L$ u; R, ?9 p+ n D% num_custs_delayed=[];
+ z; L0 k ~5 b/ @* m9 I% num_delays_required=[];
6 n R0 r/ z$ D$ S7 v8 y& H: W% num_events=[];9 x" ?& h$ E1 A' F3 @$ A# y
% num_in_q=[];6 p7 a0 A' B% T! X! W6 z
% server_status=[];) W* O9 I6 t" C0 X, e& a- o3 f
% area_num_in_q=[];2 e$ |9 G; N# L" r+ r
% area_server_status=[];3 r6 k; l1 y. N' o6 F* d$ Q* X
% mean_interarrival=[];
) z1 r( b9 l4 D6 O+ r F+ W% mean_service=[];5 M+ t% c' c* n: ]7 b; E+ N
% sim_time=[];: I1 A+ p; j1 F: Y: H5 Q! R) X
% time_last_event=[];
! x% ]: K% \, i% total_of_delays=[];
% j0 o/ I% X2 X: [; x% 2 [$ }/ T) O* J' i4 _' r
time_arrival=[]; %到达时刻 time_next_event=zeros(1,EV_LEN);
0 ]- a5 } y/ O: E%仿真参数
+ l: h$ n$ Y3 R/ v% ?: i. P9 hnum_events=EV_LEN-1;
/ r& x5 E7 s* O! amean_interarrival=1;
% K5 q0 j" X4 @6 E" ]$ N) emean_service=.5;
0 N( {4 p* o6 @- q: K. tnum_delays_required=2000; % outfile=fopen('mm1.txt','w');( f# T/ r2 X) W" U. ^% R$ f
fprintf(outfile, 'Single-server queueing system\n\n');' Y0 d. k$ O1 w3 U
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
$ Y4 s/ j% U. R" ]6 d; ufprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
% k' r! h+ c$ F8 y3 K4 r1 kfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required); %%%%%%%%%%%%%part1
4 x( N; t# C9 {$ ~8 }, [$ L, msim_time=0.0;
7 q1 i5 i% @5 l% /* Initialize the state variables. */ server_status = 0;%idle
* K0 E) l' U6 ?# W. f num_in_q = 0;; t/ V" P" E5 w/ ^( R: m
time_last_event = 0.0;9 N6 s# |2 ?: N! y
+ P* \; h. D9 E0 ^% /* Initialize the statistical counters. */ num_custs_delayed = 0;
: P: A$ Q4 i# S9 o8 e2 d total_of_delays = 0.0;2 q9 I4 e& V5 b4 Y1 s
area_num_in_q = 0.0;
2 \9 ^0 J, z- Z, d; W" x( d- _ area_server_status = 0.0;
2 L( }/ z8 Q( x # j# L: [, X% K! w) z: f! i
% /* Initialize event list. Since no customers are present, the departure8 ~$ d7 \* w; s+ v. p7 q' M. t
% (service completion) event is eliminated from consideration. */
2 _; h0 k( Y% C! l: m time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);" d/ `) x- |# M' s
time_next_event(EV_Depart) = 1.0e+230;
2 ?: i: i- k" v# u! V5 [
. N3 k5 \5 D' Z' X- A; C# I
# X+ {4 `. z: E9 U) K* e& D
. C! l' R- l1 y, `( }' g %%%%%%%%%%%%part2
+ i) M( o, d+ ?$ cwhile (num_custs_delayed < num_delays_required)* W7 s/ f9 C6 G+ T
%Run the simulation while more delays are still needed.
" T2 }+ x; z0 ^ ]- m* W%/* Determine the next event. */
min_time_next_event = 1.0e+290;0 k0 z/ W, z; l2 C4 f
next_event_type = 0;
9 k) |+ R& o) Y I6 n6 ^4 v8 I/ ^# ?, ^
%/* Determine the event type of the next event to occur. */ for i = 1: num_events7 i/ k- z/ d/ X' {) x
if (time_next_event(i) < min_time_next_event) 1 q! f7 L4 y: u+ z9 a+ d0 P1 l6 a
min_time_next_event = time_next_event(i);
: S- e/ U) M$ w! Z4 |; _$ L7 { next_event_type = i;
6 d3 G: n8 J9 ~& k# [( r5 l& q end. f) b% D3 r5 }$ f( j- O& h6 V
end; s1 v; q8 y* I. Y
# ^, l z; h1 N. o* F
%/* Check to see whether the event list is empty. */ if (next_event_type == 0) - f1 a+ x2 C1 {
; c5 t+ H$ y; [- F%/* The event list is empty, so stop the simulation. */ fprintf(outfile, '\nEvent list empty at time %f', sim_time);/ S% n1 c+ t' ?. c, Z9 N+ N8 U
exit(1);
# M* ?* ~1 J+ i5 x end
/ G% E C1 _0 f# h$ X%/* The event list is not empty, so advance the simulation clock. */ sim_time = min_time_next_event;
# r7 r+ h, Z5 Q2 c1 e6 A) T
% i" e1 @& J5 ~! S7 G
' H; o6 `" H8 n9 u1 I! C9 _$ m, |%/* 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;, @- Y$ ^* |8 M, `# r
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;
I6 L4 u# s* o! N" T; A7 a1 v
, [7 ]+ L7 O' j/ k%/* Invoke the appropriate event function. */ if(next_event_type==EV_Arrive)
! g1 S' D- @9 r# y$ R/ L double delay;4 s7 [) [/ s0 v) N% U `- u h$ c
' ~+ _% \9 y# s/ |( v( {%/* Schedule next arrival. */ time_next_event(1) = sim_time + randexp(mean_interarrival); %/* Check to see whether server is busy. */ if (server_status == ST_Busy) ) t2 w. x& {; c, L+ j% J
l% D9 O8 |" ?2 C- {& N4 c
%/* Server is busy, so increment number of customers in queue. */ num_in_q=1+num_in_q;$ y" c" w, v/ P/ M7 }% K- z
. p, b. H6 _" M: s; j%/* Check to see whether an overflow condition exists. */ if (num_in_q > Q_LIMIT) ! n. `, A8 D! ]; w9 e
%/* The queue has overflowed, so stop the simulation. */ fprintf(outfile, '\nOverflow of the array time_arrival at');
/ W. i/ K" G, P fprintf(outfile, ' time %f', sim_time);
* j& s. p8 z& V; H+ F: ]9 C exit(2);( L1 y# Z* ^+ n
end
, p* n# @% r4 H/ I%/* 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 - K2 X% ~" v" ] ?
%/* Server is idle, so arriving customer has a delay of zero. (The following two statements are for program clarity
" n! X- y1 c$ ]8 ?6 @6 `9 ~# C" \%and do not affect the results of the simulation.) */ delay = 0.0;+ @! p/ I- W* _3 ^, N
total_of_delays =total_of_delays + delay;
- g5 S X1 N5 `9 a! o8 [, H 4 y/ T) n$ @8 z( H, }
%/* Increment the number of customers delayed, and make server busy. */ num_custs_delayed = 1 + num_custs_delayed;+ ~) @9 [# ?( g7 |: I% ?
server_status = ST_Busy;
1 J% L' V4 r6 m: H2 e; d P1 E
: { U8 C7 N1 w- i, g( q1 h& T%/* Schedule a departure (service completion). */ time_next_event(EV_Depart) = sim_time + randexp(mean_service);# c L G2 J" N' D5 V
end % if (server_status == ST_Busy)
* z# c: B5 X) K( r9 v* T4 O%%%%%%%%depart3 o8 I! e: j/ A& K6 f1 l
else
- k e' F' R! z9 } w 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;- j6 E' D w0 S+ h) x& T
time_next_event(EV_Depart) = 1.0e+230;
^1 o* v5 b6 @' L2 o6 y: H
c, ~" a/ ^+ D" G7 W# T- b 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);, O1 T: C0 w8 X! H
total_of_delays =total_of_delays + delay; %/* Increment the number of customers delayed, and schedule departure. */ num_custs_delayed = 1 + num_custs_delayed;
) o2 o7 d V. C9 p 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));) [3 Q7 e! u4 `5 x/ \
time_arrival=tempForPop;7 w; c- {# Y, z7 l5 ~6 e
end %if (num_in_q == 0)
( Q9 F) R3 i7 [ a h3 ` " b' w( V9 y- a" d5 a8 U+ l
end %if(next_event_type==EV_Arrive). M; s3 H2 A# T
' {$ h2 D$ x) p4 S) ~1 F5 M2 q5 v
end %while
n, @0 b, g" z3 \$ Q$ o9 L3 x
6 n: J! f6 m( b" z7 v. `/ v%%%%%%%%%% part 3( g" B7 o( h- K/ F. C5 w% D
%/* 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);; I# o& D, q# u( J! r
fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);- k) K I" L. P
fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
1 L. s3 F) R- B( R8 D' z% i fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);9 B" X! Z j3 S' {; j
fclose(outfile); |