QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

# N  g- c3 m( G  c* r! F. L# \2 O% T7 k
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab. V  ^% q5 H' ^' C' n; d/ l
clc;clear;
ST_Idle=0;
7 `* X! V9 C! ]7 ]  I1 CST_Busy=1;
EV_NULL=0;- f* e& c; v$ Q- l9 ?: c
EV_Arrive=1;
* n) v. K; d$ F" UEV_Depart=2;+ Z, J1 ~( y0 d) E: `. ?
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
' q3 e" y8 y/ K8 \8 x4 ?. z5 _% num_custs_delayed=[];2 T# d) G7 \) B
% num_delays_required=[];2 X  {4 v3 Z  b6 p3 @
% num_events=[];, e) Y0 D6 @* q
% num_in_q=[];
- b+ h, {9 Q! h  E3 Y4 v" J) F1 X& X% server_status=[];1 K) H( J, _$ `# g) c9 w
% area_num_in_q=[];) j0 D8 \" W; T4 ^+ F5 y$ U
% area_server_status=[];
+ y. T8 j7 G# y% l+ K1 i% mean_interarrival=[];8 Z, }+ b7 B% _0 ]. e) ?
% mean_service=[];
/ F4 b. V* v7 O: G# w' b  T/ W% sim_time=[];
8 Q) K' D3 t4 `3 f+ {% time_last_event=[];% K! z2 o, R" u9 X3 D0 G8 H! g
% total_of_delays=[];2 @) @, z4 B6 t- x9 }
% " L+ |/ T8 Z9 A* K1 i- `( H
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
: k" ?# J$ v: K  z4 G7 g% k%
仿真参数* e- w9 u7 Z+ ^' v5 r1 r
num_events=EV_LEN-1;* k2 |8 Z+ [$ Z
mean_interarrival=1;' i8 I) q- o4 d2 Q- {9 W7 V
mean_service=.5;( r3 y) j- r' C' h/ A' K
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');" H. a% b: [. @. I) O4 K
fprintf(outfile, 'Single-server queueing system\n\n');, E3 w  S; c& t' h0 V9 v
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);* q# r2 V3 B' O+ v5 }: M
fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);' x- t" F% L: ^, F
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part14 }' L9 h0 E) ?! W: s
sim_time=0.0;1 z( {5 U5 j' @* r& D$ S7 x. I
%     /* Initialize the state variables. */
    server_status   = 0;%idle2 V" L5 U; e3 W5 D
    num_in_q        = 0;3 [; G! m3 S; g
    time_last_event = 0.0;# f& ~  \# R# b8 H
   
: }$ y( F. B$ h8 s1 M' I0 Q' t: i%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;+ x1 Y! ~0 g* M* B2 f% V
    total_of_delays    = 0.0;; A( U, a( i- ]* c
    area_num_in_q      = 0.0;
* J5 i. k4 e$ O& n    area_server_status = 0.0;2 K  x( T) z& f4 A, c
    ; ~6 e* p/ S) o& A0 E
%     /* Initialize event list.  Since no customers are present, the departure
6 l' V& A2 S- T# D' T5 S) ~1 E%        (service completion) event is eliminated from consideration. */1 Q; _, K6 q$ K4 X: Z; s3 ]7 M5 U, Q
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
7 G* k* T' `$ g. [9 p    time_next_event(EV_Depart) = 1.0e+230;

" ?2 x0 ^& }0 f" B6 W) e- J   
4 M- j& P5 I0 C8 v: A7 S    , G, F! y. u& H5 G/ B! a
    7 L5 ^/ Y+ m8 \5 L5 b1 z- r
%%%%%%%%%%%%part2, W; ?" \3 S6 O7 z9 A+ q1 b2 B, k
while (num_custs_delayed < num_delays_required)
- [' g/ R) I, ^7 |' ~$ M1 y* D& j%Run the simulation while more delays are still needed.
' v5 p9 W  d6 m1 E* O  Q8 @%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
- e- I+ C' a7 k9 ^, ?, S     next_event_type = 0;, c4 u* F: C4 Q! ?: D
     0 Q. E9 L5 n1 t4 t6 q$ t* T  d
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
3 [& {& G$ m( v6 o4 s- q8 s5 Q! T5 X        if (time_next_event(i) < min_time_next_event) : S' }# y* S# A5 e: \. ], L3 h( J5 u1 N( l
            min_time_next_event = time_next_event(i);. \7 F+ ^' p; n# F4 P/ `
            next_event_type     = i;
2 c- t7 G6 [2 W# ~+ r% j5 v! V) g        end
& Z: r0 L4 U/ s0 g2 L$ P8 k    end
3 X& ~- m7 Z: C0 B( R, o   
& o. s4 r- Q: R# T+ \5 E%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
( J# z/ u' g) C) Y5 R        
7 w& p5 [9 p) K: S* a: G%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);
. p7 I6 x3 V" z3 w- ?1 r7 l        exit(1);
# d7 i% Q. O: q) z3 `' E    end  k& t% {; X  |) m' S: z' K" [
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
" G0 W; G9 H- [& a* a1 E # I# i; n# Z4 V6 J9 E, o5 S
    & x, l4 i# ]1 _2 y0 J
%/* 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;. f1 \  t2 k7 e3 Q: `
    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;
+ x" @4 q. [, t% g5 c    " Q8 l; M3 c5 T# l
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive), ~1 A6 W0 _6 H
        double delay;- b/ b. M! n( t8 p
        4 I+ g& Y; \6 o/ l$ d! p* s
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) 0 r" ?8 q9 Q2 S- ]. w
            
/ l' r, Z' D% z7 [/ A%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;+ {7 i% c! j! d
            
9 c$ B- ?) ]! Y% k5 Z" U%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
4 N& v6 w% N. G- g& H! g%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');- t# F+ Q1 e! Y1 d6 ^) j
                fprintf(outfile, ' time %f', sim_time);
5 {( D& M: S8 @% w$ L: |                exit(2);4 I; P% O1 c0 P! H0 y
            end
5 j! c# e0 A  o" l  w/ U%/* 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
3 K$ R& G- Z. T* G2 G%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
6 b  K* g4 _7 s2 X) O( j( x%and do not affect the results of the simulation.) */
            delay = 0.0;
' e: Y1 B5 D! v9 z3 J, S5 ^            total_of_delays =total_of_delays + delay;
6 E. |6 C+ N: m: |; L            6 T7 ?7 z( E+ @& v5 U5 A' h
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;/ F% S! I0 m3 v8 o
            server_status = ST_Busy;
2 [# @& r* ~  u( T            
/ a) Y  m! w4 x! P) W2 Y5 J( U' G%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);' h4 D  y9 ~/ C3 o# @# h5 A1 {
        end %    if (server_status == ST_Busy)
) s& b$ w& i7 ]* N) B7 T%%%%%%%%depart
0 A3 d: f5 c& K# a2 U0 H" n    else
) w4 _2 X4 K8 [5 Z5 Y3 u) P            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;
0 c+ n: J! w& a/ |0 M7 |            time_next_event(EV_Depart) = 1.0e+230;' w& V6 L) a! V* g: i; [0 c
   
3 C& I0 E* _+ p6 S0 ?        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);" d& M# c+ d4 E& T5 E6 S8 `
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;4 t3 j2 [9 B- p+ U  \
            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));' P6 i& ^8 ]/ X& X( [) ^4 K
            time_arrival=tempForPop;# l" t) \% d3 J2 Z! T" t
        end %if (num_in_q == 0)
+ a/ `; p- X- I& P        
$ r) V3 Q: [1 u9 \# N" J& r    end %if(next_event_type==EV_Arrive)( L7 X/ o- w8 g2 ]/ A1 g* A
   
4 e+ f1 k: ]( e  y! q$ E& xend %while# _- b$ D0 i/ @) n' J, Q. X" j
               

5 X/ y3 n$ m) d* d; M/ w1 r+ W( {%%%%%%%%%% part 3) S' s7 _, m% f" e
%/* 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);
/ ]* V+ G5 \0 ]2 ~: n    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
, D! m" Y4 I& o    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
- q! U5 i) g0 q! C  @, b    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
0 {" n5 G( L$ _  p    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-7-10 08:01 , Processed in 0.866445 second(s), 106 queries .

    回顶部