数学建模社区-数学中国

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

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

% W2 V9 Y) T4 U, u3 C& @8 U/ @% W: I7 `* o' o& O  N$ w. I& P
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab1 X7 P. s6 W3 \( F
clc;clear;
ST_Idle=0;
* T5 A- W. y* ~& m3 {" cST_Busy=1;
EV_NULL=0;
- U6 i7 k) r9 K/ k  P. eEV_Arrive=1;' o4 B3 G$ s+ w" ~5 P
EV_Depart=2;" G$ y; ]( b" S
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
* V) `" z0 L9 I  U: m/ S% num_custs_delayed=[];, x5 ^8 i- m% B& T# h2 I6 b" y7 b
% num_delays_required=[];
8 }7 m" ?+ u9 c& r% num_events=[];: B4 i& J9 Q2 U6 ~& M+ x
% num_in_q=[];8 `, x$ y: @! c5 }
% server_status=[];
: f, N4 O1 k8 o3 l* E* N% area_num_in_q=[];# H; t: [" F6 s
% area_server_status=[];
& [1 B. m/ u6 q6 F) }& @% mean_interarrival=[];
7 q# V0 ]$ [  m& n/ v! @% K% mean_service=[];
$ H9 f& d3 D/ E3 q5 H+ G! h' j7 L% sim_time=[];4 |8 p7 O& r5 h9 f' R
% time_last_event=[];
, N. \5 I& C" e' W% total_of_delays=[];
9 {" g1 r, u2 \0 A: f%
$ I; G9 X) X4 Z4 M" }time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
7 }( s  n0 d: G* m%
仿真参数1 n3 @# m& U7 [; ~: N$ X
num_events=EV_LEN-1;
9 e8 A, K  Z9 |9 u+ X5 V, F" cmean_interarrival=1;
5 W, ?5 W7 M6 n# g, f' ^mean_service=.5;
( p. U# c9 {+ R; ~num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');" y1 m  S' A6 v4 G
fprintf(outfile, 'Single-server queueing system\n\n');8 i0 Z" N* {( _/ N" B+ F, `
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
; D0 S. V" _1 q" V+ ]fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
7 \8 @7 }+ {- o+ Zfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
$ h& m6 m3 d5 Bsim_time=0.0;
8 M# Y2 I* h; s7 O$ j%     /* Initialize the state variables. */
    server_status   = 0;%idle
3 h2 Y8 y: ?; A; D% ]9 @/ O    num_in_q        = 0;
  J1 e' l' ]% }% W. J    time_last_event = 0.0;
