QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

) ^4 u9 w$ h$ s) ^. v1 k* A9 p. u, ^
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab2 O! i5 J; z+ T, I, G4 p
clc;clear;
ST_Idle=0;
. L, ]% Y7 t6 A" Z" I0 H7 T* ^ST_Busy=1;
EV_NULL=0;9 E3 E- _* H2 h) m: z5 {: n& Q8 A
EV_Arrive=1;
7 m1 o( C: U- }EV_Depart=2;3 t; h0 R# Y( p( J" t) G
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];! u/ x* E  {" K# R- v' H
% num_custs_delayed=[];
& r% M: I: K) A  p7 n% num_delays_required=[];
6 g* v4 a) G4 ~$ v. R! S% num_events=[];$ N- B- a$ X0 L, Z
% num_in_q=[];( L5 I7 Y7 H8 K/ v1 s
% server_status=[];
' k7 R+ o" j, C9 o: p8 \% area_num_in_q=[];
: i% ]8 p! R, k& N5 T% area_server_status=[];$ L' u) W2 r7 Y! R
% mean_interarrival=[];! y* v3 b6 ]$ q, C: p. M% Y
% mean_service=[];
' x( T' s; f" {6 \- Y7 l% sim_time=[];
% m, A: G6 e0 ^4 T  A! s" i/ y% time_last_event=[];
$ y, m$ u) Y6 p. [  I% total_of_delays=[];
& R( r4 {( Y" i/ J/ w%
7 e+ ?9 Y$ p5 D3 c0 b' |time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
# I  Q' k8 ?" D; R- B%
仿真参数& n5 D" m8 L& B+ h) v
num_events=EV_LEN-1;; v/ Q/ S5 P1 F, `! r  G7 y* ^
mean_interarrival=1;
+ k9 E8 ^+ c* w# K# bmean_service=.5;
+ N4 F7 b+ A: m7 r4 ?* }num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
  h4 f9 z) A+ J# u8 sfprintf(outfile, 'Single-server queueing system\n\n');
/ R- G( |$ ^! \) \fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
  N  C! P+ ?; \% [# \/ Yfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);. X- ~( D5 A$ w! m& p
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1/ F" {5 p% S. Z# w, X
sim_time=0.0;, r) p+ q/ p2 U2 B+ X7 }
%     /* Initialize the state variables. */
    server_status   = 0;%idle
- R' j. _/ O) S6 i& F: M: X) S( I    num_in_q        = 0;3 T9 G* U3 I/ o+ `4 Q* B
    time_last_event = 0.0;
( i: ^: I: U* h* T- P5 }1 u& I    ! w8 v! I. w6 O
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;% U5 q( c% j9 e2 W4 c
    total_of_delays    = 0.0;0 `( b4 I6 @2 Q4 f  U, G/ O: U$ x
    area_num_in_q      = 0.0;2 c0 f& e" U. E5 q. }% T( x9 F3 C
    area_server_status = 0.0;
# q! s$ `; u; q  \4 X5 r   
$ @1 W  Z2 x1 \2 F$ h+ O%     /* Initialize event list.  Since no customers are present, the departure
9 S/ s5 k0 J0 q2 Z8 c%        (service completion) event is eliminated from consideration. */$ U/ b+ g" {/ p( `9 }& n$ J
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);& F. n' U8 {( s1 ^' n# v" G9 {
    time_next_event(EV_Depart) = 1.0e+230;

! [+ |( ^/ j! _" z$ m% ]   
" q% J6 Q0 o6 \* m# q/ o5 B    6 {  l  Y# ^+ J+ W! ^0 K" v% }
   
* Z9 v7 S. Q1 n %%%%%%%%%%%%part2
6 D/ }2 R8 Z7 n, [while (num_custs_delayed < num_delays_required)
1 b! F/ A7 E+ R" f* b+ c3 C0 g%Run the simulation while more delays are still needed.
% Z1 R  }9 T1 H+ n& a" H1 A+ z%/* Determine the next event. */
     min_time_next_event = 1.0e+290;' B$ O+ m+ y$ |/ [
     next_event_type = 0;
  U9 Q% `4 o, E& z9 Y     , x2 e3 i# D/ k' d) X* V8 L
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
2 I% T, N1 w( S0 J7 Z( I5 y* V/ L        if (time_next_event(i) < min_time_next_event) , n8 h" g2 S3 I5 n9 K" X0 Q
            min_time_next_event = time_next_event(i);- Z$ H( O* e7 X- {9 N* [
            next_event_type     = i;1 {. {  _- ~5 H
        end3 h3 W+ E4 i4 t" e# g8 ]- h
    end
: M) q4 @+ ^; t" M1 X$ U# C   
; k- U5 E  ^' E# |" Q1 b# t( J%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)   E% A, H7 u3 Q2 K
        : Q. _& }/ i, ~3 Z( q5 R
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);9 H: [" a" U/ s4 e
        exit(1);7 C# O' l  @+ H+ O; `6 E) ]
    end& X8 w# N6 w4 W  H1 l6 V  j
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;7 P( K  u9 m' b- Y" w9 _
: I: a; L' W3 ]! a' T
   
: D6 P6 q; W$ o; |0 Y4 D+ x%/* 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;
* {' ?% W* z  C* V* r    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;+ r: r5 j( V9 G2 E, x# T
   
; _2 A4 N2 p, K%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
3 k# j# q" w5 i) ]        double delay;0 i  N( ^" [1 ^" S
        . N0 Z) {4 c5 o' L- f: n
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) # D/ K6 L2 C4 _2 E# F, _9 B' d
            
+ r% Y, M9 a/ C. l( p$ J0 B$ V2 i. u%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;, m1 I* H( U3 A( V/ M
            3 N! H6 S6 k, D# w! o) ]- z
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 6 v5 d  Z9 I! ~8 C
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
9 D: R% X8 @" I$ E" F: |                fprintf(outfile, ' time %f', sim_time);3 l, S! V3 z! d$ H! B
                exit(2);7 V7 n  z' h( j( E
            end
% o& _9 G& r0 Z/ ?5 W: p+ r5 D6 P%/* 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 9 K" |% S' x0 }+ o" H; y
%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity # j+ N: H( ^8 l' R" Y" W0 R
%and do not affect the results of the simulation.) */
            delay = 0.0;+ t, e1 A6 O0 f* Y. p" \8 S  {+ a8 d
            total_of_delays =total_of_delays + delay;
; n9 l$ b' J& U- k: z% F4 n( t            / {4 F$ u: s- F! q- K3 r
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
2 C# X; V. O: _" x: g2 Y+ o            server_status = ST_Busy;. a3 Z" t$ q1 p( N& \1 K, k
            6 p+ ~) W" S/ m+ r7 |
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);% c! O5 P! X. @7 N$ A4 P! v
        end %    if (server_status == ST_Busy)
# n: J" E* [% x4 [%%%%%%%%depart
! @# k6 G+ E; [- q7 p6 n: f    else
$ d/ c9 _. {/ j; k1 p' `! |- ^/ x4 {            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;' l# ^6 W$ I' @: o* [7 t4 f
            time_next_event(EV_Depart) = 1.0e+230;
4 s' }$ u' G. s6 g4 G: ?$ l: ]# z2 ~   
9 y* U& t( ^9 b6 [- b) T& U8 i        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);
: q3 S; E0 U" K  o# G1 I4 \/ }( ~5 Q" x            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;0 h' s, ], U- @8 O' s
            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));5 J$ N: d7 ]6 l7 V5 x$ n. W
            time_arrival=tempForPop;
: p: Q5 ?& U3 c1 a' B        end %if (num_in_q == 0)9 `. K: J& v7 R6 R
        * g  v7 l) Z! r; B7 H- o+ V
    end %if(next_event_type==EV_Arrive)
# y1 \: o( }) P  m0 p    0 Q+ W2 ^/ n$ Z! e' G- u$ _
end %while
' L5 a5 X  i& }5 [- W) I9 g               

# {. K  {2 D8 f, N5 q4 N3 }%%%%%%%%%% part 3
" V4 B3 c2 b4 y2 M* V7 z$ m# D* i%/* 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);
1 B6 g6 ]/ s8 r9 R2 Q    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
% O! E4 Y: o4 K! c    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
, P$ Q# r0 B/ k    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);1 x" M& ~3 Z* t5 _; C$ j) Y  R
    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, 2025-8-23 05:04 , Processed in 1.196410 second(s), 105 queries .

    回顶部