QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

0 W* ?3 Y% W$ w& A3 [& S7 o# f
" q$ o: j, Y/ A& q$ f
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
' {9 b- V0 N) V, W9 iclc;clear;
ST_Idle=0;
: T: Y1 ]& b, N3 C: }: QST_Busy=1;
EV_NULL=0;/ e1 T( R4 X  r5 {0 b! n% q
EV_Arrive=1;9 g! j1 L: F; ?8 y2 }. Y$ K
EV_Depart=2;
4 X' s2 y) U: S; n) L: k& bEV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
1 s! X1 ~1 V$ p5 T) y  G& @% num_custs_delayed=[];, N+ T; s+ J& P1 U2 `/ ?$ o
% num_delays_required=[];& L: m  i8 z* |% Y6 y
% num_events=[];
8 E) T. b- _$ t6 K/ ?% num_in_q=[];3 B: v: r8 m8 ?) p& P' Y
% server_status=[];
# R. j( |5 E) C6 q% area_num_in_q=[];
% `; m) R+ T' B% area_server_status=[];3 \! n4 T3 q) J9 X4 i" k0 L; j! j" V
% mean_interarrival=[];3 u' s5 K' x: Y" O& B. v
% mean_service=[];6 \6 G0 U0 Q9 r$ Y
% sim_time=[];
8 S. ~, a# R1 Y$ i7 ]- D6 U% time_last_event=[];# r2 r' w( S( c/ ?9 Y3 A! }. K
% total_of_delays=[];
! U' F. U+ y: i# J' h/ w%
+ _) m. q5 X8 K9 M  m% Dtime_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);2 ~) o5 F4 v) f5 R! v' a8 D
%
仿真参数. |- Z# H% F+ _: M+ T' h
num_events=EV_LEN-1;  i0 F9 M6 ?% n# ^( d
mean_interarrival=1;
. \; L, W/ p' l/ fmean_service=.5;
7 j# a4 M9 u# b* j/ Bnum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
# @+ l  o$ a* v  n, h4 Jfprintf(outfile, 'Single-server queueing system\n\n');2 v. `1 ~5 I. t; D1 K3 @
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
' N7 X3 M1 ], mfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
0 v8 M7 C. Z$ |! hfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
- p3 A' K$ h: s9 R8 ]sim_time=0.0;
% f9 o9 I% f( `9 b%     /* Initialize the state variables. */
    server_status   = 0;%idle
+ o  L: ~+ |8 e! x8 D/ g    num_in_q        = 0;6 g- O, k& w8 y
    time_last_event = 0.0;' {$ f" C9 K% o. {
    0 O, q4 B/ r! \2 l7 k
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
7 p) Q8 u5 e: j" x0 N$ ~6 ?    total_of_delays    = 0.0;
- c# D% |6 A' S' L, [- Y/ ?& p6 {    area_num_in_q      = 0.0;8 Y, _- y- n: S' R; c/ `
    area_server_status = 0.0;4 a1 B+ x: Q  M- C1 ]  e
   