' x0 A* q! c; p. I   
8 c5 [' e5 f" M2 f0 ~%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;+ c3 N& q$ N6 X: k2 ?
    total_of_delays    = 0.0;* Q$ w( L, u, E
    area_num_in_q      = 0.0;, `: d4 N5 z8 w, s% p2 |
    area_server_status = 0.0;  g% L2 r9 J1 ]. g' u2 J
   
/ K$ m6 U( F6 m2 i4 y' z0 I%     /* Initialize event list.  Since no customers are present, the departure& ^/ L  u, g$ z: w
%        (service completion) event is eliminated from consideration. */4 Q5 T" M7 F( u3 v/ a
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
- h  Q7 s* ~; z6 [1 L2 F% N( v    time_next_event(EV_Depart) = 1.0e+230;
, `# t; z7 K) k* l$ E$ A
    $ {4 q% k6 b* e/ g/ u& P
   
5 a% t; ?8 T5 C    ( W' Y9 T1 A7 r( x" j& K; l0 J
%%%%%%%%%%%%part2% [/ B' P+ l: }4 V9 L+ |! G
while (num_custs_delayed < num_delays_required)3 X" q+ x9 d( O$ G/ v
%Run the simulation while more delays are still needed.
) F4 `6 C. R  J3 P6 K3 H%/* Determine the next event. */
     min_time_next_event = 1.0e+290;4 d3 Y5 O8 f  B  H6 t. v/ r5 K8 g
     next_event_type = 0;
2 y7 A0 M; P" _$ y# H+ D$ u- a" ^. b     & \0 P: K$ D7 C. ^, u9 v% `
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
9 R( L5 K  ?  U2 M        if (time_next_event(i) < min_time_next_event)   d0 e: j4 D4 U1 U9 x
            min_time_next_event = time_next_event(i);; Z2 ]: V# k6 n- Y7 g9 `# o  _$ N
            next_event_type     = i;
" z" K# D* `* x4 k+ @" e        end  q2 C$ e0 ~+ Q+ g1 X2 [
    end
; d; \# V7 b8 W/ G   
" `( e0 e  C9 S* D* ]# ]%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) . E8 ~4 G, ]8 m" S& i1 g8 b
        
  ^4 a& G( Q' k- |* z1 L4 K2 e! F%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);/ f( q! g5 T: \
        exit(1);1 w  A  N; R' l2 P1 A8 r% U7 [
    end
* S5 K6 h9 p" j) l6 U4 W%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
  F& a) r& h6 L, M8 C8 S
: P/ a% o2 G2 Y8 b; V, B. z& T   
6 N& J  w; v1 t%/* 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;
/ Z9 C& ]: U# k4 C    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;
, b9 J4 I5 a1 Y- U4 v      T3 {6 j  L$ E  ]
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)/ i  J4 T' _  |( V; Z  c" z. @
        double delay;
% Y( Q9 t! y: ^3 r        0 N4 ]0 f' g5 m; _7 K% D" E
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy)
1 ^! I1 W( l# h! d, ~            
" d8 T  O$ v# s7 d+ F8 l7 o3 q- ~%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;3 i# F0 F" ^7 `7 e' B( r+ d
            0 t# r/ |' F$ X6 P; Y2 L
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 & B% h( B* v6 A0 F+ r) j. ]* j
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
4 n4 J. Y: i5 C: T+ B                fprintf(outfile, ' time %f', sim_time);! n- ]& A& V) W' @5 b
                exit(2);, W0 @3 M" {5 Y; b$ w" d
            end1 k& _' U4 T$ s) v- 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
7 _; B0 i( Z# J0 Q: t& L! `%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
9 V: F& D" P9 `5 t%and do not affect the results of the simulation.) */
            delay = 0.0;0 t; P0 L$ p4 a$ T1 V
            total_of_delays =total_of_delays + delay;( W! n# F( O0 W0 p
            4 a/ K9 {. Y, A& K( u
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
" h6 }, R- l  \' J            server_status = ST_Busy;9 h$ K" _8 M  p6 p( Z
            8 l3 D& ~' W. t( e% E
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);- {$ R5 A# |0 q+ |
        end %    if (server_status == ST_Busy)
- f; |0 T; x% K1 u* p9 @  u& f%%%%%%%%depart/ g7 v' }) u3 P& m. A0 X
    else
3 t: z( e$ ?, h7 \6 a# [3 Y            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;" j9 D" u3 J! M7 F' l
            time_next_event(EV_Depart) = 1.0e+230;
5 c& X. {6 d/ f! o+ V" g. z   
8 E/ r" a5 N0 w+ d" J& n        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);* m" v5 ]' o! B1 n: ?7 i6 e
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;
/ ^- b* U3 P8 h5 {3 ~4 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));
& t% j/ Y0 p* a, y0 }# j            time_arrival=tempForPop;
5 y3 [: H; S7 I        end %if (num_in_q == 0)
7 J" v; o9 o2 }" x3 K, K        / e( R3 j; Z( `* M
    end %if(next_event_type==EV_Arrive)
0 A5 M6 r) w* ]; `  O   
/ k5 ~7 P+ l8 D( m. Xend %while
# q: F) W/ s" y' e: E) ~& x8 c9 X               

  e' o. T1 C0 E0 C$ ^; |1 `%%%%%%%%%% part 3( z" ~& O- w3 K/ d
%/* 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);( w) Z! ~. |1 k- A0 `+ U
    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
/ g) M5 D2 F9 V3 L    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);: N0 Z  w/ M9 i
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
+ @# u8 m, S0 }- X    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