数学建模社区-数学中国

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

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

- a# s' u4 K" t4 b( Z
3 k4 R1 W) z* a% w, k, V3 d3 Z
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
/ ~# L2 r1 \8 L# y% [" U$ V5 B. [clc;clear;
ST_Idle=0;
9 {+ v5 f4 ]; J6 E, U# F0 e0 fST_Busy=1;
EV_NULL=0;
7 n6 u7 p2 G& j2 I6 DEV_Arrive=1;& H2 l0 p" K# j0 ]- G: ?+ |6 B0 E) R
EV_Depart=2;
2 |8 S) A# k, L& REV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];' U2 N# d7 ~8 j8 A! G9 h
% num_custs_delayed=[];
- p5 f5 g8 \2 _/ K% num_delays_required=[];6 y- O2 v1 A# i7 {7 f0 I( u
% num_events=[];
" l  e) J- u( N! E. p" V0 P% num_in_q=[];3 G6 ]! a  r: t" H
% server_status=[];
: s) b4 Q! c: @5 B& i- I( u/ x% area_num_in_q=[];4 g8 z, y1 H2 S0 \  \
% area_server_status=[];
( q! u, l4 s, `9 Q9 w3 p% mean_interarrival=[];1 V4 g4 A, |& {9 r( f- `
% mean_service=[];1 v$ D, ~4 e" g
% sim_time=[];, k. o* _5 r9 m! U2 c
% time_last_event=[];
* l" N) o6 e2 t. B! f. [% total_of_delays=[];3 s4 K7 `" c7 B$ a  m7 D+ [1 f
%
) O2 [4 o  @, V* ]time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
# ^2 y+ _0 Q- b/ q%
仿真参数- k& A) J0 G4 ?
num_events=EV_LEN-1;: X3 _( t" [2 \0 I$ V5 @
mean_interarrival=1;
3 a: b4 W4 T# b2 a1 rmean_service=.5;
0 z% g4 z9 r4 L7 o8 O; L1 ~num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');  l- M! v8 p7 ^/ k
fprintf(outfile, 'Single-server queueing system\n\n');
1 V5 g( E. H( u: h( N6 ?! v* jfprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
" i. Z- ]- Y3 a  g% Hfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);) W1 a: T7 l/ x
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
/ g, z% F5 g6 g. C( N0 {$ q# Y, r& o$ Z, psim_time=0.0;
& k' t) |9 @( g1 }+ o+ I0 }2 V* Z%     /* Initialize the state variables. */
    server_status   = 0;%idle
3 k4 l; \/ R, `+ f9 l+ t    num_in_q        = 0;
9 i( T! m- C  u: L! B+ C    time_last_event = 0.0;9 M# u- g: V# u  T
    5 e7 u. r2 D- Y" y1 J
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
$ h% {/ C, W2 n3 [    total_of_delays    = 0.0;2 e% x, R8 Y* J
    area_num_in_q      = 0.0;
  h4 J' f5 J. ~/ I, ?/ K: A, R1 m    area_server_status = 0.0;1 G- i5 `$ ?! u1 c5 d0 X
    / X" \7 s- q9 s7 d: @
%     /* Initialize event list.  Since no customers are present, the departure
/ n& O5 d. t2 {6 L' l3 k5 B%        (service completion) event is eliminated from consideration. */- t8 x0 Z$ E, H- Y* ^7 H
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
1 y  w- D0 G* E0 M7 ~6 D    time_next_event(EV_Depart) = 1.0e+230;
2 G: v8 t: R  `! Q% M3 b6 L! A" c; Z
    # c, }- F8 F5 w' f! S
   
8 ]- l  O8 Q4 C  b7 r$ K+ p- q   
2 a7 a2 C. c7 \6 l %%%%%%%%%%%%part2
1 ], I0 c0 {; v( l* bwhile (num_custs_delayed < num_delays_required)
' s, t$ w2 x7 A6 U+ h3 W) v7 b0 q%Run the simulation while more delays are still needed.$ S3 f- E) o" S6 i& W5 x
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
8 c: K/ {$ O* N4 o5 \$ y+ N     next_event_type = 0;
; x. \+ I6 J' p" [1 I$ b2 S     - t5 l4 K, O0 C0 _) X. L
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
# u" O3 l. M% L+ ]4 ^( |# Q        if (time_next_event(i) < min_time_next_event)
# A" m% R% Z$ o1 G+ Q, {            min_time_next_event = time_next_event(i);
$ }: {4 p, V7 G: \5 p0 c5 E6 j9 N            next_event_type     = i;
: Y! r& U; R, T& G$ u/ ^6 K        end
) ~8 w7 @% r) S+ f    end- {" e7 E) X) N7 C  Z, h
    ! q+ s4 F- [0 k9 K! O7 g" S
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) ' {  b6 ^/ ^$ A' Q; V2 m
        : Y0 j& L) {9 G2 H5 P5 q
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);" v) `$ G; G4 C# ~9 X
        exit(1);4 c, ?( O- Y# g0 q8 i  I* l7 T- V
    end
  l- K0 \2 b( \7 i8 n%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
0 w9 A, i. v; W9 _0 b6 X1 ?) T7 n
+ b* c3 [( {: Q& P" G1 j' ]    & E* [% r" U4 Y" t% Y, I' K/ ?- r
%/* 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;7 j; ]- y/ @4 P. s( B: ]8 ^/ 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;  e2 K) ^) _2 n- i9 c
    9 N+ v) {% O9 e) x/ N$ m! U  G
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
) W' P' H! X, T) {5 ^* o% `        double delay;
% v* w5 h6 n3 u! _( W' J$ U- s; z        
0 T' \4 ^# G8 j1 ?8 N5 n%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) + a5 E7 c5 q$ d" ~7 m
            
" V$ }$ K$ m- l) I7 }%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;& e/ w1 f+ ?. z, k
            
& @  C4 ~: t$ J  k$ O6 p! B%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
2 a# k+ o" V% l- h%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');1 {* K. A& ]. ^2 r4 g
                fprintf(outfile, ' time %f', sim_time);: ]) G% i! g$ J
                exit(2);
8 ~- t- V2 j9 c* V2 \            end8 D% t& {. j" a$ r4 X" s
%/* 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
; k. d/ y) A' |%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity ; N5 v/ v% H( w# O
%and do not affect the results of the simulation.) */
            delay = 0.0;2 l+ \* Q" G& P1 e3 _: c" j
            total_of_delays =total_of_delays + delay;2 M* k; B) x4 a
            2 \/ _; }6 `: v% Q; z, S; K& _" i5 P
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;. i, B. X' z, X' S; O2 G: Y
            server_status = ST_Busy;5 Y. l9 Q- ~" h  V
            
1 W) |( J( z; H; i$ S0 y- ]%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
- ~4 v  l/ }2 U- E, x        end %    if (server_status == ST_Busy) . H% Y1 R( c6 j# \& U$ h: d! J) I3 J
%%%%%%%%depart$ L: m2 h; A* ~+ l# M  l9 \8 G5 H
    else% d( W2 ?4 p9 ~+ H
            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;3 S3 J/ g9 b2 x8 s2 m/ R5 T
            time_next_event(EV_Depart) = 1.0e+230;
+ G! S, T# h. W; C2 A' N  t    ' }! `+ X, V2 O! b' g- L
        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);
. W# V, P/ i0 J/ N7 y+ N7 a! P            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;
# i6 h1 Y1 x& N) g2 ]' f            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));
! O3 w+ R" G9 L7 I& B% C            time_arrival=tempForPop;
$ ^. N5 i( ]$ j        end %if (num_in_q == 0)
% \/ w* }2 b# c9 p% w        
) f/ @( ^, n5 @& W+ M    end %if(next_event_type==EV_Arrive)
4 y8 Z' I; g, d( Z    4 X! K3 C+ ~4 _3 B* S1 r& v
end %while
6 o0 q3 R6 Q# \: R, `# ~2 L               

2 {% f! p% b+ w- |%%%%%%%%%% part 3  }6 }# l, W( {, K& P" x$ b! A
%/* 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);
1 K( A* v; F$ a' A, _+ \" N    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
7 m3 k" ^+ \( h, L2 }& e    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);( U0 R5 p9 {9 f$ d+ p
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
" c; J; a" _) G" S  [$ v    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