QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

; B3 ]3 t- \) j: v7 o! G0 y- L; p" t& b3 j: U
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab+ w5 ]& N7 y# d$ d# ^3 @
clc;clear;
ST_Idle=0;
# j, U, @0 i) RST_Busy=1;
EV_NULL=0;. S% Y5 V$ U+ T- L7 B5 R
EV_Arrive=1;
/ Q; {$ j% S  A3 r: p5 }1 iEV_Depart=2;
2 A3 l. W6 O( z% lEV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];  v$ C2 q3 C) |, S0 E  d6 N/ J
% num_custs_delayed=[];8 t: R1 B+ @8 a' P( W4 @
% num_delays_required=[];5 g0 O) a. T  }4 W3 m& {- l
% num_events=[];: p2 Z" d3 r4 w/ g* n, u
% num_in_q=[];& e* o" T9 d  S0 X! p3 x
% server_status=[];
0 P9 a/ t2 K) Q9 h% area_num_in_q=[];$ h0 d2 G; r9 b2 Q5 X# t  b
% area_server_status=[];
  t6 s1 T/ @( T) i5 s% Q. i- k% mean_interarrival=[];6 ~1 s  R- W8 N4 x
% mean_service=[];
- y) n& D" S$ V* v- e$ F% sim_time=[];
2 H4 [. N  `; y% j! i* ]% time_last_event=[];
7 c6 |+ s* s7 I0 [% total_of_delays=[];
8 {4 q, M# _, g* q) ~* Z- G% 0 f; D; x0 F& h3 r
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);5 A4 R- E6 D8 E
%
仿真参数
( v" a$ ^" n5 S5 ?/ k$ f6 znum_events=EV_LEN-1;
& @# B  Q, n* R/ {" Omean_interarrival=1;
7 R& B9 h8 A. v- Y% w3 f2 mmean_service=.5;$ ?9 b9 B# I2 J( D
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');' U. f$ z5 M0 S. Y8 ^6 M
fprintf(outfile, 'Single-server queueing system\n\n');
* a- j4 m, n% e5 P. o& e( ]fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
0 Q! r% i0 A) A/ Z% i" }fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);3 g0 e3 `7 R; t; V
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part18 P. n: p$ M2 V* i' x7 N
sim_time=0.0;
. a+ W  Z! b  s2 D0 }$ Y7 i3 B%     /* Initialize the state variables. */
    server_status   = 0;%idle
6 \5 k9 ?, y! d    num_in_q        = 0;" @  Z7 c! X/ i  i/ F0 k3 f' {
    time_last_event = 0.0;9 _+ m- A2 b' |0 l7 Z  q# o
    3 |1 n; S6 Y; ~+ `) y$ K9 U
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;; M, {, i4 K; p5 ]: o, ?! _( r
    total_of_delays    = 0.0;
8 l, M; S4 y. X8 T# }" m& F! n+ u0 V    area_num_in_q      = 0.0;) Y/ _: r: t" s
    area_server_status = 0.0;
) m" c. b) R1 L/ A+ v   
. H1 i1 I1 T6 U. L9 T; i: j%     /* Initialize event list.  Since no customers are present, the departure
  p/ N4 m. x$ z: q. O%        (service completion) event is eliminated from consideration. */
$ i6 ~) ^0 o" B( y    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
: M0 g1 m7 B( _. b1 ^8 m    time_next_event(EV_Depart) = 1.0e+230;

% }$ j3 K" j' w& E   
+ o+ `7 S. G/ a& Y9 {- z; x    1 `* ?  i3 n$ x$ ~
    1 b: J( @4 `" ?" W, ?. ?3 ~
%%%%%%%%%%%%part2% l( T# J# P5 J6 a
while (num_custs_delayed < num_delays_required)/ u& \# p, Q5 o" d! Q
%Run the simulation while more delays are still needed.& C5 \; H+ g1 G' ?: j$ ]- p
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;* W: ~" K" ^% i
     next_event_type = 0;
. d7 \- C$ \+ V6 L6 H     
1 B1 ]4 d" ]" ~# }" i7 Q' j%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
7 q( J1 |! l. d6 w# v& `  b        if (time_next_event(i) < min_time_next_event) 3 K! f7 J! b" b% Y
            min_time_next_event = time_next_event(i);( E; ~9 y  ?1 L! h, Z. U
            next_event_type     = i;' U4 r( q4 x4 t! h7 A+ m2 |
        end
9 d" F- e9 t' H0 h% _    end
. u2 R$ l/ b# f6 g5 U2 R    ( M: ~# J  q/ p& g2 X9 ~. G5 O
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) 9 @& @9 q4 f# _( L
        
4 v$ p% |8 \/ \- z1 G$ D- X%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);5 `# `8 H$ n- _1 M1 K; Q) b
        exit(1);, B, D! P! x! i1 v3 z' p
    end
