数学建模社区-数学中国

标题: M/M/1排队系统性能仿真代码(MATLAB) [打印本页]

作者: qlau2007    时间: 2009-8-12 13:02
标题: M/M/1排队系统性能仿真代码(MATLAB)

$ p  I4 T( K3 o4 o
0 E  Z0 u2 b( h6 a4 ?7 H: Y; x
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
- [; s' w. R: S- |9 }3 sclc;clear;
ST_Idle=0;- d( A, }/ k2 T. }
ST_Busy=1;
EV_NULL=0;  X; M! Z+ {$ j
EV_Arrive=1;
9 L  ]5 g" l  U/ X0 _. I$ ~6 ?0 BEV_Depart=2;9 e$ Y9 w* \+ e( Y5 b
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
4 E/ i6 J% v- O9 D: `7 f, h% num_custs_delayed=[];1 L" @+ X6 f) o( N3 H
% num_delays_required=[];
/ s. b7 ^( i& g4 `' ~6 c% num_events=[];7 v7 ~# T4 q( n: r0 M! q& ]. g
% num_in_q=[];
. o" A$ i8 M9 D) C& g; K5 ~% server_status=[];
+ V, E1 i; n+ c" [  M; ^0 h% area_num_in_q=[];
8 M' u5 z' R% a3 p% area_server_status=[];
0 X& U5 B; X' B) }8 M% mean_interarrival=[];3 y+ g6 w4 r4 r9 I' y' \6 R
% mean_service=[];: L+ @3 f6 W# i2 r4 y9 Z3 f
% sim_time=[];  o3 e8 C- I* u! H; X) A9 D3 n1 D4 J
% time_last_event=[];+ N7 m2 |: F) c" f+ T* L' [" x
% total_of_delays=[];
& C- U4 l% d) V1 |% 6 n$ S- |7 P/ X) z
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);* C+ e6 f( S& }) e: i6 N* Y# ~: F
%
仿真参数8 Z2 d) @1 x3 G& o7 ?% O
num_events=EV_LEN-1;
5 g0 k5 f6 {+ A/ ]( ?7 [mean_interarrival=1;4 y4 C4 \$ q! k. O  ]7 `
mean_service=.5;
) o% I4 l0 f! d& Knum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
; |& G5 N: [  {, Ofprintf(outfile, 'Single-server queueing system\n\n');
. V8 }6 l1 e9 rfprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
* o! T* w5 Q7 lfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
' l. i2 {9 u, [5 V& ]fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
) i" m1 ^* T* z, \1 K. o& M# msim_time=0.0;) n( g  Z; z( N& D
%     /* Initialize the state variables. */
    server_status   = 0;%idle
& ]% }, i; p& K3 M    num_in_q        = 0;; B8 T( _- h( L  o+ u3 ?/ ~) q" \# P
    time_last_event = 0.0;
9 P  t, I/ B) ]8 @$ _9 L% x    7 X+ p- I7 \" G0 v2 X7 B( ]7 G
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;% a1 F% g# Q: B7 a6 R& y
    total_of_delays    = 0.0;$ H6 y% y9 f7 ?" Q" e4 }* U- c
    area_num_in_q      = 0.0;7 q6 q3 z) I7 F
    area_server_status = 0.0;
% x% e: {2 c' Q' u3 o. k   
$ D- I# x1 j3 @7 p6 x% [2 ^%     /* Initialize event list.  Since no customers are present, the departure
1 g8 \4 [9 b% |3 \) P%        (service completion) event is eliminated from consideration. */
/ B* D0 O6 ?7 m    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
7 D' O; a2 O" N& p. |    time_next_event(EV_Depart) = 1.0e+230;
* v6 }4 A* A, `
    # C  F# e8 r1 B8 O+ B
   
  U1 i# o7 E: _: ]    + c. E3 A# J, V" E; Y; B: N+ _
%%%%%%%%%%%%part24 O3 z6 U5 A8 ?; A
while (num_custs_delayed < num_delays_required)! m5 p2 ?! a, w+ Q7 [  V+ P
%Run the simulation while more delays are still needed.  S' H3 Z  q6 z7 Y$ C) m  d
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
& k2 |% w2 W$ e6 }1 F4 |     next_event_type = 0;$ `& {* N1 Y4 T1 p, a
     4 s1 P# H, h0 w: E) {! n
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
4 M1 t) ~- w$ a+ z# a4 _: U        if (time_next_event(i) < min_time_next_event) 7 R# F$ B( `4 }; z8 B' E
            min_time_next_event = time_next_event(i);
, p0 |# M( b- l, J0 N6 A) e9 T            next_event_type     = i;* n; F8 _! X4 w# ~
        end3 |1 O9 J5 k1 y2 w2 ^4 Y. H" ^6 i
    end
