QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

, [4 j1 w1 F7 U7 ^! h" E, O* U3 Z( a7 E: g" L# @
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab! w, G& v0 O/ w
clc;clear;
ST_Idle=0;% d7 v' u: q/ u! M2 q
ST_Busy=1;
EV_NULL=0;7 h& a- \; r6 P4 ~! ]! W- w
EV_Arrive=1;
4 v+ \* y! C0 J* L4 T) ], bEV_Depart=2;- F( i: J; i9 P4 j; B# R6 m
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
" o! t9 g* P0 @8 O" k% num_custs_delayed=[];
1 ~5 r3 b/ R* t- I3 e6 T/ b% |5 }3 F% num_delays_required=[];
( P$ M( s: B' e' d% num_events=[];
$ N2 ^8 M+ y  i2 J! \6 D% num_in_q=[];! B" t6 Z0 P& I% S5 W+ Z; ~. z# s
% server_status=[];
  G1 ^' r1 y- v# N# _% area_num_in_q=[];
& o# m+ M) E5 y' v/ n% area_server_status=[];
- P$ b; v# i1 p% mean_interarrival=[];% a+ q5 D6 D7 Z6 c
% mean_service=[];
2 X3 P8 q% d& W% sim_time=[];
; l; Y7 ]; o5 m, r8 @% time_last_event=[];; I8 [. x! E/ P* G2 o/ ]/ P
% total_of_delays=[];
- y8 ~9 J  {1 N- F, k%
# N( o9 u% D- t- e- e0 N8 V5 Ptime_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
( o! M- S; L3 X1 N! {2 P/ a%
仿真参数' B$ p' w7 G  b$ m
num_events=EV_LEN-1;; x0 g  P% E; t8 U3 O8 a
mean_interarrival=1;3 ?) U9 Y/ V% _5 Q1 S" n( y
mean_service=.5;
4 |6 ~; g' M' Knum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');2 B  h* }# {4 \/ s; i
fprintf(outfile, 'Single-server queueing system\n\n');  a  ~9 a1 d% f
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
: u- C4 ^9 I1 c. ofprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
3 j* H6 i; l! \$ M5 ufprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1+ N+ e( v% z: W% ?
sim_time=0.0;: g: k/ n) f# A
%     /* Initialize the state variables. */
    server_status   = 0;%idle. {  f! Z" H  J/ ]
    num_in_q        = 0;
" M8 X) g5 h' y! o8 z4 e    time_last_event = 0.0;
) v0 ]3 {5 A4 w, M% L: s   
" y: E! i+ h9 s2 w0 j  p%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
! W9 u% r2 B/ A! v# L    total_of_delays    = 0.0;
8 {& w0 O; e2 f0 ~* H    area_num_in_q      = 0.0;
6 s3 W7 l9 N5 J* n1 I; w; A* S! `    area_server_status = 0.0;2 N+ B- w7 P* V% F4 ?0 N
   
5 w( ?1 `  z# I5 ]/ P%     /* Initialize event list.  Since no customers are present, the departure
9 o2 b; D# X  Q1 _8 q9 k+ S- L8 H%        (service completion) event is eliminated from consideration. */
* Y/ a/ `3 g3 E( [3 h5 M7 h/ o2 R% r    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);, a/ H8 ^6 I9 _; V/ w
    time_next_event(EV_Depart) = 1.0e+230;
& L5 e8 s$ i+ u( Z/ O
    3 p( T' y; z' [; N0 Q$ w
   
3 q6 n: Q" ~& f8 O! O/ U, J   
) P, S0 @6 |0 h& {0 A %%%%%%%%%%%%part2
9 _  Z6 I: S& w2 }& D" d" @while (num_custs_delayed < num_delays_required)2 i' u8 t, \% z/ P, ]5 u
%Run the simulation while more delays are still needed.
% z9 C- w+ T& E1 I$ w%/* Determine the next event. */
     min_time_next_event = 1.0e+290;& l$ `) {( X0 K
     next_event_type = 0;$ @- M0 A  H6 }% k3 v" U
     8 L$ s1 J+ F& n: ?- T, S4 a
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events- d1 s' U* P/ ]3 B# J. V8 t
        if (time_next_event(i) < min_time_next_event) 1 B7 M9 d+ ]2 H/ n, l2 Q
            min_time_next_event = time_next_event(i);
! H6 P; Z: s/ U, Q5 z            next_event_type     = i;
$ f; }& m# m7 R+ B. N        end/ l0 G2 l, p* l9 O
    end  P/ o# g& ^9 d
    8 Q0 ]; _: z, u9 n
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
* f; @4 s# T' |. D8 a! n        
( M) f3 y: H4 H; H: _5 N%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);
2 \/ h8 h3 z+ m( f4 y% Y        exit(1);
  ?; Z+ ^" N" Y" A    end
+ m: Z1 g7 o9 l3 j* M' ~3 `, |, ?%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
% ]9 u& @1 L9 q: _, D
8 i. z7 [+ `: Y/ A- b# N) ?( e    $ e5 j- p/ ?+ _" l
%/* 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 N: C# ~% p, C, r: ^5 l/ c
    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;
: v- ~$ E% z) v' x    3 M- H3 s" B! r
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)& }9 D, Y0 j9 ?- \' A
        double delay;
2 G; _2 O$ m2 \+ z& E, G$ t        
% P- K3 ~) a3 o# R: @9 ~%/* 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+ T1 D; k9 q
            
% y2 E- x/ b: G" I% d% m1 A; g%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;* D: d9 l6 F, q$ t. n) S
            
# ~; U: Q1 e% i0 m# r1 j%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
  K  j; @- m5 a%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');& V( H: A6 O6 c4 {
                fprintf(outfile, ' time %f', sim_time);& _8 t9 }7 r1 F) z3 U" J  V
                exit(2);
7 o8 ^$ A# v, R+ M! f. A+ S- H            end" ?3 a# q5 x5 G# t' G5 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 0 l; h' E3 i6 @- i( l
%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity / [+ E; B9 u1 D8 @* c- v8 s; W' p( ^
%and do not affect the results of the simulation.) */
            delay = 0.0;
6 `& _, p' j8 z& I            total_of_delays =total_of_delays + delay;  C: z3 i. w  }. ]' c/ x
            
' G  k% c) v, V%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;! F, ~) w: U, ~' W- D1 N
            server_status = ST_Busy;
' @* i" A8 D; Z+ R6 g2 B% ]3 w            / r8 O$ A( [) ?3 u- b
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);0 P$ Z( H& H4 V3 i. @& C
        end %    if (server_status == ST_Busy)
/ I/ {8 F: w& W  S7 v/ Y%%%%%%%%depart( O1 B1 `) H) L% q1 k
    else
9 {! y' B! ]/ q! D2 n% n1 }  n            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;; s0 U- V' ~* M. @* w
            time_next_event(EV_Depart) = 1.0e+230;
, K6 e" m" V# d0 E- z  @( U; B  c' g# ^   
% S- z  o0 E1 c        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);! X- }# z9 q; S2 P) l" c
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;0 O9 h3 ]- d  h
            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));
: `1 K7 X. D" D/ w* E- s- i3 P1 n: J            time_arrival=tempForPop;
$ p% }. I/ d# a8 t2 W0 H# _; q        end %if (num_in_q == 0)3 V. j0 R3 @" Z
        
5 F0 o3 k. p) q0 W    end %if(next_event_type==EV_Arrive)
/ t# f: Z" _" Q" ]    * b& {: ~8 q  n7 H! M4 G
end %while
) o% d. l0 S3 Q               
  |+ I2 h& l: c2 K  M& f6 d
%%%%%%%%%% part 3
+ k7 `' h& z# E9 Z( u; @%/* 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);" ]! x, e) X6 x, s" N- S
    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);  u3 r. f6 o0 z  Z
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
& _0 p0 M5 S5 K9 A+ \% m, s    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
" }6 V; j3 |3 o1 O8 X" r( S    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 09:07 , Processed in 0.406726 second(s), 106 queries .

    回顶部