4 V. k- E( m! u# m$ `* P) a# r
# x* @6 W) f+ n
%mm1 simulation in matlab3 [; w2 b, D' x* m8 A
clc;clear; ST_Idle=0;7 Q3 u/ Q. r- w/ o- w: {$ K% ^
ST_Busy=1; EV_NULL=0;
. J: X* o: k$ L8 R9 ZEV_Arrive=1;- `9 r0 G; ]0 \' j
EV_Depart=2;
1 K. ~; z6 ^& \5 hEV_LEN=3; % next_event_type=[];) ?! D/ B8 V; n9 s' ^' H
% num_custs_delayed=[];
4 A7 j/ i. y8 e4 A% num_delays_required=[];
/ Z$ L* d; T2 o; K3 E& N% num_events=[];
9 n7 `; X! O; K( N1 P% num_in_q=[];0 u$ ~8 Y4 c8 ]' I7 t' t
% server_status=[];; ]9 @4 W$ B7 n
% area_num_in_q=[];$ n! r8 v1 k0 U
% area_server_status=[];
; O( U2 J! b- G9 x( J& z. P% mean_interarrival=[];6 q1 J+ m+ H9 f6 U3 h! s5 S
% mean_service=[];& _5 B8 { {! K- g! q* Y: z
% sim_time=[];8 i1 s) y; g' {; h; e% A' h+ l2 r
% time_last_event=[];" u; ?4 \% p, A# D
% total_of_delays=[];- W. L$ ~3 V/ G6 g u
%
) `( I5 F% O; k5 m5 utime_arrival=[]; %到达时刻 time_next_event=zeros(1,EV_LEN);7 O* d: n% q' c8 H% U: F! r
%仿真参数
/ u5 H3 n! t# l" i* D$ N0 Bnum_events=EV_LEN-1;" g( t& k+ f* `6 c$ p' M, U/ [
mean_interarrival=1;1 w$ W4 w; O( }; z7 y
mean_service=.5;5 E! m, A9 T3 y# k2 f
num_delays_required=2000; % outfile=fopen('mm1.txt','w');1 I& X' c9 }$ u* _6 T+ {6 H/ L
fprintf(outfile, 'Single-server queueing system\n\n');
1 }* [% E2 s* s" Q" U! B- qfprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);, v, a- o& V& s( s" z; s
fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);3 S# ^2 U+ X- \; z
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required); %%%%%%%%%%%%%part1
& i3 T& u8 \" Z0 ]2 X8 m( Q" ssim_time=0.0;
% |& @, E) ?6 m* n( Y v% /* Initialize the state variables. */ server_status = 0;%idle& c8 h9 I' i) Y$ a1 X; }0 e
num_in_q = 0;. k n1 A! j& J; G3 |8 }1 B" s
time_last_event = 0.0;9 q6 q5 A/ u- H( [' h+ a
- ^2 ~% p4 e" V% /* Initialize the statistical counters. */ num_custs_delayed = 0;6 \* N# R! M3 {
total_of_delays = 0.0;4 S5 [2 T2 u! I' t" z
area_num_in_q = 0.0;
L5 b5 p" Q- P2 }! x+ Z: w area_server_status = 0.0;
4 l0 l6 C8 | k; y
$ l7 {& I& b9 O+ Z% /* Initialize event list. Since no customers are present, the departure
' f$ A& _1 q3 C5 Z5 O% (service completion) event is eliminated from consideration. */
: {& c1 r, s+ y) d# X& @ time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
* J) l! p. x& f8 r time_next_event(EV_Depart) = 1.0e+230; # V4 R6 b4 C' [- k- Q7 y2 @
# v9 ]$ J8 Q( f
- k( F( a% l: E3 o: p% t) O 5 k( D0 y( S. F" I
%%%%%%%%%%%%part2# Q& r7 N }) ]$ H3 ?
while (num_custs_delayed < num_delays_required)3 ^" ?. n! B$ e( H3 X
%Run the simulation while more delays are still needed.
! x% _: K3 d( ~# ]7 _%/* Determine the next event. */ min_time_next_event = 1.0e+290;1 ^- D& q+ |2 \' Q/ Y! q; N8 N
next_event_type = 0;6 D1 o$ ?5 y& _3 U) R7 K
$ B7 N+ I/ e+ Q' t( T
%/* Determine the event type of the next event to occur. */ for i = 1: num_events
9 W8 i. w, H c1 P$ ] if (time_next_event(i) < min_time_next_event)
- N, D, G( |& I0 y) J min_time_next_event = time_next_event(i);
/ f) w, k" C+ `% j( u! [, Y) I next_event_type = i;! k& d/ n/ p$ C# p
end
3 `: z# K; y( }: |7 [) U end
& q! c& Y* w# V7 o! [1 K: Q! H 1 V$ O/ n- c' |) {8 H
%/* Check to see whether the event list is empty. */ if (next_event_type == 0) + Z+ g7 Y8 N) ?2 m* d5 V
8 x/ ~, g1 [9 b* n2 L! P%/* The event list is empty, so stop the simulation. */ fprintf(outfile, '\nEvent list empty at time %f', sim_time);
& s8 N1 A7 P3 T exit(1);; h) Y# m9 |" F! f5 W
end" j4 U# b. i$ D
%/* The event list is not empty, so advance the simulation clock. */ sim_time = min_time_next_event;
& J+ x/ A( r& W 7 \( [' Q5 \8 g1 H) [
n$ t6 n( R. o- j% O+ ^
%/* 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;' g; m6 c |- O: S
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;+ |* c. @7 q3 x( H9 @
: @4 _% Q5 g H
%/* Invoke the appropriate event function. */ if(next_event_type==EV_Arrive), N6 @* ]+ B/ d$ @1 ]
double delay;9 k* {' x# q% o1 a- L' c& D
* c- x8 g0 V K) }0 O# K" H8 k" |* @
%/* Schedule next arrival. */ time_next_event(1) = sim_time + randexp(mean_interarrival); %/* Check to see whether server is busy. */ if (server_status == ST_Busy)
: k9 Y* D9 I' s5 c) l P, U# B
' [" r! G1 i& U# @%/* Server is busy, so increment number of customers in queue. */ num_in_q=1+num_in_q;
( u. U: S2 |" r+ ^) v9 F 7 S$ q' g, |6 K# u- O
%/* Check to see whether an overflow condition exists. */ if (num_in_q > Q_LIMIT) $ [! _/ G5 h3 M5 M/ s' v
%/* The queue has overflowed, so stop the simulation. */ fprintf(outfile, '\nOverflow of the array time_arrival at');
2 ^* S) M3 q* D2 \6 ] fprintf(outfile, ' time %f', sim_time);- F- Y1 W3 i( R8 n7 r. q. v' a J- |! w
exit(2);
, _5 ]- V% y( G end
) U! e; ~, y6 m/ 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
T8 `7 S6 r' p D; _. X3 e1 Z%/* Server is idle, so arriving customer has a delay of zero. (The following two statements are for program clarity 0 K# z% D9 \ o. c: W* `
%and do not affect the results of the simulation.) */ delay = 0.0;
3 J5 T7 M! \; M total_of_delays =total_of_delays + delay;
3 Y& w$ J- B' G; e n f+ ~
3 [& V, O6 \; A$ ~/ w%/* Increment the number of customers delayed, and make server busy. */ num_custs_delayed = 1 + num_custs_delayed;# B/ u. u! @7 G, X
server_status = ST_Busy;- i5 m: I" l7 R8 ]2 o o
( g- R9 {% @% b/ t
%/* Schedule a departure (service completion). */ time_next_event(EV_Depart) = sim_time + randexp(mean_service);
; Q) ^" H. x" g4 F5 U' }# n5 U end % if (server_status == ST_Busy) " F4 c# w; i4 E4 f
%%%%%%%%depart/ @) k2 [! {8 W2 e
else3 k! t" O6 X6 V6 `# i$ T+ w2 p( B
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;( q, w( j4 \& p7 `
time_next_event(EV_Depart) = 1.0e+230;
: _0 s1 h6 w: W3 @
1 C' V* m0 z$ W' \( ^3 q( H- X; e) { 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);
2 t `- P2 X- ^4 u total_of_delays =total_of_delays + delay; %/* Increment the number of customers delayed, and schedule departure. */ num_custs_delayed = 1 + num_custs_delayed;! `$ Q& K1 p# {6 U) u5 a1 {, _+ x% o9 Q
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));% @# ]; s# {% g Y' t& S% N
time_arrival=tempForPop;
. q6 X: B% u' P end %if (num_in_q == 0); u0 z; R4 [$ |; _2 z; m
! M0 ?5 R5 [7 @ end %if(next_event_type==EV_Arrive)- I5 r+ [! }; B, b
7 @* e7 y- I1 B' L/ |' t& Iend %while0 I7 ?( z, \* E2 O5 e) v/ n3 D% U, }
. G* a" {! b, Y# V- ?9 |; I%%%%%%%%%% part 3
1 U. @5 _6 ~. k$ G%/* 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);; ^3 W1 a- _$ k; \) ?
fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
. h3 [& ~) ~# g; D) o fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);. X) F! t6 W; p8 k+ k9 N
fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);" g4 b) _9 s/ n: L# H
fclose(outfile); |