QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 14147|回复: 10
打印 上一主题 下一主题

M/M/1排队系统性能仿真代码(MATLAB)

[复制链接]
字体大小: 正常 放大
qlau2007        

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2009-8-12 13:02 |只看该作者 |倒序浏览
|招呼Ta 关注Ta

7 J* i" w6 c: @1 t$ J0 r8 V; d: X. ?& z7 F  H8 y0 B+ l6 d
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
" Q) j, s1 W4 Z7 a1 xclc;clear;
ST_Idle=0;3 a! v: l  D0 l
ST_Busy=1;
EV_NULL=0;
) L6 A# e; m+ @6 N& kEV_Arrive=1;
- U+ Z$ S0 W( u7 V/ o/ rEV_Depart=2;
6 S* {4 r. w0 X% rEV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
' N+ H: c- Z+ d% num_custs_delayed=[];" T/ `, y% }& E, A. Y
% num_delays_required=[];
* @6 g+ j+ r) [3 p4 D' ]% num_events=[];. p# {. V" b8 e! f7 h
% num_in_q=[];  m  H5 ^! {( \7 t) t3 v
% server_status=[];
8 X" V* d& v* }6 B% area_num_in_q=[];
$ B. x5 N$ V1 G& t- L4 ~% area_server_status=[];
' _; |$ E& A" Y5 X+ [8 r& L* [% mean_interarrival=[];$ h! P3 n2 d2 E6 Y% g' X0 G* o
% mean_service=[];/ j/ k( y# b/ j
% sim_time=[];
+ [4 f/ z3 Z4 y4 [( N) w+ d4 C% q% time_last_event=[];
% n+ v* X: ]* X) B! ?* k% total_of_delays=[];7 S' e: c) w: A7 f/ B. {$ c! X
% 6 q- [4 o* Z* R" y4 m
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);* @: j1 T; z0 v! C" }
%
仿真参数
( d; f4 X, x4 ], `6 s3 Nnum_events=EV_LEN-1;
& E% O. E& b) H( E- |mean_interarrival=1;
2 ?; }3 W$ H( _' wmean_service=.5;
' L# ^8 i+ B5 W$ l& @7 e  Lnum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');" S/ O" L. G& S9 z2 x; o
fprintf(outfile, 'Single-server queueing system\n\n');6 U8 ?2 J+ H  k# `8 ?: J$ q* X
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
2 U' N; P6 j$ q0 A/ r8 t1 U, ^fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);' h1 D- ?4 m; g. ^
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
$ M  U0 {% M& Y  d, w  x1 D  }. _sim_time=0.0;
9 g! Z/ ~1 Z! _5 g, h+ d/ v$ Z%     /* Initialize the state variables. */
    server_status   = 0;%idle
: f6 Q: Y( C, J) o( T% u    num_in_q        = 0;1 t! O" P  C* r, z. s$ X
    time_last_event = 0.0;/ K1 r/ O! [- O: w
   
; @" r3 c; q0 {8 Y4 g2 N% }2 n7 Z%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
2 z4 V1 ]; D, k6 n' y& s" }5 i) D    total_of_delays    = 0.0;
/ i/ U1 g8 f4 ^* }" _. S( A6 M, X; N    area_num_in_q      = 0.0;+ A) t% N2 {2 s# Q5 S
    area_server_status = 0.0;% j1 s6 Q" w' B
    4 i: g9 N( l! f9 w4 A( h3 E4 v
%     /* Initialize event list.  Since no customers are present, the departure" U* Y+ D( l  Z, m* u1 X
%        (service completion) event is eliminated from consideration. */, r/ m! Y; C2 @1 ^, G5 n; N
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);4 O. O. P/ A1 d# u; W3 e
    time_next_event(EV_Depart) = 1.0e+230;

0 l" v) _9 p, T* {4 H' U    / M6 y: L( o- v: y: S
   
: f- e, ]3 L) o+ i0 ~    " a8 k, o! o- N) H) E- @8 }
%%%%%%%%%%%%part2/ H2 V8 m( [$ A# |9 {1 \2 s7 T/ t
while (num_custs_delayed < num_delays_required)/ N& [4 ]. G' b- g, F8 |: {
%Run the simulation while more delays are still needed.
2 {- g7 c$ N) B) @%/* Determine the next event. */
     min_time_next_event = 1.0e+290;* `- |3 e- r# I& [2 Z
     next_event_type = 0;
5 t1 J5 p1 s, \0 g     
* O- Y! ^  \( M; G" `%/* Determine the event type of the next event to occur. */
    for i = 1: num_events. K3 E: q7 T$ ~! b
        if (time_next_event(i) < min_time_next_event)
' e( L" A% x" O* v" e+ Y: O4 E            min_time_next_event = time_next_event(i);2 k$ @9 I! C6 D0 g4 w0 s% u
            next_event_type     = i;& K. n( B4 Z- G; X2 d" f
        end% |" y1 U; r% S8 i) m
    end
5 P/ h' P5 O# v6 i7 N    . Q  ?+ r, s7 Y3 T
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
' }& A+ c) `' ^        
% W. ^6 T* p) u+ k/ W%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);
5 n, Z, S. S0 C" V! [        exit(1);
2 O. W; O& U$ D; c! \' i. f, c    end
8 l) X/ Q2 t1 F- w. s4 B5 r1 A%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;7 {- v. j9 W  ~1 y& m0 C

' [& i$ l3 k1 @' Z  m% o   
# t/ }8 g9 C  P%/* 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;' y, ^5 Y, _6 ?/ E; N) M) s9 T, y
    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;
* D. j& L. @7 Z, A3 O   
+ k* R: q: B0 g2 T7 G& s/ Z%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
* H3 z: }0 t- ~9 C2 p  t        double delay;
3 N, y) |( m/ j2 q& p# \4 C: p        
! U/ v+ I; c3 g. e- Y8 k. y%/* 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 |5 Z/ i' M" ^8 c$ K            
* p" D6 I6 Z! u3 h%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
, E) ?3 V/ m6 b8 B            
& M, B$ G' Z1 _9 R. V6 R$ E. Q/ g%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 ; Y4 N# I4 o! C1 L
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');8 K1 X! O1 l" ?1 t8 L
                fprintf(outfile, ' time %f', sim_time);+ E  I' A. P$ `; @# {7 l4 l
                exit(2);) |1 j2 ~3 T$ K# P" K1 ?3 k% N
            end
" q. @9 V' D/ m6 M5 C3 N- q%/* 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
! s5 c7 P' i7 H% A# X%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity 3 w& N- t9 ^1 `% O, x% o
%and do not affect the results of the simulation.) */
            delay = 0.0;
; b; U6 t6 ^% @* c& P/ J$ H! M            total_of_delays =total_of_delays + delay;
" G/ g0 y6 x% M/ ]6 y' o. q4 n; d            & `  f, v+ ]% K. J
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;9 a2 d- H- Y# f4 e9 r$ d
            server_status = ST_Busy;
1 ]$ q+ |5 X4 L" E5 q            
) s! W$ }8 u3 Y7 c2 i%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);9 U- m( T; b2 D0 y2 ^
        end %    if (server_status == ST_Busy)
/ u5 n0 W  S* y7 w4 t3 p* V%%%%%%%%depart) }% t0 }0 M- M3 p  `9 [" G, [
    else
0 M( b( p1 A4 i/ n            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;
5 C$ w' E; Z) e5 i  f! w+ b7 X) C            time_next_event(EV_Depart) = 1.0e+230;
! h9 D# d' m, N, @   
' B' x/ W" W; V+ w& g        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);
* J/ P$ ^2 H  K1 r& M5 }( L5 c; z- e            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;, Q) J& z+ [4 N, e0 p8 ?* c( a
            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));
/ g7 R' h5 z" M: n  G            time_arrival=tempForPop;
& g  Z! ~' a3 g        end %if (num_in_q == 0)8 N) `3 q3 ?; G6 C# D8 a
        6 `7 y' i' {7 l+ M6 w  C3 i
    end %if(next_event_type==EV_Arrive)2 I3 o1 v9 Z9 i3 \5 O/ x8 g2 C
   
, x- Z/ u" S5 X3 gend %while: G1 J3 s1 r3 _& F7 D
               

) t/ c3 O, u- h9 m%%%%%%%%%% part 3/ v* o7 g+ u+ u" 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);
5 _3 |/ L& R5 S    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);: R+ F* o* R0 t3 R
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);  r6 c( Z! g3 }( \: K
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);, {" b; x* u& ]2 p2 H
    fclose(outfile);
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
liuyaliss        

0

主题

3

听众

439

积分

升级  46.33%

该用户从未签到

新人进步奖

回复

使用道具 举报

rockhuman        

2

主题

4

听众

415

积分

升级  38.33%

该用户从未签到

新人进步奖

回复

使用道具 举报

2

主题

5

听众

152

积分

升级  26%

该用户从未签到

回复

使用道具 举报

leeeff        

0

主题

3

听众

85

积分

升级  84.21%

该用户从未签到

回复

使用道具 举报

2

主题

4

听众

253

积分

升级  76.5%

该用户从未签到

群组数学建模

群组数学趣味、游戏、IQ等

回复

使用道具 举报

2

主题

2

听众

406

积分

升级  35.33%

该用户从未签到

新人进步奖

回复

使用道具 举报

3

主题

3

听众

41

积分

升级  37.89%

该用户从未签到

新人进步奖

回复

使用道具 举报

GunBreaK        

0

主题

4

听众

32

积分

升级  28.42%

  • TA的每日心情
    奋斗
    2014-5-29 09:00
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    新人进步奖

    回复

    使用道具 举报

    jamesgeng        

    0

    主题

    4

    听众

    6

    积分

    升级  1.05%

    该用户从未签到

    新人进步奖

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2026-4-10 10:26 , Processed in 0.410811 second(s), 106 queries .

    回顶部