QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

  r6 S& _) x  a( ^8 B  [
( I( H) V% C- }5 k7 }- m9 `
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
- j* M0 a6 w* t8 z: r* }" Kclc;clear;
ST_Idle=0;
. y& }% n: t9 RST_Busy=1;
EV_NULL=0;
5 M; b% o0 M7 Y. i# t4 _EV_Arrive=1;2 e0 }& e. O$ c+ F
EV_Depart=2;
. @  j* m  q% }" jEV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];6 W- v% Z  \, Z
% num_custs_delayed=[];
  y; x! K. J. p. d5 G  S. o, t: n% num_delays_required=[];( m7 b! ~- h& d7 K
% num_events=[];
) o4 e$ F* r  x7 [7 N8 O2 u: l% num_in_q=[];9 V9 d: @8 ~- q5 q1 i% [
% server_status=[];+ t0 T& d! c, V0 t8 ~5 g
% area_num_in_q=[];' q- H2 m  u, F& X# H
% area_server_status=[];
, ^( r5 o, \# W- {* p% mean_interarrival=[];8 I2 y0 U, ^9 B) h: z& B
% mean_service=[];  i) ^, H, e6 Q& W
% sim_time=[];
6 y* r' M3 K# _% time_last_event=[];( p0 p( R0 }( C7 i7 j! |1 ]; O
% total_of_delays=[];$ t- Q: C+ r: R' f' e4 H5 {% c  ^2 T
%
- M1 g  O6 _' ~2 s; U& j% n/ Ktime_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
1 _, c' [" }# ?; k%
仿真参数
" w( u7 C' V/ O$ Knum_events=EV_LEN-1;, b& F9 |4 I. c5 ?; _$ w" n
mean_interarrival=1;
; E# @7 K* b% a8 Y7 V6 mmean_service=.5;4 S" B# O, ]: B, G! z) V& ?
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');7 P3 b7 H' M. l- u' `; F1 d$ R: l% b
fprintf(outfile, 'Single-server queueing system\n\n');
# k/ J7 R8 c) X  R8 p, i# _6 ], x- rfprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
' m( u) `- @- p* cfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
# n' Y* ?5 U7 ^' qfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
, x% L+ Y7 |: b6 Q! S; e, bsim_time=0.0;
: d# H! v: S& J6 q%     /* Initialize the state variables. */
    server_status   = 0;%idle
" @7 c% E5 V" u1 V    num_in_q        = 0;- G, n7 r6 c* F* I, r0 F
    time_last_event = 0.0;3 w; K/ n5 p1 j$ p  G3 s% ]. l
    % b% B. j( U5 r2 y2 R3 [
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
! p( v& w2 u8 Y4 t9 c& F( g' s9 N    total_of_delays    = 0.0;9 v4 G" G! |) j0 P% ]* w3 A
    area_num_in_q      = 0.0;
8 [7 A& y% F, S* D" Y" U+ E, k- @5 b, B    area_server_status = 0.0;
+ y) x) N+ s# F   
4 J1 M( ~7 j0 ]0 s; J%     /* Initialize event list.  Since no customers are present, the departure) a9 F, w9 K. K* `) ^0 P5 ?
%        (service completion) event is eliminated from consideration. */: d! t  ?$ }2 j. F6 y
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);  n/ ~1 b% N- E
    time_next_event(EV_Depart) = 1.0e+230;

- X: N4 R$ k( R2 I, F. V    5 x0 h" w: b  T- ^
   
+ v  ~; D$ `* J/ s. T$ B   
  Q2 h/ t7 Y( B3 k %%%%%%%%%%%%part2
* y0 o- @# ~) M5 c) pwhile (num_custs_delayed < num_delays_required)9 P: |2 b. f& _" h4 G' Y
%Run the simulation while more delays are still needed.
/ d- V3 D0 O) }$ b%/* Determine the next event. */
     min_time_next_event = 1.0e+290;, O) ?/ l$ u' \% Y/ x2 R* _3 H6 b+ x% `
     next_event_type = 0;. l. Z4 S$ ]/ s: w5 T
     
8 I5 m# x5 r& x%/* Determine the event type of the next event to occur. */
    for i = 1: num_events' [  Q( z* W9 q7 X3 E
        if (time_next_event(i) < min_time_next_event) # l8 Q: J" X. O  L( ^* i3 N
            min_time_next_event = time_next_event(i);' r4 S. {( V0 f0 e) r6 R
            next_event_type     = i;3 W, s4 W% b. x2 U0 ?4 j  C8 ?
        end# P! D5 j. Q: @4 v
    end
1 ~" }$ x* g0 ]  B   
6 h) H& G3 j- A; y2 P3 F%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
/ @, m; g; c! n$ Z- r6 l        
7 D* M; D6 S) X+ `%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);* M) {9 a- U% |9 }7 Y
        exit(1);7 ~$ h$ h) x4 i5 T2 J2 u0 h3 \9 t
    end
* F* O' f. c) m0 W%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
0 i. a; R% i9 W2 p- H& |: I9 y* Q
4 U: b( V8 K, t3 ]2 D   
- E3 A' V9 h/ Y# s* ~4 ^7 g%/* 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;
- {6 w* O& ^8 N! R' C0 {' z, X8 T    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;
; k: e6 e9 g  a/ {2 z! S    8 |. c* J4 @3 |( d# d2 O# i
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)0 i0 r: u& w, }+ d/ B
        double delay;
) H) @7 b. D/ J        * X5 ?1 C" H" m* {" g( `3 T! c; h
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) 4 p$ G9 Y( Q' ^9 J. H
            + x$ T( U$ ]. ^3 c' e) Q
%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
# d& s+ H9 c" I* I8 W            
; Q* M6 d. v4 \" s%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 3 G0 g) t' {  o, p/ l
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
( t) Y/ Q8 D, \5 I- z                fprintf(outfile, ' time %f', sim_time);
2 h8 I6 i- @  N* o                exit(2);& w( t" R0 v  L) G0 O) Z5 i
            end
8 Z: O" D: B" \% B' k7 v%/* 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
( {+ h; ]9 m7 q4 L* i1 A%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
8 M% H; T7 S' a3 ]- e' ]%and do not affect the results of the simulation.) */
            delay = 0.0;
# b9 o8 F( o& Q- I: _9 f4 j            total_of_delays =total_of_delays + delay;3 `/ s) }6 Z$ d
            
. H1 j( S  R* Q' W3 b( N; w* I%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;* U' X  ^- v5 I% s7 }
            server_status = ST_Busy;- y+ n+ |4 W$ L) g" W3 v2 ^+ v5 Z& [' T
            
