QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

( d% L( b8 o; A( L0 s
7 e: ?1 e% X- t0 l4 T. _! `
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab3 B: j8 e* }" [! O' C
clc;clear;
ST_Idle=0;
0 w: K, F9 i+ A9 q, s5 s1 \" AST_Busy=1;
EV_NULL=0;. l& b( A  ~; ^5 b! L# T! X) c1 w
EV_Arrive=1;
0 P; W' E; F; m- s/ \  s7 T. `EV_Depart=2;
2 P) Y3 P: G0 SEV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
8 {3 ]: t. b- j7 g2 R$ I% num_custs_delayed=[];
" b: c9 A2 J7 k- a% num_delays_required=[];
5 e( Z0 U# k) g8 K. @: p+ v( H% num_events=[];
- K2 I: b5 |2 L% num_in_q=[];
7 S2 o; E4 H1 O+ z# ]% server_status=[];$ _$ p- X2 X0 H" F- G
% area_num_in_q=[];
) n2 b) V- o2 n2 z% area_server_status=[];0 n- N# r1 @& m1 e2 I$ }$ _
% mean_interarrival=[];; o4 U( U7 _6 H% `" ]! H& |) {; Y
% mean_service=[];
5 B% W9 C4 o+ a  ]  P% sim_time=[];. J: M5 |5 L4 D- A
% time_last_event=[];* e1 |3 I' y6 v! k: a# t2 d' O/ R% Q+ e2 n
% total_of_delays=[];
4 C9 d, d8 ~8 P( _1 i%
$ W& ^5 Y' k, b5 G* T. z0 c, Stime_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
- i" P6 o8 Y- q9 u% `$ V3 }%
仿真参数9 k1 O' D; i# V$ W+ {
num_events=EV_LEN-1;
, G# @+ k% b* b9 Jmean_interarrival=1;
  m$ z' y" E4 T: ymean_service=.5;6 `$ F; r  P, m2 N
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
. N* b4 N& C8 [7 S( Ufprintf(outfile, 'Single-server queueing system\n\n');8 X& q! ?. A$ `" Y1 N% I
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);6 L/ N4 o+ p% D0 y$ \
fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);( q' {1 L- M6 d- K+ n5 |8 c
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
$ |5 M1 l, \1 S7 B0 q$ zsim_time=0.0;
, ], _) _5 }# k%     /* Initialize the state variables. */
    server_status   = 0;%idle
4 F& g/ T  z6 o& ~    num_in_q        = 0;
+ K/ S' `+ y$ P. o$ Q* C. P    time_last_event = 0.0;
& J9 b" V5 U7 v3 C* Y) L    * p/ f" i- g. x, J' g1 y8 N/ ^: l
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
$ ]# R0 N6 E7 f5 j8 O) M    total_of_delays    = 0.0;
+ `: M; ]4 M8 D) i7 w% D( k    area_num_in_q      = 0.0;* ~* M1 t( O" A% @) `+ H
    area_server_status = 0.0;0 h1 `4 Z# Z- D
   
- Z5 `; V1 `' F' _0 I0 I%     /* Initialize event list.  Since no customers are present, the departure
* D5 C1 h6 m  V; M6 R%        (service completion) event is eliminated from consideration. */
# j6 u  F6 r0 j; E* V    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
; j# w2 ]& T! q) \; I    time_next_event(EV_Depart) = 1.0e+230;

* K% r& w% H% P! p    2 a- s5 m7 D1 i! l* }2 P
   
( `$ x# ~2 C6 l$ e' s; [; x    % |* @$ g1 C) {- E) g4 t
%%%%%%%%%%%%part26 ]2 }/ g9 C, ?+ G. R1 X# ?
while (num_custs_delayed < num_delays_required)$ \# {# u3 S/ K
%Run the simulation while more delays are still needed.8 c/ J" [* n0 {  E; P7 @3 F( y
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;3 Y& o& }* D1 d; U3 L
     next_event_type = 0;
  K: w- W! G, O. _9 ~! W9 f     
