QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

6 g8 i) o: ?4 H" e& p
/ `, s* L* t7 j+ t3 X+ F
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab1 G" V4 ~7 t/ `: M
clc;clear;
ST_Idle=0;2 y$ [3 U7 |5 g6 U
ST_Busy=1;
EV_NULL=0;5 U3 o8 C2 t0 R
EV_Arrive=1;$ N& g2 Q/ _: l% P/ E3 A
EV_Depart=2;+ Z0 l1 W7 K* g: }" E# Z! C
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];  z9 A7 y8 B, ]
% num_custs_delayed=[];+ j1 Z+ J+ \, B  K9 S
% num_delays_required=[];
7 g9 [9 n8 U7 H6 q4 E% num_events=[];; B5 ]  ]6 `8 f, _3 W
% num_in_q=[];+ ^- m7 c2 M2 v- ?- r# p8 ?
% server_status=[];
$ z  W8 ]: m( ^/ i9 A, x, \5 c% area_num_in_q=[];
8 n5 s6 F) D; M* Q% area_server_status=[];
( p$ Q( j- F) |% mean_interarrival=[];
/ K9 u* N1 H& h' h) C$ a9 B% mean_service=[];/ J' y$ _- z) t# J$ x9 S
% sim_time=[];
7 A- @- ?" i) s2 r+ Y0 E# H% time_last_event=[];; t# }8 T- J7 R
% total_of_delays=[];
; Q0 N$ z- T: a% L7 J( R  \% " G  h% P3 s( q) ~  \
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);6 L& k7 e& `6 T# M
%
仿真参数
2 T) y* T" e2 O2 I; ]( Knum_events=EV_LEN-1;
) H6 W2 }: Q) c8 `, Amean_interarrival=1;
' Q  |* U' L+ W8 Q: dmean_service=.5;
7 y( k8 p! t( S8 Wnum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');# {0 m6 U$ N/ v" E/ d* @* E
fprintf(outfile, 'Single-server queueing system\n\n');
. I' G* v8 m8 v$ @fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
/ ]; B0 K  M1 j7 R! l4 \4 vfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
! A0 ^/ K0 R* z& Y2 }8 |4 Mfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
* o3 U1 n& ?: R3 u1 S$ isim_time=0.0;
9 p8 D: s$ E' u1 m' D, r* v%     /* Initialize the state variables. */
    server_status   = 0;%idle9 [+ y% z% m0 w6 B9 o6 ]
    num_in_q        = 0;
- c& d/ l6 G1 a' i$ _  D& Z4 c    time_last_event = 0.0;
3 W2 v# H8 P. C( h. [   
- w% n: h! B+ T6 q: S% o%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;9 R# V2 k. U$ @$ ^
    total_of_delays    = 0.0;$ e' g, Y8 q/ f& {* W
    area_num_in_q      = 0.0;
% s: v0 _3 v4 ~! w: H; ]1 N    area_server_status = 0.0;* _" C% b6 [5 K( R5 ]
    . c; |  t7 f" v+ Z% o
%     /* Initialize event list.  Since no customers are present, the departure
  f0 D9 X3 ?+ m" a6 k5 ~# g%        (service completion) event is eliminated from consideration. */
% O, e4 |/ r) q- O    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
* v3 l8 _! v# O  n    time_next_event(EV_Depart) = 1.0e+230;

8 d4 p6 ~! u/ u   
; b" h7 |8 W  j' o    . V$ Z+ x! X: b) J
    ' Q" L, i  h; E9 F+ }( u+ g
%%%%%%%%%%%%part2
0 q% Z: ?; [" w; Vwhile (num_custs_delayed < num_delays_required)
' ?/ d# A# ?7 M0 T/ s+ e% ~3 U; l3 R( a%Run the simulation while more delays are still needed./ @* j) M# W& t4 c
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
! @) E# Z2 a5 X0 l. }     next_event_type = 0;( Q! I* v# ~* W+ a8 X) P3 C
     
, s$ Q# Q- m5 x7 S* ^. R) ]%/* Determine the event type of the next event to occur. */
    for i = 1: num_events7 G( {' g1 t9 I9 ?* A
        if (time_next_event(i) < min_time_next_event)   h1 A- W- n( j! h. @+ }. z
            min_time_next_event = time_next_event(i);) \) t! }0 `* I/ F
            next_event_type     = i;
