QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2009-8-12 13:02 |只看该作者 |正序浏览
|招呼Ta 关注Ta
, r2 L, q- W* C% o
9 ?# ]& E+ y' N4 a3 P
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab" I8 O: p( ~. F% x
clc;clear;
ST_Idle=0;# g$ o( z( T: _& ?2 b4 i" W
ST_Busy=1;
EV_NULL=0;" z; ^9 U$ L& g, L  M  A! X/ @! C
EV_Arrive=1;( u% o9 p: x- l8 G8 C$ F
EV_Depart=2;
9 V1 S  v7 Z- q+ ]EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];0 E. [7 q6 K. g3 J- H" u4 K
% num_custs_delayed=[];
% V. n. I8 h, c" \0 x6 B% num_delays_required=[];- {& H4 c1 t3 n; P2 Q, x2 Z/ Q
% num_events=[];- ^3 y; C* o8 v1 e6 c6 i
% num_in_q=[];9 g9 m) y% p: f/ e; v* E4 k
% server_status=[];$ B8 Y) f" A/ q% f  E
% area_num_in_q=[];
4 t% O( ]4 g/ r' ~/ q: d% area_server_status=[];
3 ]% C; V% u4 Q% S% mean_interarrival=[];
/ w! s1 V0 C: D0 b% mean_service=[];1 }3 F9 Z* `) Q) O- {
% sim_time=[];
1 a+ n' q, m! j% k8 K; O4 b3 i% time_last_event=[];4 ]6 L" W) a/ R* W9 T
% total_of_delays=[];
( t* w- z7 z$ H2 a# U) ^( q+ Z" n%
6 J; n6 e5 n# Y* N2 Otime_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);' W8 u0 m- A0 y6 |3 {
%
仿真参数
6 n9 q: O" \. Tnum_events=EV_LEN-1;
9 E% k. m7 s& H+ n4 z( {) hmean_interarrival=1;- |, M' [5 D3 w+ f
mean_service=.5;' `, O, o/ D+ b8 |6 N
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');5 R& J) I' ?" y6 @+ T
fprintf(outfile, 'Single-server queueing system\n\n');
9 }& K! F$ H& k" }fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
3 R  T/ [" s8 e! }fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);- Y& i* V+ ?4 z) N* U2 j
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1. U  Q4 f/ N9 d2 I: H9 S$ D$ Z. S
sim_time=0.0;, k4 w1 y; k$ t$ G4 ]) m
%     /* Initialize the state variables. */
    server_status   = 0;%idle
9 G( N- r8 N% _: @, u0 ]1 _7 K    num_in_q        = 0;
2 Y8 T& F2 v( L    time_last_event = 0.0;
. P3 s/ w$ P4 E/ ~8 V    + n& j( W7 \) N
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
, _  p( W' Z3 }! W' Y$ o    total_of_delays    = 0.0;3 W4 e' {/ e, u% M" P
    area_num_in_q      = 0.0;
) l3 R% f5 h, k5 x2 e  s- a9 s7 Q, h    area_server_status = 0.0;
0 W- l9 W% G, C+ V, v# b5 C) e3 f! [   
0 w8 D8 I) Z, J) L7 G% d- e3 x%     /* Initialize event list.  Since no customers are present, the departure% G. o6 T3 J/ o$ Y" ~
%        (service completion) event is eliminated from consideration. */
2 ?8 M# K0 l' @' y    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);* c+ x" G2 d/ r  n- {5 [0 y- n
    time_next_event(EV_Depart) = 1.0e+230;

, f% r0 A1 A' E& t  \    $ K5 L8 z8 k6 D& E
   
; J/ _+ L4 n# X/ e    3 ?* |6 }% b; J* |" \, W2 g
%%%%%%%%%%%%part2
  y$ C8 e1 y0 s4 Pwhile (num_custs_delayed < num_delays_required)
4 P) q6 u2 s& Q3 v5 }%Run the simulation while more delays are still needed.
" [, E$ P1 o  {( i%/* Determine the next event. */
     min_time_next_event = 1.0e+290;  @4 g7 R1 X/ i3 h8 V8 F
     next_event_type = 0;, S" E3 S- W& s- g: ^- ^
     ' H9 O* }5 g( O( g
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
8 J7 h6 B2 X3 u1 O% {        if (time_next_event(i) < min_time_next_event)
9 x# p, ^: b7 n            min_time_next_event = time_next_event(i);
- _/ v# M1 _9 V- e( j            next_event_type     = i;. c& R5 s4 u1 ^
        end
& D% F* S2 B. H+ w    end
# ?5 N+ \4 B- p   
& K& e5 f& i, m9 g# N6 ]9 X%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
  z) T$ n8 z3 @        