. f: f1 W2 ~( m7 M: x3 b1 E%     /* Initialize event list.  Since no customers are present, the departure( l/ p# Z7 B6 F
%        (service completion) event is eliminated from consideration. */
5 R. g% R& J6 z( x    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
& u  G' n9 L" i/ L; x    time_next_event(EV_Depart) = 1.0e+230;
, d7 K0 j5 Y4 T/ \
   
4 b+ @, e2 M* R. d      h3 [% D- w3 f, l. C: v. U  {0 q9 K
   
& _& Q( ~6 Q; L: L3 d. D  C %%%%%%%%%%%%part2
$ u0 \/ x$ H' E$ m# Kwhile (num_custs_delayed < num_delays_required)" f1 v0 D' v% V/ Q; R
%Run the simulation while more delays are still needed.
& x2 Q3 x6 s- N%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
' Y* j1 J, i- d     next_event_type = 0;, m1 V, t. R6 P% ?& |% b
     
: x. v. Y! _/ k8 O%/* Determine the event type of the next event to occur. */
    for i = 1: num_events3 v% e& g' K8 n6 ]+ U
        if (time_next_event(i) < min_time_next_event)
5 }6 o9 n" c1 h1 C            min_time_next_event = time_next_event(i);
% P/ W' o0 j7 M5 \            next_event_type     = i;& v8 d) Q: g1 j/ `
        end/ I3 |2 f9 C$ e9 S7 s$ H3 H
    end; e1 m' c% X* G% x
   
6 m( I( L: L2 C: v7 f%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) & g# J, |1 _2 Y$ V0 ?; z0 L. |
        
6 @( ^$ n7 I9 K. s" t%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);6 A0 Y) x$ H8 p( f$ s
        exit(1);- ^! s4 D7 Y" N  L
    end3 E: D, c8 \' T$ V: ]5 B6 j
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
+ U; J3 t% Y1 K( m- O
& W* @4 |* D7 s7 K5 @( n! W    * I* c$ Z1 m' o1 E! {
%/* 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;: I$ t, K# `/ b8 v
    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;
2 J. R+ Y, B  O- L) Z  j9 h$ M   
" x) g& b) Y; l* c0 x1 {%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
4 C6 {& g# a0 L, K2 P" W( w3 t        double delay;
6 |& E- Q! D  u5 v" [        0 L- t6 t$ ?) [8 g) D1 ?. L
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy)
7 _; ?2 f* F& y1 _! m, r+ H            
5 R9 N7 o4 ]0 Q+ b% y& M%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
$ `+ \7 {, l+ y/ B. n+ }$ y# {            
" |* o* C8 \/ a- i/ m5 b& D! t%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 ( K2 z$ Z5 ~1 U9 C% D+ \
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
' p0 _! |8 @- i4 z6 f                fprintf(outfile, ' time %f', sim_time);
: ]. e' w+ F! ~+ H, Y! [+ Z3 V' x                exit(2);& z7 _$ V' e4 a, {
            end1 H8 M: Q9 U9 |. g. L( s) y
%/* 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
+ A$ G+ `/ C& T" P$ `3 \7 C%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
% B4 ~5 Q/ P6 V& @%and do not affect the results of the simulation.) */
            delay = 0.0;
5 g, N0 x. A" v0 B9 R3 l4 k            total_of_delays =total_of_delays + delay;
7 m# G8 O- ^* D6 \7 x. `            + Q" \8 w4 J+ f9 s2 v9 S
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;- O4 Y4 D# C2 i: Y, y# ?$ b
            server_status = ST_Busy;6 _$ J- ~( K, n) y
            + R$ W2 A7 ]6 k! y/ z4 E$ L/ ]
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);- j' i% O. K! K! C4 B6 `1 G: u
        end %    if (server_status == ST_Busy)
3 }+ Y: Y; P9 C* n& `+ v8 O; X%%%%%%%%depart2 T. r, x# i+ p. U( J, A
    else, }8 P. D8 Z! R5 E* @+ 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;
% I: _) U% j/ a2 w. F$ q+ ~            time_next_event(EV_Depart) = 1.0e+230;6 t' ]# s2 J& B* w: _% S. p
   
: l" @. P8 B  M% f, c7 T* 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);
5 W3 b6 v, o) L/ g- i            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;' n# C$ K" _" v6 D4 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));) w' J  q# s+ N) Q: j
            time_arrival=tempForPop;
  C" g4 C) R" ~& p9 q        end %if (num_in_q == 0)
% A$ l7 d3 m: M$ ^3 d* Q        
/ u% Z- I8 ?3 ^    end %if(next_event_type==EV_Arrive)( M& B* p5 `- R" ?/ {% a
    * P( |* P& S+ S5 \: P3 k( F+ v
end %while* z$ n2 O$ X' F! n
               

- H9 h" P( a' ]0 [! I/ P4 b+ N%%%%%%%%%% part 3
9 t9 V  \! ]& C& M( \$ |* w2 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);
) C0 ?; H. U- Y4 \* s    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
- n6 C$ m# R. d3 L! k* O    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);/ k9 Q7 z- A" z, }1 `2 W  S
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);7 X: v, S" L) i, @
    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-11 22:56 , Processed in 0.418824 second(s), 106 queries .

    回顶部