# j9 M0 q1 f2 n        end
" I, b. P& w4 @/ [5 G, M4 c    end
3 a* z6 Y" G" p- T0 B* O# Z/ T   
# y+ U) H8 F* R0 I6 M%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
0 e( _+ R+ \) r# a' [! ]; C4 ]        % r; B. y2 K4 s- d' W) R" j- V6 n- A
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);6 u9 Z8 O  m0 `/ d+ B
        exit(1);
& @7 C1 `) _( V' \* v    end# C' Y9 Z; S( p/ T$ V# B: h
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
4 b* ?* }) L* k7 {3 @ ) `6 T$ r' z1 {- N$ X4 \( @/ a
    + N: G/ D! A1 Q8 w& |" 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;! v  _* t1 N% w9 \( ?
    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;5 W) z, \; s8 m2 A, B' V; M: x
    " P( K5 S' w# ^5 x+ A+ ~
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
% a1 [) n0 o  E8 `3 c) h! r4 e$ p/ H        double delay;
! v0 c. r) i1 L  U2 g( V        + D5 T! T0 b4 ?$ O' U6 j
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy)
! z' p2 X: @! ?  j# Q+ H            % B; f8 [$ @* s3 l0 q, Y
%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;0 M# a: \5 S# h4 G
            # q3 @( K; A6 G. {; i4 ?' @% h
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
' ]( z, b6 j8 \%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
* ]' u2 w5 L2 ?3 a7 w                fprintf(outfile, ' time %f', sim_time);3 P+ n" F- \- m. r* \$ \6 O
                exit(2);
9 [$ ?$ Y' d! {  s6 I6 T% P            end
: ]/ u. l/ U! Q& Z, e) G' i%/* 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 / q5 k. [" H) @' b+ B- v1 P
%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity 5 f( l+ d7 j/ G4 D0 z. G
%and do not affect the results of the simulation.) */
            delay = 0.0;
* W4 ?1 s7 w6 N6 F1 {6 o2 o& n. z            total_of_delays =total_of_delays + delay;
/ O# {; _+ S6 {. o2 a. n            / _! v: _, j9 [2 _7 M  j1 M
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;8 X# u4 k, q' ]5 ^5 m& Q& U9 F1 u
            server_status = ST_Busy;
" s; s) w; N! @6 K% {& C3 S2 ^            ) j2 u& Y( `( o
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);8 j: d5 l8 O1 d1 W) G
        end %    if (server_status == ST_Busy) , ?% v: T3 g& m5 d5 R2 Q
%%%%%%%%depart
% x) \& j# l. k/ S) j% P( l    else4 O. O% y& _) p- z6 [1 z3 a* s/ u
            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;) Y7 i8 f8 Y! \: ~; Z
            time_next_event(EV_Depart) = 1.0e+230;
' ^4 V, h+ b; f4 w# V+ k    ( ]* ~, k7 J! B4 `# F- Y
        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);
: G$ c8 M2 l$ }" E! E+ y5 h            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;* C" `0 X! {! v7 c; P
            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));
6 h* K6 T/ G7 j0 B0 H            time_arrival=tempForPop;! O  \/ L- _& a  ?( {
        end %if (num_in_q == 0)
) i, `8 y, c! |, t8 G' X6 `/ A        0 Y" o( A( ]0 V# K  H; t1 ]
    end %if(next_event_type==EV_Arrive)' [7 u% ~( z- T+ c9 a- T
    4 M1 `! D' r/ B! p( }9 I
end %while
/ o5 D! z, I+ C- X8 N: F) u               
. w' B6 E6 h( x* [5 @
%%%%%%%%%% part 3
' p4 E5 @$ l& T' k% O%/* 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);" |4 x# m* k7 Y* ~* e5 i( P. p
    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);3 ]: C( ]7 s8 A6 i2 \# z
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
, e, X: r: S( {6 K1 s    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);$ V- O6 s0 X8 w0 P9 Z. \) f
    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-6-19 11:38 , Processed in 0.611486 second(s), 106 queries .

    回顶部