- T' t2 D8 {8 I: |! e/ I%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);
# J, B5 v- B9 b* K        exit(1);
6 M" w( z4 z, C! f. F    end
3 D( f( N, l5 e) I& v8 J* K%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
; i4 ^: }! q9 @' R, P; P- i . r+ q: G% g% Y. @
    + w- g3 q! \, n, S
%/* 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;: L/ H& {6 T% J6 n. O
    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;' `1 L% K4 @: t. ^
    9 c+ o" [+ O5 v$ N+ I9 d
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
+ ~% T3 _+ D$ m7 \6 |4 I7 i7 ^, I7 W        double delay;
/ l1 _* V' I( m0 w) k; o/ b  i( A        & k$ [9 q' m3 G6 p6 f' Q2 z
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy)
. \. G: E; }& ]& E* W. a            
- n' g1 Z# E) d: ^%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;# w* E* N  u$ r3 i& W; b
            
6 K  P, Q/ l# v2 v; a! A# z%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
8 e' x& S! r6 X5 q* ^%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
  V) v3 b) x  @- v                fprintf(outfile, ' time %f', sim_time);
4 J0 F; e: r6 X2 [! Z; g                exit(2);
% r( `# |3 \" d- c            end
7 E; L1 m6 D& |6 V) e%/* 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
2 v( o- c9 U0 n9 _%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
* q  d- B1 s- J+ N%and do not affect the results of the simulation.) */
            delay = 0.0;
' _1 W8 f: O( \0 D' G            total_of_delays =total_of_delays + delay;' |/ o& r' ?$ v5 m3 U( r
            $ q( z' H! R6 v& a( J% {
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
3 N4 _/ V) M2 E2 \- c            server_status = ST_Busy;
1 z' |$ a- ~% f7 O, `: Z            
5 l5 ?- ~4 g2 D0 O1 u, l%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
8 D! \8 e$ [7 v/ m! X        end %    if (server_status == ST_Busy) 3 e; V+ K# f' |9 m1 S5 S! Y/ y$ v
%%%%%%%%depart* |0 H0 U" `3 s9 z, f) b; X/ m
    else4 Q0 H  Z( W3 o) w$ l9 H
            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;
  Z2 L3 z$ J: S4 H            time_next_event(EV_Depart) = 1.0e+230;
7 W+ o. m. ?: f    ' j6 q, ]+ g$ s9 z9 @& S0 j8 U
        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);
1 n# i: o, R4 C( v            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;
9 n( p9 f* t( c- d$ k9 u  x            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 w* r: x9 H. S* R" q
            time_arrival=tempForPop;, Q! X% T, Q% X( B
        end %if (num_in_q == 0)
+ r. B0 V: q7 }/ H        2 z0 j8 A3 g" x! v; S! n2 P
    end %if(next_event_type==EV_Arrive)
/ t, n' q/ U/ C& W: D   
! q  O; @" P" c0 x- p+ aend %while+ ^# t2 l( m: d
               
+ ]. \, f$ P+ I
%%%%%%%%%% part 30 i  \+ F2 A' 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);
  ^( g8 p0 }6 `0 }    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);4 f: W5 a- s2 T% _
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
1 z. e. x8 {5 n; x  o$ ?) X$ D    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);$ G5 V3 T* e  Q$ O- A; m0 T
    fclose(outfile);
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
chuan0810        

0

主题

4

听众

12

积分

升级  7.37%

该用户从未签到

新人进步奖

回复

使用道具 举报

jamesgeng        

0

主题

4

听众

6

积分

升级  1.05%

该用户从未签到

新人进步奖

回复

使用道具 举报

GunBreaK        

0

主题

4

听众

32

积分

升级  28.42%

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

    [LV.2]偶尔看看I

    新人进步奖

    回复

    使用道具 举报

    3

    主题

    3

    听众

    41

    积分

    升级  37.89%

    该用户从未签到

    新人进步奖

    回复

    使用道具 举报

    2

    主题

    2

    听众

    406

    积分

    升级  35.33%

    该用户从未签到

    新人进步奖

    回复

    使用道具 举报

    2

    主题

    4

    听众

    253

    积分

    升级  76.5%

    该用户从未签到

    群组数学建模

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

    回复

    使用道具 举报

    leeeff        

    0

    主题

    3

    听众

    85

    积分

    升级  84.21%

    该用户从未签到

    回复

    使用道具 举报

    2

    主题

    5

    听众

    152

    积分

    升级  26%

    该用户从未签到

    回复

    使用道具 举报

    rockhuman        

    2

    主题

    4

    听众

    415

    积分

    升级  38.33%

    该用户从未签到

    新人进步奖

    回复

    使用道具 举报

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

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

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

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

    蒙公网安备 15010502000194号

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

    GMT+8, 2026-4-13 17:29 , Processed in 0.509068 second(s), 107 queries .

    回顶部