; @5 ]& \$ M$ g2 H9 `' U* }; L/ {%/* Determine the event type of the next event to occur. */
    for i = 1: num_events8 d6 J5 |6 v$ l1 a* O9 m
        if (time_next_event(i) < min_time_next_event) # L7 H7 X$ f0 O
            min_time_next_event = time_next_event(i);
1 P1 E; Z  n! t# _            next_event_type     = i;
, S& c6 Q$ k8 \7 V$ T        end' A8 e, I$ y# |% z/ R
    end
: m7 z1 j4 K4 A* w$ b! [' M   
" _. n; t; F3 ~" l# h3 Z/ I%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) ( {$ ~1 U* P) _. n% w! a/ _
        
) r; E& k8 I$ s%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);
+ x! G9 g' d+ Y2 g        exit(1);* I- y+ k" p3 {  F0 Y" Q; N
    end% X, _# N2 h4 ^4 b7 ^2 z
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
- b' J& {  W$ ^
; t0 Q  ]* N2 T$ Q    8 J7 n; `2 @: e2 J  c5 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;4 R# F1 H- v$ P0 U3 l; \# ^
    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;; Q* ?0 Q* A: ^; a
   
+ A: Q' p- P2 y7 b  b  `%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)& b- I) L& Y$ R% r  [" p
        double delay;/ b" ?  g  c" @7 s; |- ?0 K5 ]
          Z. o$ ?/ 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) % _: I; x3 Y- ?: X
            
8 C1 y2 }( ~( I( z1 j; ]%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
7 R4 m" D) S7 U6 z4 _            
8 z' L3 j7 c! g" x( V8 z%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 9 y, n$ N9 o5 c3 x
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');  y* s* ]  ?; T: u
                fprintf(outfile, ' time %f', sim_time);% w5 z$ ^% ~4 j+ c. X
                exit(2);8 Q0 G$ {0 R% {$ [( U
            end
$ f  L( G3 o! B2 u" G%/* 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
% O, C4 \1 L! u0 X- i%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
& [! Y$ W1 b( Y$ u%and do not affect the results of the simulation.) */
            delay = 0.0;: m8 `$ }. F# U/ x5 G- e
            total_of_delays =total_of_delays + delay;
2 D0 g: I$ T3 L: Q$ ^7 V0 t8 }            
. {2 f. @+ n1 H; E0 P; m%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;; T4 O5 @/ H$ h
            server_status = ST_Busy;+ n  n! ~- D0 }8 q2 v. B4 W
            - ?9 m$ m. c& {+ V9 s' _" P  j
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
1 ?3 R3 y+ x* j: b, _        end %    if (server_status == ST_Busy) ' W( _0 ?3 E" x5 S0 u9 |* m8 w
%%%%%%%%depart+ Z/ p3 `( A( I: v
    else
8 Y/ o: Y1 p; f1 J3 v            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* J$ l' H8 T. M+ r  u            time_next_event(EV_Depart) = 1.0e+230;
$ [- H1 r8 V/ H' m' G# G$ _    : o% K1 X" @3 l1 T' ]) e
        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);: h4 }& ?5 U7 E& t
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;
! k& v8 }# _8 b3 t            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));0 m! U9 Q  x5 ~! C" N
            time_arrival=tempForPop;
# F% K8 R# _" S6 Y; w        end %if (num_in_q == 0)+ u/ T: t9 w- m3 i7 Q. G
        
4 i; O2 m) _5 N' S4 Z    end %if(next_event_type==EV_Arrive)
/ X" A# o6 X$ I( r0 U    : Q& G: i& A# M/ a) d4 `" F
end %while! G/ I1 p1 m2 o) v4 ?
               

# w- y* ~% N/ n7 E0 t1 M%%%%%%%%%% part 3
! i8 G, f/ \. E0 [" g%/* 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);
; t2 m+ H6 c& B$ q9 U    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
5 o. R2 C1 R! b6 ?7 j* g    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);1 r$ U& ~$ J/ T! H
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);4 S+ Z8 n0 l7 l4 ?
    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-8-4 09:44 , Processed in 0.427870 second(s), 105 queries .

    回顶部