数学建模社区-数学中国

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

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

5 A7 z# l6 {& U' u* [) j  z4 ?$ f7 W- Z2 [& u% u4 c+ N  W# w2 r) E( t
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
- A/ U/ g$ E, E) [- N8 oclc;clear;
ST_Idle=0;
, \7 Y) b4 B0 S4 zST_Busy=1;
EV_NULL=0;7 M% G) t" P; ~" {1 I
EV_Arrive=1;; H! [' a2 C0 k3 r% K8 E1 `9 f8 f
EV_Depart=2;
8 R9 {& I" A. J# z$ E) @! REV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];, g( N. _$ G# K9 U& G  u
% num_custs_delayed=[];% L2 D# ]9 n2 C5 `+ b! t/ ~! Z
% num_delays_required=[];( ?- I5 Y' B$ d; G& p' o
% num_events=[];
$ O: {1 M/ e9 q" \6 E% num_in_q=[];+ E" c# f2 d# [3 Q& u/ N: J
% server_status=[];
" B& Y# i0 J5 \9 H% area_num_in_q=[];1 U' P) r7 A' l/ m: W# |
% area_server_status=[];
/ Y3 [/ Z  `4 g, x/ I/ X% mean_interarrival=[];
& R% O2 g' s8 ~% H6 q% mean_service=[];7 m3 Q) t) \' T
% sim_time=[];6 ~: \5 \+ Z8 X! N
% time_last_event=[];
1 _9 K! C5 L) [6 f: P* B% total_of_delays=[];
6 g# R6 I3 |+ F4 b' j3 A3 O3 p% 5 j2 W! o5 ?- @/ d6 N
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
1 e7 \& g& @. C/ z) y  v%
仿真参数
- {1 d; p4 M. R- U; J( bnum_events=EV_LEN-1;- x* s) m  Z# K9 `  H9 T* ^' B) |! f
mean_interarrival=1;
) F1 T$ e/ @  smean_service=.5;
6 X& g: ^' `& P9 k+ Unum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');! j: w9 j2 W/ r  u& G' H2 W
fprintf(outfile, 'Single-server queueing system\n\n');  Z; G+ ]+ d& X6 N
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);) e, }4 K6 b! h+ Q6 i* q! w0 u5 H
fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
5 I8 T" q( `' I) Y2 Z$ @% s' _! jfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
( T) ^  n3 B. @sim_time=0.0;/ f5 M& A) h3 f; e, I
%     /* Initialize the state variables. */
    server_status   = 0;%idle' r1 V5 a7 k, P- ]4 g; s0 C, N
    num_in_q        = 0;
4 a6 ?4 w! \$ f, P    time_last_event = 0.0;
/ b, |+ C( B" V; n/ |: E   
6 ]: @. h3 Z, A) g: A; p%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
1 o/ }. M7 f$ K3 w" u3 w    total_of_delays    = 0.0;
* N* f9 Q! k+ u    area_num_in_q      = 0.0;$ ~" f: a2 B+ e( f. r% n$ s) k% _
    area_server_status = 0.0;/ p0 H1 f' E* H! Q5 Y
   
. M# n2 K! g5 z1 u%     /* Initialize event list.  Since no customers are present, the departure+ E0 b2 z2 ?; d) V0 ?4 @+ V
%        (service completion) event is eliminated from consideration. */
' @; E8 z7 h# s    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);" h6 ^5 v# M7 B# X6 r
    time_next_event(EV_Depart) = 1.0e+230;

$ w1 ?! J% o% s2 L' j! [1 t    ) y, }+ B5 k/ r* |
    5 g+ X, K5 {/ s7 p: b4 z; [) l7 l, i
    - }4 ], W: q; I! b# r3 n3 g