1 t  x1 ^% b  a" c9 }%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);5 P7 V* \0 ~1 f) |4 M
        end %    if (server_status == ST_Busy) , p& _. g. ~6 B
%%%%%%%%depart0 ]$ m! l) z" v; j' E4 l4 r9 I
    else
8 y" u- S5 O5 f& r, Y6 G4 q. F$ `            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 W* M2 A8 f            time_next_event(EV_Depart) = 1.0e+230;
+ X6 |0 [( D# x$ v0 k   
& i1 |/ k) e) W  D& T, h        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);
9 p; O% N) t5 V( e            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;( j9 j# B/ l9 \9 r: |" p0 r' k
            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));7 L% Y+ a7 I" M# Y6 L4 P; u# J. `
            time_arrival=tempForPop;9 Z. h4 ~* U! w; w. z
        end %if (num_in_q == 0); M& \2 Q, K, }8 u
        / G; @+ M) z- Z$ X$ U4 [% s
    end %if(next_event_type==EV_Arrive)
3 r* V! \1 A% v/ i# |0 Q   
- W& ]4 x1 x1 T! j/ Pend %while
6 ]4 g9 P" Z  `/ b% M# w               

3 F9 V. h; B+ k+ ~6 n%%%%%%%%%% part 3  C' C  `/ W4 U' H
%/* 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);
& U3 V, O  ?/ \9 G2 r" o8 A+ P$ Q+ R# ~    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);- P* i: z, J5 {4 }, ]! F7 i) d
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);& {4 {; w6 |8 F+ A
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
3 @, ?. R$ J, y    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-13 21:02 , Processed in 0.547789 second(s), 105 queries .

    回顶部