r6 S& _) x a( ^8 B [
( I( H) V% C- }5 k7 }- m9 `
%mm1 simulation in matlab
- j* M0 a6 w* t8 z: r* }" Kclc;clear; ST_Idle=0;
. y& }% n: t9 RST_Busy=1; EV_NULL=0;
5 M; b% o0 M7 Y. i# t4 _EV_Arrive=1;2 e0 }& e. O$ c+ F
EV_Depart=2;
. @ j* m q% }" jEV_LEN=3; % next_event_type=[];6 W- v% Z \, Z
% num_custs_delayed=[];
y; x! K. J. p. d5 G S. o, t: n% num_delays_required=[];( m7 b! ~- h& d7 K
% num_events=[];
) o4 e$ F* r x7 [7 N8 O2 u: l% num_in_q=[];9 V9 d: @8 ~- q5 q1 i% [
% server_status=[];+ t0 T& d! c, V0 t8 ~5 g
% area_num_in_q=[];' q- H2 m u, F& X# H
% area_server_status=[];
, ^( r5 o, \# W- {* p% mean_interarrival=[];8 I2 y0 U, ^9 B) h: z& B
% mean_service=[]; i) ^, H, e6 Q& W
% sim_time=[];
6 y* r' M3 K# _% time_last_event=[];( p0 p( R0 }( C7 i7 j! |1 ]; O
% total_of_delays=[];$ t- Q: C+ r: R' f' e4 H5 {% c ^2 T
%
- M1 g O6 _' ~2 s; U& j% n/ Ktime_arrival=[]; %到达时刻 time_next_event=zeros(1,EV_LEN);
1 _, c' [" }# ?; k%仿真参数
" w( u7 C' V/ O$ Knum_events=EV_LEN-1;, b& F9 |4 I. c5 ?; _$ w" n
mean_interarrival=1;
; E# @7 K* b% a8 Y7 V6 mmean_service=.5;4 S" B# O, ]: B, G! z) V& ?
num_delays_required=2000; % outfile=fopen('mm1.txt','w');7 P3 b7 H' M. l- u' `; F1 d$ R: l% b
fprintf(outfile, 'Single-server queueing system\n\n');
# k/ J7 R8 c) X R8 p, i# _6 ], x- rfprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
' m( u) `- @- p* cfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
# n' Y* ?5 U7 ^' qfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required); %%%%%%%%%%%%%part1
, x% L+ Y7 |: b6 Q! S; e, bsim_time=0.0;
: d# H! v: S& J6 q% /* Initialize the state variables. */ server_status = 0;%idle
" @7 c% E5 V" u1 V num_in_q = 0;- G, n7 r6 c* F* I, r0 F
time_last_event = 0.0;3 w; K/ n5 p1 j$ p G3 s% ]. l
% b% B. j( U5 r2 y2 R3 [
% /* Initialize the statistical counters. */ num_custs_delayed = 0;
! p( v& w2 u8 Y4 t9 c& F( g' s9 N total_of_delays = 0.0;9 v4 G" G! |) j0 P% ]* w3 A
area_num_in_q = 0.0;
8 [7 A& y% F, S* D" Y" U+ E, k- @5 b, B area_server_status = 0.0;
+ y) x) N+ s# F
4 J1 M( ~7 j0 ]0 s; J% /* Initialize event list. Since no customers are present, the departure) a9 F, w9 K. K* `) ^0 P5 ?
% (service completion) event is eliminated from consideration. */: d! t ?$ }2 j. F6 y
time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival); n/ ~1 b% N- E
time_next_event(EV_Depart) = 1.0e+230;
- X: N4 R$ k( R2 I, F. V 5 x0 h" w: b T- ^
+ v ~; D$ `* J/ s. T$ B
Q2 h/ t7 Y( B3 k %%%%%%%%%%%%part2
* y0 o- @# ~) M5 c) pwhile (num_custs_delayed < num_delays_required)9 P: |2 b. f& _" h4 G' Y
%Run the simulation while more delays are still needed.
/ d- V3 D0 O) }$ b%/* Determine the next event. */
min_time_next_event = 1.0e+290;, O) ?/ l$ u' \% Y/ x2 R* _3 H6 b+ x% `
next_event_type = 0;. l. Z4 S$ ]/ s: w5 T
8 I5 m# x5 r& x%/* Determine the event type of the next event to occur. */ for i = 1: num_events' [ Q( z* W9 q7 X3 E
if (time_next_event(i) < min_time_next_event) # l8 Q: J" X. O L( ^* i3 N
min_time_next_event = time_next_event(i);' r4 S. {( V0 f0 e) r6 R
next_event_type = i;3 W, s4 W% b. x2 U0 ?4 j C8 ?
end# P! D5 j. Q: @4 v
end
1 ~" }$ x* g0 ] B
6 h) H& G3 j- A; y2 P3 F%/* Check to see whether the event list is empty. */ if (next_event_type == 0)
/ @, m; g; c! n$ Z- r6 l
7 D* M; D6 S) X+ `%/* The event list is empty, so stop the simulation. */ fprintf(outfile, '\nEvent list empty at time %f', sim_time);* M) {9 a- U% |9 }7 Y
exit(1);7 ~$ h$ h) x4 i5 T2 J2 u0 h3 \9 t
end
* F* O' f. c) m0 W%/* The event list is not empty, so advance the simulation clock. */ sim_time = min_time_next_event;
0 i. a; R% i9 W2 p- H& |: I9 y* Q
4 U: b( V8 K, t3 ]2 D
- E3 A' V9 h/ Y# s* ~4 ^7 g%/* 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;
- {6 w* O& ^8 N! R' C0 {' z, X8 T 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;
; k: e6 e9 g a/ {2 z! S 8 |. c* J4 @3 |( d# d2 O# i
%/* Invoke the appropriate event function. */ if(next_event_type==EV_Arrive)0 i0 r: u& w, }+ d/ B
double delay;
) H) @7 b. D/ J * X5 ?1 C" H" m* {" g( `3 T! c; h
%/* Schedule next arrival. */ time_next_event(1) = sim_time + randexp(mean_interarrival); %/* Check to see whether server is busy. */ if (server_status == ST_Busy) 4 p$ G9 Y( Q' ^9 J. H
+ x$ T( U$ ]. ^3 c' e) Q
%/* Server is busy, so increment number of customers in queue. */ num_in_q=1+num_in_q;
# d& s+ H9 c" I* I8 W
; Q* M6 d. v4 \" s%/* Check to see whether an overflow condition exists. */ if (num_in_q > Q_LIMIT) 3 G0 g) t' { o, p/ l
%/* The queue has overflowed, so stop the simulation. */ fprintf(outfile, '\nOverflow of the array time_arrival at');
( t) Y/ Q8 D, \5 I- z fprintf(outfile, ' time %f', sim_time);
2 h8 I6 i- @ N* o exit(2);& w( t" R0 v L) G0 O) Z5 i
end
8 Z: O" D: B" \% B' k7 v%/* 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
( {+ h; ]9 m7 q4 L* i1 A%/* Server is idle, so arriving customer has a delay of zero. (The following two statements are for program clarity
8 M% H; T7 S' a3 ]- e' ]%and do not affect the results of the simulation.) */ delay = 0.0;
# b9 o8 F( o& Q- I: _9 f4 j total_of_delays =total_of_delays + delay;3 `/ s) }6 Z$ d
. H1 j( S R* Q' W3 b( N; w* I%/* Increment the number of customers delayed, and make server busy. */ num_custs_delayed = 1 + num_custs_delayed;* U' X ^- v5 I% s7 }
server_status = ST_Busy;- y+ n+ |4 W$ L) g" W3 v2 ^+ v5 Z& [' T
1 t x1 ^% b a" c9 }%/* Schedule a departure (service completion). */ time_next_event(EV_Depart) = sim_time + randexp(mean_service);5 P7 V* \0 ~1 f) |4 M
end % if (server_status == ST_Busy) , p& _. g. ~6 B
%%%%%%%%depart0 ]$ m! l) z" v; j' E4 l4 r9 I
else
8 y" u- S5 O5 f& r, Y6 G4 q. F$ ` 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;
; ~5 W* M2 A8 f time_next_event(EV_Depart) = 1.0e+230;
+ X6 |0 [( D# x$ v0 k
& i1 |/ k) e) W D& T, h 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);
9 p; O% N) t5 V( e total_of_delays =total_of_delays + delay; %/* Increment the number of customers delayed, and schedule departure. */ num_custs_delayed = 1 + num_custs_delayed;( j9 j# B/ l9 \9 r: |" p0 r' 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));7 L% Y+ a7 I" M# Y6 L4 P; u# J. `
time_arrival=tempForPop;9 Z. h4 ~* U! w; w. z
end %if (num_in_q == 0); M& \2 Q, K, }8 u
/ G; @+ M) z- Z$ X$ U4 [% s
end %if(next_event_type==EV_Arrive)
3 r* V! \1 A% v/ i# |0 Q
- W& ]4 x1 x1 T! j/ Pend %while
6 ]4 g9 P" Z `/ b% M# w
3 F9 V. h; B+ k+ ~6 n%%%%%%%%%% part 3 C' C `/ W4 U' 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);
& U3 V, O ?/ \9 G2 r" o8 A+ P$ Q+ R# ~ fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);- P* i: z, J5 {4 }, ]! F7 i) d
fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);& {4 {; w6 |8 F+ A
fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
3 @, ?. R$ J, y fclose(outfile); |