%%%%%%%%%%%%part2% @. l( ]6 }5 H0 `1 L: s/ R4 y# t
while (num_custs_delayed < num_delays_required)
# v8 m" |8 L0 A3 G6 s# g%Run the simulation while more delays are still needed.% L2 a' v6 O$ [" k" x
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
: ?/ m+ x; Y  ]! S% v  n     next_event_type = 0;; ?/ U* B! X* w% K1 \/ R
     % t  M: c5 Q4 Y; m; G; _
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
. c: j0 v5 a& c! k7 a1 X5 u        if (time_next_event(i) < min_time_next_event) 9 w9 ]/ i+ w3 M) X  p
            min_time_next_event = time_next_event(i);. N4 R, \8 e% y
            next_event_type     = i;
. u4 n, q& s6 E1 m7 h8 U# y6 }        end; U. w0 q  F" G/ H
    end$ e5 f4 c6 `1 f* @  p! }2 Y
    + I9 u' I- K- ]
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
7 }' ^5 d/ u! o6 D! j3 T        ) r  A2 Y/ q: t1 `
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);) m) M' b# }3 f, ~0 A! \# S" Q
        exit(1);
/ \; z: g: C9 c    end
5 X$ ]6 M, l- h( w: n: s6 A%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
3 S# C6 }1 \; B+ b: N- W4 d , @4 N. ^, U# E) O/ n, I
    ( `# F# e/ l3 v8 H9 {
%/* 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;
8 C8 G9 L8 x, J    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 m( V: x& z3 R, V# s0 X2 M; y   
3 ?% ?3 [; l$ S%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)' \( l0 v. _3 A$ ^! ?! k; m2 s) G
        double delay;
4 Y/ E' m9 L$ M; G* {" O8 r! c        - s" \: J. W6 p  k4 Q& F; B
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy)
- C" ?  Y& b, F, G( q0 Y# R            
$ L) i6 Y. H9 s  W  Q% \: O%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;) r, H& N8 f6 H- b" i
            1 i$ J; o; }: V  W1 j* q' Q/ e
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
) {6 n' i7 b* d/ `%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
) ?& p+ ?8 N7 s5 X( [; n                fprintf(outfile, ' time %f', sim_time);  q9 \. A" Q" @2 M
                exit(2);
% {, e, ~& S5 Y            end
) f$ ]3 W; l; I6 _$ Z* h3 {/ O! T%/* 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
, r: h5 _: k# q, I$ E+ n+ G& c9 f%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity - @/ W7 y% L- P2 x
%and do not affect the results of the simulation.) */
            delay = 0.0;4 l$ w; {9 M! I8 d3 j/ ?$ N3 r
            total_of_delays =total_of_delays + delay;* v1 w1 g2 [) T; N% }
            0 P4 J+ `$ t5 @9 l; }
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;$ r2 u; C' i+ Z1 U. k
            server_status = ST_Busy;8 u) Y; `, G5 ^. [3 s9 a
            . L- O- J5 \! i8 A) b. y
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
2 H- K( k% w+ t1 a8 _3 y        end %    if (server_status == ST_Busy) ( e/ E7 i' \$ o; P. r
%%%%%%%%depart
: e1 b9 A2 ^1 D2 _; J    else
4 H& W7 f7 j$ a4 e& K2 C& o1 ]. C            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;
( d, j, N0 ~/ K            time_next_event(EV_Depart) = 1.0e+230;- b3 h, f& C$ k8 k0 S# S( _
   
2 c% k  Y" C8 a! b        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);2 C0 P, a0 Z1 f( h
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;6 K5 W9 u0 ~5 @7 t
            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));
& i* d  E, v) g$ m) y. R, C, J            time_arrival=tempForPop;
- ?, w" W" w) A4 A- ^3 y8 T        end %if (num_in_q == 0)0 f! g3 A0 D! U8 e8 U
        & l* N) f- S& j$ d) @, k( n4 q
    end %if(next_event_type==EV_Arrive)
  L& |' _# D1 k0 y( N    / M  a/ R3 u( O* u
end %while
7 D; O9 B, n( s" p               
; D% e) G# b2 Z2 i+ F7 C4 K. X" ^
%%%%%%%%%% part 3# M; i8 t+ r; c$ [+ P) m9 y3 p1 X2 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);
; |; a+ b" Y7 P  M+ E    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);# Q! j# q$ [: x3 Y; c4 |
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
; c& \0 B4 A: a" o/ g: Q/ x0 H* E    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);6 l! M+ F7 X! m% M& w& Q
    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