9 I. D5 h: `0 f4 _( ^+ W    4 ]0 }: y, N9 w1 f
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
6 H. P2 ]2 c% i- I9 g- E        
1 O! F  O0 K/ A; k2 O%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);
: {/ ?( e2 A$ u# A. t, U4 m; ]# _: A        exit(1);
& j& g" W3 N, |1 M    end- ~0 v$ B' n: X
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;4 x# a* [# X/ m7 z. `

. D* g! I" f4 H7 m. p- n0 v: \+ V   
5 n$ g2 A  b- l%/* 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;+ T$ v7 _4 i2 n) K; J0 ?4 U2 |
    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 w! c' Y% o$ b6 @
    8 I, U# Z: s" t) {  Y
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)5 t% a8 _' T- R9 F2 \1 |! |
        double delay;/ n3 B4 {* x4 u5 f6 B" b+ h
        ; ^" F, V4 l& p& r' e4 M
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy)
* O. h  k( a; U! K            0 Q+ O* U% w$ @. W
%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
5 y" ?4 Z& V' v3 I$ y            ' U5 |3 h/ V, b. W
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
6 E" a% |+ r6 }! z0 |2 T8 y%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
) D5 i5 x) W- F% f+ w                fprintf(outfile, ' time %f', sim_time);1 }# e4 |0 z7 Z/ j* S
                exit(2);& J- B. P* e  J; ~
            end. D! ^# l8 z8 K8 s# W" N
%/* 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
+ ?3 J8 e' O6 D0 o* O0 T%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity ( S$ {: n: h; a
%and do not affect the results of the simulation.) */
            delay = 0.0;, e( e+ M/ L' a: `6 S4 @) Y
            total_of_delays =total_of_delays + delay;
* I4 [) S* r' L: b+ h            7 o# A( v" d  [, S. y( P
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
: O3 N3 E' E4 N# B+ k            server_status = ST_Busy;5 Y+ \; y2 f; g9 p7 l$ N
            ) C. J, e, k; e  p1 i, l
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
3 R" t9 M; c9 G% H1 v* [/ q        end %    if (server_status == ST_Busy)
  j+ Z) p( ?: Z. N+ T%%%%%%%%depart
& J$ C; `# J$ x& K    else8 n- ~" Q0 h! r  ~* E) K, S7 H2 X; z
            double delay;
%/* Check to see whether the queue is empty. */
        if (num_in_q == 0)
% /* The queue is empty so make the server idle and eliminate the departure (service completion) event from consideration. */
            server_status      = ST_Idle;2 l' d+ c9 q) W/ S! k" n7 Q
            time_next_event(EV_Depart) = 1.0e+230;4 ^% k$ H, H6 ^9 j; t, m* M2 k
    ' Y+ ^# f8 d2 I  p
        else
%/* The queue is nonempty, so decrement the number of customers in queue. */
            num_in_q=num_in_q-1;
%/* Compute the delay of the customer who is beginning service and update the total delay accumulator. */
            delay = sim_time - time_arrival(1);
( E. E$ k/ l) I$ |0 F            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;0 x: e- d% `: J; U4 h
            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 V; V# W( c, w/ k  N; G6 o            time_arrival=tempForPop;! f4 b: P- M$ v; G# H% M
        end %if (num_in_q == 0)
5 l% k# F7 K5 `) R3 d        
- L$ ~( G2 B9 ?# `( @3 g" p    end %if(next_event_type==EV_Arrive)
( p" J: y. H. l0 L    : n, R  O$ O0 M1 G) V
end %while0 \8 V0 D5 d' S! E2 S7 p
               
$ |. p, T5 a0 e# P! W1 `
%%%%%%%%%% part 3
, G& n" S* \3 S/ m1 A' a: o0 X( T%/* 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);
$ m% c6 @( r  t: ^    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);0 r- T% u: y8 ]/ ~6 K
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
  j1 _/ @5 l& a5 W9 Q    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);# C3 o6 H: j9 v6 M5 z2 c
    fclose(outfile);

作者: liuyaliss    时间: 2009-8-22 09:28
看看~~~~谢谢楼主
作者: rockhuman    时间: 2009-8-22 09:41
资料发上来 最好加上 一点解释  让大家都那能理解一下
作者: purplelesly    时间: 2009-8-29 12:52
谢谢楼主 ~~学习学习
作者: leeeff    时间: 2009-8-29 18:24
dddddddddddddddddddddddddddddddddddddddddddddd
作者: norah8291829    时间: 2009-9-3 12:27
看不懂~
作者: perfectgreen    时间: 2009-9-11 13:57
啊!!很好很强大啊!感谢楼主。。
作者: JocelynHUI    时间: 2009-9-11 14:14
高手啊!请教一下,B题第一问怎么做呀?
作者: GunBreaK    时间: 2009-9-11 16:20
没有矩阵币怎么办?。。。
作者: jamesgeng    时间: 2009-9-11 23:05
好东西啊,就是要用仿真的
作者: chuan0810    时间: 2009-9-11 23:46
有用没有那个也是!




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5