2 F$ v% ]( T2 a( ^7 D%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;/ y* Y$ X; v% v8 P# ^" @/ M
0 U# a, u2 I' M4 [  _( J. P
   
$ C7 }3 g6 t6 ^6 }: ]3 h/ I, {%/* 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;
2 i( ?$ ]' I5 g" k7 s    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 X5 C( M0 c6 m/ g    7 p2 P4 W+ ?4 ?, h7 t  f6 B% u
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
5 Z0 S5 v2 g. ~. l. b, s) `        double delay;
! e3 j; x2 y  U        & v% _8 h) d; s/ Q5 _1 L
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) ' b4 E0 Z8 I5 U* p' h4 s3 Q& c
            + r+ }1 G. y0 j$ I3 J4 N4 j7 [
%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
7 G: t/ k; ^% i, w: {            
# \+ M1 Q3 U; n6 f%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
6 X8 k; N2 R, j$ ]1 W%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');( K- |# @/ }% Q; U% n" _
                fprintf(outfile, ' time %f', sim_time);
) e$ b+ N2 F) ]" ]8 x0 ]: c, o                exit(2);1 _- ?7 d2 h+ L' J
            end+ S* b$ U" Q3 E/ E& K" I% o
%/* 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 0 R" b4 V- J' d
%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity 4 m, Q+ {6 z8 `. y1 q9 l. z
%and do not affect the results of the simulation.) */
            delay = 0.0;
0 C! ?# y5 S3 `  Q; C8 Y7 f7 C. r" ?+ M            total_of_delays =total_of_delays + delay;
' R3 M. r- q3 [  v  h7 M# X$ v  v            
8 r: w- U$ N5 p. s%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
/ Y9 X% O2 }! a1 q) }            server_status = ST_Busy;" H* g' K+ G) \1 L' v$ F
            
4 A3 r6 C' d1 k%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);: l0 Z  s+ d7 f0 o
        end %    if (server_status == ST_Busy)
3 r: c9 p3 k5 R# y%%%%%%%%depart
" p* p* _# D0 ~- K" y9 C$ z, d" a    else
* t1 d2 W! I( ~  F# Z6 W3 D$ ?3 [            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;
- E+ ^0 |1 ^# [& x  }            time_next_event(EV_Depart) = 1.0e+230;, _' O0 }+ b8 t# ~
   
; N6 R6 k9 ~" }7 m        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);+ `6 P0 k4 \# {" g7 _; g8 J' ]/ F& c
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;
1 H; \# \$ \/ o) 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));
$ ^3 v) T& f1 j! b& `; R            time_arrival=tempForPop;1 i: |" O5 u+ q: \$ R  }6 d  e! O
        end %if (num_in_q == 0)
* x4 o0 _- Y3 l" g) K2 b8 j, a        
' q/ R3 F( m& X    end %if(next_event_type==EV_Arrive)
, F3 J$ _" }3 t  E   
1 B. S& D5 e% `8 `+ cend %while& a& p# V3 J* t" y3 f* w6 G7 i$ V
               

. W5 Q1 u  b! X2 H5 |2 i%%%%%%%%%% part 3  q8 G9 Q' b7 h6 H1 m5 q
%/* 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);
2 P& M6 I' }* o& t' r- d, S    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
) K9 E/ G& M2 h; s' l    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
5 F% @0 ?7 t. {" T! \5 c) d    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);+ \- s# a- b* S6 O5 ?
    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-11 04:24 , Processed in 0.530772 second(s), 105 queries .

    回顶部